This repository has been archived by the owner on Oct 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlexer.l
59 lines (46 loc) · 1.4 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
52
53
54
55
56
57
58
59
%{
#include <stdlib.h> // atol, malloc, free, etc.
#include <stdio.h>
#include "symtab.h" // for symrec *lookup(char *)
#include "parser.tab.h" // parser token definitions
void yyerror(char *);
%}
%option yylineno
%option noyywrap
/* exclusive start condition for block comments */
%x BLOCK_COMMENT
IDENTIFIER [a-zA-Z][a-zA-Z0-9]*
%%
"//"[^\n]* { /* Ignore single-line comments */ }
"/*" { BEGIN(BLOCK_COMMENT); }
<BLOCK_COMMENT>"*/" { BEGIN(INITIAL); }
<BLOCK_COMMENT>. { /* Ignore block comments */ }
<BLOCK_COMMENT>\n { }
[ \t\r\n]+ { /* Ignore whitespace */ }
<<EOF>> { return END; }
"{" { return '{'; }
"}" { return '}'; }
"(" { return '('; }
")" { return ')'; }
";" { return ';'; }
"~" { return '~'; }
"!" { return '!'; }
"+"
"-" { return '-'; }
"*" { return '*'; }
"/" { return '/'; }
"%" { return '%'; }
[0-9]+ { yylval.num = atol(yytext);
return NUMBER;
}
"return" { return RETURN; }
"int" { yylval.val_type = INT;
return TYPE;
}
"void" { yylval.val_type = VOID;
return TYPE;
}
{IDENTIFIER} { yylval.symbol = lookup(yytext);
return IDENTIFIER;
}
%%