-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGP.flex
153 lines (109 loc) · 5.44 KB
/
GP.flex
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
147
148
149
150
package proyecto;
import java_cup.runtime.*;
/**
* Scanner para la gramatica de prueba del curso de Compiladores e Interpretes.
*/
%%
%class Scanner
%unicode
%cup
%line
%column
%function next_token
%{
StringBuffer string = new StringBuffer();
private Symbol symbol(int type) {
return new Symbol(type, yyline, yycolumn);
}
private Symbol symbol(int type, Object value) {
return new Symbol(type, yyline, yycolumn, value);
}
%}
LineTerminator = \r|\n|\r\n
InputCharacter = [^\r\n]
WhiteSpace = {LineTerminator} | [ \t\f]
/* comments */
Comment = {TraditionalComment} | {EndOfLineComment} | {DocumentationComment}
TraditionalComment = "/*" [^*] ~"*/" | "/*" "*"+ "/"
EndOfLineComment = "//" {InputCharacter}* {LineTerminator}
DocumentationComment = "/**" {CommentContent} "*"+ "/"
CommentContent = ( [^*] | \*+ [^/*] )*
CharLiteral = [:jletter:]
Identifier = [:jletter:] [:jletterdigit:]*
IntegerLiteral = 0 | [1-9][0-9]*
%state STRING
%%
/* Palabras reservadas */
<YYINITIAL> "class" { return symbol(sym.CLASS,yytext()); }
<YYINITIAL> "public" { return symbol(sym.PUBLIC,yytext()); }
<YYINITIAL> "static" { return symbol(sym.STATIC,yytext()); }
<YYINITIAL> "void" { return symbol(sym.VOID,yytext()); }
<YYINITIAL> "main" { return symbol(sym.MAIN,yytext()); }
<YYINITIAL> "String" { return symbol(sym.STRING,yytext()); }
<YYINITIAL> "extends" { return symbol(sym.EXTENDS,yytext()); }
<YYINITIAL> "return" { return symbol(sym.RETURN,yytext()); }
<YYINITIAL> "char" { return symbol(sym.CHAR,yytext()); }
<YYINITIAL> "Boolean" { return symbol(sym.BOOLEAN,yytext()); }
<YYINITIAL> "int" { return symbol(sym.INT,yytext()); }
<YYINITIAL> "if" { return symbol(sym.IF,yytext()); }
<YYINITIAL> "else" { return symbol(sym.ELSE,yytext()); }
<YYINITIAL> "while" { return symbol(sym.WHILE,yytext()); }
<YYINITIAL> "System.out.println" { return symbol(sym.SYSTEM_OUT_PRINTln,yytext()); }
<YYINITIAL> "lenght" { return symbol(sym.LENGHT,yytext()); }
<YYINITIAL> "true" { return symbol(sym.TRUE,yytext()); }
<YYINITIAL> "false" { return symbol(sym.FALSE,yytext()); }
<YYINITIAL> "this" { return symbol(sym.THIS,yytext()); }
<YYINITIAL> "new" { return symbol(sym.NEW,yytext()); }
<YYINITIAL> "switch" { return symbol(sym.SWITCH,yytext()); }
<YYINITIAL> "case" { return symbol(sym.CASE,yytext()); }
<YYINITIAL> "break" { return symbol(sym.BREAK,yytext()); }
<YYINITIAL> "default" { return symbol(sym.DEFAULT,yytext()); }
//Revisar esto y como agregar las comillas a los string
<YYINITIAL> {
/* literales Char */
{CharLiteral} { return symbol(sym.myCHAR_LITERAL,yytext()); }
/* identificadores */
{Identifier} { return symbol(sym.myIDENTIFIER,yytext()); }
/* literales enteros positivos */
{IntegerLiteral} { return symbol(sym.myINTEGER_LITERAL,yytext()); }
/* Inicia reconocimiento de string literal */
\" { string.setLength(0); yybegin(STRING); }
/* operadores */
"=" { return symbol(sym.ASIGN,yytext()); }
"*" { return symbol(sym.MULT,yytext()); }
"+" { return symbol(sym.SUMA,yytext()); }
"-" { return symbol(sym.RESTA,yytext()); }
"/" { return symbol(sym.DIV,yytext()); }
"<" { return symbol(sym.MENOR_QUE,yytext()); }
">" { return symbol(sym.MAYOR_QUE,yytext()); }
/* otros simbolos validos */
"{" { return symbol(sym.LLAVEi,yytext()); }
"}" { return symbol(sym.LLAVEd,yytext()); }
"[" { return symbol(sym.CORCHETEi,yytext()); }
"]" { return symbol(sym.CORCHETEd,yytext()); }
"(" { return symbol(sym.PARENi,yytext()); }
")" { return symbol(sym.PARENd,yytext()); }
";" { return symbol(sym.PUNTOyCOMA,yytext()); }
"," { return symbol(sym.COMA,yytext()); }
"&&" { return symbol(sym.AND,yytext()); }
"." { return symbol(sym.PUNTO,yytext()); }
"!" { return symbol(sym.NOT,yytext()); }
":" { return symbol(sym.DOSPUNTOS,yytext()); }
/* commentarios */
{Comment} { /* ignore */ }
/* espacios en blanco */
{WhiteSpace} { /* ignore */ }
}
<STRING> {
\" { yybegin(YYINITIAL);
return symbol(sym.mySTRING_LITERAL,string.toString()); }
[^\n\r\"\\]+ { string.append( yytext() ); }
\\t { string.append('\t'); }
\\n { string.append('\n'); }
\\r { string.append('\r'); }
\\\" { string.append('\"'); }
\\ { string.append('\\'); }
}
/* caracteres no validos */
.|\n { throw new MyException("Caracter no permitido <"+
yytext()+">", yyline, yycolumn); }