]> www.ginac.de Git - ginac.git/blob - ginac/input_parser.yy
- fix LaTeX-output bug reported by Stefan, remove obsolete has(matrix,ex).
[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-2001 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 "matrix.h"
40 #include "inifcns.h"
41
42 namespace GiNaC {
43
44 #define YYERROR_VERBOSE 1
45
46 // Parsed output expression
47 ex parsed_ex;
48
49 // Last error message returned by parser
50 static std::string parser_error;
51 %}
52
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
56
57 /* Operator precedence and associativity */
58 %right '='
59 %left T_EQUAL T_NOTEQ
60 %left '<' '>' T_LESSEQ T_GREATEREQ
61 %left '+' '-'
62 %left '*' '/' '%'
63 %nonassoc NEG
64 %right '^'
65 %nonassoc '!'
66
67 %start input
68
69
70 /*
71  *  Grammar rules
72  */
73
74 %%
75 input   : exp {
76                 try {
77                         parsed_ex = $1;
78                         YYACCEPT;
79                 } catch (std::exception &err) {
80                         parser_error = err.what();
81                         YYERROR;
82                 }
83         }
84         | error         {yyclearin; yyerrok;}
85         ;
86
87 exp     : T_NUMBER              {$$ = $1;}
88         | T_SYMBOL {
89                 if (is_lexer_symbol_predefined($1))
90                         $$ = $1.eval();
91                 else
92                         throw (std::runtime_error("unknown symbol '" + ex_to_symbol($1).get_name() + "'"));
93         }
94         | T_LITERAL             {$$ = $1;}
95         | T_DIGITS              {$$ = $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);
99         }
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));}
117         ;
118
119 exprseq : exp                   {$$ = exprseq($1);}
120         | exprseq ',' exp       {exprseq es(static_cast<exprseq &>(*($1.bp))); $$ = es.append($3);}
121         ;
122
123 list_or_empty: /* empty */      {$$ = *new lst;}
124         | list                  {$$ = $1;}
125         ;
126
127 list    : exp                   {$$ = lst($1);}
128         | list ',' exp          {lst l(static_cast<lst &>(*($1.bp))); $$ = l.append($3);}
129         ;
130
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);}
133         ;
134
135 row     : exp                   {$$ = lst($1);}
136         | row ',' exp           {lst l(static_cast<lst &>(*($1.bp))); $$ = l.append($3);}
137         ;
138
139
140 /*
141  *  Routines
142  */
143
144 %%
145 // Get last error encountered by parser
146 std::string get_parser_error(void)
147 {
148         return parser_error;
149 }
150
151 } // namespace GiNaC
152
153 // Error print routine (store error string in parser_error)
154 int ginac_yyerror(char *s)
155 {
156         GiNaC::parser_error = std::string(s) + " at " + std::string(ginac_yytext);
157         return 0;
158 }