Code Monkey home page Code Monkey logo

Comments (3)

Zij-IT avatar Zij-IT commented on June 12, 2024 3

Your expression grammar is what is referred to as left-recursive because of this bit:

let expression = recursive(|expression| {
        let logic = expression
            .clone()
            .then(choice((
                just("==").to(LogicOperator::Equal),
                just("!=").to(LogicOperator::NotEqual),
                just(">").to(LogicOperator::Greater),
                just("<").to(LogicOperator::Less),
                just(">=").to(LogicOperator::GreaterOrEqual),
                just("<=").to(LogicOperator::LessOrEqual),
                just("&&").to(LogicOperator::And),
                just("||").to(LogicOperator::Or),
            )))
            .padded()
            .then(expression)
            .map(|((left, operator), right)| {
                Expression::Logic(Box::new(Logic {
                    left,
                    operator,
                    right,
                }))
            });

        let value = value.map(|value| Expression::Value(value));

        choice((logic, value))
    });

You have defined an expression as this:

expression := expression op expression
           | value

In order to parse an expression, we have to parse an expression… and to do this we have to parse an expression… and to do this we have to parse an expression.

A weakness of parser combinators (or recursive descent parsers in general) is that they aren’t suited to handle left recursion. When they encounter it, they blow the stack because they don’t know when to stop trying to parse the left hand side.

There are many ways to solve this problem, such as either precedence climbing or pratt parsing (described here). Chumsky has built-in support for pratt parsing using the pratt feature!

To see how to use it, check out the documentation, which has an example for you.

from chumsky.

zesterer avatar zesterer commented on June 12, 2024 1

We did previously attempt to add a panic for this case, but it ended up producing false positives when used in combination with memoisation. I'd like to resurrect attempts at solving that at some point.

from chumsky.

solaeus avatar solaeus commented on June 12, 2024

Thank you for the quick response. I was able to get the pratt parser working.

let value_expression = value.map(|value| Expression::Value(value));

let logic_expression = value_expression.pratt((
    infix(left(1), operator("=="), |left, right| {
        Expression::Logic(Box::new(Logic::Equal(left, right)))
    }),
    infix(left(1), operator("!="), |left, right| {
        Expression::Logic(Box::new(Logic::NotEqual(left, right)))
    }),
    infix(left(1), operator(">"), |left, right| {
        Expression::Logic(Box::new(Logic::Greater(left, right)))
    }),
    infix(left(1), operator("<"), |left, right| {
        Expression::Logic(Box::new(Logic::Less(left, right)))
    }),
    infix(left(1), operator(">="), |left, right| {
        Expression::Logic(Box::new(Logic::GreaterOrEqual(left, right)))
    }),
    infix(left(1), operator("<="), |left, right| {
        Expression::Logic(Box::new(Logic::LessOrEqual(left, right)))
    }),
    infix(left(1), operator("&&"), |left, right| {
        Expression::Logic(Box::new(Logic::And(left, right)))
    }),
    infix(left(1), operator("||"), |left, right| {
        Expression::Logic(Box::new(Logic::Or(left, right)))
    }),
));

It would be cool if there were some warning about this when using recursive since it's so easy to make an infinite loop. I'm not sure if that's possible but it would be nice.

from chumsky.

Related Issues (20)

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.