This repository was archived by the owner on Sep 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlexer.jflex
146 lines (124 loc) · 5.09 KB
/
lexer.jflex
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
135
136
137
138
139
140
141
142
143
144
145
146
package cup.example;
import java_cup.runtime.ComplexSymbolFactory;
import java_cup.runtime.ComplexSymbolFactory.Location;
import java_cup.runtime.Symbol;
import java.lang.*;
import java.io.InputStreamReader;
%%
%class Lexer
%implements sym
%public
%unicode
%line
%column
%cup
%char
%{
public Lexer(ComplexSymbolFactory sf, java.io.InputStream is){
this(is);
symbolFactory = sf;
}
public Lexer(ComplexSymbolFactory sf, java.io.Reader reader){
this(reader);
symbolFactory = sf;
}
private StringBuffer sb;
private ComplexSymbolFactory symbolFactory;
private int csline,cscolumn;
public Symbol symbol(String name, int code){
return symbolFactory.newSymbol(name, code,
new Location(yyline+1,yycolumn+1, yychar), // -yylength()
new Location(yyline+1,yycolumn+yylength(), yychar+yylength())
);
}
public Symbol symbol(String name, int code, Object lexem){
return symbolFactory.newSymbol(name, code,
new Location(yyline+1, yycolumn +1, yychar),
new Location(yyline+1,yycolumn+yylength(), yychar+yylength()), lexem);
}
// private Symbol symbol(String name, int sym, Object val,int buflength) {
// Location left = new Location(yyline+1,yycolumn+yylength()-buflength,yychar+yylength()-buflength);
// Location right= new Location(yyline+1,yycolumn+yylength(), yychar+yylength());
// return symbolFactory.newSymbol(name, sym, left, right,val);
// }
protected void emit_warning(String message){
System.out.println("scanner warning: " + message + " at : 2 "+
(yyline+1) + " " + (yycolumn+1) + " " + yychar);
}
protected void emit_error(String message){
System.out.println("scanner error: " + message + " at : 2" +
(yyline+1) + " " + (yycolumn+1) + " " + yychar);
}
%}
line_terminator = \r|\n|\r\n
white_space = {line_terminator} | [ \t\f]
// keywords = if|array|list|library|while|function
string_literal = \"([^\"])*\" // (\")(.)*?(\") // ([""])(.)*?\1
identifier = ([a-zA-Z_])([a-zA-Z0-9_.])*|([.][a-zA-Z_])([a-zA-Z0-9_.])*
integer_literal = [+-]?[1-9][0-9]*[lL]?|0[xX][0-9a-fA-F]+
float_literal = [+-]?([0-9]+([.][0-9]*)?|[.][0-9]+)
bool_literal=TRUE|FALSE
// delimiters = [(){}<>[]]
// arith_operators = [+\-*/\^]|%[*/o]?%|%{identifier}%
// logic_operators = <=|>=|==|\!=|>|<|\!|&[&]?|\|[\|]?|:|\?|\~|\$
// assignment_operators = =|->[>]?|<[<]?-
// operators = {logic_operators}|{arith_operators}|{assignment_operators}
comments = #.*
%eofval{
return symbolFactory.newSymbol("EOF",sym.EOF);
%eofval}
%state CODESEG
%%
<YYINITIAL> "if" { return symbol("if",IF); }
<YYINITIAL> "in" { return symbol("in",IN); }
<YYINITIAL> "else" { return symbol("else",ELSE); }
<YYINITIAL> "for" { return symbol("for",FOR); }
<YYINITIAL> "function" { return symbol("function",FUNCTION); }
<YYINITIAL> "int" { return symbol("int",INT); }
<YYINITIAL> "string" { return symbol("string",STRING); }
<YYINITIAL> "float" { return symbol("float",FLOAT); }
<YYINITIAL> "bool" { return symbol("bool",BOOL); }
<YYINITIAL> {
"," { return symbol("comma",COMMA); }
"(" { return symbol("(",LPAR); }
")" { return symbol(")",RPAR); }
"{" { return symbol("{",BEGIN); }
"}" { return symbol("}",END); }
"<-" { return symbol("<-",ASSIGN); }
"+" { return symbol("plus",PLUS); }
"-" { return symbol("minus",MINUS); }
"*" { return symbol("mult",MULT); }
"/" { return symbol("div",DIV); }
"<=" { return symbol("leq",LEQ); }
">=" { return symbol("gtq",GTQ); }
"==" { return symbol("eq",EQ); }
"!=" { return symbol("neq",NEQ); }
"<" { return symbol("le",LE); }
">" { return symbol("gt",GT); }
"&&" { return symbol("and",AND); }
"||" { return symbol("or",OR); }
"!" { return symbol("not",NOT); }
":" { return symbol("colon",COLON);}
{identifier} { return symbol("Identifier",IDENT, yytext()); }
{integer_literal} { return symbol("INTCONST",INTCONST, new Integer(Integer.parseInt(yytext()))); }
{float_literal} { return symbol("FLOATCONST",FLOATCONST, new Float(Float.parseFloat(yytext()))); }
{bool_literal} { return symbol("BOOLCONST",BOOLCONST, new Boolean(Boolean.parseBoolean(yytext()))); }
// \" { string.setLength(0); yybegin(STRING); }
{string_literal} { return symbol("STRINGCONST",STRINGCONST, yytext()); }
{comments} {}
{white_space} {}
}
// <STRING> {
// \" {
// yybegin(YYINITIAL);
// return symbol("STRINGCONST",STRINGCONST, string.toString(), string.length());
// }
// [^\n\r\"\\]+ { string.append( yytext() ); }
// \\t { string.append('\t'); }
// \\n { string.append('\n'); }
// \\r { string.append('\r'); }
// \\\" { string.append('\"'); }
// \\ { string.append('\\'); }
// }
/* error fallback */
[^] { throw new Error("Parsing Failed! Illegal character ["+ yytext()+"] at line " + yyline + ", col " + yycolumn); }