首页 » Haskell辅导 » Haskell代写 | CSCC24 2020 Winter – Assignment 3

Haskell代写 | CSCC24 2020 Winter – Assignment 3

这个任务是用Haskell完成玩具语言的解析器
CSCC24 2020 Winter – Assignment 3
Due: Wednesday, March 25, 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
We will have a “simple” (just you wait) expression syntax. Here is the EBNF grammar, together
with the informal points afterwards for completion and disambiguation.
::= < literal >
|
|
| < expr >
| “(” “)”
::= “!” | ” -”
::= “+” | ” -” | “==” | “&&” | “||”
Informal points:
• The start symbol is .
• is for natural number literals: One or more digits. (Unary prefix minus is handled
separately.)
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: or, assert, while, do. (These
are for constructs that will appear in the next assignment!)
• Ambiguity under is resolved by operator precedence and association. From lowest
precendence to highest:
operator association
|| right
&& right
== none, e.g., “x == y == z” is unexpected
+, infix binary – left
!, prefix unary –
literal, var, parentheses
• Whitespaces around tokens are possible.
The abstract syntax tree is defined by these types:
1
data Expr = LitNat Integer
| Var String — typo fixed Mar 19
| Prim1 Op1 Expr
| Prim2 Op2 Expr Expr
deriving (Eq , Show )
data Op1 = Not | Neg
deriving (Eq , Show )
data Op2 = Add | Sub | EqNat | And | Or
deriving (Eq , Show )
Basically Prim1 is for the unary operators, and Prim2 is for the binary operators.
Implement a parser for Orlang. A main parser mainParser is already provided, so you can
focus on the start symbol parser expr :: Parser Expr and downwards.
From the lectures, you already know how to implement infix operator precedence. The extra
challenges in this assignment are == and the prefix unary operators.
In the case of the prefix unary operators, take care that these inputs are legal and their
corresponding abstract syntax trees are:
input AST
– – 5 Neg (Neg (LitNat 5))
– – – 5 Neg (Neg (Neg (LitNat 5)))
! – ! 5 Not (Neg (Not (LitNat 5)))
How do you get this to work?
End of questions.
2


程序辅导定制C/C++/JAVA/安卓/PYTHON/留学生/PHP/APP开发/MATLAB


本网站支持 Alipay WeChatPay PayPal等支付方式

E-mail: vipdue@outlook.com  微信号:vipnxx


如果您使用手机请先保存二维码,微信识别。如果用电脑,直接掏出手机果断扫描。

blank

发表评论

您的电子邮箱地址不会被公开。