forked from leethater/ADS4_2018
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parser.java
183 lines (174 loc) · 4.8 KB
/
Parser.java
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import java.io.*;
import java.util.*;
class Parser{
protected LookAhead1 reader;
public Parser(LookAhead1 r){
reader=r;
}
public AST progNotTerm() throws Exception{
List<Instruction> list=instructionsList(new ArrayList<Instruction>());
if(!reader.isEmpty()) throw new ParserException(reader.getLexer().getPosition()+" Not valid end of file");
return new AST(list);
}
public Instruction instruction() throws Exception{
Instruction i;
if(reader.check(Sym.BEGIN)){
reader.eat(Sym.BEGIN);
List<Instruction> l=instructionsList(new ArrayList<Instruction>());
reader.eat(Sym.END);
i=new SuperInstruction(l);
}
else{
boolean b=false;
Token b1=reader.getCurrent();
if(reader.check(Sym.CONST)){
reader.eat(Sym.CONST);
String s=((WordToken)reader.getCurrent()).getContent();
reader.eat(Sym.IDENT);
reader.eat(Sym.EQUALS);
Expression e=expr();
//useless
i=new Declaration(Sym.CONST,s,e);
}
else if(reader.check(Sym.VAR)){
reader.eat(Sym.VAR);
String s=((WordToken)reader.getCurrent()).getContent();
reader.eat(Sym.IDENT);
reader.eat(Sym.EQUALS);
Expression e=expr();
i=new Declaration(Sym.VAR,s,e);
}
else if(reader.check(Sym.IDENT)){
String s=((WordToken)reader.getCurrent()).getContent();
reader.eat(Sym.IDENT);
reader.eat(Sym.EQUALS);
Expression e2=expr();
i=new Assignment(b1.getSym(),s,e2);
}
else if(reader.check(Sym.WHILE)){
reader.eat(Sym.WHILE);
Expression e = expr();
reader.eat(Sym.DO);
Instruction i2 = instruction();
i = new WhileInstruction(e,i2);
}
else if(reader.check(Sym.IF)){
reader.eat(Sym.IF);
Expression e=expr();
reader.eat(Sym.THEN);
Instruction i1=instruction();
Instruction i2;
if(reader.check(Sym.ELSE)){
reader.eat(Sym.ELSE);
i2=instruction();
}
else{
reader.eat(Sym.FI);
i2=null;
}
i=new Conditional(e,i1,i2);
}
else if(reader.check(Sym.FOR)){
reader.eat(Sym.FOR);
reader.eat(Sym.LPAR);
reader.eat(Sym.VAR);
String s=((WordToken)reader.getCurrent()).getContent();
reader.eat(Sym.IDENT);
reader.eat(Sym.EQUALS);
Expression e=expr();
Declaration dec = new Declaration(Sym.VAR,s,e);
reader.eat(Sym.COMMA);
Expression cond = expr();
reader.eat(Sym.COMMA);
String s1=((WordToken)reader.getCurrent()).getContent();
reader.eat(Sym.IDENT);
reader.eat(Sym.EQUALS);
Expression e2=expr();
Assignment affect = new Assignment(Sym.VAR,s1,e2);
reader.eat(Sym.RPAR);
reader.eat(Sym.DO);
Instruction l = instruction();
i = new ForInstruction(dec, cond, affect, l);
}
else{
List<Expression> l=new ArrayList<Expression>();
if(reader.check(Sym.DRAWC)) reader.eat(Sym.DRAWC);
else if(reader.check(Sym.FILLC)) reader.eat(Sym.FILLC);
else if(reader.check(Sym.DRAWR)){
reader.eat(Sym.DRAWR);
b=true;
}
else if(reader.check(Sym.FILLR)){
reader.eat(Sym.FILLR);
b=true;
}
reader.eat(Sym.LPAR);
l.add(expr());
reader.eat(Sym.COMMA);
l.add(expr());
reader.eat(Sym.COMMA);
l.add(expr());
reader.eat(Sym.COMMA);
if(b){
l.add(expr());
reader.eat(Sym.COMMA);
}
Token b2=reader.getCurrent();
reader.eat(Sym.COL);
ColorToken b3=(ColorToken)b2;
reader.eat(Sym.RPAR);
i=new LineInstruction(b1,l,b3);
}
}
return i;
}
public List<Instruction> instructionsList(List<Instruction> l) throws Exception{
Instruction i=this.instruction();
l.add(i);
reader.eat(Sym.SEMIC);
if(!reader.isEmpty() && !reader.check(Sym.END)){
instructionsList(l);
}
return l;
}
public Expression expr() throws Exception{
Expression e;
if(reader.check(Sym.NUM)){
Token t=reader.getCurrent();
reader.eat(Sym.NUM);
NumberToken n=(NumberToken)t;
e=new IntExpression(n.getValue());
}
else if(reader.check(Sym.IDENT)){
String key=((WordToken)reader.getCurrent()).getContent();
reader.eat(Sym.IDENT);
e=new Identifier(key);
}
else{
reader.eat(Sym.LPAR);
Expression e1=expr();
Token o=reader.getCurrent();
switch(o.getSym()){
case PLUS: reader.eat(Sym.PLUS);
break;
case MINUS: reader.eat(Sym.MINUS);
break;
case MULT:reader.eat(Sym.MULT);
break;
case DIV: reader.eat(Sym.DIV);
break;
case LESS: reader.eat(Sym.LESS);
break;
case MORE: reader.eat(Sym.MORE);
break;
case EQUALEQUALS: reader.eat(Sym.EQUALEQUALS);
break;
default: throw new ParserException(reader.getLexer().getPosition()+" Not valid operator encountered !");
}
Expression e2=expr();
reader.eat(Sym.RPAR);
e=new ExpressionPlus(e1,e2,o);
}
return e;
}
}