-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlexer.l
More file actions
157 lines (127 loc) · 2.79 KB
/
lexer.l
File metadata and controls
157 lines (127 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
%{
#include <iostream>
#include <string>
#include "y.tab.h"
extern YYSTYPE yylval;
char atoc(const char* str) {
switch(str[1]) {
case '\"': return '\"';
case '\'': return '\'';
case '\\': return '\\';
case '0': return '\0';
case 'a': return '\a';
case 'b': return '\b';
case 'f': return '\f';
case 'n': return '\n';
case 'r': return '\r';
case 't': return '\t';
case 'v': return '\v';
default: return 0;
}
}
%}
%option noyywrap
/* helpers */
DIGIT [0-9]
LETTER [a-zA-Z]
LP "("
RP ")"
UNIT "{}"|"()"
NL "\n"
WS (" "|"\t")
CHR \'(\\.|[^\'])\'
STR \"(\\.|[^\"])*\"
/* basics */
ID ({LETTER}|"_")({LETTER}|{DIGIT}|"_")*
REAL ({DIGIT}*"."{DIGIT}+)|({DIGIT}+"."{DIGIT}*)
INT {DIGIT}+
CBEG "(*"
CEND "*)"
CONS "::"
/* scope delimiters */
LET "let"
IN "in"
END "end"
/* conditionals */
IF "if"
THEN "then"
ELSE "else"
/* logical operators */
EQ "=="
NE "!="
GT ">"
GE ">="
LT "<"
LE "<="
/* booleans and boolean operators */
BOOL "true"|"false"
AND "&&"
OR "||"
NOT "!"
/* arithmetic operators */
ADD "+"
SUB "-"
MUL "*"
DIV "/"
MOD "%"
NEG "~"
/* values and functions */
BIND "="
VAL "val"
FUN "fun"
FN "fn"
/* records and lists */
RBEG "{"
REND "}"
LBEG "["
LEND "]"
SEP ","
GET "."
%%
{LP} { return LP; }
{RP} { return RP; }
{CBEG} { return CBEG; }
{CEND} { return CEND; }
{LET} { printf("lexer sees LET\n"); return LET; }
{IN} { return IN; }
{END} { return END; }
{IF} { return IF; }
{THEN} { return THEN; }
{ELSE} { return ELSE; }
{EQ} { return EQ; }
{NE} { return NE; }
{LT} { return LT; }
{LE} { return LE; }
{GT} { return GT; }
{GE} { return GE; }
{BOOL} { yylval = new BoolNode(yytext == "true"); return BOOL; }
{AND} { return AND; }
{OR} { return OR; }
{NOT} { return NOT; }
{ADD} { return ADD; }
{SUB} { return SUB; }
{MUL} { return MUL; }
{DIV} { return DIV; }
{BIND} { return BIND; }
{VAL} { return VAL; }
{FUN} { return FUN; }
{FN} { return FN; }
{UNIT} { return UNIT; }
{CONS} { return CONS; }
{RBEG} { return RBEG; }
{REND} { return REND; }
{LBEG} { return LBEG; }
{LEND} { return LEND; }
{SEP} { return SEP; }
{GET} { return GET; }
{CHR} { if (*yytext == '/') yylval = new CharNode(atoc(yytext));
else yylval = new CharNode(*yytext); return CHR; }
{STR} { std::string *str = new std::string(yytext);
yylval = new StringNode(str->substr(1, str->length()-2)); return STR; }
{ID} { yylval = new IdentifierNode(yytext); return ID; }
{REAL} { yylval = new RealNode(atof(yytext)); return REAL; }
{INT} { yylval = new IntNode(atoi(yytext)); return INT; }
{NL} { }
{WS} { }
%%
extern int main();