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
34 /// identifier_expr: identifier | identifier '(' expression* ')'
35 ex parser::parse_identifier_expr()
37 std::string name = scanner->str;
38 get_next_tok(); // eat identifier.
40 if (token != '(') // symbol
41 return find_or_insert_symbol(name, syms, strict);
43 // function/ctor call.
44 get_next_tok(); // eat (
48 ex e = parse_expression();
55 Parse_error("expected ')' or ',' in argument list");
62 prototype the_prototype = make_pair(name, args.size());
63 prototype_table::const_iterator reader = funcs.find(the_prototype);
64 if (reader == funcs.end()) {
65 Parse_error_("no function \"" << name << "\" with " <<
66 args.size() << " arguments");
68 ex ret = reader->second(args);
72 /// paren_expr: '(' expression ')'
73 ex parser::parse_paren_expr()
75 get_next_tok(); // eat (.
76 ex e = parse_expression();
79 Parse_error("expected ')'");
80 get_next_tok(); // eat ).
84 extern numeric* _num_1_p;
87 /// unary_expr: [+-] expression
88 ex parser::parse_unary_expr()
90 // Unlike most other parse_* method this one does NOT consume
91 // current token so parse_binop_rhs() knows what kind of operator
94 // There are different kinds of expressions which need to be handled:
99 // Delegete the work to parse_binop_rhs(), otherwise we end up
100 // duplicating it here.
101 ex lhs = _ex0; // silly trick
102 ex e = parse_binop_rhs(0, lhs);
106 /// primary: identifier_expr | number_expr | paren_expr | unary_expr
107 ex parser::parse_primary()
110 case lexer::token_type::identifier:
111 return parse_identifier_expr();
112 case lexer::token_type::number:
113 return parse_number_expr();
115 return parse_paren_expr();
118 return parse_unary_expr();
119 case lexer::token_type::literal:
120 return parse_literal_expr();
121 case lexer::token_type::eof:
123 Parse_error("unexpected token");
127 /// expression ::= primary binoprhs
128 ex parser::parse_expression()
130 ex lhs = parse_primary();
131 ex res = parse_binop_rhs(0, lhs);
135 /// number_expr: number
136 ex parser::parse_number_expr()
138 ex n = numeric(scanner->str.c_str());
139 get_next_tok(); // consume the number
143 /// literal_expr: 'I' | 'Pi' | 'Euler' | 'Catalan'
144 ex parser::parse_literal_expr()
146 get_next_tok(); // consume the literal
147 if (scanner->str == "I")
149 else if (scanner->str == "Pi")
151 else if (scanner->str == "Euler")
153 else if (scanner->str == "Catalan")
155 bug("unknown literal: \"" << scanner->str << "\"");
158 ex parser::operator()(std::istream& input)
160 scanner->switch_input(&input);
162 ex ret = parse_expression();
163 // parse_expression() stops if it encounters an unknown token.
164 // This is not a bug: since the parser is recursive checking
165 // whether the next token is valid is responsibility of the caller.
166 // Hence make sure nothing is left in the stream:
167 if (token != lexer::token_type::eof)
168 Parse_error("expected EOF");
173 ex parser::operator()(const std::string& input)
175 std::istringstream is(input);
176 ex ret = operator()(is);
180 int parser::get_next_tok()
182 token = scanner->gettok();
186 parser::parser(const symtab& syms_, const bool strict_,
187 const prototype_table& funcs_) : strict(strict_),
188 funcs(funcs_), syms(syms_)
190 scanner = new lexer();