]> www.ginac.de Git - ginac.git/blob - ginac/input_parser.yy
778b8347da3c88a239b8c83a4a4fa4a123ba2e18
[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-2002 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 "operators.h"
36 #include "symbol.h"
37 #include "lst.h"
38 #include "power.h"
39 #include "exprseq.h"
40 #include "matrix.h"
41 #include "inifcns.h"
42
43 namespace GiNaC {
44
45 #define YYERROR_VERBOSE 1
46
47 // Parsed output expression
48 ex parsed_ex;
49
50 // Last error message returned by parser
51 static std::string parser_error;
52 %}
53
54 /* Tokens (T_LITERAL means a literal value returned by the parser, but not
55    of class numeric or symbol (e.g. a constant or the FAIL object)) */
56 %token T_NUMBER T_SYMBOL T_LITERAL T_DIGITS T_EQUAL T_NOTEQ T_LESSEQ T_GREATEREQ
57
58 /* Operator precedence and associativity */
59 %right '='
60 %left T_EQUAL T_NOTEQ
61 %left '<' '>' T_LESSEQ T_GREATEREQ
62 %left '+' '-'
63 %left '*' '/' '%'
64 %nonassoc NEG
65 %right '^'
66 %nonassoc '!'
67
68 %start input
69
70
71 /*
72  *  Grammar rules
73  */
74
75 %%
76 input   : exp {
77                 try {
78                         parsed_ex = $1;
79                         YYACCEPT;
80                 } catch (std::exception &err) {
81                         parser_error = err.what();
82                         YYERROR;
83                 }
84         }
85         | error         {yyclearin; yyerrok;}
86         ;
87
88 exp     : T_NUMBER              {$$ = $1;}
89         | T_SYMBOL {
90                 if (is_lexer_symbol_predefined($1))
91                         $$ = $1.eval();
92                 else
93                         throw (std::runtime_error("unknown symbol '" + ex_to<symbol>($1).get_name() + "'"));
94         }
95         | T_LITERAL             {$$ = $1;}
96         | T_DIGITS              {$$ = $1;}
97         | T_SYMBOL '(' exprseq ')' {
98                 std::string n = ex_to<symbol>($1).get_name();
99                 if (n == "sqrt") {
100                         if ($3.nops() != 1)
101                                 throw (std::runtime_error("too many arguments to sqrt()"));
102                         $$ = sqrt($3.op(0));
103                 } else {
104                         unsigned i = function::find_function(n, $3.nops());
105                         $$ = function(i, ex_to<exprseq>($3)).eval(1);
106                 }
107         }
108         | exp T_EQUAL exp       {$$ = $1 == $3;}
109         | exp T_NOTEQ exp       {$$ = $1 != $3;}
110         | exp '<' exp           {$$ = $1 < $3;}
111         | exp T_LESSEQ exp      {$$ = $1 <= $3;}
112         | exp '>' exp           {$$ = $1 > $3;}
113         | exp T_GREATEREQ exp   {$$ = $1 >= $3;}
114         | exp '+' exp           {$$ = $1 + $3;}
115         | exp '-' exp           {$$ = $1 - $3;}
116         | exp '*' exp           {$$ = $1 * $3;}
117         | exp '/' exp           {$$ = $1 / $3;}
118         | '-' exp %prec NEG     {$$ = -$2;}
119         | '+' exp %prec NEG     {$$ = $2;}
120         | exp '^' exp           {$$ = pow($1, $3);}
121         | exp '!'               {$$ = factorial($1);}
122         | '(' exp ')'           {$$ = $2;}
123         | '{' list_or_empty '}' {$$ = $2;}
124         | '[' matrix ']'        {$$ = lst_to_matrix(ex_to<lst>($2));}
125         ;
126
127 exprseq : exp                   {$$ = exprseq($1);}
128         | exprseq ',' exp       {exprseq es(ex_to<exprseq>($1)); $$ = es.append($3);}
129         ;
130
131 list_or_empty: /* empty */      {$$ = *new lst;}
132         | list                  {$$ = $1;}
133         ;
134
135 list    : exp                   {$$ = lst($1);}
136         | list ',' exp          {lst l(ex_to<lst>($1)); $$ = l.append($3);}
137         ;
138
139 matrix  : '[' row ']'           {$$ = lst($2);}
140         | matrix ',' '[' row ']' {lst l(ex_to<lst>($1)); $$ = l.append($4);}
141         ;
142
143 row     : exp                   {$$ = lst($1);}
144         | row ',' exp           {lst l(ex_to<lst>($1)); $$ = l.append($3);}
145         ;
146
147
148 /*
149  *  Routines
150  */
151
152 %%
153 // Get last error encountered by parser
154 std::string get_parser_error(void)
155 {
156         return parser_error;
157 }
158
159 } // namespace GiNaC
160
161 // Error print routine (store error string in parser_error)
162 int ginac_yyerror(char *s)
163 {
164         GiNaC::parser_error = std::string(s) + " at " + std::string(ginac_yytext);
165         return 0;
166 }