-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgram.g
More file actions
145 lines (119 loc) · 3.63 KB
/
gram.g
File metadata and controls
145 lines (119 loc) · 3.63 KB
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
class MyParser extends Parser;
{
private Programa programa;
private Comando cmd;
private int writeType;
private String element;
private StackCommand stack;
public void init(){
programa = new Programa();
stack = new StackCommand();
}
}
prog : "programa" declara corpo "fimprog"
{
//System.out.println(programa.writeCode());
System.out.println(programa.writeJava());
//System.out.println("Agora vai rodar...");
//programa.run();
}
;
declara : "declare" ID
{
programa.addVariable(LT(0).getText());
}
(VIRG ID
{
programa.addVariable(LT(0).getText());
}
)* PF
;
corpo : "inicio" (comando)+ "fim"
;
comando : (cmdIf | cmdPrnt | cmdRead)
;
cmdIf : "se" AP
(ID | NUM) {element = LT(0).getText(); }
OPREL {element += LT(0).getText(); }
(ID | NUM) {element += LT(0).getText(); }
{
cmd = new ComandoIf();
((ComandoIf)cmd).setLogicalExpr(element);
stack.push(cmd);
}
FP "entao" corpo
("senao"
{
ComandoIf tmp = (ComandoIf)stack.getTopo();
tmp.changeMode(ComandoIf.ELSE_MODE);
}
corpo
) ?
{
Comando cmd = stack.pop();
if (stack.isEmpty()){
programa.addCommand(cmd);
}
else{
ComandoIf tmp = (ComandoIf)stack.getTopo();
tmp.addCommand(cmd);
}
}
;
cmdPrnt : "mostre" {cmd = new ComandoWrite();}
( ID {writeType = ComandoWrite.TYPE_ID;}
|
TEXTO {writeType = ComandoWrite.TYPE_TEXT;}
) {element = LT(0).getText();}
PF
{
((ComandoWrite)cmd).setType(writeType);
((ComandoWrite)cmd).setContent(element);
if (stack.isEmpty()){
programa.addCommand(cmd);
}
else{
Comando tmp = stack.getTopo();
((ComandoIf)tmp).addCommand(cmd);
}
}
;
cmdRead : "leia" { cmd = new ComandoRead(); }
(ID)
{
element = LT(0).getText();
}
PF
{
((ComandoRead)cmd).setId(element);
if (stack.isEmpty()){
programa.addCommand(cmd);
}
else{
Comando tmp = stack.getTopo();
((ComandoIf)tmp).addCommand(cmd);
}
}
;
class MyLexer extends Lexer;
options{
caseSensitive = true;
}
WS : (' ' | '\n' | '\r' | '\t') {_ttype=Token.SKIP;}
;
ID : ('a'..'z' | 'A'..'Z') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*
;
NUM : ('0'..'9')+ ('.' ('0'..'9')+)?
;
AP : '('
;
FP : ')'
;
PF : '.'
;
VIRG : ','
;
OPREL : '>' | '<' | '='
;
TEXTO : '"' ('a'..'z' | 'A'..'Z' | ' ' | '0'..'9')* '"'
;