Skip to content

Commit fdd4418

Browse files
committed
expression need to be done
1 parent c96b8f4 commit fdd4418

File tree

2 files changed

+184
-3
lines changed

2 files changed

+184
-3
lines changed

src/cop5556fa17/SimpleParser.java

Lines changed: 174 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33

44

5+
import java.beans.Expression;
56
import java.util.Arrays;
67

78
import cop5556fa17.Scanner.Kind;
89
import cop5556fa17.Scanner.Token;
910
import cop5556fa17.SimpleParser.SyntaxException;
10-
1111
import static cop5556fa17.Scanner.Kind.*;
1212

1313
public class SimpleParser {
@@ -53,12 +53,172 @@ public void parse() throws SyntaxException {
5353
*/
5454
void program() throws SyntaxException {
5555
//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+
5773
}
5874

5975

6076

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+
/**
62222
* Expression ::= OrExpression OP_Q Expression OP_COLON Expression | OrExpression
63223
*
64224
* Our test cases may invoke this routine directly to support incremental development.
@@ -67,6 +227,8 @@ void program() throws SyntaxException {
67227
*/
68228
void expression() throws SyntaxException {
69229
//TODO implement this.
230+
231+
70232
throw new UnsupportedOperationException();
71233
}
72234

@@ -86,4 +248,13 @@ private Token matchEOF() throws SyntaxException {
86248
String message = "Expected EOL at " + t.line + ":" + t.pos_in_line;
87249
throw new SyntaxException(t, message);
88250
}
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+
89260
}

src/cop5556fa17/SimpleParserTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,16 @@ public void testDec1() throws LexicalException, SyntaxException {
7272
parser.parse();
7373
}
7474

75+
@Test
76+
public void testDec2() throws LexicalException, SyntaxException {
77+
String input = "prog int k;";
78+
show(input);
79+
Scanner scanner = new Scanner(input).scan(); //Create a Scanner and initialize it
80+
show(scanner); //Display the Scanner
81+
SimpleParser parser = new SimpleParser(scanner); //
82+
parser.parse();
83+
}
84+
7585

7686
/**
7787
* This example invokes the method for expression directly.

0 commit comments

Comments
 (0)