-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.jflex
127 lines (110 loc) · 4.37 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
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;
import Model.*;
%%
%class Lexer
%implements sym
%unicode
%cup
%line
%column
%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;
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);
}
public Symbol symbol(String name, int code, int lexem){
return symbolFactory.newSymbol(name, code,
new Location(yyline+1, yycolumn +1, yychar),
new Location(yyline+1,yycolumn+yylength(), yychar+yylength()), lexem);
}
public Symbol symbol(String name, int code, Color lexem){
return symbolFactory.newSymbol(name, code,
new Location(yyline+1, yycolumn +1, yychar),
new Location(yyline+1,yycolumn+yylength(), yychar+yylength()), lexem);
}
public Symbol symbol(String name, int code, Direction 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]+
Color = A | R | V | B | N
Direction = S|N|E|O
/* comments */
Comment = {TraditionalComment} | {EndOfLineComment} | {DocumentationComment}
TraditionalComment = "/*" [^*] ~"*/" | "/*" "*"+ "/"
// Comment can be the last line of the file, without line terminator.
EndOfLineComment = "//" {InputCharacter}* {LineTerminator}?
DocumentationComment = "/**" {CommentContent} "*"+ "/"
CommentContent = ( [^*] | \*+ [^/*] )*
Identifier = [:jletter:] [:jletterdigit:]*
num = 0 | [1-9][0-9]*
%eofval{
return symbol("EOF",EOF);
%eofval}
%%
<YYINITIAL> {
{Whitespace} { }
";" { return symbol("SEMI",sym.SEMI); }
"if" { return symbol("IF",sym.IF); }
"then" { return symbol("THEN",sym.THEN); }
"else" { return symbol("ELSE",sym.ELSE); }
"while" { return symbol("WHILE",sym.WHILE); }
"do" { return symbol("DO",sym.DO); }
"bajar-pluma" { return symbol("BAJARP",sym.BAJARP); }
"levantar-pluma" { return symbol("SUBIRP",sym.SUBIRP); }
"tablero-col" { return symbol("TABCOL", sym.TABCOL); }
"color-pluma" { return symbol("COLORP",sym.COLORP); }
"direccion-pluma" { return symbol("DIRP",sym.DIRP); }
"avanzar" { return symbol("AVAZ",sym.AVAZ); }
"pluma-dir" { return symbol("PLUMADIR",sym.PLUMADIR); }
"pluma-col" { return symbol("PLUMACOL",sym.PLUMACOL); }
"pluma-arriba" { return symbol("PLUMAUP",sym.PLUMAUP); }
"pluma-abajo" { return symbol("PLUMADOWN",sym.PLUMADOWN); }
"and" { return symbol("AND",sym.AND); }
"or" { return symbol("OR",sym.OR); }
"not" { return symbol("NOT",sym.NOT); }
"borde" { return symbol("BORDE",sym.BORDE); }
"{" { return symbol("BRACKETL",sym.BRACKETL); }
"}" { return symbol("BRACKETR" ,sym.BRACKETR); }
"N" { return symbol("N" ,sym.N); }
{Number} { return symbol("num", sym.num, Integer.parseInt(yytext())); }
{Color} { return symbol("c",sym.c, new Color(yytext().charAt(0))); }
{Direction} { return symbol("d",sym.d, new Direction(yytext().charAt(0)));}
}
[^] { throw new Error("Illegal character <"+
yytext()+">"); }