]> www.ginac.de Git - ginac.git/blob - ginac/input_parser.yy
function(unsigned, const exprseq &) constructor clears status_flags::evaluated
[ginac.git] / ginac / input_parser.yy
1 /** @file input_parser.yy
2  *
3  *  Input grammar definition for reading expressions.
4  *  This file must be processed with yacc/bison. */
5
6 /*
7  *  GiNaC Copyright (C) 1999-2003 Johannes Gutenberg University Mainz, Germany
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or
12  *  (at your option) any later version.
13  *
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License
20  *  along with this program; if not, write to the Free Software
21  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23
24
25 /*
26  *  Definitions
27  */
28
29 %{
30 #include <stdexcept>
31
32 #include "input_lexer.h"
33 #include "ex.h"
34 #include "relational.h"
35 #include "symbol.h"
36 #include "lst.h"
37 #include "power.h"
38 #include "exprseq.h"
39 #include "idx.h"
40 #include "indexed.h"
41 #include "matrix.h"
42 #include "inifcns.h"
43
44 namespace GiNaC {
45
46 #define YYERROR_VERBOSE 1
47
48 // Parsed output expression
49 ex parsed_ex;
50
51 // Last error message returned by parser
52 static std::string parser_error;
53
54 // Prototypes
55 ex attach_index(const ex & base, ex i, bool covariant);
56 %}
57
58 /* Tokens (T_LITERAL means a literal value returned by the parser, but not
59    of class numeric or symbol (e.g. a constant or the FAIL object)) */
60 %token T_EOF T_NUMBER T_SYMBOL T_LITERAL T_DIGITS T_EQUAL T_NOTEQ T_LESSEQ T_GREATEREQ
61
62 /* Operator precedence and associativity */
63 %right '='
64 %left T_EQUAL T_NOTEQ
65 %left '<' '>' T_LESSEQ T_GREATEREQ
66 %left '+' '-'
67 %left '*' '/' '%'
68 %nonassoc NEG
69 %right '^'
70 %left '.' '~'
71 %nonassoc '!'
72
73 %start input
74
75
76 /*
77  *  Grammar rules
78  */
79
80 %%
81 input   : exp T_EOF {
82                 try {
83                         parsed_ex = $1;
84                         YYACCEPT;
85                 } catch (std::exception &err) {
86                         parser_error = err.what();
87                         YYERROR;
88                 }
89         }
90         ;
91
92 exp     : T_NUMBER              {$$ = $1;}
93         | T_SYMBOL {
94                 if (is_lexer_symbol_predefined($1))
95                         $$ = $1.eval();
96                 else
97                         throw (std::runtime_error("unknown symbol '" + get_symbol_name($1) + "'"));
98         }
99         | T_LITERAL             {$$ = $1;}
100         | T_DIGITS              {$$ = $1;}
101         | T_SYMBOL '(' exprseq ')' {
102                 std::string n = get_symbol_name($1);
103                 if (n == "sqrt") {
104                         if ($3.nops() != 1)
105                                 throw (std::runtime_error("too many arguments to sqrt()"));
106                         $$ = sqrt($3.op(0));
107                 } else {
108                         unsigned i = function::find_function(n, $3.nops());
109                         $$ = function(i, ex_to<exprseq>($3)).eval(1);
110                 }
111         }
112         | exp T_EQUAL exp       {$$ = $1 == $3;}
113         | exp T_NOTEQ exp       {$$ = $1 != $3;}
114         | exp '<' exp           {$$ = $1 < $3;}
115         | exp T_LESSEQ exp      {$$ = $1 <= $3;}
116         | exp '>' exp           {$$ = $1 > $3;}
117         | exp T_GREATEREQ exp   {$$ = $1 >= $3;}
118         | exp '+' exp           {$$ = $1 + $3;}
119         | exp '-' exp           {$$ = $1 - $3;}
120         | exp '*' exp           {$$ = $1 * $3;}
121         | exp '/' exp           {$$ = $1 / $3;}
122         | '-' exp %prec NEG     {$$ = -$2;}
123         | '+' exp %prec NEG     {$$ = $2;}
124         | exp '^' exp           {$$ = pow($1, $3);}
125         | exp '.' exp           {$$ = attach_index($1, $3, true);}
126         | exp '~' exp           {$$ = attach_index($1, $3, false);}
127         | exp '!'               {$$ = factorial($1);}
128         | '(' exp ')'           {$$ = $2;}
129         | '{' list_or_empty '}' {$$ = $2;}
130         | '[' matrix ']'        {$$ = lst_to_matrix(ex_to<lst>($2));}
131         ;
132
133 exprseq : exp                   {$$ = exprseq($1);}
134         | exprseq ',' exp       {exprseq es(ex_to<exprseq>($1)); $$ = es.append($3);}
135         ;
136
137 list_or_empty: /* empty */      {$$ = *new lst;}
138         | list                  {$$ = $1;}
139         ;
140
141 list    : exp                   {$$ = lst($1);}
142         | list ',' exp          {lst l(ex_to<lst>($1)); $$ = l.append($3);}
143         ;
144
145 matrix  : '[' row ']'           {$$ = lst($2);}
146         | matrix ',' '[' row ']' {lst l(ex_to<lst>($1)); $$ = l.append($4);}
147         ;
148
149 row     : exp                   {$$ = lst($1);}
150         | row ',' exp           {lst l(ex_to<lst>($1)); $$ = l.append($3);}
151         ;
152
153
154 /*
155  *  Routines
156  */
157
158 %%
159 // Attach index to expression
160 ex attach_index(const ex & base, ex i, bool covariant)
161 {
162         // Toggle index variance if necessary
163         if (is_a<varidx>(i)) {
164                 const varidx &vi = ex_to<varidx>(i);
165                 if (vi.is_covariant() != covariant)
166                         i = vi.toggle_variance();
167         } else if (!covariant)
168                 throw (std::runtime_error("index '" + get_symbol_name(i) + "' is not a varidx and cannot be contravariant"));
169
170         // Add index to an existing indexed object, or create a new indexed
171         // object if there are no indices yet
172         if (is_a<indexed>(base)) {
173                 const ex &b = base.op(0);
174                 exvector iv;
175                 for (unsigned n=1; n<base.nops(); n++)
176                         iv.push_back(base.op(n));
177                 iv.push_back(i);
178                 return indexed(b, iv);
179         } else
180                 return indexed(base, i);
181 }
182
183 // Get last error encountered by parser
184 std::string get_parser_error(void)
185 {
186         return parser_error;
187 }
188
189 } // namespace GiNaC
190
191 // Error print routine (store error string in parser_error)
192 int ginac_yyerror(char *s)
193 {
194         GiNaC::parser_error = std::string(s) + " at " + std::string(ginac_yytext);
195         return 0;
196 }