Question

how to solve yacc if else shift/reduce problem

Asked by: fabytes

Hi, I have a rule defined as follow:

  comando :  IF '(' exp ')' comando parte_else
                    | WHILE '(' exp ')' comando
                    | FOR '(' exp '=' exp ';' exp ';' exp ')' comando
                    ;

    parte_else : ELSE comando
                      | /* empty */
               ;

It's a more then famous shift/reduce problem, but the weird thing is that in my output is says the reduce is the default action, when I want to shift. I was told that bison would make shift default. How do I fix it?

grammar.output:

state 213

   63 comando: IF '(' exp ')' comando .
   64        | IF '(' exp ')' comando . ELSE comando

    ELSE  shift, and go to state 216

    ELSE      [reduce using rule 63 (comando)]
    $default  reduce using rule 63 (comando)

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2008-03-30 at 11:51:40ID23280991
Tags

bison yacc

Topics

Parsers

,

C Programming Language

Participating Experts
1
Points
500
Comments
17

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Shift/Reduce Conflict in YACC
    I am getting a Shift/Reduce conflict in YACC. This (Reduce)conflict is on the production that is an € (empty) production. Since YACC chooses to do a shift on every Shift/Reduce conflict, Can I be assured that shifting is the safest thing to do on any input occurence since the...
  2. Trapping SHIFT keys
    How can I trap the SHIFT buttons on the keyboard so that, for the sake of example, when I click the left SHIFT button I can display a "left SHIFT" message and when I click the right SHIFT button I can display a "right SHIFT" message?
  3. Caps Lock + Shift
    Hi There, I am using Windows XP Home on a notebook. When I want to turn the Caps Lock on or off, I have to press the shift key and the Caps Lock key. On my other computer I only had to press the Caps Lock key, and I preferred this setup. How can I get it so I only need to...
  4. normal web pages shift right and wordpress pages shift left
    Hi, I have a weird problem. you have to be working on screen resolution: 1024x768 to see this issue. when you open www.adhd.org.sa/home.php, start navigating through the top links one by one. links number 3 and 4 from the left side; navigate to pages where the content is ...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: Infinity08Posted on 2008-03-31 at 02:11:51ID: 21244036

>> I was told that bison would make shift default.

I usually use yacc, and there the shift is indeed the default - I assume that that's the same for bison.

And it does, no ?

        ELSE  shift, and go to state 216

where state 216 is probably :

        IF '(' exp ')' comando ELSE . comando

so, it does do the shift.

I think (I'm not familiar with bison's output, so I might be mistaken), that this part :

        ELSE      [reduce using rule 63 (comando)]
        $default  reduce using rule 63 (comando)

refers to the reduction of the comando rule., not the if rule. Meaning that we've just seen the comando rule, so we reduce it to the comando "token", and can then continue with the ELSE by shifting the ELSE, or reducing the if rule if no ELSE is seen.

 

by: fabytesPosted on 2008-03-31 at 06:38:21ID: 21245202

yacc and bison have the same output in this case. what I understand for that:

  64        | IF '(' exp ')' comando . ELSE comando

    ELSE  shift, and go to state 216

    ELSE      [reduce using rule 63 (comando)]
    $default  reduce using rule 63 (comando)

Is that the next token is an ELSE I can shift to state 216 or reduce to state 63 and the default action is to reduce ( $default  reduce using rule 63 (comando) ). Or Am I wrong? I'm newbee in yacc.

 

by: Infinity08Posted on 2008-03-31 at 06:54:51ID: 21245348

>> yacc and bison have the same output in this case.

Not the yacc version I use ;) (byacc). But similar, yes.

The output I would get is something similar to this :

        213: shift/reduce conflict (shift 216, reduce 63) on ELSE
        state 213
              comando : IF '(' exp ')' comando .  (63)
              comando : IF '(' exp ')' comando . ELSE comando  (64)
       
              ELSE  shift 216
                <SNIP>



Can you show your entire grammar, and the entire grammar.output file ?

 

by: fabytesPosted on 2008-03-31 at 07:05:39ID: 21245446

There it goes. The entire grammar and output

 

by: fabytesPosted on 2008-03-31 at 07:08:27ID: 21245473

Ps: Don't care about other reduce/reduce and shift/reduce problems. Those I'll solve later. And are more complicated.
thanks.  

 

by: Infinity08Posted on 2008-03-31 at 07:25:24ID: 21245621

The way I usually fix a shift/reduce conflict is like this :

        parte_else : ELSE comando
               | %prec LOWER_THAN_ELSE
               ;

(so, using a dummy token LOWER_THAN_ELSE that has a lower precedence than the ELSE token, so if an ELSE is found, it always shifts).

To make the LOWER_THAN_ELSE token a lower precedence than the ELSE token, you have to add this before the first %% :

        /* resolve shift/reduce conflict for ELSE */
        %nonassoc LOWER_THAN_ELSE
        %nonassoc ELSE


