@@ -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}
0 commit comments