1 /** @file parser_bugs.cpp
3 * Check for some silly bugs in the parser. */
6 * GiNaC Copyright (C) 1999-2011 Johannes Gutenberg University Mainz, Germany
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 using namespace GiNaC;
31 // - a - b was misparsed as -a + b due to a bug in parser::parse_unary_expr()
32 static int check1(std::ostream& err_str)
34 const std::string srep("-a-b");
37 ex a = reader.get_syms()["a"];
38 ex b = reader.get_syms()["b"];
40 ex d = (e - g).expand();
42 err_str << "\"" << srep << "\" was misparsed as \""
43 << e << "\"" << std::endl;
49 /// Parser was rejecting the valid expression '5 - (3*x)/10'.
50 static int check2(std::ostream& err_str)
52 const std::string srep("5-(3*x)/10");
55 ex x = reader.get_syms()["x"];
57 ex d = (e - g).expand();
59 err_str << "\"" << srep << "\" was misparsed as \""
60 << e << "\"" << std::endl;
66 /// parse_literal_expr forget to consume the token, so parser get
68 static int check3(std::ostream& err_str)
70 const std::string srep("5-(2*I)/3");
73 ex g = numeric(5) - (numeric(2)*I)/3;
74 ex d = (e - g).expand();
76 err_str << "\"" << srep << "\" was misparsed as \""
77 << e << "\"" << std::endl;
83 /// parser happily accepted various junk like 'x^2()+1'
84 static int check4(std::ostream& err_str)
86 const std::string junk("x^2()+1");
91 err_str << "parser accepts junk: \"" << junk << "\"" << std::endl;
93 } catch (parse_error& err) {
94 // Ok, parser rejects the nonsense.
99 int main(int argc, char** argv)
101 std::cout << "checking for parser bugs. " << std::flush;
102 std::ostringstream err_str;
104 errors += check1(err_str);
105 errors += check2(err_str);
106 errors += check3(err_str);
107 errors += check4(err_str);
109 std::cout << "Yes, unfortunately:" << std::endl;
110 std::cout << err_str.str();
112 std::cout << "Not found. ";