3 * Interface to 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
23 #ifndef GINAC_PARSER_H
24 #define GINAC_PARSER_H
26 #include "parse_context.h"
35 class parse_error : public std::invalid_argument
38 const std::size_t line;
39 const std::size_t column;
40 parse_error(const std::string& what_,
41 const std::size_t line_ = 0,
42 const std::size_t column_ = 0) throw () :
43 std::invalid_argument(what_), line(line_), column(column_)
48 * Recursive descent parser for GiNaC expressions.
52 // The actual parser rules (in EBNF-alike notation):
54 /// expression: primary binoprhs
55 ex parse_expression();
57 /// primary: indentifier_expr | number_expr | paren_expr | unary_expr
60 /// binoprhs: ([+*/^-] primary)*
61 ex parse_binop_rhs(int, ex&);
63 /// identifier_expr: identifier |
64 /// identifier '(' expression (',' expression)* ')'
65 ex parse_identifier_expr();
67 /// paren_expr: '(' expression ')'
68 ex parse_paren_expr();
70 /// number_expr: number
71 ex parse_number_expr();
73 /// unary_expr: [+-] expression
74 ex parse_unary_expr();
76 /// literal_expr: 'I' | 'Pi' | 'Euler' | 'Catalan'
77 ex parse_literal_expr();
81 * @param syms_ symbol table.
82 * @param funcs_ function/ctors table.
83 * @param strict_ if true, throw an exception if unknown
84 * symbol is encountered.
86 parser(const symtab& syms_ = symtab(),
87 const bool strict_ = false,
88 const prototype_table& funcs_ = get_default_reader());
91 /// parse the stream @a input
92 ex operator()(std::istream& input);
93 /// parse the string @a input
94 ex operator()(const std::string& input);
96 /// report the symbol table used by parser
97 symtab get_syms() const
101 /// read/write access to the symbol table
107 /// If true, throw an exception if an unknown symbol is encountered.
111 * Function/ctor table, maps a prototype (which is a name and number
112 * arguments) to a C++ function. Used for parsing identifier_expr's
113 * (see parse_identifier_expr). If expression contains unknown
114 * prototype, an exception will be thrown.
116 const prototype_table funcs;
118 * Symbol (variable) table. Used for parsing identifier_expr's
119 * (see parse_identifier_expr). If parser is strict, exception is
120 * thrown if an unknown symbol is encountered. Non-strict parser
121 * appends unknown symbols to the symbol table.
126 /// current token the parser is looking at
128 /// read the next token from the scanner
134 #endif // ndef GINAC_PARSER_H