X-Git-Url: https://www.ginac.de/ginac.git//ginac.git?p=ginac.git;a=blobdiff_plain;f=ginac%2Finput_parser.yy;h=5df99b6639bca69a3bc3b7fd8e8e3cc9c56f74a0;hp=561b0693ed3038d25cc827ca2775181d3e145157;hb=3a63743e24046766b37c3d1bd38605542ee0a536;hpb=383d5eb3b0f0506810d9105a268f939125bfc347 diff --git a/ginac/input_parser.yy b/ginac/input_parser.yy index 561b0693..5df99b66 100644 --- a/ginac/input_parser.yy +++ b/ginac/input_parser.yy @@ -39,9 +39,7 @@ #include "matrix.h" #include "inifcns.h" -#ifndef NO_NAMESPACE_GINAC namespace GiNaC { -#endif // ndef NO_NAMESPACE_GINAC #define YYERROR_VERBOSE 1 @@ -54,7 +52,7 @@ static std::string parser_error; /* Tokens (T_LITERAL means a literal value returned by the parser, but not of class numeric or symbol (e.g. a constant or the FAIL object)) */ -%token T_NUMBER T_SYMBOL T_LITERAL T_DIGITS T_EQUAL T_NOTEQ T_LESSEQ T_GREATEREQ T_MATRIX_BEGIN T_MATRIX_END +%token T_NUMBER T_SYMBOL T_LITERAL T_DIGITS T_EQUAL T_NOTEQ T_LESSEQ T_GREATEREQ /* Operator precedence and associativity */ %right '=' @@ -91,13 +89,20 @@ exp : T_NUMBER {$$ = $1;} if (is_lexer_symbol_predefined($1)) $$ = $1.eval(); else - throw (std::runtime_error("unknown symbol '" + ex_to_symbol($1).getname() + "'")); + throw (std::runtime_error("unknown symbol '" + ex_to($1).get_name() + "'")); } | T_LITERAL {$$ = $1;} | T_DIGITS {$$ = $1;} | T_SYMBOL '(' exprseq ')' { - unsigned i = function::find_function(ex_to_symbol($1).getname(), $3.nops()); - $$ = function(i, static_cast(*($3.bp))).eval(1); + std::string n = ex_to($1).get_name(); + if (n == "sqrt") { + if ($3.nops() != 1) + throw (std::runtime_error("too many arguments to sqrt()")); + $$ = sqrt($3.op(0)); + } else { + unsigned i = function::find_function(n, $3.nops()); + $$ = function(i, ex_to($3)).eval(1); + } } | exp T_EQUAL exp {$$ = $1 == $3;} | exp T_NOTEQ exp {$$ = $1 != $3;} @@ -109,18 +114,17 @@ exp : T_NUMBER {$$ = $1;} | exp '-' exp {$$ = $1 - $3;} | exp '*' exp {$$ = $1 * $3;} | exp '/' exp {$$ = $1 / $3;} - | exp '%' exp {$$ = $1 % $3;} | '-' exp %prec NEG {$$ = -$2;} | '+' exp %prec NEG {$$ = $2;} | exp '^' exp {$$ = pow($1, $3);} | exp '!' {$$ = factorial($1);} | '(' exp ')' {$$ = $2;} - | '[' list_or_empty ']' {$$ = $2;} - | T_MATRIX_BEGIN matrix T_MATRIX_END {$$ = lst_to_matrix($2);} + | '{' list_or_empty '}' {$$ = $2;} + | '[' matrix ']' {$$ = lst_to_matrix(ex_to($2));} ; exprseq : exp {$$ = exprseq($1);} - | exprseq ',' exp {exprseq es(static_cast(*($1.bp))); $$ = es.append($3);} + | exprseq ',' exp {exprseq es(ex_to($1)); $$ = es.append($3);} ; list_or_empty: /* empty */ {$$ = *new lst;} @@ -128,15 +132,15 @@ list_or_empty: /* empty */ {$$ = *new lst;} ; list : exp {$$ = lst($1);} - | list ',' exp {lst l(static_cast(*($1.bp))); $$ = l.append($3);} + | list ',' exp {lst l(ex_to($1)); $$ = l.append($3);} ; -matrix : T_MATRIX_BEGIN row T_MATRIX_END {$$ = lst($2);} - | matrix ',' T_MATRIX_BEGIN row T_MATRIX_END {lst l(static_cast(*($1.bp))); $$ = l.append($4);} +matrix : '[' row ']' {$$ = lst($2);} + | matrix ',' '[' row ']' {lst l(ex_to($1)); $$ = l.append($4);} ; row : exp {$$ = lst($1);} - | row ',' exp {lst l(static_cast(*($1.bp))); $$ = l.append($3);} + | row ',' exp {lst l(ex_to($1)); $$ = l.append($3);} ; @@ -151,14 +155,11 @@ std::string get_parser_error(void) return parser_error; } -#ifndef NO_NAMESPACE_GINAC } // namespace GiNaC -using GiNaC::parser_error; -#endif // ndef NO_NAMESPACE_GINAC - // Error print routine (store error string in parser_error) int ginac_yyerror(char *s) { - parser_error = std::string(s) + " at " + std::string(ginac_yytext); + GiNaC::parser_error = std::string(s) + " at " + std::string(ginac_yytext); + return 0; }