]> www.ginac.de Git - ginac.git/blob - ginac/input_lexer.ll
Fixed information about required autoconf/make versions.
[ginac.git] / ginac / input_lexer.ll
1 /** @file input_lexer.ll
2  *
3  *  Lexical analyzer definition for reading expressions.
4  *  This file must be processed with flex. */
5
6 /*
7  *  GiNaC Copyright (C) 1999-2005 Johannes Gutenberg University Mainz, Germany
8  *
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.
13  *
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.
18  *
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
22  */
23
24
25 /*
26  *  Definitions
27  */
28
29 %pointer
30
31 %{
32 #include <iostream>
33 #include <string>
34 #include <map>
35 #include <stdexcept>
36
37 #include "input_lexer.h"
38 #include "ex.h"
39 #include "constant.h"
40 #include "fail.h"
41 #include "numeric.h"
42 #include "symbol.h"
43 #include "lst.h"
44 #include "idx.h"
45
46 using namespace GiNaC;
47 namespace GiNaC {
48
49 #include "input_parser.h"
50
51 } // namespace GiNaC
52
53 // Table of all used symbols/indices
54 struct sym_def {
55         sym_def() : predefined(false) {}
56         sym_def(const ex &s, bool predef) : sym(s), predefined(predef) {}
57         ~sym_def() {}
58
59         sym_def(const sym_def &other) {sym = other.sym; predefined = other.predefined;}
60         const sym_def &operator=(const sym_def &other)
61         {
62                 if (this != &other) {
63                         sym = other.sym;
64                         predefined = other.predefined;
65                 }
66                 return *this;
67         }
68
69         ex sym;
70         bool predefined;        // true = user supplied symbol, false = lexer generated symbol
71 };
72 typedef std::map<std::string, sym_def> sym_tab;
73 static sym_tab syms;
74
75 // lex input function
76 static int lexer_input(char *buf, int max_size);
77 #define YY_INPUT(buf, result, max_size) (result = lexer_input(buf, max_size))
78 %}
79
80         /* Abbreviations */
81 D       [0-9]
82 E       [elEL][-+]?{D}+
83 A       [a-zA-Z_]
84 AN      [0-9a-zA-Z_]
85
86
87 /*
88  *  Lexical rules
89  */
90
91 %%
92 [ \t\n]+                /* skip whitespace */
93
94                         /* special values */
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;
101
102                         /* comparison */
103 "=="                    return T_EQUAL;
104 "!="                    return T_NOTEQ;
105 "<="                    return T_LESSEQ;
106 ">="                    return T_GREATEREQ;
107
108                         /* numbers */
109 {D}+                    |
110 "#"{D}+"R"{AN}+         |
111 "#b"([01])+             |
112 "#o"[0-7]+              |
113 "#x"[0-9a-fA-F]+        |
114 {D}+"."{D}*({E})?       |
115 {D}*"."{D}+({E})?       |
116 {D}+{E}                 ginac_yylval = numeric(yytext); return T_NUMBER;
117
118                         /* symbols */
119 {A}{AN}*                {
120                                 sym_tab::const_iterator i = syms.find(yytext);
121                                 if (i == syms.end()) {
122                                         syms[yytext] = sym_def(ginac_yylval = *(new symbol(yytext)), false);
123                                 } else
124                                         ginac_yylval = (*i).second.sym;
125                                 return T_SYMBOL;
126                         }
127
128                         /* end of input */
129 <<EOF>>                 return T_EOF;
130
131                         /* everything else */
132 .                       return *yytext;
133
134 %%
135
136
137 /*
138  *  Routines
139  */
140
141 // The string from which we will read
142 static std::string lexer_string;
143
144 // The current position within the string
145 static int curr_pos = 0;
146
147 // Input function that reads from string
148 static int lexer_input(char *buf, int max_size)
149 {
150         int actual = lexer_string.length() - curr_pos;
151         if (actual > max_size)
152                 actual = max_size;
153         if (actual <= 0)
154                 return YY_NULL;
155         lexer_string.copy(buf, actual, curr_pos);
156         curr_pos += actual;
157         return actual;
158 }
159
160 // EOF encountered, terminate the scanner
161 int ginac_yywrap()
162 {
163         return 1;
164 }
165
166 namespace GiNaC {
167
168 // Set the input string
169 void set_lexer_string(const std::string &s)
170 {
171         lexer_string = s;
172         curr_pos = 0;
173 }
174
175 // Get name of symbol/index
176 std::string get_symbol_name(const ex & s)
177 {
178         if (is_a<symbol>(s))
179                 return ex_to<symbol>(s).get_name();
180         else if (is_a<idx>(s) && is_a<symbol>(s.op(0)))
181                 return ex_to<symbol>(s.op(0)).get_name();
182         else
183                 throw (std::runtime_error("get_symbol_name(): unexpected expression type"));
184 }
185
186 // Set the list of predefined symbols/indices
187 void set_lexer_symbols(ex l)
188 {
189         syms.clear();
190         if (!is_exactly_a<lst>(l))
191                 return;
192         for (unsigned i=0; i<l.nops(); i++) {
193                 const ex &o = l.op(i);
194                 if (is_a<symbol>(o) || (is_a<idx>(o) && is_a<symbol>(o.op(0))))
195                         syms[get_symbol_name(o)] = sym_def(o, true);
196         }
197 }
198
199 // Check whether symbol/index was predefined
200 bool is_lexer_symbol_predefined(const ex &s)
201 {
202         sym_tab::const_iterator i = syms.find(get_symbol_name(s));
203         if (i == syms.end())
204                 return false;
205         else
206                 return (*i).second.predefined;
207 }
208
209 } // namespace GiNaC