From d4593e1981a168677f98e4d3b098696bd31088e2 Mon Sep 17 00:00:00 2001 From: Alexei Sheplyakov Date: Sun, 14 Sep 2008 02:36:16 +0400 Subject: [PATCH] [bugfix] parser::parse_literal_expr(): don't forget to consume the token... ... so parser won't process it twice (and get either spurious error or wrong result). --- check/parser_bugs.cpp | 18 ++++++++++++++++++ ginac/parser/parser.cpp | 1 + 2 files changed, 19 insertions(+) diff --git a/check/parser_bugs.cpp b/check/parser_bugs.cpp index 340fe20f..a83f668a 100644 --- a/check/parser_bugs.cpp +++ b/check/parser_bugs.cpp @@ -41,6 +41,23 @@ static int check2(std::ostream& err_str) return 0; } +/// parse_literal_expr forget to consume the token, so parser get +/// very confused. +static int check3(std::ostream& err_str) +{ + const std::string srep("5-(2*I)/3"); + parser reader; + ex e = reader(srep); + ex g = numeric(5) - (numeric(2)*I)/3; + ex d = (e - g).expand(); + if (!d.is_zero()) { + err_str << "\"" << srep << "\" was misparsed as \"" + << e << "\"" << std::endl; + return 1; + } + return 0; +} + int main(int argc, char** argv) { std::cout << "checking for parser bugs. " << std::flush; @@ -48,6 +65,7 @@ int main(int argc, char** argv) int errors = 0; errors += check1(err_str); errors += check2(err_str); + errors += check3(err_str); if (errors) { std::cout << "Yes, unfortunately:" << std::endl; std::cout << err_str.str(); diff --git a/ginac/parser/parser.cpp b/ginac/parser/parser.cpp index f48d37fd..6cac523c 100644 --- a/ginac/parser/parser.cpp +++ b/ginac/parser/parser.cpp @@ -121,6 +121,7 @@ ex parser::parse_number_expr() /// literal_expr: 'I' | 'Pi' | 'Euler' | 'Catalan' ex parser::parse_literal_expr() { + get_next_tok(); // consume the literal if (scanner->str == "I") return I; else if (scanner->str == "Pi") -- 2.44.0