3 * Interface to the parser. */
6 * GiNaC Copyright (C) 1999-2018 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 /// lst_expr: '{' expression { ',' expression } '}'
73 /// number_expr: number
74 ex parse_number_expr();
76 /// unary_expr: [+-] expression
77 ex parse_unary_expr();
79 /// literal_expr: 'I' | 'Pi' | 'Euler' | 'Catalan'
80 ex parse_literal_expr();
84 * @param syms_ symbol table.
85 * @param funcs_ function/ctors table.
86 * @param strict_ if true, throw an exception if unknown
87 * symbol is encountered.
89 parser(const symtab& syms_ = symtab(),
90 const bool strict_ = false,
91 const prototype_table& funcs_ = get_default_reader());
94 /// parse the stream @a input
95 ex operator()(std::istream& input);
96 /// parse the string @a input
97 ex operator()(const std::string& input);
99 /// report the symbol table used by parser
100 symtab get_syms() const
104 /// read/write access to the symbol table
110 /// If true, throw an exception if an unknown symbol is encountered.
114 * Function/ctor table, maps a prototype (which is a name and number
115 * arguments) to a C++ function. Used for parsing identifier_expr's
116 * (see parse_identifier_expr). If expression contains unknown
117 * prototype, an exception will be thrown.
119 const prototype_table funcs;
121 * Symbol (variable) table. Used for parsing identifier_expr's
122 * (see parse_identifier_expr). If parser is strict, exception is
123 * thrown if an unknown symbol is encountered. Non-strict parser
124 * appends unknown symbols to the symbol table.
129 /// current token the parser is looking at
131 /// read the next token from the scanner
137 #endif // ndef GINAC_PARSER_H