Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue while trying to fine-tune token precedence using Associativity rules #5

Open
springcomp opened this issue May 11, 2023 · 0 comments

Comments

@springcomp
Copy link

springcomp commented May 11, 2023

The crate documentation shows a very simple grammar to tokenize and parse arithmetic addition of integers.

use santiago::lexer::LexerRules;
use santiago::grammar::{Grammar, Associativity};

pub fn lexer_rules() -> LexerRules {
    santiago::lexer_rules!(
        // One more sequential digits from 0 to 9 will be mapped to an "INT"
        "DEFAULT" | "INT" = pattern r"[0-9]+";
        // A literal "+" will be mapped to "PLUS"
        "DEFAULT" | "PLUS" = string "+";
        // Whitespace " " will be skipped
        "DEFAULT" | "WS" = pattern r"\s" => |lexer| lexer.skip();
    )
}
pub fn grammar() -> Grammar<()> {
    santiago::grammar!(
        "sum" => rules "sum" "plus" "sum";
        "sum" => lexemes "INT";

        "plus" => lexemes "PLUS";
    )
}

The example demonstrates that parsing this grammar yields two different abstract syntax trees, because there can be two different ways to interpret the binary addition with respect to one another in the following expression: 10 + 20 + 30.

However, If I change the grammar slightly, then:

  • I get a single abstract syntax tree.
  • Associativity rules no longer have any effect.

This is a sample repro-case of an issue I’m facing on a more complex grammar:

    santiago::grammar!(
        "expr" => rules "sum";
        "expr" => rules "num";

        "sum" => rules "expr" "plus" "expr";

        "num" => lexemes "INT";
        "plus" => lexemes "PLUS";
    )

Is there anything I’m doing wrong?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant