键入missmatch with packrat scala / kiama error

时间:2016-08-31 09:55:48

标签: scala compiler-errors kiama

我正在Scala(kiama)中创建一个迷你java编译器。我的一个名为“tipe”的代码块给了我一个错误,我在Scala的介绍性知识不能破解。

这是我的代码(有点不完整,但我不相信给我错误)

lazy val tipe : PackratParser[Type] =
 "bool"|
 "int" |
 "obj" |
 tipe ~("->" ~> tipe) |
 ("(" ~> tipe <~")")

当我尝试编译程序时出现以下错误:

  

发现类型不匹配:

     

找到:SyntaxAnalysis.this.Parser [Object]

     

必需:SyntaxAnalysis.this.PackratParser [funjs.FunJSTree.type]

     

tipe~(“ - &gt;”〜&gt; tipe)|

用箭头指向|

任何帮助都会受到赞赏,我是Scala的新手,这对我来说相当复杂。

1 个答案:

答案 0 :(得分:0)

The compiler supposes tipe has the type you provide: PackratParser[Type]. This means tipe ~("->" ~> tipe) is a Parser[Type ~ Type], while "bool" etc. are converted to Parser[String]. Combining Parser[String] and Parser[Type ~ Type] using | gives you a Parser[Object] (as the common supertype of String and Type ~ Type). To solve this, you need to make sure every alternative (argument of |) is a Parser[Type]. Typically it should look like

lazy val tipe : PackratParser[Type] =
 "bool" ^^^ BoolType |
 ...
 tipe ~("->" ~> tipe) ^^ { case (t1, t2) => someFunctionOf(t1, t2) } |
 ("(" ~> tipe <~")")

using ^^ and ^^^ combinators.

(Note: if you are unfamiliar with { case (t1, t2) => ... } syntax, I recommend starting with something more basic.)