A simple expression is reported to be recursive in parser combinators

Guess what is the result of this compilation?

import scala.util.parsing.combinator._

object ExprParser extends JavaTokenParsers {
    lazy val literal = int  
    lazy val int = rep("0")
}

The compiler says it intis recursive and requests its type . My experiment says that the core of recursion is hidden in the literal declaration! Delete it and you will see that the recursion is gone!

+4
source share
1 answer

It is relevant here that JavaTokenParsersdefines something called literal.

And rep("0")actually rep(literal("0"))( literal- this is an implicit conversion from Stringto Parser[String]).

literal JavaTokenParsers , - . , , , - int literal.

, :

object ExprParser extends JavaTokenParsers {
  lazy val literal: Parser[List[String]] = int 
  lazy val int: Parser[List[String]] = rep("0")
}

.

, , ?

, . , , . Scala Language Specification, .

, , , , , . , .

, literal int, int literal, , .

+5

Source: https://habr.com/ru/post/1621784/


All Articles