Programming
Assignment 3
Due Date: 11:59 PM
on Mar 31, 2009
For PA3 you will further develop the compiler for a Bronco# programming language. Besides the syntactic errors you have found in PA2, your PA3 program is required to find all semantic errors. The semantic errors include:
To complete this project, your program must find all semantic errors based on the above requirements:
INPUT GRAMMAR FOR PA2
Module
module → unit { unit } //{…} represents 0 or many
repetitions
unit → declaration ‘;’ //global variables
→ function //functions
→ type_decl ‘;’ //types
→ λ // NULL
Type Declarations
type_decl → type ident ‘=’ type_def
type_def → array ‘[‘ int_const “..” int_const ‘]’ of type_id
→ record field_list end
→ enum ‘{‘ ident_list ‘}’
→ pointer of type_id
field_list → field {‘;’ field}
field → type_id ident_list
type_id → ident | base_type //’ident’ is a defined type
base_type →
int | float | short // short takes 2 bytes and
others take 4 bytes
Variable Declarations
declaration_list→declaration { ‘;’ declaration }
declaration → type_id ident_list
ident_list →
ident { ‘,’ ident }
Function Declarations
function → function return_type ident ‘(‘ parameter_list ‘)’
compound_stmt
return_type → type_id | void //Function returns nothing
parameter_list → parameter {‘,’ parameter } | λ
parameter →
mode type_id ident
mode → ref | λ
Statements
stmt_list → statement { ‘;’ statement}
statement → assign_stmt
→ while_stmt
→ if_stmt
→ function_call //Must return VOID
→ compound_stmt
→ return_stmt
→ print_stmt
→ new_stmt
→ λ //NULL statement
assign_stmt → variable “=” expr
while_stmt → while ‘(‘ expr ‘)’ statement
if_stmt → if ‘(‘ expr ‘)’ statement
→ if ‘(‘ expr ‘)’ statement else statement
function_call → ident ‘(‘ expr_list ‘)’
compound_stmt →’{‘ declaration_list ‘;’ stmt_list ‘}’
→ ‘{‘ stmt_list ‘}’
return_stmt →
return ‘(‘ expr ‘)’ | return
print_stmt → print ‘(‘ output_list ‘)’
→
println
‘(‘ output_list ‘)’ | println
new_stmt → new variable
Output List
output_list → output_element { ‘,’ output_element }
output_element →
expr | string
Expression List
expr_list → expr { ‘,’ expr } | λ
Variables
variable → ident variable_tail
variable_tail → ‘[‘ expr ‘]’ variable_tail
→ ‘.’ ident variable_tail //Ident is defined field
→ ‘->’ ident variable_tail
→ λ
Expressions
expr → expr bin_op expr
→ unary_op expr
→ ‘(‘ expr ‘)’
→ variable
→ function_call
→
int_const
→
real_const
bin_op → ‘+’ | ‘-‘ | ‘*’ | ‘/’ | ‘%’
→ ‘<’ | ‘>’ | ‘<=’ | ‘>=’ | ‘!=’ | ‘==’
→ ‘&&’ | ‘||’
unary_op → ‘-‘ | ‘!’
Standard mathematical rules of precedence and associativity apply (‘%’ has the same associativity as ‘+’). Binary operators are LEFT associative.
Note: All keywords are written in bold. All other terminals/tokens are in italic.