Closed solaeus closed 9 months ago
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.
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.
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.
I get a stack overflow when running any of the tests for this library. I tried to look over similar issues to figure out what I'm doing wrong but I've had no luck. I'm using verion 1.0.0-alpha.6.
EDIT: The tests run when I remove the
let expression
andlet statement
sections. Removing just the statement section causes the same stack overflow so it appears to be an issue with the expression parsing.