Debian Codegraph @

demos/calc/calclex.l

raw
1 /* Lexical analyzer for calc program.
2
3 Copyright 2000-2002 Free Software Foundation, Inc.
4
5 This file is part of the GNU MP Library.
6
7 This program is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free Software
9 Foundation; either version 3 of the License, or (at your option) any later
10 version.
11
12 This program is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
14 PARTICULAR PURPOSE. See the GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License along with
17 this program. If not, see https://www.gnu.org/licenses/. */
18
19 %{
20 #include <string.h>
21 #include "calc-common.h"
22
23
24 #if WITH_READLINE
25 /* Let GNU flex use readline. See the calcread.c redefined input() for a
26 way that might work for a standard lex too. */
27 #define YY_INPUT(buf,result,max_size) \
28 result = calc_input (buf, max_size);
29 #endif
30
31
32 /* Non-zero when reading the second or subsequent line of an expression,
33 used to give a different prompt when using readline. */
34 int calc_more_input = 0;
35
36
37 const struct calc_keywords_t calc_keywords[] = {
38 { "abs", ABS },
39 { "bin", BIN },
40 { "decimal", DECIMAL },
41 { "fib", FIB },
42 { "hex", HEX },
43 { "help", HELP },
44 { "gcd", GCD },
45 { "kron", KRON },
46 { "lcm", LCM },
47 { "lucnum", LUCNUM },
48 { "nextprime", NEXTPRIME },
49 { "powm", POWM },
50 { "quit", QUIT },
51 { "root", ROOT },
52 { "sqrt", SQRT },
53 { NULL }
54 };
55 %}
56
57 %%
58
59 [ \t\f] { /* white space is skipped */ }
60
61 [;\n] { /* semicolon or newline separates statements */
62 calc_more_input = 0;
63 return EOS; }
64 \\\n { /* escaped newlines are skipped */ }
65
66
67 #(([^\\\n]*)\\)+\n {
68 /* comment through to escaped newline is skipped */ }
69 #[^\n]*\n { /* comment through to newline is a separator */
70 calc_more_input = 0;
71 return EOS; }
72 #[^\n]* { /* comment through to EOF skipped */ }
73
74
75 [-+*/%()<>^!=,] { return yytext[0]; }
76 "<=" { return LE; }
77 ">=" { return GE; }
78 "==" { return EQ; }
79 "!=" { return NE; }
80 "<<" { return LSHIFT; }
81 ">>" { return RSHIFT; }
82 "&&" { return LAND; }
83 "||" { return LOR; }
84
85 (0[xX])?[0-9A-F]+ {
86 yylval.str = yytext;
87 return NUMBER; }
88
89 [a-zA-Z][a-zA-Z0-9]* {
90 int i;
91
92 for (i = 0; calc_keywords[i].name != NULL; i++)
93 if (strcmp (yytext, calc_keywords[i].name) == 0)
94 return calc_keywords[i].value;
95
96 if (yytext[0] >= 'a' && yytext[0] <= 'z' && yytext[1] == '\0')
97 {
98 yylval.var = yytext[0] - 'a';
99 return VARIABLE;
100 }
101
102 return BAD;
103 }
104
105 . { return BAD; }
106
107 %%
108
109 int
110 yywrap ()
111 {
112 return 1;
113 }