2
2
3
3
4
4
5
+ import java .beans .Expression ;
5
6
import java .util .Arrays ;
6
7
7
8
import cop5556fa17 .Scanner .Kind ;
8
9
import cop5556fa17 .Scanner .Token ;
9
10
import cop5556fa17 .SimpleParser .SyntaxException ;
10
-
11
11
import static cop5556fa17 .Scanner .Kind .*;
12
12
13
13
public class SimpleParser {
@@ -53,12 +53,172 @@ public void parse() throws SyntaxException {
53
53
*/
54
54
void program () throws SyntaxException {
55
55
//TODO implement this
56
- throw new UnsupportedOperationException ();
56
+ if (t .kind .equals (IDENTIFIER )){
57
+ match (IDENTIFIER );
58
+ while (t .kind .equals (KW_int ) || t .kind .equals (KW_image ) || t .kind .equals (KW_url ) || t .kind .equals (IDENTIFIER )){
59
+ if (t .kind .equals (KW_int ) || t .kind .equals (KW_image ) || t .kind .equals (KW_url )) {
60
+ Declaration ();
61
+ match (SEMI );
62
+ }
63
+ else if (t .kind .equals (IDENTIFIER )){
64
+ Statement ();
65
+ match (SEMI );
66
+ }
67
+
68
+ }
69
+ matchEOF ();
70
+ }
71
+ else throw new SyntaxException (t , "Empty language not supported" );
72
+
57
73
}
58
74
59
75
60
76
61
- /**
77
+ private void Statement () throws SyntaxException {
78
+ // TODO Auto-generated method stub
79
+ if (t .kind .equals (IDENTIFIER )){
80
+ match (IDENTIFIER );
81
+ if (t .kind .equals (OP_ASSIGN )|| t .kind .equals (LSQUARE )) Assignment ();
82
+ else if (t .kind .equals (OP_RARROW )) ImageOutStatement ();
83
+ else if (t .kind .equals (OP_LARROW )) ImageInStatement ();
84
+ else throw new SyntaxException (t , "Syntax error" );
85
+ }
86
+ }
87
+
88
+ private void ImageInStatement () throws SyntaxException {
89
+ // TODO Auto-generated method stub
90
+ match (OP_LARROW );
91
+ Sink ();
92
+
93
+ }
94
+
95
+ private void Sink () throws SyntaxException {
96
+ // TODO Auto-generated method stub
97
+ if (t .kind .equals (IDENTIFIER )) match (IDENTIFIER );
98
+ else if (t .kind .equals (KW_SCREEN )) match (KW_SCREEN );
99
+ else throw new SyntaxException (t , "Syntax error" );
100
+
101
+ }
102
+
103
+ private void ImageOutStatement () throws SyntaxException {
104
+ // TODO Auto-generated method stub
105
+ match (OP_RARROW );
106
+ Source ();
107
+ }
108
+
109
+ private void Assignment () throws SyntaxException {
110
+ // TODO Auto-generated method stub
111
+ lhs ();
112
+ match (OP_ASSIGN );
113
+ expression ();
114
+ }
115
+
116
+ private void lhs () throws SyntaxException {
117
+ // TODO Auto-generated method stub
118
+ match (LSQUARE );
119
+ lhsSelector ();
120
+ match (RSQUARE );
121
+ }
122
+
123
+ private void lhsSelector () throws SyntaxException {
124
+ // TODO Auto-generated method stub
125
+ match (LSQUARE );
126
+ if (t .kind .equals (KW_x )) XySelector ();
127
+ else if (t .kind .equals (KW_r )) RaSelector ();
128
+ else throw new SyntaxException (t , "Syntax error" );
129
+ match (RSQUARE );
130
+
131
+ }
132
+
133
+ private void RaSelector () throws SyntaxException {
134
+ // TODO Auto-generated method stub
135
+ match (KW_r );
136
+ match (COMMA );
137
+ match (KW_A );
138
+ }
139
+
140
+ private void XySelector () throws SyntaxException {
141
+ // TODO Auto-generated method stub
142
+ match (KW_x );
143
+ match (COMMA );
144
+ match (KW_y );
145
+ }
146
+
147
+ private void Declaration () throws SyntaxException {
148
+ // TODO Auto-generated method stub
149
+ if (t .kind .equals (KW_int )) VariableDeclaration ();
150
+ else if (t .kind .equals (KW_image )) ImageDeclaration ();
151
+ else if (t .kind .equals (KW_url )) SourceSinkDeclaration ();
152
+ else throw new SyntaxException (t , "syntax error in token" );
153
+ }
154
+
155
+ private void VariableDeclaration () throws SyntaxException {
156
+ // TODO Auto-generated method stub
157
+ varType ();
158
+ match (IDENTIFIER );
159
+ if (t .kind .equals (OP_ASSIGN )){
160
+ match (OP_ASSIGN );
161
+ expression ();
162
+ }
163
+
164
+ }
165
+
166
+ private void varType () throws SyntaxException {
167
+ // TODO Auto-generated method stub
168
+ if (t .kind .equals (KW_int )) match (KW_int );
169
+ else if (t .kind .equals (KW_boolean )) match (KW_boolean );
170
+ else throw new SyntaxException (t , "syntax error in token" );
171
+
172
+ }
173
+
174
+ private void SourceSinkDeclaration () throws SyntaxException {
175
+ // TODO Auto-generated method stub
176
+ SourceSinkType ();
177
+ match (IDENTIFIER );
178
+ match (OP_ASSIGN );
179
+ Source ();
180
+ }
181
+
182
+ private void SourceSinkType () throws SyntaxException {
183
+ // TODO Auto-generated method stub
184
+ if (t .kind .equals (KW_url )) match (KW_url );
185
+ else if (t .kind .equals (KW_file )) match (KW_file );
186
+ else throw new SyntaxException (t , "Syntax error" );
187
+ }
188
+
189
+ private void ImageDeclaration () throws SyntaxException {
190
+ // TODO Auto-generated method stub
191
+
192
+ match (KW_image );
193
+ if (t .kind .equals (LSQUARE )){
194
+ match (LSQUARE );
195
+ expression ();
196
+ match (COMMA );
197
+ expression ();
198
+ match (RSQUARE );
199
+ }
200
+ match (IDENTIFIER );
201
+ if (t .kind .equals (OP_LARROW )){
202
+ match (OP_LARROW );
203
+ Source ();
204
+ }
205
+
206
+
207
+
208
+ }
209
+
210
+ private void Source () throws SyntaxException {
211
+ // TODO Auto-generated method stub
212
+ if (t .kind .equals (STRING_LITERAL )) match (STRING_LITERAL );
213
+ else if (t .kind .equals (OP_AT )){
214
+ match (OP_AT );
215
+ expression ();
216
+ }
217
+ else if (t .kind .equals (IDENTIFIER )) match (IDENTIFIER );
218
+
219
+ }
220
+
221
+ /**
62
222
* Expression ::= OrExpression OP_Q Expression OP_COLON Expression | OrExpression
63
223
*
64
224
* Our test cases may invoke this routine directly to support incremental development.
@@ -67,6 +227,8 @@ void program() throws SyntaxException {
67
227
*/
68
228
void expression () throws SyntaxException {
69
229
//TODO implement this.
230
+
231
+
70
232
throw new UnsupportedOperationException ();
71
233
}
72
234
@@ -86,4 +248,13 @@ private Token matchEOF() throws SyntaxException {
86
248
String message = "Expected EOL at " + t .line + ":" + t .pos_in_line ;
87
249
throw new SyntaxException (t , message );
88
250
}
251
+
252
+
253
+ private void match (Kind kind ) throws SyntaxException {
254
+ if (t .kind .equals (kind )) {
255
+ t = scanner .nextToken ();
256
+ }
257
+ else throw new SyntaxException (t ,"Syntax error" );
258
+ }
259
+
89
260
}
0 commit comments