Note that there are some explanatory texts on larger screens.

plurals
  1. POScala parser combinators, parsers fails due to precedence
    primarykey
    data
    text
    <p>I am trying to write an interpreter for the programming language Icon. One of the steps in this process is writing a parser for Icon, which I've done in the following way:</p> <pre><code>import java.io.FileReader import scala.util.parsing.combinator.syntactical._ import scala.util.parsing.combinator.RegexParsers import scala.util.parsing.combinator.PackratParsers import scala.util.parsing.combinator.JavaTokenParsers abstract class expr case class CstInt(val value : Int) extends expr case class FromTo(val from : expr, val to : expr) extends expr case class Write(val value : expr) extends expr case class And(val e1 : expr, val e2 : expr) extends expr case class Or(val e1 : expr, val e2 : expr) extends expr object ExprParser extends JavaTokenParsers with PackratParsers{ lazy val exp : PackratParser[expr] = andexp | exp2 lazy val exp2 : PackratParser[expr] = fromTo | exp3 lazy val exp3 :PackratParser[expr] = orexp | exp4 lazy val exp4 : PackratParser[expr] = integer | exp5 lazy val exp5 : PackratParser[expr] = write lazy val integer : PackratParser[expr] = wholeNumber ^^ { s =&gt; CstInt(s.toInt)} lazy val write : PackratParser[Write] = "write" ~&gt; "(" ~&gt; exp &lt;~ ")" ^^ { e =&gt; Write(e)} lazy val fromTo : PackratParser[FromTo] = ("(" ~&gt; integer) ~ ("to" ~&gt; integer &lt;~ ")") ^^ { case from ~ to =&gt; FromTo(from, to)} lazy val andexp : PackratParser[And] = exp ~ ("&amp;" ~&gt; exp) ^^ { case e1 ~ e2 =&gt; And(e1, e2)} lazy val orexp : PackratParser[Or] = exp ~ ("|" ~&gt; exp) ^^ { case e1 ~ e2 =&gt; Or(e1, e2)} def parseInput(input: String) : expr = parseAll (exp, input) match { case Success(tree, _) =&gt; tree case e: NoSuccess =&gt; throw new IllegalArgumentException(e.toString()) } } object Interpret { def main(args : Array[String]) : Unit = { println(ExprParser.parseInput(args(0))) } } </code></pre> <p>However, I've run into a few problems when I try to parse the following expression:</p> <pre><code>write((1 to 4) | 4) </code></pre> <p>I get this error:</p> <pre><code>java.lang.IllegalArgumentException: [9.17] failure: `)' expected but ` ' found </code></pre> <p>Whereas parsing</p> <pre><code>write((1 to 4) &amp; 4) </code></pre> <p>works just fine. The first expression works fine if I move the orexp parser to an exp group above the fromto parser. However, this does not adhere to the rules given by Icon, and does not solve the underlying problem. </p> <p>Does anyone have any ideas for solutions? According to the Scala docs, mixing packrat parsers and regular parsers should be ok.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload