-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlexer.flex
64 lines (56 loc) · 1.73 KB
/
lexer.flex
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
%%
%public
%class Lexer
%unicode
%type Token
%line
%column
%yylexthrow LexerException
%{
class LexerException extends Exception{
public LexerException(int line, int column, String c){
super("Line :"+line+" Column : "+column+" Character : "+c);
}
}
public String getPosition(){
return "Reading at line "+yyline+", column "+yycolumn;
}
%}
blank=" "|"\n"|"\t"|"\r"
number=[0-9]+
hex=[0-9A-F]
color=#{hex}{hex}{hex}{hex}{hex}{hex}
ident = [a-z][a-zA-Z_]*
%%
"Begin" {return new Token(Sym.BEGIN);}
"End" {return new Token(Sym.END);}
"If" {return new Token(Sym.IF);}
"Then" {return new Token(Sym.THEN);}
"Else" {return new Token(Sym.ELSE);}
"DrawCircle" {return new Token(Sym.DRAWC);}
"FillCircle" {return new Token(Sym.FILLC);}
"DrawRect" {return new Token(Sym.DRAWR);}
"FillRect" {return new Token(Sym.FILLR);}
"Const" {return new Token(Sym.CONST);}
"Var" {return new Token(Sym.VAR);}
"While" {return new Token(Sym.WHILE);}
"Do" {return new Token(Sym.DO);}
"Fi" {return new Token(Sym.FI);}
"For" {return new Token(Sym.FOR);}
{color} {return new ColorToken(yytext());}
{number} {return new NumberToken(Integer.parseInt(yytext()));}
{ident} {return new WordToken(Sym.IDENT, yytext());}
"+" {return new Token(Sym.PLUS);}
"-" {return new Token(Sym.MINUS);}
"/" {return new Token(Sym.DIV);}
"*" {return new Token(Sym.MULT);}
"(" {return new Token(Sym.LPAR);}
")" {return new Token(Sym.RPAR);}
"," {return new Token(Sym.COMMA);}
";" {return new Token(Sym.SEMIC);}
"==" {return new Token(Sym.EQUALEQUALS);}
"=" {return new Token(Sym.EQUALS);}
"<" {return new Token(Sym.LESS);}
">" {return new Token(Sym.MORE);}
{blank} {}
[^] {throw new LexerException(yyline, yycolumn,yytext());}