CSC254 Parser Assignment

Due by 11:59pm 09/29/2010


Extend the parser of your calculator to support the full input language. The parser should check whether the program is grammatically correct. The specific requirements are:

  • As in the scanner project, your parser should ignore (but copy) "meta-statements".
  • See the detailed syntax specification below. Regardless the choice of your implementation, you must convert the grammar into an LL(1) grammar and submit the modified grammar in writing.
  • For each input program, your parser should report "pass" or "error" as the result of grammar analysis.
  • Count the number of global and local variables (<data decls>), functions (<func list> but not function declarations), and statements (<statements>) in correct programs.  Output the result in the following format: variable _num_variables  function _num_functions statement _num_statements.  For example, for a correct program with 4 variables, 2 functions and 6 statements, your compiler should output at least the following:
    • Pass variable 4 function 2 statement 6
    • "int a,b;" increases the counter of variables 2 not 1

Syntax specification of the input language

<program> --> <data decls> <func list>
<func list> --> empty | <func> <func list>
<func> --> <func decl> semicolon | <func decl> left_brace <data decls> <statements> right_brace
<func decl> --> <type name> ID left_parenthesis <parameter list> right_parenthesis
<type name> --> int | void
<parameter list> --> empty | void | <non-empty list>
<non-empty list> --> <type name> ID | <non-empty list> comma <type name> ID
<data decls> --> empty | <type name> <id list> semicolon <data decls>
<id list> --> <id> | <id list> comma <id>
<id> --> ID | ID left_bracket <expression> right_bracket

<block statements> --> left_brace <statements> right_brace
<statements> --> empty | <statement> <statements>
<statement> --> <assignment> | <general func call> | <printf func call> | <scanf func call> | <if statement> | <while statement> | <return statement> | <break statement> | <continue statement>
<assignment> --> <id> equal_sign <expression> semicolon
<general func call> --> ID left_parenthesis <expr list> right_parenthesis semicolon     (ID is not "printf" or "scanf".)
<printf func call> --> ID left_parenthesis <string> right_parenthesis semicolon | ID left_parenthesis string comma <expression> right_parenthesis semicolon    (ID is "printf".)
<scanf func call> --> ID left_parenthesis string comma and_sign <expression> right_parenthesis semicolon    (ID is "scanf".)

<expr list> --> empty | <non-empty expr list>
<non-empty expr list> --> <expression> | <non-empty expr list> comma <expression>

<if statement> --> if left_parenthesis <condition expression> right_parenthesis <block statements>
<condition expression> -->  <condition> | <condition> <condition op> <condition>
<condition op> --> double_end_sign | double_or_sign
<condition> --> <expression> <comparison op> <expression>
<comparison op> --> == | != | > | >= | < | <=

<while statement> --> while left_parenthesis <condition expression> right_parenthesis <block statements>
<return statement> --> return <expression> semicolon | return semicolon
<break statement> ---> break semicolon
<continue statement> ---> continue semicolon

<expression> --> <term> | <expression> <addop> <term>
<addop> --> plus_sign | minus_sign
<term> --> <factor> | <term> <mulop> <factor>
<mulop> --> star_sign | forward_slash
<factor> --> ID | ID left_bracket <expression> right_bracket | ID left_parenthesis <expr list> right_parenthesis | NUMBER | minus_sign NUMBER | left_parenthesis <expression> right_parenthesis

What/When to Submit

Turn in your code with a report of your design including the used LL(1) grammar with instructions to run your compiler. Put the results  for all passed test cases and the problems for other test cases. In the report, write down the name of passed test cases with the results, e.g., "Pass variable 4 function 2 statement 6" and the reasons for failure in other test cases.  List the provided test cases first and then those provided by you and others. Submit these files in project_code/parser/user_id before 11:59pm Wednesday 09/29/2010.

What/When to Re-submit

Turn in your parser with your intra-func code generator to project_code/intra/user_id. Use a single copy of source code and report. Do the required statistical countings along with code-gen. If you want the TA to do grading for your parser again, put "I NEED REGRADING FOR THE PARSER ASSIGNMENT" at the beginning of the report. Put the results for all the provided test cases in your report. (All the provided test cases in project_code/c_test are correct now.)