Closed idgrasp closed 2 years ago
Very good catch! I can confirm the problem
I'm gonna give it a look
This solves the problem:
--- a/tests/calculator_with_ast/grammar.rs
+++ b/tests/calculator_with_ast/grammar.rs
@@ -13,16 +13,15 @@ pub enum AST {
pub fn grammar() -> Grammar<AST> {
santiago::grammar!(
- "expr" => rules "bin_op";
"expr" => rules "int";
- "bin_op" => rules "expr" "add" "expr" =>
+ "expr" => rules "expr" "add" "expr" =>
AST::BinaryOperation;
- "bin_op" => rules "expr" "subtract" "expr" =>
+ "expr" => rules "expr" "subtract" "expr" =>
AST::BinaryOperation;
- "bin_op" => rules "expr" "multiply" "expr" =>
+ "expr" => rules "expr" "multiply" "expr" =>
AST::BinaryOperation;
- "bin_op" => rules "expr" "divide" "expr" =>
+ "expr" => rules "expr" "divide" "expr" =>
BinaryOperation(
[
BinaryOperation(
[
Int(
1,
),
OperatorAdd,
BinaryOperation(
[
BinaryOperation(
[
Int(
2,
),
OperatorMultiply,
Int(
3,
),
],
),
OperatorDivide,
Int(
6,
),
],
),
],
),
OperatorSubtract,
Int(
7,
),
],
)
I'm pending to investigate it further
I just reviewed more thoroughly and:
precedence and associativity can only be expressed between productions that involve rules of the same type, for instance: "a" => rules "a" "operator" "a"
would allow specifying associativity and precedence like: Associativity::Left => rules "operator"
, while "example" => rules "foo" "operator" "bar"
is not subject to associativity and precedence because foo
, bar
, and example
are not of the same type and they won't nest while parsing, (so there is nothing to disambiguate/associate with the precedence rules)
so the mistake in the documentation was due to and error in the grammar of the calculator, and not really in Santiago. The problem in the calculator was fixed in: https://github.com/kamadorueda/santiago/commit/1b85ae686b8521609f707ed4da04d3f6a99d26e4. Thanks for the report
You mentioned that you tried using Santiago in a project of yours, that's nice! thank you. If you can share the grammar here I would be able to make more specific suggestions
Thank you a lot! That worked! Was debugging satisfies_disambiguation
to try to find an answer :)
P.S. I'm practicing rust (switched from C++) and working on a pet project. I went through a lot of generators (including PEG generators without left recursion...) and I think Santiago is the best project for my purposes. After Flex+Lemon in C++ projects, it looks really handy and comfortable!
Thank you for your work!
I think this issue can be closed :)
That's awesome, thank you!
Hi Kevin!
I tried to use Santiago in a project and found a problem with precedence rules: https://github.com/kamadorueda/santiago/blob/264bf39a29318ee9686aae4d9d805853123cbcc0/src/lib.rs#L447 The result of evaluated expression (
1 + 2 * 3 / 6 - 7
) must be -5. I've set the sameAssociativity
rules in my grammar, and it doesn't work properly. String1 + 2 * 3
is parsed intoBinaryOp([BinaryOp([Int(1), Plus, Int(2)]), Multiply, Int(3)])
, which is(1 + 2) * 3