X-Git-Url: https://ginac.de/ginac.git//ginac.git?a=blobdiff_plain;f=ginac%2Finput_parser.yy;h=fffbd1792531a874fba86af122ae65be50db4680;hb=e98841136efa88c951edafc0cd43ba1343f20b5b;hp=778b8347da3c88a239b8c83a4a4fa4a123ba2e18;hpb=09f37bdbd46f469b3a8a902a43d0f795c41a89bf;p=ginac.git diff --git a/ginac/input_parser.yy b/ginac/input_parser.yy index 778b8347..fffbd179 100644 --- a/ginac/input_parser.yy +++ b/ginac/input_parser.yy @@ -4,7 +4,7 @@ * This file must be processed with yacc/bison. */ /* - * GiNaC Copyright (C) 1999-2002 Johannes Gutenberg University Mainz, Germany + * GiNaC Copyright (C) 1999-2003 Johannes Gutenberg University Mainz, Germany * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -37,6 +37,8 @@ #include "lst.h" #include "power.h" #include "exprseq.h" +#include "idx.h" +#include "indexed.h" #include "matrix.h" #include "inifcns.h" @@ -49,11 +51,14 @@ ex parsed_ex; // Last error message returned by parser static std::string parser_error; + +// Prototypes +ex attach_index(const ex & base, ex i, bool covariant); %} /* 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 +%token T_EOF T_NUMBER T_SYMBOL T_LITERAL T_DIGITS T_EQUAL T_NOTEQ T_LESSEQ T_GREATEREQ /* Operator precedence and associativity */ %right '=' @@ -63,6 +68,7 @@ static std::string parser_error; %left '*' '/' '%' %nonassoc NEG %right '^' +%left '.' '~' %nonassoc '!' %start input @@ -73,7 +79,7 @@ static std::string parser_error; */ %% -input : exp { +input : exp T_EOF { try { parsed_ex = $1; YYACCEPT; @@ -82,7 +88,6 @@ input : exp { YYERROR; } } - | error {yyclearin; yyerrok;} ; exp : T_NUMBER {$$ = $1;} @@ -90,12 +95,12 @@ exp : T_NUMBER {$$ = $1;} if (is_lexer_symbol_predefined($1)) $$ = $1.eval(); else - throw (std::runtime_error("unknown symbol '" + ex_to($1).get_name() + "'")); + throw (std::runtime_error("unknown symbol '" + get_symbol_name($1) + "'")); } | T_LITERAL {$$ = $1;} | T_DIGITS {$$ = $1;} | T_SYMBOL '(' exprseq ')' { - std::string n = ex_to($1).get_name(); + std::string n = get_symbol_name($1); if (n == "sqrt") { if ($3.nops() != 1) throw (std::runtime_error("too many arguments to sqrt()")); @@ -118,6 +123,8 @@ exp : T_NUMBER {$$ = $1;} | '-' exp %prec NEG {$$ = -$2;} | '+' exp %prec NEG {$$ = $2;} | exp '^' exp {$$ = pow($1, $3);} + | exp '.' exp {$$ = attach_index($1, $3, true);} + | exp '~' exp {$$ = attach_index($1, $3, false);} | exp '!' {$$ = factorial($1);} | '(' exp ')' {$$ = $2;} | '{' list_or_empty '}' {$$ = $2;} @@ -150,6 +157,30 @@ row : exp {$$ = lst($1);} */ %% +// Attach index to expression +ex attach_index(const ex & base, ex i, bool covariant) +{ + // Toggle index variance if necessary + if (is_a(i)) { + const varidx &vi = ex_to(i); + if (vi.is_covariant() != covariant) + i = vi.toggle_variance(); + } else if (!covariant) + throw (std::runtime_error("index '" + get_symbol_name(i) + "' is not a varidx and cannot be contravariant")); + + // Add index to an existing indexed object, or create a new indexed + // object if there are no indices yet + if (is_a(base)) { + const ex &b = base.op(0); + exvector iv; + for (unsigned n=1; n