-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLexer.l
51 lines (40 loc) · 1.64 KB
/
Lexer.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
%{
#include "Symbol.h"
#include "Struct.h"
#include "Parser.h"
#include "Symbol.h"
#include <stdlib.h>
#include <stdio.h>
static char* strbuf = NULL;
int yyerror(const char *msg) {
if (strbuf) {
free(strbuf);
strbuf = NULL;
}
//fprintf(stderr, "Lexer: %s\n", msg);
return 1;
}
%}
%option outfile="Lexer.c" header-file="Lexer.h"
%option warn
%option reentrant noyywrap never-interactive nounistd
%option bison-bridge
%x STRING
%%
[ \r\n\t]* { continue; }
[0-9]+ { sscanf(yytext, "%d", &yylval->value); return TOKEN_NUMBER; }
[a-z\-+/*?!]{1,12}[0-9\-+/*?!]? { yylval->name = compressSymbol(yytext); return TOKEN_NAME; }
[a-z\-+/*?!]{0,11}_[0-9\-+/*?!] { yylval->name = compressSymbol(yytext); return TOKEN_NAME; }
"(" { return TOKEN_LPAREN; }
")" { return TOKEN_RPAREN; }
"[" { return TOKEN_LSQBRK; }
"]" { return TOKEN_RSQBRK; }
\" { BEGIN STRING; }
<STRING>[^\\"\n]* { strbuf = strdup(yytext); }
<STRING>\" { yylval->string = strbuf; strbuf = NULL;
BEGIN 0; return TOKEN_STRING; }
<STRING>\\ { yyerror("escape sequences not implemented in strings"); return *yytext; }
<STRING>\n { yyerror("string not terminated before EOL"); return *yytext; }
<STRING><<EOF>> { yyerror("string not terminated before EOF"); return *yytext; }
. { yyerror("invalid character in input stream"); return *yytext; }
%%