-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJavalette.l
153 lines (128 loc) · 5.08 KB
/
Javalette.l
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
/* -*- c++ -*- File generated by the BNF Converter (bnfc 2.9.4). */
/* Lexer definition for use with FLex */
%option noyywrap noinput nounput
%option reentrant bison-bridge bison-locations
/* Additional data for the lexer: a buffer for lexing string literals. */
%option extra-type="Buffer"
%{
#include "Absyn.H"
#include "Bison.H"
#define initialize_lexer javalette__initialize_lexer
/* BEGIN extensible string buffer */
#include "Buffer.H"
/* The initial size of the buffer to lex string literals. */
#define LITERAL_BUFFER_INITIAL_SIZE 1024
/* The pointer to the literal buffer. */
#define literal_buffer yyextra
/* Initialize the literal buffer. */
#define LITERAL_BUFFER_CREATE() literal_buffer = newBuffer(LITERAL_BUFFER_INITIAL_SIZE)
/* Append characters at the end of the buffer. */
#define LITERAL_BUFFER_APPEND(s) bufferAppendString(literal_buffer, s)
/* Append a character at the end of the buffer. */
#define LITERAL_BUFFER_APPEND_CHAR(c) bufferAppendChar(literal_buffer, c)
/* Release the buffer, returning a pointer to its content. */
#define LITERAL_BUFFER_HARVEST() releaseBuffer(literal_buffer)
/* In exceptional cases, e.g. when reaching EOF, we have to free the buffer. */
#define LITERAL_BUFFER_FREE() freeBuffer(literal_buffer)
/* END extensible string buffer */
static void update_loc(YYLTYPE* loc, char* text)
{
loc->first_line = loc->last_line;
loc->first_column = loc->last_column;
int i = 0;
for (; text[i] != '\0'; ++i) {
if (text[i] == '\n') {
++loc->last_line;
loc->last_column = 0;
} else {
++loc->last_column;
}
}
}
#define YY_USER_ACTION update_loc(yylloc, yytext);
%}
LETTER [a-zA-Z]
CAPITAL [A-Z]
SMALL [a-z]
DIGIT [0-9]
IDENT [a-zA-Z0-9'_]
%START CHAR CHARESC CHAREND STRING ESCAPED COMMENT
%% /* Rules. */
<INITIAL>";" return _SEMI;
<INITIAL>"*" return _STAR;
<INITIAL>"{" return _LBRACE;
<INITIAL>"}" return _RBRACE;
<INITIAL>"(" return _LPAREN;
<INITIAL>")" return _RPAREN;
<INITIAL>"," return _COMMA;
<INITIAL>"[]" return _EMPTYBRACK;
<INITIAL>"=" return _EQ;
<INITIAL>":" return _COLON;
<INITIAL>"->" return _RARROW;
<INITIAL>"." return _DOT;
<INITIAL>"[" return _LBRACK;
<INITIAL>"]" return _RBRACK;
<INITIAL>"++" return _DPLUS;
<INITIAL>"--" return _DMINUS;
<INITIAL>"-" return _MINUS;
<INITIAL>"!" return _BANG;
<INITIAL>"&&" return _DAMP;
<INITIAL>"||" return _DBAR;
<INITIAL>"+" return _PLUS;
<INITIAL>"/" return _SLASH;
<INITIAL>"%" return _PERCENT;
<INITIAL>"<" return _LT;
<INITIAL>"<=" return _LDARROW;
<INITIAL>">" return _GT;
<INITIAL>">=" return _GTEQ;
<INITIAL>"==" return _DEQ;
<INITIAL>"!=" return _BANGEQ;
<INITIAL>"boolean" return _KW_boolean;
<INITIAL>"class" return _KW_class;
<INITIAL>"double" return _KW_double;
<INITIAL>"else" return _KW_else;
<INITIAL>"enum" return _KW_enum;
<INITIAL>"extends" return _KW_extends;
<INITIAL>"false" return _KW_false;
<INITIAL>"for" return _KW_for;
<INITIAL>"if" return _KW_if;
<INITIAL>"int" return _KW_int;
<INITIAL>"new" return _KW_new;
<INITIAL>"null" return _KW_null;
<INITIAL>"return" return _KW_return;
<INITIAL>"struct" return _KW_struct;
<INITIAL>"true" return _KW_true;
<INITIAL>"typedef" return _KW_typedef;
<INITIAL>"void" return _KW_void;
<INITIAL>"while" return _KW_while;
<INITIAL>"#"[^\n]* /* skip */; /* BNFC: comment "#" */
<INITIAL>"//"[^\n]* /* skip */; /* BNFC: comment "//" */
<INITIAL>"/*" BEGIN COMMENT;
<COMMENT>"*/" BEGIN INITIAL;
<COMMENT>. /* skip */;
<COMMENT>[\n] /* skip */;
<INITIAL>"\"" LITERAL_BUFFER_CREATE(); BEGIN STRING;
<STRING>\\ BEGIN ESCAPED;
<STRING>\" yylval->_string = LITERAL_BUFFER_HARVEST(); BEGIN INITIAL; return _STRING_;
<STRING>. LITERAL_BUFFER_APPEND_CHAR(yytext[0]);
<ESCAPED>f LITERAL_BUFFER_APPEND_CHAR('\f'); BEGIN STRING;
<ESCAPED>n LITERAL_BUFFER_APPEND_CHAR('\n'); BEGIN STRING;
<ESCAPED>r LITERAL_BUFFER_APPEND_CHAR('\r'); BEGIN STRING;
<ESCAPED>t LITERAL_BUFFER_APPEND_CHAR('\t'); BEGIN STRING;
<ESCAPED>\" LITERAL_BUFFER_APPEND_CHAR('"'); BEGIN STRING;
<ESCAPED>\\ LITERAL_BUFFER_APPEND_CHAR('\\'); BEGIN STRING;
<ESCAPED>. LITERAL_BUFFER_APPEND(yytext); BEGIN STRING;
<STRING,ESCAPED><<EOF>> LITERAL_BUFFER_FREE(); return _ERROR_;
<INITIAL>{DIGIT}+"."{DIGIT}+("e"(\-)?{DIGIT}+)? yylval->_double = atof(yytext); return _DOUBLE_;
<INITIAL>{DIGIT}+ yylval->_int = atoi(yytext); return _INTEGER_;
<INITIAL>{LETTER}{IDENT}* yylval->_string = strdup(yytext); return _IDENT_;
<INITIAL>[ \t\r\n\f] /* ignore white space. */;
<INITIAL>. return _ERROR_;
%% /* Initialization code. */
yyscan_t initialize_lexer(FILE *inp)
{
yyscan_t scanner;
if (yylex_init_extra(NULL, &scanner)) return 0;
if (inp) yyrestart(inp, scanner);
return scanner;
}