-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer.l
135 lines (102 loc) · 8.75 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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import java.util.*;
%%
/* %class lexer */
%public
%standalone
%unicode
%line
%column
%byaccj
/* main character classes */
LineTerminator = \r|\n|;|\r\n
InputCharacter = [^\r\n]
WhiteSpace = {LineTerminator} | [ \t\n\r]+
/* comments */
Comment = {TraditionalComment} | {EndOfLineComment}
TraditionalComment = "/*" [^*] ~"*/" | "/*" "*"+ "/"
EndOfLineComment = "//" {InputCharacter}* {LineTerminator}?
/* identifiers*/
Identifier = [:jletter:][:jletterdigit:]*
/* integer literals */
IntegerLiteral = 0 | [1-9][0-9]*
/* Real number literals */
RealLiteral = ({FLit1}|{FLit2}|{FLit3}) {Exponent}? [fF]
FLit1 = [0-9]+ \. [0-9]*
FLit2 = \. [0-9]+
FLit3 = [0-9]+
Exponent = [eE] [+-]? [0-9]+
/* string literals */
StringCharacter = [a-zA-Z_]
UNKNOWN_TOKEN = .
%state STRING, CHARLITERAL
%%
<YYINITIAL> {
/* keywords */
"is" { System.out.println("[token at line " + yyline + " = \"" + yytext() + "\" : KW_IS]"); return parser.ParserLexer.KW_IS; }
"var" { System.out.println("[token at line " + yyline + " = \"" + yytext() + "\" : KW_VAR]"); return parser.ParserLexer.KW_VAR; }
"if" { System.out.println("[token at line " + yyline + " = \"" + yytext() + "\" : KW_IF]"); return parser.ParserLexer.KW_IF; }
"then" { System.out.println("[token at line " + yyline + " = \"" + yytext() + "\" : KW_THEN]"); return parser.ParserLexer.KW_THEN; }
"else" { System.out.println("[token at line " + yyline + " = \"" + yytext() + "\" : KW_ELSE]"); return parser.ParserLexer.KW_ELSE; }
"end" { System.out.println("[token at line " + yyline + " = \"" + yytext() + "\" : KW_END]"); return parser.ParserLexer.KW_END; }
"while" { System.out.println("[token at line " + yyline + " = \"" + yytext() + "\" : KW_WHILE]"); return parser.ParserLexer.KW_WHILE; }
"for" { System.out.println("[token at line " + yyline + " = \"" + yytext() + "\" : KW_FOR]"); return parser.ParserLexer.KW_FOR; }
"loop" { System.out.println("[token at line " + yyline + " = \"" + yytext() + "\" : KW_LOOP]"); return parser.ParserLexer.KW_LOOP; }
"return" { System.out.println("[token at line " + yyline + " = \"" + yytext() + "\" : KW_RETURN]"); return parser.ParserLexer.KW_RETURN; }
"print" { System.out.println("[token at line " + yyline + " = \"" + yytext() + "\" : KW_PRINT]"); return parser.ParserLexer.KW_PRINT; }
"funct" { System.out.println("[token at line " + yyline + " = \"" + yytext() + "\" : KW_FUNCT]"); return parser.ParserLexer.KW_FUNCT; }
"in" { System.out.println("[token at line " + yyline + " = \"" + yytext() + "\" : KW_IN]"); return parser.ParserLexer.KW_IN; }
/* boolean literals */
"true" { System.out.println("[token at line " + yyline + " = \"" + yytext() + "\" : BOOLEAN_LITERAL]"); return parser.ParserLexer.BOOLEAN_LITERAL; }
"false" { System.out.println("[token at line " + yyline + " = \"" + yytext() + "\" : BOOLEAN_LITERAL]"); return parser.ParserLexer.BOOLEAN_LITERAL; }
/* separators */
"(" { System.out.println("[token at line " + yyline + " = \"" + yytext() + "\" : LPAREN]"); return parser.ParserLexer.LPAREN; }
")" { System.out.println("[token at line " + yyline + " = \"" + yytext() + "\" : RPAREN]"); return parser.ParserLexer.RPAREN; }
"{" { System.out.println("[token at line " + yyline + " = \"" + yytext() + "\" : LBRACE]"); return parser.ParserLexer.LBRACE; }
"}" { System.out.println("[token at line " + yyline + " = \"" + yytext() + "\" : RBRACE]"); return parser.ParserLexer.RBRACE; }
"[" { System.out.println("[token at line " + yyline + " = \"" + yytext() + "\" : LBRACK]"); return parser.ParserLexer.LBRACK; }
"]" { System.out.println("[token at line " + yyline + " = \"" + yytext() + "\" : RBRACK]"); return parser.ParserLexer.RBRACK; }
";" { System.out.println("[token at line " + yyline + " = \"" + yytext() + "\" : SEMICOLON]"); return parser.ParserLexer.SEMICOLON; }
"," { System.out.println("[token at line " + yyline + " = \"" + yytext() + "\" : COMMA]"); return parser.ParserLexer.COMMA; }
"." { System.out.println("[token at line " + yyline + " = \"" + yytext() + "\" : DOT]"); return parser.ParserLexer.DOT; }
/* operators */
"=" { System.out.println("[token at line " + yyline + " = \"" + yytext() + "\" : EQ]"); return parser.ParserLexer.EQ; }
":=" { System.out.println("[token at line " + yyline + " = \"" + yytext() + "\" : ASSIGN]"); return parser.ParserLexer.ASSIGN; }
">" { System.out.println("[token at line " + yyline + " = \"" + yytext() + "\" : GT]"); return parser.ParserLexer.GT; }
"<" { System.out.println("[token at line " + yyline + " = \"" + yytext() + "\" : LT]");; return parser.ParserLexer.LT; }
"<=" { System.out.println("[token at line " + yyline + " = \"" + yytext() + "\" : LTEQ]"); return parser.ParserLexer.LTEQ; }
">=" { System.out.println("[token at line " + yyline + " = \"" + yytext() + "\" : GTEQ]"); return parser.ParserLexer.GTEQ; }
"+" { System.out.println("[token at line " + yyline + " = \"" + yytext() + "\" : PLUS]"); return parser.ParserLexer.PLUS; }
"-" { System.out.println("[token at line " + yyline + " = \"" + yytext() + "\" : MINUS]"); return parser.ParserLexer.MINUS; }
"*" { System.out.println("[token at line " + yyline + " = \"" + yytext() + "\" : MULT]");; return parser.ParserLexer.MULT; }
"/" { System.out.println("[token at line " + yyline + " = \"" + yytext() + "\" : DIV]"); return parser.ParserLexer.DIV; }
"=>" { System.out.println("[token at line " + yyline + " = \"" + yytext() + "\" : EXPR]"); return parser.ParserLexer.EXPR; }
"or" { System.out.println("[token at line " + yyline + " = \"" + yytext() + "\" : TOKEN_OR]"); return parser.ParserLexer.TOKEN_OR; }
"and" { System.out.println("[token at line " + yyline + " = \"" + yytext() + "\" : TOKEN_AND]"); return parser.ParserLexer.TOKEN_AND; }
"xor" { System.out.println("[token at line " + yyline + " = \"" + yytext() + "\" : TOKEN_XOR]"); return parser.ParserLexer.TOKEN_XOR; }
"not" { System.out.println("[token at line " + yyline + " = \"" + yytext() + "\" : NOT]"); return parser.ParserLexer.TOKEN_NOT; }
/* numeric literals */
{IntegerLiteral} { System.out.println("[token at line " + yyline + " = \"" + yytext() + "\" : INTEGER_LITERAL]"); return parser.ParserLexer.INTEGER_LITERAL; }
{RealLiteral} { System.out.println("[token at line " + yyline + " = \"" + yytext() + "\" : REAL_LITERAL]"); return parser.ParserLexer.REAL_LITERAL; }
/* comments */
{Comment} { System.out.println("[token at line " + yyline + " = \"" + yytext() + "\" : COMMENT]"); return parser.ParserLexer.COMMENT; }
/* whitespace */
{WhiteSpace} { }
{Identifier} { System.out.println("[token at line " + yyline + " = \"" + yytext() + "\" : IDENTIFIER]"); return parser.ParserLexer.IDENTIFIER; }
}
<STRING> {
/*\" { yybegin(YYINITIAL); return parser.ParserLexer.STRING;}*/
{StringCharacter}+ { System.out.println("[token at line " + yyline + " = \"" + yytext() + "\" : STRING]"); return parser.ParserLexer.STRING; }
/* escape sequences */
"\\n" { return parser.ParserLexer.STRING; }
"\\\"" { return parser.ParserLexer.STRING; }
"\\'" { return parser.ParserLexer.STRING; }
"\\\\" { return parser.ParserLexer.STRING; }
/* error cases */
\\. { throw new RuntimeException("Illegal escape sequence \""+yytext()+"\""); }
{LineTerminator} { throw new RuntimeException("Unterminated string at end of line"); }
}
/* UNKNOWN TOKENS */
{UNKNOWN_TOKEN} {return parser.ParserLexer.UNKNOWN_TOKEN;}
/* error fallback */
[^] { throw new RuntimeException("Illegal character \""+yytext()+"\" at line "+yyline+", column "+yycolumn); }
<<EOF>> { return 0; }