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

  • Home
  • SEARCH
  • 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 8645709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:37:07+00:00 2026-06-12T12:37:07+00:00

How do I remove shift-reduce conflict for bison for the given grammar? selection-stmt ->

  • 0

How do I remove shift-reduce conflict for bison for the given grammar?

 selection-stmt -> if ( expression ) statement |
                      if ( expression ) statement else statement

A solution giving the modified grammar would be highly appreciated.

  • 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-06-12T12:37:08+00:00Added an answer on June 12, 2026 at 12:37 pm

    There is a much simpler solution. If you know how LR parsers work, then you know that the conflict happens here:

    if ( expression ) statement * else statement
    

    where the star marks the current position of the cursor. The question the parser must answer is "should I shift, or should I reduce". Usually, you want to bind the else to the closest if, which means you want to shift the else token now. Reducing now would mean that you want the else to wait to be bound to an "older" if.

    Now you want to "tell" your parser generator that "when there is a shift/reduce conflict between the token "else" and the rule "stm -> if ( exp ) stm", then the token must win". To do so, "give a name" to the precedence of your rule (e.g., "then"), and specify that "then" has less precedence than "else". Something like:

    // Precedences go increasing, so "then" < "else".
    %nonassoc "then"
    %nonassoc "else"
    %%
    stm: "if" "(" exp ")" stm            %prec "then"
       | "if" "(" exp ")" stm "else" stm
    

    using Bison syntax.

    I’m uneasy with the %nonassoc here, because it really says that "then" and "else" are non associative, which is true in most grammars, but I only meant to give them precedence levels, not associativity. Bison provides %precedence to this end:

    // Precedences go increasing, so "then" < "else".
    %precedence "then"
    %precedence "else"
    %%
    stm: "if" "(" exp ")" stm            %prec "then"
       | "if" "(" exp ")" stm "else" stm
    

    Actually, my favorite answer is even to give "then" and "else" the same precedence. When the precedences are equal, to break the tie between the token that wants to be shifted, and the rule that wants to be reduced, Bison/Yacc will look at associativity. Here, you want to promote right-associativity so to speak (more exactly, you want to promote "shift"), so:

    %right "then" "else" // Same precedence, but "shift" wins.
    

    will suffice.

    According to Bison manual (3.8.1), "Neither solution is perfect however."

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

Sidebar

Related Questions

The error message: bison -vdy tjc.y conflicts: 2 shift/reduce tjc.y:72.26-29: warning: rule useless in
How do I remove an element of an array and shift the remaining elements
sub DirectoryExists { my $param = shift; # Remove first element of the array
Here is the problem: Remove specified characters from a given string. Input: The string
I would like to remove a size from titles i.e. Shift Fuel Street Motorcycle
how to shift the top element from array based on a regular expression using
I'm trying to parse a simple grammar using an LALR(1) parser generator (Bison, but
For example, given a str of Stackoverflow is for every one and remove of
I know that shift, push, and pop are Array methods used to add/remove elements
Remove first comma on each line in a file. I presume sed is needed.

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.