@@ -7,56 +7,65 @@ public class MyParser implements Parser{
7
7
8
8
List <Token > tokens ;
9
9
10
+
10
11
@ Override
11
- public Block parse (List <Token > input ) throws SyntaxException {
12
+ public Block parse (List <Token > input ) throws SyntaxException , Task2Exception {
12
13
tokens = input ;
13
14
// TODO Auto-generated method stub
14
15
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
+ }
16
21
return AST ;
17
22
}
18
23
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 ){
22
36
Block b = new Block (eneParser ());
23
- if (tokens . get ( 0 ) instanceof T_RightCurlyBracket ){
37
+ if (grabToken ( ) instanceof T_RightCurlyBracket ){
24
38
return b ;
25
- }else {
26
- throw new SyntaxException ("Closing curly bracket missing" );
27
39
}
28
40
}
29
41
throw new SyntaxException ("Closing curly bracket missing" );
30
42
}
31
43
32
- private List <Exp > eneParser () throws SyntaxException {
44
+ private List <Exp > eneParser () throws SyntaxException , Task2Exception {
33
45
List <Exp > list = new ArrayList <Exp >();
34
- list .add (eParser ());
35
- if (tokens .get (0 ) instanceof T_Semicolon ) {
46
+ do {
36
47
tokens .remove (0 );
37
48
list .add (eParser ());
38
- }
49
+ }while ( grabToken () instanceof T_Semicolon );
39
50
return list ;
40
51
}
41
52
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 ){
44
55
IntLiteral i = new IntLiteral (((T_Integer )tokens .get (0 )).n );
45
- System .out .println (((T_Integer )tokens .get (0 )).n );
46
56
tokens .remove (0 );
47
57
return i ;
48
- }else if (tokens . get ( 0 ) instanceof T_Skip ){
58
+ }else if (grabToken ( ) instanceof T_Skip ){
49
59
Skip s = new Skip ();
50
- System .out .println (tokens .get (0 ));
51
60
tokens .remove (0 );
52
61
return s ;
53
- }else if (tokens . get ( 0 ) instanceof T_LeftCurlyBracket ){
62
+ }else if (grabToken ( ) instanceof T_LeftCurlyBracket ){
54
63
BlockExp b = new BlockExp (blockParser ());
55
- System .out .println (tokens .get (0 ));
56
64
tokens .remove (0 );
57
65
return b ;
58
66
}else {
59
67
throw new SyntaxException ("" );
60
68
}
61
69
}
70
+
62
71
}
0 commit comments