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 797483 : %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 24 : %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 797477 : sequence: seq {$$=$1; (void) pari_nerrs;} /* skip the destructor */
91 : ;
92 :
93 12596 : seq: /**/ %prec SEQ {$$=NOARG(@$);}
94 1117037 : | expr %prec SEQ {$$=$1;}
95 37539 : | seq ';' {$$=$1; @$=@1;}
96 27015 : | seq ';' expr {$$=newnode(Fseq,$1,$3,&@$);}
97 : ;
98 :
99 808 : range: /* */ { $$=newnode(Frange,NORANGE(@$),NORANGE(@$),&@$); }
100 12077 : | expr { $$=newnode(Frange,$1,NORANGE(@$),&@$); }
101 602 : | expr ".." expr { $$=newnode(Frange,$1,$3,&@$); }
102 72 : | '^' expr { $$=newnode(Frange,NORANGE(@$),$2,&@$); }
103 : ;
104 :
105 1473 : matrix_index: '[' range ',' range ']' {$$=newnode(Fmatrix,$2,$4,&@$);}
106 10613 : | '[' range ']' {$$=newnode(Fmatrix,$2,-1,&@$);}
107 : ;
108 :
109 30 : backticks: '`' {$$=1;}
110 72 : | backticks '`' {$$=$1+1;}
111 : ;
112 :
113 41 : history: '%' {$$=newopcall(OPhist,-1,-1,&@$);}
114 12 : | '%' KINTEGER {$$=newopcall(OPhist,newintnode(&@2),-1,&@$);}
115 24 : | '%' backticks {$$=newopcall(OPhist,newnode(Fsmall,-$2,-1,&@$),-1,&@$);}
116 6 : | '%' '#' {$$=newopcall(OPhisttime,-1,-1,&@$);}
117 6 : | '%' '#' KINTEGER {$$=newopcall(OPhisttime,newintnode(&@3),-1,&@$);}
118 6 : | '%' '#' backticks{$$=newopcall(OPhisttime,newnode(Fsmall,-$3,-1,&@$),-1,&@$);}
119 : ;
120 :
121 138 : deriv: '\'' {$$ = 1;}
122 33 : | deriv '\'' {$$ = $1+1;}
123 : ;
124 :
125 26878267 : expr: KINTEGER %prec INT {$$=newintnode(&@1);}
126 5046 : | KREAL {$$=newconst(CSTreal,&@$);}
127 0 : | '.' {$$=newconst(CSTreal,&@$);}
128 0 : | KINTEGER '.' KENTRY {$$=newnode(Ffunction,newconst(CSTmember,&@3),
129 : newintnode(&@1),&@$);}
130 3263664 : | KSTRING {$$=newconst(CSTstr,&@$);}
131 3165 : | '\'' KENTRY {$$=newconst(CSTquote,&@$);}
132 95 : | history {$$=$1;}
133 263 : | expr '(' listarg ')' {$$=newnode(Fcall,$1,$3,&@$);}
134 170775 : | funcid {$$=$1;}
135 246064 : | lvalue %prec LVAL {$$=$1;}
136 13233573 : | matrix {$$=$1;}
137 691 : | compr {$$=$1;}
138 7667 : | definition {$$=$1;}
139 1017 : | matrix '=' expr {$$=newnode(Fassign,$1,$3,&@$);}
140 42950 : | lvalue '=' expr {$$=newnode(Fassign,$1,$3,&@$);}
141 150 : | lvalue "++" {$$=newopcall(OPpp,$1,-1,&@$);}
142 21 : | lvalue "--" {$$=newopcall(OPss,$1,-1,&@$);}
143 159 : | lvalue "*=" expr {$$=newopcall(OPme,$1,$3,&@$);}
144 30 : | lvalue "/=" expr {$$=newopcall(OPde,$1,$3,&@$);}
145 5 : | lvalue "\\/=" expr {$$=newopcall(OPdre,$1,$3,&@$);}
146 5 : | lvalue "\\=" expr {$$=newopcall(OPeuce,$1,$3,&@$);}
147 5 : | lvalue "%=" expr {$$=newopcall(OPmode,$1,$3,&@$);}
148 5 : | lvalue "<<=" expr {$$=newopcall(OPsle,$1,$3,&@$);}
149 5 : | lvalue ">>=" expr {$$=newopcall(OPsre,$1,$3,&@$);}
150 147 : | lvalue "+=" expr {$$=newopcall(OPpe,$1,$3,&@$);}
151 50 : | lvalue "-=" expr {$$=newopcall(OPse,$1,$3,&@$);}
152 733 : | '!' expr {$$=newopcall(OPnb,$2,-1,&@$);}
153 3147 : | '#' expr {$$=newopcall(OPlength,$2,-1,&@$);}
154 328 : | expr "||" expr {$$=newopcall(OPor,$1,$3,&@$);}
155 683 : | expr "&&" expr {$$=newopcall(OPand,$1,$3,&@$);}
156 0 : | expr '&' expr {$$=newopcall(OPand,$1,$3,&@$);}
157 294 : | expr "===" expr {$$=newopcall(OPid,$1,$3,&@$);}
158 9843 : | expr "==" expr {$$=newopcall(OPeq,$1,$3,&@$);}
159 1831 : | expr "!=" expr {$$=newopcall(OPne,$1,$3,&@$);}
160 95 : | expr ">=" expr {$$=newopcall(OPge,$1,$3,&@$);}
161 403 : | expr '>' expr {$$=newopcall(OPg,$1,$3,&@$);}
162 189 : | expr "<=" expr {$$=newopcall(OPle,$1,$3,&@$);}
163 1117 : | expr '<' expr {$$=newopcall(OPl,$1,$3,&@$);}
164 23981 : | expr '-' expr {$$=newopcall(OPs,$1,$3,&@$);}
165 46732 : | expr '+' expr {$$=newopcall(OPp,$1,$3,&@$);}
166 117 : | expr "<<" expr {$$=newopcall(OPsl,$1,$3,&@$);}
167 15 : | expr ">>" expr {$$=newopcall(OPsr,$1,$3,&@$);}
168 578 : | expr '%' expr {$$=newopcall(OPmod,$1,$3,&@$);}
169 23 : | expr "\\/" expr {$$=newopcall(OPdr,$1,$3,&@$);}
170 160 : | expr '\\' expr {$$=newopcall(OPeuc,$1,$3,&@$);}
171 775191 : | expr '/' expr {$$=newopcall(OPd,$1,$3,&@$);}
172 48524 : | expr '*' expr {$$=newopcall(OPm,$1,$3,&@$);}
173 63 : | '+' expr %prec SIGN {$$=$2;}
174 8013569 : | '-' expr %prec SIGN {$$=newopcall(OPn,$2,-1,&@$);}
175 60938 : | expr '^' expr {$$=newopcall(OPpow,$1,$3,&@$);}
176 4300 : | expr '~' {$$=newopcall(OPtrans,$1,-1,&@$);}
177 138 : | expr deriv %prec DERIV {$$=newopcall(OPderivn,$1, newnode(Fsmall,$2,-1,&@$),&@$);}
178 145 : | expr '!' {$$=newopcall(OPfact,$1,-1,&@$);}
179 20 : | expr '#' {$$=newopcall(OPprim,$1,-1,&@$);}
180 3895 : | expr matrix_index {$$=newnode(Fmatcoeff,$1,$2,&@$);}
181 10772 : | memberid {$$=$1;}
182 0 : | expr ':' KENTRY {$$=newnode(Ftag,$1,0,&@$);}
183 11617 : | '(' expr ')' {$$=$2;}
184 : ;
185 :
186 293350 : lvalue: KENTRY %prec LVAL {$$=newnode(Fentry,newconst(CSTentry,&@1),-1,&@$);}
187 8191 : | lvalue matrix_index {$$=newnode(Fmatcoeff,$1,$2,&@$);}
188 0 : | lvalue ':' KENTRY {$$=newnode(Ftag,$1,newconst(CSTentry,&@2),&@$);}
189 : ;
190 :
191 29807878 : exprno: expr {$$=$1;}
192 25 : | /**/ {$$=NOARG(@$);}
193 :
194 29807904 : matrixeltsno: matrixelts {$$=$1;}
195 5 : | /**/ {$$=NOARG(@$);}
196 : ;
197 :
198 11869653 : matrixelts: expr {$$=$1;}
199 29807903 : | matrixeltsno ',' exprno {$$=newnode(Fmatrixelts,$1,$3,&@$);}
200 : ;
201 :
202 6756 : matrixlines: matrixelts ';' matrixelts {$$=newnode(Fmatrixlines,$1,$3,&@$);}
203 15775 : | matrixlines ';' matrixelts {$$=newnode(Fmatrixlines,$1,$3,&@$);}
204 : ;
205 :
206 1385869 : matrix: '[' ']' {$$=newnode(Fvec,-1,-1,&@$);}
207 751 : | '[' expr ".." expr ']' {$$=newopcall(OPrange,$2,$4,&@$);}
208 849 : | '[' ';' ']' {$$=newnode(Fmat,-1,-1,&@$);}
209 11840365 : | '[' matrixelts ']' {$$=newnode(Fvec,$2,-1,&@$);}
210 6756 : | '[' matrixlines ']' {$$=newnode(Fmat,$2,-1,&@$);}
211 : ;
212 :
213 765 : in: lvalue '<' '-' expr {$$=newnode(Flistarg,$4,$1,&@$);}
214 : ;
215 :
216 556 : inseq: in {$$=newopcall(OPcompr,$1,-2,&@$);}
217 135 : | in ',' expr {$$=newopcall3(OPcompr,$1,-2,$3,&@$);}
218 64 : | in ';' inseq {$$=newopcall(OPcomprc,$1,$3,&@$);}
219 10 : | in ',' expr ';' inseq {$$=newopcall3(OPcomprc,$1,$5,$3,&@$);}
220 : ;
221 :
222 691 : compr: '[' expr '|' inseq ']' {$$=addcurrexpr($4,$2,&@$);}
223 : ;
224 :
225 324489 : arg: seq {$$=$1;}
226 16 : | lvalue '[' ".." ']' {$$=newnode(Fvararg,$1,-1,&@$);}
227 1284 : | '&' lvalue {$$=newnode(Frefarg,$2,-1,&@$);}
228 588 : | '~' lvalue {$$=newnode(Findarg,$2,-1,&@$);}
229 122 : | arg error {if (!pari_once) { yyerrok; } pari_once=1;} expr
230 85 : {pari_once=0; $$=newopcall(OPcat,$1,$4,&@$);}
231 : ;
232 :
233 177604 : listarg: arg {$$=$1;}
234 148767 : | listarg ',' arg {$$=newnode(Flistarg,$1,$3,&@$);}
235 : ;
236 :
237 170775 : funcid: KENTRY '(' listarg ')' {$$=newnode(Ffunction,newconst(CSTentry,&@1),$3,&@$);}
238 : ;
239 :
240 10772 : memberid: expr '.' KENTRY {$$=newnode(Ffunction,newconst(CSTmember,&@3),$1,&@$);}
241 : ;
242 :
243 : definition: KENTRY '(' listarg ')' '=' seq %prec DEFFUNC
244 3055 : {$$=newfunc(CSTentry,&@1,$3,$6,&@$);}
245 : | expr '.' KENTRY '=' seq %prec DEFFUNC
246 12 : {$$=newfunc(CSTmember,&@3,newnode(Findarg,$1,-1,&@1),$5,&@$);}
247 1101 : | lvalue "->" seq {$$=newnode(Flambda, $1,$3,&@$);}
248 3499 : | '(' listarg ")->" seq {$$=newnode(Flambda, $2,$4,&@$);}
249 : ;
250 :
251 : %%
|