3 * Implementation of GiNaC's parser. */
6 * GiNaC Copyright (C) 1999-2009 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
35 /// identifier_expr: identifier | identifier '(' expression* ')'
36 ex parser::parse_identifier_expr()
38 std::string name = scanner->str;
39 get_next_tok(); // eat identifier.
41 if (token != '(') // symbol
42 return find_or_insert_symbol(name, syms, strict);
44 // function/ctor call.
45 get_next_tok(); // eat (
49 ex e = parse_expression();
56 Parse_error("expected ')' or ',' in argument list");
63 prototype the_prototype = make_pair(name, args.size());
64 prototype_table::const_iterator reader = funcs.find(the_prototype);
65 if (reader == funcs.end()) {
66 Parse_error_("no function \"" << name << "\" with " <<
67 args.size() << " arguments");
69 ex ret = function(reader->second, args);
73 /// paren_expr: '(' expression ')'
74 ex parser::parse_paren_expr()
76 get_next_tok(); // eat (.
77 ex e = parse_expression();
80 Parse_error("expected ')'");
81 get_next_tok(); // eat ).
85 extern numeric* _num_1_p;
88 /// unary_expr: [+-] expression
89 ex parser::parse_unary_expr()
91 // Unlike most other parse_* method this one does NOT consume
92 // current token so parse_binop_rhs() knows what kind of operator
95 // There are different kinds of expressions which need to be handled:
100 // Delegete the work to parse_binop_rhs(), otherwise we end up
101 // duplicating it here.
102 ex lhs = _ex0; // silly trick
103 ex e = parse_binop_rhs(0, lhs);
107 /// primary: identifier_expr | number_expr | paren_expr | unary_expr
108 ex parser::parse_primary()
111 case lexer::token_type::identifier:
112 return parse_identifier_expr();
113 case lexer::token_type::number:
114 return parse_number_expr();
116 return parse_paren_expr();
119 return parse_unary_expr();
120 case lexer::token_type::literal:
121 return parse_literal_expr();
122 case lexer::token_type::eof:
124 Parse_error("unexpected token");
128 /// expression ::= primary binoprhs
129 ex parser::parse_expression()
131 ex lhs = parse_primary();
132 ex res = parse_binop_rhs(0, lhs);
136 /// number_expr: number
137 ex parser::parse_number_expr()
139 ex n = numeric(scanner->str.c_str());
140 get_next_tok(); // consume the number
144 /// literal_expr: 'I' | 'Pi' | 'Euler' | 'Catalan'
145 ex parser::parse_literal_expr()
147 get_next_tok(); // consume the literal
148 if (scanner->str == "I")
150 else if (scanner->str == "Pi")
152 else if (scanner->str == "Euler")
154 else if (scanner->str == "Catalan")
156 bug("unknown literal: \"" << scanner->str << "\"");
159 ex parser::operator()(std::istream& input)
161 scanner->switch_input(&input);
163 ex ret = parse_expression();
164 // parse_expression() stops if it encounters an unknown token.
165 // This is not a bug: since the parser is recursive checking
166 // whether the next token is valid is responsibility of the caller.
167 // Hence make sure nothing is left in the stream:
168 if (token != lexer::token_type::eof)
169 Parse_error("expected EOF");
174 ex parser::operator()(const std::string& input)
176 std::istringstream is(input);
177 ex ret = operator()(is);
181 int parser::get_next_tok()
183 token = scanner->gettok();
187 parser::parser(const symtab& syms_, const bool strict_,
188 const prototype_table& funcs_) : strict(strict_),
189 funcs(funcs_), syms(syms_)
191 scanner = new lexer();