Giter Club home page Giter Club logo

krunch's People

Contributors

ibelieve avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

krunch's Issues

Backtracking?

Hello!

I'm trying to implement a simple calculator-like grammar with variables. It's supposed to parse arbitrary expressions and variable assignments like:

a = 1 + 2
10 + a

So the code I came up with, looking at the provided calculator grammar sample, is as follows (only + operator supported for the sake of brevity):

object SampleParser: RegexParsers<Expr>() {
    val number = literal("[0-9]+".toRegex())
    val identifier = literal("[a-z]+".toRegex())

    val variable = identifier map { Var(it) }
    val value = number map { Value(it.toInt()) }

    val atom = oneOf(variable, value)
    val expr = atom and some(literal("+") and atom) map {
        it.second.fold(it.first) { left, (_, right) -> Add(left, right) }
    }
    val assign = identifier and literal("=") and expr map { Assign(it.first.first, it.second) }
    val stmt = oneOf(assign, expr)

    override val goal = stmt
}

So my issue is that the parser cannot distinguish expr and assign. Only the first rule in oneOf gets parsed correctly, the second one is not even attempted due to the first one failing:

> a = 2
result = 2

> a + 2
Failed to parse expression!
ERROR: Expected: '=' (1:3)

a + 2
  ^

After coming across + in the second example, I expect it to backtrack from assign and try the next one in oneOf, which is expr - but that does not happen. What am I doing wrong?

The Expr nodes are implemented along the lines of:

sealed class Expr {
    abstract fun evaluate(ctx: CalcContext): Int
}

class Value(private val value: Int): Expr() {
    override fun evaluate(ctx: CalcContext)= value
}

class Var(private val varName: String): Expr() {
    override fun evaluate(ctx: CalcContext)= ctx.getValue(varName)
}

class Add(private val left: Expr, private val right: Expr): Expr() {
    override fun evaluate(ctx: CalcContext) = left.evaluate(ctx) + right.evaluate(ctx)
}

class Assign(private val varName: String, private val expr: Expr): Expr() {
    override fun evaluate(ctx: CalcContext): Int {
        val value = expr.evaluate(ctx);
        ctx.setValue(varName, value)
        return value
    }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.