An LALR(1) parser for Lotus


This LALR(1) parser is generated by using the tool Bison. The grammar is given as follows.


Grammar

program -> variable_declarations function_definitions
variable_declarations -> empty | variable_declarations variable_declaration
variable_declaration -> int Identifier ;
function_definitions -> function_definition | function_definitions function_definition
function_definition -> int Identifier ( parameters ) function_body
parameters -> empty | parameter_list
parameter_list -> int Identifier | parameter_list , int Identifier
function_body -> { variable_declarations statements }
statements -> empty | statements statement
statement -> assignment_statement | compound_statement
| if_statement | while_statement | return_statement
| exit_statement | read_statement | write_statement
assignment_statement -> Identifier = arith_expression ;
compound_statement -> { statements }
if_statement -> if ( bool_expression ) statement
| if ( bool_expression ) statement else statement
while_statement -> while ( bool_expression ) statement
return_statement -> return arith_expression ;
exit_statement -> exit ;
read_statement -> read Identifier ;
write_statement -> write arith_expression ;
bool_expression -> bool_term | bool_expression || bool_term
bool_term -> bool_factor | bool_term && bool_factor
bool_factor -> bool_primary | ! bool_primary
bool_primary -> arith_expression == arith_expression
| arith_expression != arith_expression
| arith_expression > arith_expression
| arith_expression >= arith_expression
| arith_expression < arith_expression
| arith_expression <= arith_expression
| ( bool_expression )
arith_expressions -> empty | arith_expression_list
arith_expression_list -> arith_expression | arith_expression_list , arith_expression
arith_expression -> arith_term | arith_expression + arith_term
| arith_expression - arith_term
arith_term -> arith_factor
| arith_term * arith_factor
| arith_term / arith_factor
| arith_term % arith_factor
arith_factor -> arith_primary | - arith_primary
arith_primary -> Integer | Identifier
| Identifier ( arith_expressions ) | ( arith_expression )


Description

The parser can parse a program written in Lotus defined above. The parser can trace the parsing process by using the option "-p" to print each production reduced by the parser. Yet, no syntax error recovery is performed. Each syntax error message will include the line number where the error is detected. The parser reads input from stdin, writes output to stdout, and writes errors to stderr.