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 7995979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:34:53+00:00 2026-06-04T14:34:53+00:00

Why this raises a warning about reduce/reduce conflict root : set1 ‘X’ | set2

  • 0

Why this raises a warning about reduce/reduce conflict

root : set1 'X'     
     | set2 'X' 'X' 

set1 : 'A'          
     | 'B'             

set2 : 'B'          
     | 'C'  

but the next is ok?

root : 'A' 'X'     
     | 'B' 'X'     
     | 'B' 'X' 'X'  
     | 'C' 'X' 'X' 
  • 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-04T14:34:55+00:00Added an answer on June 4, 2026 at 2:34 pm

    The difference is because, in the first case, the parser has to make a choice about the reduction before it gets to see whether one 'X' or two will follow.

    In the second, the parser can use the same state, let’s call it BX, when it’s seen a B and an X—both shifted—and then depending on the next token, can shift (if an X), and then reduce the 'B' 'X' 'X' rule, or otherwise reduce 'B' 'X' immediately.

    Notice that if they didn’t have identical tokens immediately following—e.g. you had set1 'X' but set2 'Y'—then there would be no problem, because the lookahead would be able to kick in and pick which reduction to take.

    Here are the relevant sections from the output from bison -v for exposing this issue:

    Case one

    state 0
    
    $accept: . root $end
    
    'A'  shift, and go to state 1
    'B'  shift, and go to state 2
    'C'  shift, and go to state 3
    
    root  go to state 4
    set1  go to state 5
    set2  go to state 6
    

    Assuming we get a 'B', we go to state 2:

    state 2
    
    set1: 'B' .
    set2: 'B' .
    
    'X'       reduce using rule 4 (set1)
    'X'       [reduce using rule 5 (set2)]
    $default  reduce using rule 4 (set1)
    

    Note that there are two possible reductions we can make: to set1 or set2, both with the same input token. Hence a reduce/reduce; we only have one token of lookahead, and with this grammar, the only token could be a 'X'—in either case!

    Case two

    state 0
    
    $accept: . root $end
    
    'A'  shift, and go to state 1
    'B'  shift, and go to state 2
    'C'  shift, and go to state 3
    
    root  go to state 4
    

    Assuming we get a 'B', we go to state 2:

    state 2
    
    root: 'B' . 'X'
        | 'B' . 'X' 'X'
    
    'X'  shift, and go to state 6
    

    While we only have one token of lookahead, the parser generator can produce a state where it’s seen 'B' 'X' due to the accommodating structure of the input. Hence we go to state 6 in any event (or error otherwise ;-)):

    state 6

    root: 'B' 'X' .
        | 'B' 'X' . 'X'
    
    'X'  shift, and go to state 9
    
    $default  reduce using rule 2 (root)
    

    Here’s where the magic happens: if we see an 'X', we shift and go to state 9 (where we reduce), otherwise we reduce 'B' 'X' immediately.

    For completeness, here’s state 9:

    state 9
    
    root: 'B' 'X' 'X' .
    
    $default  reduce using rule 3 (root)
    

    If we had a token of lookahead to disambiguate

    With this sample grammar:

    root: set1 'X'
        | set2 'Y'
    
    set1: 'A'          
        | 'B'             
    
    set2: 'B'          
        | 'C'
    

    Then, we start:

    state 0
    
    $accept: . root $end
    
    'A'  shift, and go to state 1
    'B'  shift, and go to state 2
    'C'  shift, and go to state 3
    
    root  go to state 4
    set1  go to state 5
    set2  go to state 6
    

    We shift 'B' and go to state 2:

    state 2
    
    set1: 'B' .
    set2: 'B' .
    
    'Y'       reduce using rule 5 (set2)
    $default  reduce using rule 4 (set1)
    

    So, this state is reached in both rules for set1 and set2, where we have a single 'B' token on the stack. In this case, if we next see a 'Y', we reduce to set2—or in any other case, reduce to set1.

    The fact that it’s picked set1 as the “default” reduction could have implications for error handling.

    Appendix on GLR

    Happy (and bison or yacc) produce LALR(1) parsers by default, though you can produce a GLR parser with --glr (or %glr-parser in your bison declarations file). This can resolve ambiguities by concurrently trying both “possibilities”; the parser will fork and see how far it gets in either case.

    This is probably unwise unless you really need it, know you need it, and know what could happen if things go wrong, though. I’m not sure what happens if both forks terminate successfully; with my non-scientific testing, it seemed to always end up picking the longer parse.

    Lexer hacks

    If you don’t want to use GLR, but don’t want to restructure your parser significantly, you could consider using a lexer hack to overcome this issue.

    Right now, you have this:

    root : set1 'X'     
         | set2 'X' 'X' 
    

    You could instead emit a token for a single 'X' character, and a different token for two:

    root : set1 ONE_X
         | set2 TWO_XS
    

    This resolves ambiguity within a single token, and is an unambiguous LALR(1) grammar as a result.

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

Sidebar

Related Questions

This is a warning that Firefox raises when I want to leave certain pages.
For a block like this: try: #some stuff except Exception: pass pylint raises warning
Consider this scenario. I have an object, lets call it.... Foo. Foo raises a
When I try and run my program I get this warning and some weird
I would like some kind of warning to be raisen as errors, but only
I am not sure what is going on, but i get this weird issue
I'm trying to use Checkstyle ( this check ) to raise a warning when
I've read a few things, including this and this , but I think the
With the code below, Resharper raises a 'virtual member call in constructor' warning: public
I have raised this question on the WP Answers as well. But since this

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.