1 /** @file input_parser.yy
3 * Input grammar definition for reading expressions.
4 * This file must be processed with yacc/bison. */
7 * GiNaC Copyright (C) 1999-2001 Johannes Gutenberg University Mainz, Germany
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.
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.
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
32 #include "input_lexer.h"
34 #include "relational.h"
44 #define YYERROR_VERBOSE 1
46 // Parsed output expression
49 // Last error message returned by parser
50 static std::string parser_error;
53 /* Tokens (T_LITERAL means a literal value returned by the parser, but not
54 of class numeric or symbol (e.g. a constant or the FAIL object)) */
55 %token T_NUMBER T_SYMBOL T_LITERAL T_DIGITS T_EQUAL T_NOTEQ T_LESSEQ T_GREATEREQ T_MATRIX_BEGIN T_MATRIX_END
57 /* Operator precedence and associativity */
60 %left '<' '>' T_LESSEQ T_GREATEREQ
79 } catch (std::exception &err) {
80 parser_error = err.what();
84 | error {yyclearin; yyerrok;}
87 exp : T_NUMBER {$$ = $1;}
89 if (is_lexer_symbol_predefined($1))
92 throw (std::runtime_error("unknown symbol '" + ex_to_symbol($1).get_name() + "'"));
94 | T_LITERAL {$$ = $1;}
96 | T_SYMBOL '(' exprseq ')' {
97 unsigned i = function::find_function(ex_to_symbol($1).get_name(), $3.nops());
98 $$ = function(i, static_cast<const exprseq &>(*($3.bp))).eval(1);
100 | exp T_EQUAL exp {$$ = $1 == $3;}
101 | exp T_NOTEQ exp {$$ = $1 != $3;}
102 | exp '<' exp {$$ = $1 < $3;}
103 | exp T_LESSEQ exp {$$ = $1 <= $3;}
104 | exp '>' exp {$$ = $1 > $3;}
105 | exp T_GREATEREQ exp {$$ = $1 >= $3;}
106 | exp '+' exp {$$ = $1 + $3;}
107 | exp '-' exp {$$ = $1 - $3;}
108 | exp '*' exp {$$ = $1 * $3;}
109 | exp '/' exp {$$ = $1 / $3;}
110 | '-' exp %prec NEG {$$ = -$2;}
111 | '+' exp %prec NEG {$$ = $2;}
112 | exp '^' exp {$$ = pow($1, $3);}
113 | exp '!' {$$ = factorial($1);}
114 | '(' exp ')' {$$ = $2;}
115 | '[' list_or_empty ']' {$$ = $2;}
116 | T_MATRIX_BEGIN matrix T_MATRIX_END {$$ = lst_to_matrix(ex_to_lst($2));}
119 exprseq : exp {$$ = exprseq($1);}
120 | exprseq ',' exp {exprseq es(static_cast<exprseq &>(*($1.bp))); $$ = es.append($3);}
123 list_or_empty: /* empty */ {$$ = *new lst;}
127 list : exp {$$ = lst($1);}
128 | list ',' exp {lst l(static_cast<lst &>(*($1.bp))); $$ = l.append($3);}
131 matrix : T_MATRIX_BEGIN row T_MATRIX_END {$$ = lst($2);}
132 | matrix ',' T_MATRIX_BEGIN row T_MATRIX_END {lst l(static_cast<lst &>(*($1.bp))); $$ = l.append($4);}
135 row : exp {$$ = lst($1);}
136 | row ',' exp {lst l(static_cast<lst &>(*($1.bp))); $$ = l.append($3);}
145 // Get last error encountered by parser
146 std::string get_parser_error(void)
153 // Error print routine (store error string in parser_error)
154 int ginac_yyerror(char *s)
156 GiNaC::parser_error = std::string(s) + " at " + std::string(ginac_yytext);