-
Notifications
You must be signed in to change notification settings - Fork 10
/
SimpleLex.lex
102 lines (87 loc) · 2.35 KB
/
SimpleLex.lex
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
%using SimpleParser;
%using QUT.Gppg;
%using System.Linq;
%namespace SimpleScanner
Alpha [a-zA-Z_]
Digit [0-9]
AlphaDigit {Alpha}|{Digit}
INTNUM {Digit}+
BOOL false|true
ID {Alpha}{AlphaDigit}*
%%
{INTNUM} {
yylval.iVal = int.Parse(yytext);
return (int)Tokens.INUM;
}
{BOOL} {
yylval.bVal = bool.Parse(yytext);
return (int)Tokens.BOOL;
}
{ID} {
int res = ScannerHelper.GetIDToken(yytext);
if (res == (int)Tokens.ID)
yylval.sVal = yytext;
return res;
}
"=" { return (int)Tokens.ASSIGN; }
";" { return (int)Tokens.SEMICOLON; }
"{" { return (int)Tokens.BEGIN; }
"}" { return (int)Tokens.END; }
"," {return (int)Tokens.COMMA; }
"(" {return (int)Tokens.LPAR; }
")" {return (int)Tokens.RPAR; }
"+" {return (int)Tokens.PLUS; }
"-" {return (int)Tokens.MINUS; }
"*" {return (int)Tokens.MULT; }
"/" {return (int)Tokens.DIV; }
"==" {return (int)Tokens.EQUAL; }
"!=" {return (int)Tokens.NOTEQUAL; }
">" {return (int)Tokens.GREATER; }
"<" {return (int)Tokens.LESS; }
"<=" {return (int)Tokens.EQLESS; }
">=" {return (int)Tokens.EQGREATER; }
":" {return (int)Tokens.COLON; }
"!" {return (int)Tokens.NOT; }
[^ \r\n\t] {
LexError();
}
%{
yylloc = new LexLocation(tokLin, tokCol, tokELin, tokECol);
%}
%%
public override void yyerror(string format, params object[] args)
{
var ww = args.Skip(1).Cast<string>().ToArray();
string errorMsg = string.Format("({0},{1}): Encountered {2}, expected {3}", yyline, yycol, args[0], string.Join(" or ", ww));
throw new SyntaxException(errorMsg);
}
public void LexError()
{
string errorMsg = string.Format("({0},{1}): Unknown symbol {2}", yyline, yycol, yytext);
throw new LexException(errorMsg);
}
class ScannerHelper
{
private static Dictionary<string,int> keywords;
static ScannerHelper()
{
keywords = new Dictionary<string,int>();
keywords.Add("for",(int)Tokens.FOR);
keywords.Add("while",(int)Tokens.WHILE);
keywords.Add("if",(int)Tokens.IF);
keywords.Add("else",(int)Tokens.ELSE);
keywords.Add("print",(int)Tokens.PRINT);
keywords.Add("var",(int)Tokens.VAR);
keywords.Add("and",(int)Tokens.AND);
keywords.Add("or",(int)Tokens.OR);
keywords.Add("goto",(int)Tokens.GOTO);
keywords.Add("input",(int)Tokens.INPUT);
}
public static int GetIDToken(string s)
{
if (keywords.ContainsKey(s.ToLower()))
return keywords[s];
else
return (int)Tokens.ID;
}
}