Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 546947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:55:14+00:00 2026-05-13T10:55:14+00:00

I’ve been working on a C-like grammar for my personal amusement. However, I’ve been

  • 0

I’ve been working on a C-like grammar for my personal amusement. However, I’ve been running into shift/reduce conflicts, and I’m quite sure they could be resolved.

Right now my expressions look like this, in a simplified form, stripped of actions:

%left '+' '-'

%%
expr
 : NUMBER
 | IDENTIFIER
 | expr '+' expr
 | expr '-' expr
 /* other operators like '*', '/', etc. */
 | expr '(' expr ')' /* function call */
%%

However, this results in shift/reduce conflicts: the parser is unsure about how to treat parentheses. From what -v tells me, it is unclear whether an expression like expr '+' expr '(' should reduce expr '+' expr into expr or shift the parenthesis.

Obviously, I want the parenthesis to be shifted. foo % bar(4) shouldn’t end up being (foo % bar)(4). However, I’ve had no success using the %prec directive to that mean. Adding %left FUNCALL and %prec FUNCALL after the rule yields no change.

I know that the default path Bison’s LALR parsers will go when encountering a shift/reduce will be to shift, and that I could just use %expect to fix work around the problem. However, one conflict is generated for each expression, and would I ever need to change that list, I’ll also need to change the %expect declaration, which looks like a rather ugly solution to me. Besides, I’m sure one of you wise kids have a solution to that problem.

My goal is to have a rule similar to the one above, for which Bison will know that whenever it encounters the '(' from the function call rule, it has shift the parenthesis, without a possible shift/reduce conflict. For the record, my use of the %prec directive is the following, so if I’m just doing it wrong you can correct me. It does have the shift/reduce conflict.

%left '+' '-'
%left FUNCALL

%%

expr
    : NUMBER
    | IDENTIFIER
    | expr '+' expr
    | expr '-' expr
    /* other operators like '*', '/', etc. */
    | expr '(' expr ')' %prec FUNCALL /* function call */

%%
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T10:55:14+00:00Added an answer on May 13, 2026 at 10:55 am

    You need to add %left '(' to your precedence rules (or %nonassoc '(' might be better).

    The way that precedence works to resolve shift/reduce conflicts in yacc/bison is that it compares the precedence of the rule to be reduced with the precedence of the token to be shifted. In your example, the conflict is between reducing expr: expr '+' expr and shifting a '(', so to resolve it, you need a precedence on '(' (and you want it to be higher than the rule, which comes from '+')

    The %prec directive just sets the precedence of a rule, overriding its default precedence which comes from the first token on its rhs. It doesn’t affect the precedence of tokens that appear in the rule in any way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am currently running into a problem where an element is coming back from
I have a jquery bug and I've been looking for hours now, I can't
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I would like to run a str_replace or preg_replace which looks for certain words
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.