Your parser should automatically choose shift as default though (unless you configured it not to, by means of command line options or precedence rules or so)

 

by: fabytesPosted on 2008-03-31 at 07:45:39ID: 21245800

Good. That works! If you could help me with the reduce/reduce problems on that grammar I can increase your points to 500.  Can you?

The $default action that the parser says it takes on the reduce/reduce conflicts it the soluction I want. But yacc does not compile when you have reduce/reduce.
ps: when you have shift/reduce you can use %expect to say to the parser it's ok. But I dn't know any mean to do that on reduce/reduce.

 

by: Infinity08Posted on 2008-03-31 at 08:14:42ID: 21246116

>> ps: when you have shift/reduce you can use %expect to say to the parser it's ok. But I dn't know any mean to do that on reduce/reduce.

The default for reduce/reduce conflicts is to reduce by the earlier rule. But be careful to depend on the default, because reduce/reduce conflicts usually indicate a grammar problem (the grammar is ambiguous). Usually you should resolve the reduce/reduce conflict by rewriting the grammar.



About solving the reduce/reduce conflicts : I think you made a typo here :

   exp :  var
         | LITERAL
         | INTEGER          
         | FLOAT            
         | '(' exp ')'      
         | chamada          
         | '-' exp          
         | '&' exp                                 <----- here (was : '&' var)
         | INC_OP exp        
         | DEC_OP exp      

 

by: fabytesPosted on 2008-03-31 at 08:25:56ID: 21246235

You really know stuff! I spent hell of a time on that and I couldn't fix. Just one last question, do you think that var and exp rules now are ok?

 

by: Infinity08Posted on 2008-03-31 at 08:28:59ID: 21246268

>> Just one last question, do you think that var and exp rules now are ok?

That depends on what grammar you are trying to implement ... Is this for a specific language ?

 

by: fabytesPosted on 2008-03-31 at 08:36:58ID: 21246343

It's a sub C grammar, not a complete C grammar, but a simpler one based on C. I want that every code that compiles on that grammar compiles in gcc  too. The other is not true.

 

by: fabytesPosted on 2008-03-31 at 08:38:03ID: 31444262

thanks one more time!

 

by: Infinity08Posted on 2008-03-31 at 08:45:02ID: 21246412

Verifying that each specific part of your grammar complies with the C grammar (as a subset) would take a lot of time, because it's not structured like the standard C grammar.

Most likely there will be inconsistencies ... (maybe not harmful).


Is there any reason you work with a subset of the C grammar, or are you just doing that to make it easier ?

The C standard contains the complete grammar in annex A, and requires little changes to make it work with yacc. So you could base yourself on that grammar ...

 

by: fabytesPosted on 2008-03-31 at 08:57:29ID: 21246541

I'm doing that just to make easier. To have something to base it on. I know but I thought C grammar too complex. And I'm doing this to learn how to write a grammar from scretch. I'll build an abstract tree and generate assembly code from that. Do you think I can solve those "inconsistencies" you talk about in my semantic analiser? Or that's a grammar problem?

 

by: Infinity08Posted on 2008-03-31 at 09:20:50ID: 21246784

>> Do you think I can solve those "inconsistencies" you talk about in my semantic analiser?

If this is just for learning, I wouldn't worry about making the parser 100% compatible with the standard C language. Especially since even different compilers are not 100% compatible in this respect.

Worry about getting your parser to work, and then throw some C code at it, and see what it does ...
If you followed the C syntax (as detailed by the C standard), you shouldn't notice too many problems (the C language doesn't have a complicated grammar).

 

by: fabytesPosted on 2008-04-01 at 16:38:31ID: 21259056

Hi Infinity08, I still have a problem. Make | & exp  does not make sense, what means the adderess of (a+b) for example. Or &(--a). It only make sense to & var  where var is a rule that describes variables which are space in memory. You got me? Could you think of another way to solve those reduce/reduces conflicts without changing the | & var  rule?

thanks  

 

by: Infinity08Posted on 2008-04-02 at 01:41:03ID: 21261076

>> what means the adderess of (a+b) for example. Or &(--a).

The C language syntax allows that. Here's the relevant part of the standard (C99) syntax :

        unary-expression:
            postfix-expression
            ++ unary-expression
            -- unary-expression
            unary-operator cast-expression
            sizeof unary-expression
            sizeof ( type-name )

        unary-operator: one of
            & * + - ~ !

        cast-expression:
            unary-expression
            ( type-name ) cast-expression


BUT : it's not valid C, because it doesn't make sense to take the address of a temporary. So, the C standard added this constraint :

        The operand of the unary & operator shall be either a function designator, the
        result of a [] or unary * operator, or an lvalue that designates an object that
        is not a bit-field and is not declared with the register storage-class specifier.

So, your parser can let it pass, but the constraint should be checked later.

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...