CSCC24 2020 Summer – Assignment 3
Due: Monday, August 3, midnight
This assignment is worth 10% of the course grade.
In this assignment, you will implement in Haskell a parser for a toy language.
As usual, you should also aim for reasonably efficient algorithms and reasonably organized, comprehensible code.
Expression Syntax
You will implement a parser for the following toy language “Trycat”. Here is the grammar in EBNF, followed by informal points for completion and disambiguation.
| “try” {
Informal points:
• The start symbol is
•
• is for variable names: A letter followed by zero or more letters or digits. However, the following are reserved words and cannot be variable names: try, catch, endtry, begin, end.
• Whitespaces around tokens are possible.
The abstract syntax tree is defined by these types:
data Stmt
= Assign String Expr | Try [Stmt] [Stmt] | Compound [Stmt]
data Expr
= Add Operand Operand | Div Operand Operand
data Operand = Var String
— the String is for the LHS variable
— 1st list for try-block, 2nd for catch-block — begin … end
| Lit Integer
Implement a parser for Trycat. A main parser trycat is already provided (to skip leading spaces and check trailing junk), so you can focus on the start symbol parser stmt :: Parser Stmt and downwards.
End of questions.
1