-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlexer.jflex
98 lines (77 loc) · 2.75 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
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, String lexem){
return symbolFactory.newSymbol(name, code,
new Location(yyline+1, yycolumn +1, yychar),
new Location(yyline+1,yycolumn+yylength(), yychar+yylength()), lexem);
}
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);
}
%}
Newline = \r | \n | \r\n
Whitespace = [ \t\f] | {Newline}
Number = [0-9]+
Id = [a-zA-Z0-9]
/* comments */
Comment = {TraditionalComment} | {EndOfLineComment}
TraditionalComment = "/*" {CommentContent} \*+ "/"
EndOfLineComment = "//" [^\r\n]* {Newline}
CommentContent = ( [^*] | \*+[^*/] )*
ident = ([:jletter:] | "_" ) ([:jletterdigit:] | [:jletter:] | "_" )*
%eofval{
return symbolFactory.newSymbol("EOF",sym.EOF);
%eofval}
%state CODESEG
%%
<YYINITIAL> {
{Whitespace} { }
";" { return symbolFactory.newSymbol("SEMI", SEMI); }
"+" { return symbolFactory.newSymbol("PLUS", PLUS); }
"-" { return symbolFactory.newSymbol("MINUS", MINUS); }
"*" { return symbolFactory.newSymbol("TIMES", TIMES); }
"(" { return symbolFactory.newSymbol("LPAREN", LPAREN); }
")" { return symbolFactory.newSymbol("RPAREN", RPAREN); }
"=" { return symbolFactory.newSymbol("EQUALS", EQUALS); }
{Number} { return symbolFactory.newSymbol("NUMBER", NUMBER, Integer.parseInt(yytext())); }
"LET" { return symbolFactory.newSymbol("LET",LET);}
{ident} { return symbolFactory.newSymbol("ID", ID, yytext()); }
}
// error fallback
.|\n { emit_warning("Unrecognized character '" +yytext()+"' -- ignored"); }