]> www.ginac.de Git - ginac.git/blob - ginac/input_lexer.ll
- dirac_trace() is twice as fast
[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-2001 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
42 using namespace GiNaC;
43 namespace GiNaC {
44
45 #include "input_parser.h"
46
47 } // namespace GiNaC
48
49 // Table of all used symbols
50 struct sym_def {
51         sym_def() : predefined(false) {}
52         sym_def(const ex &s, bool predef) : sym(s), predefined(predef) {}
53         ~sym_def() {}
54
55         sym_def(const sym_def &other) {sym = other.sym; predefined = other.predefined;}
56         const sym_def &operator=(const sym_def &other)
57         {
58                 if (this != &other) {
59                         sym = other.sym;
60                         predefined = other.predefined;
61                 }
62                 return *this;
63         }
64
65         ex sym;
66         bool predefined;        // true = user supplied symbol, false = lexer generated symbol
67 };
68 typedef std::map<std::string, sym_def> sym_tab;
69 static sym_tab syms;
70
71 // lex input function
72 static int lexer_input(char *buf, int max_size);
73 #define YY_INPUT(buf, result, max_size) (result = lexer_input(buf, max_size))
74 %}
75
76         /* Abbreviations */
77 D       [0-9]
78 E       [elEL][-+]?{D}+
79 A       [a-zA-Z_]
80 AN      [0-9a-zA-Z_]
81
82
83 /*
84  *  Lexical rules
85  */
86
87 %%
88 [ \t]+                  /* skip whitespace */
89
90                         /* special values */
91 Pi                      ginac_yylval = Pi; return T_LITERAL;
92 Euler                   ginac_yylval = Euler; return T_LITERAL;
93 Catalan                 ginac_yylval = Catalan; return T_LITERAL;
94 FAIL                    ginac_yylval = *new fail(); return T_LITERAL;
95 I                       ginac_yylval = I; return T_NUMBER;
96 Digits                  ginac_yylval = (long)Digits; return T_DIGITS;
97
98                         /* comparison */
99 "=="                    return T_EQUAL;
100 "!="                    return T_NOTEQ;
101 "<="                    return T_LESSEQ;
102 ">="                    return T_GREATEREQ;
103
104                         /* matrix delimiters */
105 \[\[                    return T_MATRIX_BEGIN;
106 \]\]                    return T_MATRIX_END;
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                         /* everything else */
129 .                       return *yytext;
130
131 %%
132
133
134 /*
135  *  Routines
136  */
137
138 // The string from which we will read
139 static std::string lexer_string;
140
141 // The current position within the string
142 static int curr_pos = 0;
143
144 // Input function that reads from string
145 static int lexer_input(char *buf, int max_size)
146 {
147         int actual = lexer_string.length() - curr_pos;
148         if (actual > max_size)
149                 actual = max_size;
150         if (actual <= 0)
151                 return YY_NULL;
152         lexer_string.copy(buf, actual, curr_pos);
153         curr_pos += actual;
154         return actual;
155 }
156
157 // EOF encountered, terminate the scanner
158 int ginac_yywrap()
159 {
160         return 1;
161 }
162
163 namespace GiNaC {
164
165 // Set the input string
166 void set_lexer_string(const std::string &s)
167 {
168         lexer_string = s;
169         curr_pos = 0;
170 }
171
172 // Set the list of predefined symbols
173 void set_lexer_symbols(ex l)
174 {
175         syms.clear();
176         if (!is_ex_exactly_of_type(l, lst))
177                 return;
178         for (int i=0; i<l.nops(); i++) {
179                 if (is_ex_exactly_of_type(l.op(i), symbol))
180                         syms[ex_to_symbol(l.op(i)).get_name()] = sym_def(l.op(i), true);
181         }
182 }
183
184 // Check whether symbol was predefined
185 bool is_lexer_symbol_predefined(const ex &s)
186 {
187         sym_tab::const_iterator i = syms.find(ex_to_symbol(s).get_name());
188         if (i == syms.end())
189                 return false;
190         else
191                 return (*i).second.predefined;
192 }
193
194 } // namespace GiNaC