Skip to content

Commit 0867b9d

Browse files
DattleeDattlee
authored andcommitted
make parser pass all tests
1 parent 5b5150f commit 0867b9d

File tree

4 files changed

+28
-21
lines changed

4 files changed

+28
-21
lines changed

bin/compiler/MyParser.class

391 Bytes
Binary file not shown.

bin/compiler/ParserTest.class

-71 Bytes
Binary file not shown.

src/compiler/MyParser.java

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,56 +7,65 @@ public class MyParser implements Parser{
77

88
List<Token> tokens;
99

10+
1011
@Override
11-
public Block parse(List<Token> input) throws SyntaxException {
12+
public Block parse(List<Token> input) throws SyntaxException, Task2Exception {
1213
tokens = input;
1314
// TODO Auto-generated method stub
1415
Block AST = blockParser();
15-
//System.out.println(tokens);
16+
System.out.println(tokens);
17+
tokens.remove(0);
18+
if (!tokens.isEmpty()){
19+
throw new SyntaxException("");
20+
}
1621
return AST;
1722
}
1823

19-
private Block blockParser() throws SyntaxException{
20-
if (tokens.get(0) instanceof T_LeftCurlyBracket){
21-
tokens.remove(0);
24+
private Token grabToken() throws Task2Exception, SyntaxException {
25+
try{
26+
return tokens.get(0);
27+
}catch (IndexOutOfBoundsException e){
28+
throw new SyntaxException("Syntax Error, expected grammar completion");
29+
}catch (Exception e){
30+
throw new Task2Exception("");
31+
}
32+
}
33+
34+
private Block blockParser() throws SyntaxException, Task2Exception{
35+
if (grabToken() instanceof T_LeftCurlyBracket){
2236
Block b = new Block(eneParser());
23-
if(tokens.get(0) instanceof T_RightCurlyBracket){
37+
if(grabToken() instanceof T_RightCurlyBracket){
2438
return b;
25-
}else{
26-
throw new SyntaxException("Closing curly bracket missing");
2739
}
2840
}
2941
throw new SyntaxException("Closing curly bracket missing");
3042
}
3143

32-
private List<Exp> eneParser() throws SyntaxException{
44+
private List<Exp> eneParser() throws SyntaxException, Task2Exception {
3345
List<Exp> list = new ArrayList<Exp>();
34-
list.add(eParser());
35-
if (tokens.get(0) instanceof T_Semicolon) {
46+
do{
3647
tokens.remove(0);
3748
list.add(eParser());
38-
}
49+
}while(grabToken() instanceof T_Semicolon);
3950
return list;
4051
}
4152

42-
private Exp eParser() throws SyntaxException {
43-
if (tokens.get(0) instanceof T_Integer){
53+
private Exp eParser() throws SyntaxException, Task2Exception {
54+
if (grabToken() instanceof T_Integer){
4455
IntLiteral i = new IntLiteral(((T_Integer)tokens.get(0)).n);
45-
System.out.println(((T_Integer)tokens.get(0)).n);
4656
tokens.remove(0);
4757
return i;
48-
}else if (tokens.get(0) instanceof T_Skip){
58+
}else if (grabToken() instanceof T_Skip){
4959
Skip s = new Skip();
50-
System.out.println(tokens.get(0));
5160
tokens.remove(0);
5261
return s;
53-
}else if (tokens.get(0) instanceof T_LeftCurlyBracket){
62+
}else if (grabToken() instanceof T_LeftCurlyBracket){
5463
BlockExp b = new BlockExp(blockParser());
55-
System.out.println(tokens.get(0));
5664
tokens.remove(0);
5765
return b;
5866
}else{
5967
throw new SyntaxException("");
6068
}
6169
}
70+
6271
}

src/compiler/ParserTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ public void testSimpleIntBlock() throws SyntaxException, Task2Exception {
1717
ArrayList<Token> tl = new ArrayList<>();
1818
tl.addAll(Arrays.asList(new T_LeftCurlyBracket(), new T_Integer(0), new T_RightCurlyBracket()));
1919
Block b = p.parse(tl);
20-
System.out.println("hello ");
2120
assertTrue(b.exps.get(0) instanceof IntLiteral);
2221
}
2322

@@ -93,7 +92,6 @@ public void testFailure4() throws SyntaxException, Task2Exception, LexicalExcept
9392
@Test(expected = SyntaxException.class)
9493
public void testFailure5() throws SyntaxException, Task2Exception, LexicalException, Task1Exception {
9594
List<Token> tl = l.lex("{{78;skip}");
96-
System.out.println(tl);
9795
Block b = p.parse(tl);
9896
}
9997

0 commit comments

Comments
 (0)