-
Notifications
You must be signed in to change notification settings - Fork 0
/
lex.l
74 lines (72 loc) · 1.5 KB
/
lex.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
%option noyywrap
%{
#include <stdlib.h>
#include"globals.h"
#include"bison.tab.h"
char id[10];
int value;
int lineno;
void comment();
// typedef enum TokenType{
// PLUS,MINUS,MULTI,DIVIDE,
// LESS,LESSEQUAL,MORE,MOREEQUAL,EQUAL,NOTEQUAL,
// ASSIGN,
// SEMI,COMMA,
// LPAREN,RPAREN,LBRACE,RBRACE,LBRACKET,RBRACKET,LCOMM,RCOMM,
// IF,ELSE,INT,RETURN,VOID,WHILE,
// ID,NUM
// }TokenType;
%}
newline [\n\r]
whitespace [ \t]+
letter [a-zA-Z]
digit [0-9]
%%
if {return IF;}
else {return ELSE;}
int {return INT;}
return {return RETURN;}
void {return VOID;}
while {return WHILE;}
"+" {return PLUS;}
"-" {return MINUS;}
"*" {return MULTI;}
"/" {return DIVIDE;}
"<" {return LESS;}
"<=" {return LESSEQUAL;}
">" {return MORE;}
">=" {return MOREEQUAL;}
"==" {return EQUAL;}
"!=" {return NOTEQUAL;}
"=" {return ASSIGN;}
";" {return SEMI;}
"," {return COMMA;}
"(" {return LPAREN;}
")" {return RPAREN;}
"[" {return LBRACKET;}
"]" {return RBRACKET;}
"{" {return LBRACE;}
"}" {return RBRACE;}
"/*" {comment();}
{newline} {}
{whitespace} {}
{letter}|{letter}* { sstrcpy(id, yytext); return ID;}
{digit}|{digit}* { value = atoi(yytext);return NUM;}
%%
void comment()
{
while(1){
while(yyinput()!='*');
if(yyinput() == '/') return;
}
}
/*
TokenType getToken()
{
yyin = fd;
int type = yylex();
if(type == ID) id = yytext;
else if(type == NUM) value = atoi(yytext);
return TokenType(type);
}
*/