Project 2: PDef Parser Solution
You were graded based on your submission, that your code compiled, and that it successfully parsed valid PDef and successfully indicated where there was invalid PDef.
Part of your assignment included understanding how the provided PDef grammar works in terms of what is expected for a particular input – what is allowable and what is not. This was a critical step of building the parser. You are responsible for testing that your code works with a variety of inputs.
Here were the tests files I used when checking your work.
Valid PDef
- test-ok1 - block with 1 declaration statement)
{ int a }
- test-ok2 - block containing: 1 declaration, 1 assignment, 1 declaration
{ int a, a = b, float b}
- test-ok3 - block containing assignment with expression
{ b = a * c }
- test-ok4 - block containing assignment with expression
{ b = a - c }
- test-ok5 - nested blocks with declarations and assignments
{ int a, {int b, int c}, float d, a = b, {int b, { d=b } } }
- test-ok6 - longer expressions
{ a = (b*c) - d, float a, b = (b+c) - (d-c) }
Invalid PDef
Your error messages may have varied, but it should have caught the error and indicated correctly what was expected instead.
- test-bad1 - missing end curly bracket.
{ int a, int b
- Expected to see token RCB_T, but saw the token EOF_T( )
- test-bad2 - missing identifier name in declaration.
{ int }
- Expected to see token IDENT_T, but saw the token RCB_T( } )
- test-bad3 - missing end of assignment.
{ a = }
- Expected to see INT_T, FLOAT_T, IDENT_T or LP_T, but saw the token RCB_T( } )
- test-bad4 - missing part of expression.
{ a = a + }
- Expected to see INT_T, FLOAT_T, IDENT_T or LP_T, but saw the token RCB_T( } )
- test-bad5 - unexpected/unsupported tokens.
{ b^2, $, !! }
- Expected to see token ASSIGN_T, but saw the token ERROR_T( ^ )
- Expected to see TYPE_T, IDENT_T or LCB_T, but saw the token ERROR_T( $ )
- Expected to see TYPE_T, IDENT_T or LCB_T, but saw the token ERROR_T( ! )
- test-bad6 - multiple errors in a file
{ int b, a = , float c, d = a*b-(), (), {}, int }
- Expected to see INT_T, FLOAT_T, IDENT_T or LP_T, but saw the token COMMA_T( , )
- Expected to see INT_T, FLOAT_T, IDENT_T or LP_T, but saw the token RP_T( ) )
- Expected to see TYPE_T, IDENT_T or LCB_T, but saw the token LP_T( ( )
- Expected to see TYPE_T, IDENT_T or LCB_T, but saw the token RCB_T( } )
- Expected to see token IDENT_T, but saw the token RCB_T( } )
How it’s accomplished… The code in Parser.java