首先,这是一个家庭作业。我必须制作一个可以解析此处描述的语言的程序:http ://www.cs.princeton.edu/courses/archive/spring12/cos320/resources/fun_language_definition.html
这是 Fun 语言的一个示例:
fun id(x:<int,int>):<int,int> = x
fun main(arg:int):int = id(3)
这应该被解析为:
[((1,1),(id"id",id"x",Tupletp([Inttp,Inttp]),Tupletp([Inttp,Inttp]),Id(id"x"))),
((2,1),(id"main",id"arg",Inttp,Inttp, Call (Id(id"id"),Int 3)))]
我将给出我的代码的某些部分的快照:
prog: fundeclist EOF (fundeclist)
fundeclist: fundec ([fundec])
| fundeclist fundec (fundeclist @ [fundec])
fundec: FUN ID LPAREN ID COLON ftype RPAREN COLON ftype EQ exp
( ((FUNleft, expright), (Symbol.symbol ID1,
Symbol.symbol ID2, A.Inttp, A.Inttp, exp)) ) (* FIXME types *)
exp:
LPAREN exp RPAREN (exp)
| ID (A.Pos((IDleft, IDright),
A.Id (Symbol.symbol(ID)) ))
| INT (A.Pos((INTleft,INTright),
A.Int(INT)))
然后 Exp 将继续以 Fun 语言进行各种可能的表达。我还有一些上部声明终端、非终端、关联规则。
现在我遇到的最大问题是在编译我的代码后这些 SML 消息:
fun.grm.sml:342.60-346.5 Error: operator and operand don't agree [tycon mismatch]
operator domain: unit -> Absyn.prog
operand: unit -> unit
in expression:
prog (fn _ => let val <binding> in fundeclist end)
fun.grm.sml:362.6-362.27 Error: operator and operand don't agree [tycon mismatch]
operator domain: 'Z list * 'Z list
operand: unit * Absyn.fundec list
in expression:
fundeclist @ fundec :: nil
val it = false : bool
我使用非终端的方式有问题吗?在解析时,这个错误通常会说什么?这个错误从下到上传播,所以我一直在尝试用虚拟值进行各种替换以使错误消失 - 直到我来到这里。如何解决这个问题?