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-2004 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
35 #include "input_lexer.h"
44 using namespace GiNaC;
47 #include "input_parser.h"
51 // Table of all used symbols/indices
53 sym_def() : predefined(false) {}
54 sym_def(const ex &s, bool predef) : sym(s), predefined(predef) {}
57 sym_def(const sym_def &other) {sym = other.sym; predefined = other.predefined;}
58 const sym_def &operator=(const sym_def &other)
62 predefined = other.predefined;
68 bool predefined; // true = user supplied symbol, false = lexer generated symbol
70 typedef std::map<std::string, sym_def> sym_tab;
74 static int lexer_input(char *buf, int max_size);
75 #define YY_INPUT(buf, result, max_size) (result = lexer_input(buf, max_size))
90 [ \t\n]+ /* skip whitespace */
93 Pi ginac_yylval = Pi; return T_LITERAL;
94 Euler ginac_yylval = Euler; return T_LITERAL;
95 Catalan ginac_yylval = Catalan; return T_LITERAL;
96 FAIL ginac_yylval = *new fail(); return T_LITERAL;
97 I ginac_yylval = I; return T_NUMBER;
98 Digits ginac_yylval = (long)Digits; return T_DIGITS;
103 "<=" return T_LESSEQ;
104 ">=" return T_GREATEREQ;
114 {D}+{E} ginac_yylval = numeric(yytext); return T_NUMBER;
118 sym_tab::const_iterator i = syms.find(yytext);
119 if (i == syms.end()) {
120 syms[yytext] = sym_def(ginac_yylval = *(new symbol(yytext)), false);
122 ginac_yylval = (*i).second.sym;
127 <<EOF>> return T_EOF;
129 /* everything else */
139 // The string from which we will read
140 static std::string lexer_string;
142 // The current position within the string
143 static int curr_pos = 0;
145 // Input function that reads from string
146 static int lexer_input(char *buf, int max_size)
148 int actual = lexer_string.length() - curr_pos;
149 if (actual > max_size)
153 lexer_string.copy(buf, actual, curr_pos);
158 // EOF encountered, terminate the scanner
166 // Set the input string
167 void set_lexer_string(const std::string &s)
173 // Get name of symbol/index
174 std::string get_symbol_name(const ex & s)
177 return ex_to<symbol>(s).get_name();
178 else if (is_a<idx>(s) && is_a<symbol>(s.op(0)))
179 return ex_to<symbol>(s.op(0)).get_name();
181 throw (std::runtime_error("get_symbol_name(): unexpected expression type"));
184 // Set the list of predefined symbols/indices
185 void set_lexer_symbols(ex l)
188 if (!is_exactly_a<lst>(l))
190 for (unsigned i=0; i<l.nops(); i++) {
191 const ex &o = l.op(i);
192 if (is_a<symbol>(o) || (is_a<idx>(o) && is_a<symbol>(o.op(0))))
193 syms[get_symbol_name(o)] = sym_def(o, true);
197 // Check whether symbol/index was predefined
198 bool is_lexer_symbol_predefined(const ex &s)
200 sym_tab::const_iterator i = syms.find(get_symbol_name(s));
204 return (*i).second.predefined;