1 /** @file input_lexer.ll
3 * Lexical analyzer definition for reading expressions.
4 * This file must be processed with flex. */
7 * GiNaC Copyright (C) 1999-2007 Johannes Gutenberg University Mainz, Germany
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
37 #include "input_lexer.h"
46 using namespace GiNaC;
49 #include "input_parser.h"
53 // Table of all used symbols/indices
55 sym_def() : predefined(false) {}
56 sym_def(const ex &s, bool predef) : sym(s), predefined(predef) {}
59 sym_def(const sym_def &other) {sym = other.sym; predefined = other.predefined;}
60 const sym_def &operator=(const sym_def &other)
64 predefined = other.predefined;
70 bool predefined; // true = user supplied symbol, false = lexer generated symbol
72 typedef std::map<std::string, sym_def> sym_tab;
76 static int lexer_input(char *buf, int max_size);
77 #define YY_INPUT(buf, result, max_size) (result = lexer_input(buf, max_size))
92 [ \t\n]+ /* skip whitespace */
95 Pi ginac_yylval = Pi; return T_LITERAL;
96 Euler ginac_yylval = Euler; return T_LITERAL;
97 Catalan ginac_yylval = Catalan; return T_LITERAL;
98 FAIL ginac_yylval = *new fail(); return T_LITERAL;
99 I ginac_yylval = I; return T_NUMBER;
100 Digits ginac_yylval = (long)Digits; return T_DIGITS;
105 "<=" return T_LESSEQ;
106 ">=" return T_GREATEREQ;
116 {D}+{E} ginac_yylval = numeric(yytext); return T_NUMBER;
120 sym_tab::const_iterator i = syms.find(yytext);
121 if (i == syms.end()) {
124 syms[yytext] = sym_def(tmp, false);
126 ginac_yylval = (*i).second.sym;
131 <<EOF>> return T_EOF;
133 /* everything else */
143 // The string from which we will read
144 static std::string lexer_string;
146 // The current position within the string
147 static int curr_pos = 0;
149 // Input function that reads from string
150 static int lexer_input(char *buf, int max_size)
152 int actual = lexer_string.length() - curr_pos;
153 if (actual > max_size)
157 lexer_string.copy(buf, actual, curr_pos);
162 // EOF encountered, terminate the scanner
170 // Set the input string
171 void set_lexer_string(const std::string &s)
177 // Get name of symbol/index
178 std::string get_symbol_name(const ex & s)
181 return ex_to<symbol>(s).get_name();
182 else if (is_a<idx>(s) && is_a<symbol>(s.op(0)))
183 return ex_to<symbol>(s.op(0)).get_name();
185 throw (std::runtime_error("get_symbol_name(): unexpected expression type"));
188 // Set the list of predefined symbols/indices
189 void set_lexer_symbols(ex l)
192 if (!is_exactly_a<lst>(l))
194 for (unsigned i=0; i<l.nops(); i++) {
195 const ex &o = l.op(i);
196 if (is_a<symbol>(o) || (is_a<idx>(o) && is_a<symbol>(o.op(0))))
197 syms[get_symbol_name(o)] = sym_def(o, true);
201 // Check whether symbol/index was predefined
202 bool is_lexer_symbol_predefined(const ex &s)
204 sym_tab::const_iterator i = syms.find(get_symbol_name(s));
208 return (*i).second.predefined;