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