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

Precedence operators to resolve S/R conflicts #12

Closed
kivikakk opened this issue Oct 19, 2016 · 6 comments
Closed

Precedence operators to resolve S/R conflicts #12

kivikakk opened this issue Oct 19, 2016 · 6 comments

Comments

@kivikakk
Copy link

This isn't a feature request, more feeling out: would you be interested in a PR that added precedence operators to automatically solve shift–reduce conflicts? (per bison's %left / %right / %nonassoc) If so, I might give it a shot. (No guarantees I'm actually capable of it!)

@goffrie
Copy link
Owner

goffrie commented Oct 19, 2016

There's already some support for this, though undocumented, haha. Look for
"overriding" and "no_reduce".

On Oct 18, 2016 19:24, "Yuki Izumi" notifications@github.com wrote:

This isn't a feature request, more feeling out: would you be interested in
a PR that added precedence operators to automatically solve shift–reduce
conflicts? (per bison's %left / %right / %nonassoc) If so, I might give
it a shot. (No guarantees I'm actually capable of it!)


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
#12, or mute the thread
https://github.com/notifications/unsubscribe-auth/ABLtTrE3vbNDGrmAc7aeBI-vJQ-W2vcgks5q1X9igaJpZM4KafmF
.

@goffrie
Copy link
Owner

goffrie commented Oct 19, 2016

See https://github.com/rudi-c/cs444-java-compiler-in-rust/blob/master/ast/src/parser.rs for an old example of how I used them. If you want to improve the interface or make them more general, or whatever, that would be great!

@kivikakk
Copy link
Author

Great, will look at this tonight! Thanks :)

@Addisonbean
Copy link

Sorry if this is the wrong place for this, but I am getting a shift reduce conflict, and am unable to figure out how to use these macros to fix it. Here is the code:

expr: Expr {
    // #[overriding]
    Ident(var) Equals expr[rhs] => Expr {
        span: span!(),
        node: Expr_::Assign(var, Box::new(rhs)),
    },
    expr[e] LParen RParen => Expr {
        span: span!(),
        node: Expr_::FuncCall(e),
    },
    term[t] => t,
}

That second line, the one commented out, was my attempt at trying to solve the error, but that did nothing to fix it. Do you have any idea how I'd use these macros to fix this problem?

Here is the error:

error: shift-reduce conflict:
state: expr -> Ident Equals expr •
       expr -> expr • LParen RParen
token: LParen

Thank you for your time!

@goffrie
Copy link
Owner

goffrie commented Aug 28, 2017

So I believe you can fix this by using #[no_reduce(LParen)], but I don't recommend that as a proper solution. The normal way to fix this kind of issue in an LR grammar is to introduce precedence levels for different kinds of expressions (in this case, you'd want the function-call expression to have higher precedence than the assignment-expression). I don't know of a good reference to point you to off-hand, but any classical compiler textbook should cover this kind of thing. Hope this helps!

@Addisonbean
Copy link

The #[no_reduce(LParen)] worked great, and then I was able to get it working with out that by looking further into that java compiler link your shared here. Thank you!

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

3 participants