Line data Source code
1 : %{
2 : /* Copyright (C) 2006 The PARI group.
3 :
4 : This file is part of the PARI package.
5 :
6 : PARI/GP is free software; you can redistribute it and/or modify it under the
7 : terms of the GNU General Public License as published by the Free Software
8 : Foundation; either version 2 of the License, or (at your option) any later
9 : version. It is distributed in the hope that it will be useful, but WITHOUT
10 : ANY WARRANTY WHATSOEVER.
11 :
12 : Check the License for details. You should have received a copy of it, along
13 : with the package; see the file 'COPYING'. If not, write to the Free Software
14 : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
15 :
16 : #define PARI_STYPE union token_value
17 : #define PARI_LTYPE struct node_loc
18 : #define YYPTRDIFF_T long
19 : #define YYPTRDIFF_MAXIMUM LONG_MAX
20 : #define YYSIZE_T size_t
21 : #define YYLLOC_DEFAULT(Current, Rhs, N) \
22 : ((Current).start = ((N)?(Rhs)[1].start:(Rhs)[0].end), \
23 : (Current).end = (Rhs)[N].end)
24 : #include "parsec.h"
25 : #define NOARG(x) newnode(Fnoarg,-1,-1,&(x))
26 : #define NORANGE(x) newnode(Fnorange,-1,-1,&(x))
27 : %}
28 : %define parse.error verbose
29 : %define api.prefix {pari_}
30 : %define api.pure full
31 : %parse-param {char **lex}
32 : %lex-param {char **lex}
33 932053 : %initial-action{ @$.start=@$.end=*lex; }
34 : %token KPARROW ")->"
35 : %token KARROW "->"
36 : %token KDOTDOT ".."
37 : %token KPE "+="
38 : %token KSE "-="
39 : %token KME "*="
40 : %token KDE "/="
41 : %token KDRE "\\/="
42 : %token KEUCE "\\="
43 : %token KMODE "%="
44 : %token KAND "&&"
45 : %token KOR "||"
46 : %token KID "==="
47 : %token KEQ "=="
48 : %token KNE "!="
49 : %token KGE ">="
50 : %token KLE "<="
51 : %token KSRE ">>="
52 : %token KSLE "<<="
53 : %token KSR ">>"
54 : %token KSL "<<"
55 : %token KDR "\\/"
56 : %token KPP "++"
57 : %token KSS "--"
58 : %token <gen> KINTEGER "integer"
59 : %token <gen> KREAL "real number"
60 : %token KENTRY "variable name"
61 : %token KSTRING "character string"
62 : %left SEQ DEFFUNC
63 : %left INT LVAL
64 : %right ")->" "->"
65 : %left ';' ',' ".."
66 : %right '=' "+=" "-=" "*=" "/=" "\\/=" "\\=" "%=" ">>=" "<<="
67 : %left '&' "&&" "||"
68 : %left "===" "==" "!=" '>' ">=" '<' "<="
69 : %left '+' '-'
70 : %left '%' "\\/" '\\' '/' '*' ">>" "<<"
71 : %left SIGN
72 : %right '^'
73 : %left '#'
74 : %left '!' '~' '[' DERIV
75 : %left '\''
76 : %left '.'
77 : %left "++" "--"
78 : %left '('
79 : %left ':'
80 : %type <val> seq sequence
81 : %type <val> range matrix matrix_index expr exprno
82 : %type <val> lvalue deriv
83 : %type <val> matrixelts matrixeltsno matrixlines arg listarg definition
84 : %type <val> funcid memberid
85 : %type <val> backticks history
86 : %type <val> compr in inseq
87 0 : %destructor { pari_discarded++; } seq matrix range matrix_index expr exprno lvalue matrixelts matrixeltsno matrixlines arg listarg definition funcid memberid backticks history compr in inseq deriv
88 : %%
89 :
90 932053 : sequence: seq {$$=$1; (void) pari_nerrs;} /* skip the destructor */
91 : ;
92 :
93 15035 : seq: /**/ %prec SEQ {$$=NOARG(@$);}
94 1309376 : | expr %prec SEQ {$$=$1;}
95 43267 : | seq ';' {$$=$1; @$=@1;}
96 32039 : | seq ';' expr {$$=newnode(Fseq,$1,$3,&@$);}
97 : ;
98 :
99 952 : range: /* */ { $$=newnode(Frange,NORANGE(@$),NORANGE(@$),&@$); }
100 14079 : | expr { $$=newnode(Frange,$1,NORANGE(@$),&@$); }
101 735 : | expr ".." expr { $$=newnode(Frange,$1,$3,&@$); }
102 98 : | '^' expr { $$=newnode(Frange,NORANGE(@$),$2,&@$); }
103 : ;
104 :
105 1750 : matrix_index: '[' range ',' range ']' {$$=newnode(Fmatrix,$2,$4,&@$);}
106 12364 : | '[' range ']' {$$=newnode(Fmatrix,$2,-1,&@$);}
107 : ;
108 :
109 35 : backticks: '`' {$$=1;}
110 84 : | backticks '`' {$$=$1+1;}
111 : ;
112 :
113 49 : history: '%' {$$=newopcall(OPhist,-1,-1,&@$);}
114 14 : | '%' KINTEGER {$$=newopcall(OPhist,newintnode(&@2),-1,&@$);}
115 28 : | '%' backticks {$$=newopcall(OPhist,newnode(Fsmall,-$2,-1,&@$),-1,&@$);}
116 7 : | '%' '#' {$$=newopcall(OPhisttime,-1,-1,&@$);}
117 11 : | '%' '#' KINTEGER {$$=newopcall(OPhisttime,newintnode(&@3),-1,&@$);}
118 7 : | '%' '#' backticks{$$=newopcall(OPhisttime,newnode(Fsmall,-$3,-1,&@$),-1,&@$);}
119 : ;
120 :
121 147 : deriv: '\'' {$$ = 1;}
122 42 : | deriv '\'' {$$ = $1+1;}
123 : ;
124 :
125 11949687 : expr: KINTEGER %prec INT {$$=newintnode(&@1);}
126 5408 : | KREAL {$$=newconst(CSTreal,&@$);}
127 0 : | '.' {$$=newconst(CSTreal,&@$);}
128 0 : | KINTEGER '.' KENTRY {$$=newnode(Ffunction,newconst(CSTmember,&@3),
129 : newintnode(&@1),&@$);}
130 879996 : | KSTRING {$$=newconst(CSTstr,&@$);}
131 3643 : | '\'' KENTRY {$$=newconst(CSTquote,&@$);}
132 116 : | history {$$=$1;}
133 322 : | expr '(' listarg ')' {$$=newnode(Fcall,$1,$3,&@$);}
134 201452 : | funcid {$$=$1;}
135 289392 : | lvalue %prec LVAL {$$=$1;}
136 4472697 : | matrix {$$=$1;}
137 823 : | compr {$$=$1;}
138 9092 : | definition {$$=$1;}
139 1187 : | matrix '=' expr {$$=newnode(Fassign,$1,$3,&@$);}
140 50039 : | lvalue '=' expr {$$=newnode(Fassign,$1,$3,&@$);}
141 183 : | lvalue "++" {$$=newopcall(OPpp,$1,-1,&@$);}
142 28 : | lvalue "--" {$$=newopcall(OPss,$1,-1,&@$);}
143 195 : | lvalue "*=" expr {$$=newopcall(OPme,$1,$3,&@$);}
144 35 : | lvalue "/=" expr {$$=newopcall(OPde,$1,$3,&@$);}
145 7 : | lvalue "\\/=" expr {$$=newopcall(OPdre,$1,$3,&@$);}
146 7 : | lvalue "\\=" expr {$$=newopcall(OPeuce,$1,$3,&@$);}
147 7 : | lvalue "%=" expr {$$=newopcall(OPmode,$1,$3,&@$);}
148 7 : | lvalue "<<=" expr {$$=newopcall(OPsle,$1,$3,&@$);}
149 7 : | lvalue ">>=" expr {$$=newopcall(OPsre,$1,$3,&@$);}
150 229 : | lvalue "+=" expr {$$=newopcall(OPpe,$1,$3,&@$);}
151 63 : | lvalue "-=" expr {$$=newopcall(OPse,$1,$3,&@$);}
152 777 : | '!' expr {$$=newopcall(OPnb,$2,-1,&@$);}
153 3759 : | '#' expr {$$=newopcall(OPlength,$2,-1,&@$);}
154 413 : | expr "||" expr {$$=newopcall(OPor,$1,$3,&@$);}
155 816 : | expr "&&" expr {$$=newopcall(OPand,$1,$3,&@$);}
156 0 : | expr '&' expr {$$=newopcall(OPand,$1,$3,&@$);}
157 343 : | expr "===" expr {$$=newopcall(OPid,$1,$3,&@$);}
158 11467 : | expr "==" expr {$$=newopcall(OPeq,$1,$3,&@$);}
159 2149 : | expr "!=" expr {$$=newopcall(OPne,$1,$3,&@$);}
160 125 : | expr ">=" expr {$$=newopcall(OPge,$1,$3,&@$);}
161 494 : | expr '>' expr {$$=newopcall(OPg,$1,$3,&@$);}
162 237 : | expr "<=" expr {$$=newopcall(OPle,$1,$3,&@$);}
163 1361 : | expr '<' expr {$$=newopcall(OPl,$1,$3,&@$);}
164 28885 : | expr '-' expr {$$=newopcall(OPs,$1,$3,&@$);}
165 56007 : | expr '+' expr {$$=newopcall(OPp,$1,$3,&@$);}
166 147 : | expr "<<" expr {$$=newopcall(OPsl,$1,$3,&@$);}
167 21 : | expr ">>" expr {$$=newopcall(OPsr,$1,$3,&@$);}
168 686 : | expr '%' expr {$$=newopcall(OPmod,$1,$3,&@$);}
169 28 : | expr "\\/" expr {$$=newopcall(OPdr,$1,$3,&@$);}
170 175 : | expr '\\' expr {$$=newopcall(OPeuc,$1,$3,&@$);}
171 116171 : | expr '/' expr {$$=newopcall(OPd,$1,$3,&@$);}
172 58122 : | expr '*' expr {$$=newopcall(OPm,$1,$3,&@$);}
173 84 : | '+' expr %prec SIGN {$$=$2;}
174 4110652 : | '-' expr %prec SIGN {$$=newopcall(OPn,$2,-1,&@$);}
175 73404 : | expr '^' expr {$$=newopcall(OPpow,$1,$3,&@$);}
176 4851 : | expr '~' {$$=newopcall(OPtrans,$1,-1,&@$);}
177 147 : | expr deriv %prec DERIV {$$=newopcall(OPderivn,$1, newnode(Fsmall,$2,-1,&@$),&@$);}
178 243 : | expr '!' {$$=newopcall(OPfact,$1,-1,&@$);}
179 28 : | expr '#' {$$=newopcall(OPprim,$1,-1,&@$);}
180 4492 : | expr matrix_index {$$=newnode(Fmatcoeff,$1,$2,&@$);}
181 12565 : | memberid {$$=$1;}
182 0 : | expr ':' KENTRY {$$=newnode(Ftag,$1,0,&@$);}
183 13851 : | '(' expr ')' {$$=$2;}
184 : ;
185 :
186 344566 : lvalue: KENTRY %prec LVAL {$$=newnode(Fentry,newconst(CSTentry,&@1),-1,&@$);}
187 9622 : | lvalue matrix_index {$$=newnode(Fmatcoeff,$1,$2,&@$);}
188 0 : | lvalue ':' KENTRY {$$=newnode(Ftag,$1,newconst(CSTentry,&@2),&@$);}
189 : ;
190 :
191 11964161 : exprno: expr {$$=$1;}
192 35 : | /**/ {$$=NOARG(@$);}
193 :
194 11964189 : matrixeltsno: matrixelts {$$=$1;}
195 7 : | /**/ {$$=NOARG(@$);}
196 : ;
197 :
198 4136429 : matrixelts: expr {$$=$1;}
199 11964196 : | matrixeltsno ',' exprno {$$=newnode(Fmatrixelts,$1,$3,&@$);}
200 : ;
201 :
202 8610 : matrixlines: matrixelts ';' matrixelts {$$=newnode(Fmatrixlines,$1,$3,&@$);}
203 20902 : | matrixlines ';' matrixelts {$$=newnode(Fmatrixlines,$1,$3,&@$);}
204 : ;
205 :
206 365071 : matrix: '[' ']' {$$=newnode(Fvec,-1,-1,&@$);}
207 895 : | '[' expr ".." expr ']' {$$=newopcall(OPrange,$2,$4,&@$);}
208 994 : | '[' ';' ']' {$$=newnode(Fmat,-1,-1,&@$);}
209 4098314 : | '[' matrixelts ']' {$$=newnode(Fvec,$2,-1,&@$);}
210 8610 : | '[' matrixlines ']' {$$=newnode(Fmat,$2,-1,&@$);}
211 : ;
212 :
213 921 : in: lvalue '<' '-' expr {$$=newnode(Flistarg,$4,$1,&@$);}
214 : ;
215 :
216 676 : inseq: in {$$=newopcall(OPcompr,$1,-2,&@$);}
217 147 : | in ',' expr {$$=newopcall3(OPcompr,$1,-2,$3,&@$);}
218 84 : | in ';' inseq {$$=newopcall(OPcomprc,$1,$3,&@$);}
219 14 : | in ',' expr ';' inseq {$$=newopcall3(OPcomprc,$1,$5,$3,&@$);}
220 : ;
221 :
222 823 : compr: '[' expr '|' inseq ']' {$$=addcurrexpr($4,$2,&@$);}
223 : ;
224 :
225 383266 : arg: seq {$$=$1;}
226 21 : | lvalue '[' ".." ']' {$$=newnode(Fvararg,$1,-1,&@$);}
227 1505 : | '&' lvalue {$$=newnode(Frefarg,$2,-1,&@$);}
228 588 : | '~' lvalue {$$=newnode(Findarg,$2,-1,&@$);}
229 140 : | arg error {if (!pari_once) { yyerrok; } pari_once=1;} expr
230 105 : {pari_once=0; $$=newopcall(OPcat,$1,$4,&@$);}
231 : ;
232 :
233 209520 : listarg: arg {$$=$1;}
234 175860 : | listarg ',' arg {$$=newnode(Flistarg,$1,$3,&@$);}
235 : ;
236 :
237 201452 : funcid: KENTRY '(' listarg ')' {$$=newnode(Ffunction,newconst(CSTentry,&@1),$3,&@$);}
238 : ;
239 :
240 12565 : memberid: expr '.' KENTRY {$$=newnode(Ffunction,newconst(CSTmember,&@3),$1,&@$);}
241 : ;
242 :
243 : definition: KENTRY '(' listarg ')' '=' seq %prec DEFFUNC
244 3646 : {$$=newfunc(CSTentry,&@1,$3,$6,&@$);}
245 : | expr '.' KENTRY '=' seq %prec DEFFUNC
246 14 : {$$=newfunc(CSTmember,&@3,newnode(Findarg,$1,-1,&@1),$5,&@$);}
247 1332 : | lvalue "->" seq {$$=newnode(Flambda, $1,$3,&@$);}
248 4100 : | '(' listarg ")->" seq {$$=newnode(Flambda, $2,$4,&@$);}
249 : ;
250 :
251 : %%
|