Scala - StdTokenParsers解析不完全符合预期

时间:2014-10-02 18:55:37

标签: scala parsing

我有一个分析演示语言的作业,这是有问题的代码,其他工作正如我所料:

  def parse(s: String) = phrase(program)(new lexical.Scanner(s))

  def program: Parser[Any] = rep(sttment)



  //Operator

  def expression: Parser[Any] = lv1 ~ rep(("<" | ">" | "<=" | ">=") ~ lv1)

  def lv1: Parser[Any] = lv2 ~ rep(("<>" | "==") ~ lit)

  def lv2: Parser[Any] = lit ~ opt("." ~ ident ~ opt("(" ~ repsep(expression, ",") ~ ")"))

  def lit: Parser[Any] = ident | boollit | floatlit | intlit | stringlit

  // Statements
  def sttment: Parser[Any] = sttm | "{" ~  rep(sttment) ~ "}"

  def sttm: Parser[Any] = (assignment ||| returnsttm ||| invokesttm ||| ifsttm ||| whilesttm |||
        repeatsttm ||| forsttm ||| breaksttm ||| continuesttm ) ~ ";"

  def assignment: Parser[Any] = lhs ~ ":=" ~ expression

  def lhs: Parser[Any] = ( "self" ~ "." ~ ident )|||( ident ~ "." ~ ident )|||( ident ~ "[" ~ expression ~ "]")|||ident

  def ifsttm: Parser[Any] = "if" ~ expression ~ "then" ~ sttment ~ opt("else" ~ sttment)

  def whilesttm: Parser[Any] = "while" ~ expression ~ "do" ~ sttment

  def repeatsttm: Parser[Any] = "repeat" ~ sttment ~ "until" ~ expression

  def forsttm: Parser[Any] = "for" ~ ident ~ ":=" ~ expression ~ ("to" | "downto") ~ expression ~ "do" ~ sttment

  def breaksttm: Parser[Any] = "break"

  def continuesttm: Parser[Any] = "continue"

  def returnsttm: Parser[Any] = "return" ~ expression

  def invokesttm: Parser[Any] = expression ~ "." ~ ident ~ "(" ~ repsep(expression, ",") ~ ")"

  def primtype: Parser[Any] = "integer" | "float" | "bool" | "string" | "void"


  def boollit: Parser[Any] = elem("boolean", _.isInstanceOf[lexical.BooleanLit])

  def floatlit: Parser[Any] = elem("real", _.isInstanceOf[lexical.FloatLit])

  def intlit: Parser[Any] = elem("integer", _.isInstanceOf[lexical.IntLit])

  def stringlit: Parser[Any] = elem("string", _.isInstanceOf[lexical.StringLit])

例如,当我解析这个字符串时:

io.writeFloatLn(s.getArea());

它返回:

``.'' expected but `;' found"

在&#34;返回1&#34;。有人能告诉我我犯了什么错误吗?

编辑: - 我很抱歉,因为我没有充分理解我的问题,我问错了方法,现在我写了它所犯的确切错误。

  • 分隔符和关键字列表:

    reserved ++ = List(&#34; bool&#34;,&#34; break&#34;,&#34; continue&#34;,&#34; do&#34;,&#34; downto& #34;,&#34; else&#34;,&#34; float&#34;,&#34; for&#34;,     &#34;如果&#34;,&#34;整数&#34;,&#34;新&#34;,&#34;重复&#34;,&#34;字符串&#34;,&#34;然后& #34;,&#34;到&#34;,&#34;直到&#34;,&#34;而&#34;,&#34;返回&#34;,     &#34; true&#34;,&#34; false&#34;,&#34; void&#34;,&#34; null&#34;,&#34; self&#34;,&#34; final& #34;,&#34;类&#34;,&#34;扩展&#34;,&#34;摘要&#34;)

    分隔符++ =列表(&#34; [&#34;,&#34;]&#34;,&#34;(&#34;,&#34;)&#34;,& #34;:&#34;,&#34 ;;&#34;,&#34;。&#34;,&#34;,&#34;,&#34; {&#34;,& #34;}&#34;,&#34; +&#34;,&#34; =&#34;,     &#34; - &#34;,&#34; *&#34;,&#34; /&#34;,&#34; \&#34;,&#34;%&#34;, &#34;:=&#34;,&#34; ==&#34;,&#34;&lt;&#34;,&#34;&lt; =&#34;,&#34;&gt; ;&#34;,&#34;&gt; =&#34;,&#34;&lt;&gt;&#34;,&#34;&amp;&amp;&#34;,&#34;!& #34;,&#34; ||&#34;,&#34; ^&#34;)

2 个答案:

答案 0 :(得分:0)

我必须看到更多代码才能确定,但​​我在returnsttm列表中看不到sttm。这样可以防止sttment在查找左大括号之前匹配returnsttm

答案 1 :(得分:0)

似乎它们按预期工作 - io.writeFloatLn(s.getArea())被解析为语句中的表达式 - 所以解析器只是等待&#34;。&#34;来自你的陈述,如:

io.writeFloatLn(s.getArea()).writeFloatLn()

我认为您只能将括号用作语句(调用操作)的一部分,或仅作为表达式(应用程序)的一部分使用,具体取决于您需要哪种语言 - 命令式或功能性。与.相同。