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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:14:25+00:00 2026-06-13T03:14:25+00:00

I m trying the following yacc code and m receiving shift/reduce errors. I m

  • 0

I m trying the following yacc code and m receiving shift/reduce errors. I m pretty new to this

The Purpose of the code is to prepare the syntax for if - else with logical operators also incorporated

           %{
               #include<stdio.h>
               #include"lex.yy.c"
               int syntax_status=0;
           %}
           %token IF ELS id EE LE GE closep openp num openb closeb SP logicop 
           %start S
           %%
           S : S SP IF SP openp SP EXP SP closep SP openb SP closeb SP ELS SP openb SP closeb SP {syntax_status=1;}
             | S SP IF SP openp SP EXP SP closep SP openb SP closeb SP {syntax_status = 1;}
             | 
             ;
         EXP : EXP CMP logicop CMP
             | EXP CMP
             |
             ;
         CMP : CMP id EE id 
             | CMP id LE id
             | CMP id GE id
             | CMP id EE num
             | CMP id GE num
             | CMP id LE num
             | CMP num EE id
             | CMP num GE id
             | CMP num LE id
             | 
             ;
         %%
         int main()
         {
              printf("\n\n\n Enter the Syntax :  ");
              yyparse();
              if(syntax_status==1)
              {
                     printf("\n\n\n The Syntax is Correct ");
              }
              else
              {
                     printf("\n\n\n The Syntax is Imcorrect");
              }
              return 0;
          }
          yyerror(char *s)
          {
              syntax_status=0;
          }

The Lex Program for this corresponding yacc code is as follows :

           %{
              #include<stdio.h>
              #include"y.tab.h"
           %}
           IF (if)
           ELS (else)
           iden [a-zA-Z][a-zA-Z0-9]*
           num [0-9]+
           space [ ]* 
           %%
           {IF} { return IF; }
           {ELS} {return ELSE;}
           {iden} {return id;}
           (==) {return EE;}
           (<=) { return LE;}
           (>=) { return GE;}
           ")" { return closep;}
           "(" { return openp;}
          {num} { return num;}
          {space} { return SP; }
          "{" { return openb;}
          "}" { return closeb;}
          "||"|"&&"|"!=" {return logicop;}

%%

  • 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-13T03:14:26+00:00Added an answer on June 13, 2026 at 3:14 am

    The CMP and EXP rules both look very problematic. Take this rightmost derivation, for example:

     EXP ==> (by EXP -> CMP)
     EXP CMP ==>  (by CMP -> CMP id EE id)
     EXP CMP id EE id ==> (by CMP -> CMP id EE id)
     EXP CMP id EE id id EE id ==> (by CMP -> epsilon)
     EXP id EE id id EE id ==> (by EXP -> epsilon )
     id EE id id EE id ==>
     id == id id == id
    

    I’m pretty sure this isn’t what you want. It also introduces ambiguity into the grammar: the same string of terminals can be derived by this different rightmost derivation:

     EXP ==> (by EXP -> CMP)
     EXP CMP ==> (by CMP -> CMP id EE id)
     EXP CMP id EE id ==> (by CMP -> epsilon)
     EXP id EE id ==> (by EXP -> CMP)
     EXP CMP id EE id ==> (by CMP -> CMP id EE id)
     EXP CMP id EE id id EE id ==> (by CMP -> epsilon)
     EXP id EE id  id EE id ==> (by EXP-> epsilon)
     id EE id  id EE id ==>
     id == id id == id
    

    You see, yacc will have no way of knowing if id == id id == id is really exp exp or exp. Also, the exp->epsilon rule allows for expressions like if(){}, which isn’t very nice.

    Now, consider the following grammar:

     exp -> exp logicop cmp | cmp
     cmp -> rvalue < rvalue | rvalue > rvalue ...
     rvalue -> id | num
    

    But it could also be done like this:

    exp -> exp op exp | rvalue
    op -> "||" | "&&" | "<" | ">" ...
    

    The latter is the traditional way of doing it, but you’d need semantic checks to ensure that the operands are of the right types, while the previous grammar ensures that on a syntactic level. On the long run (once you introduce boolean variables), the second approach is much preferable, since type safety is a matter of semantics, not syntactics.

    On another note: spaces don’t seem relevant in your language, but lex is handing them up to yacc. You could make lex discard the spaces on the lexical level by saying

    {space} //nothing after a pattern discards the matched characters
    

    This would save you the headache of writing out all those SPs in yacc.

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

Sidebar

Related Questions

I'm trying to learn about shift-reduce parsing. Suppose we have the following grammar, using
I currently am trying the following code: $sql = $db->query(select id,username,fullname from userss ORDER
I am trying the following piece of code in java, but it doesn't seem
I am trying the following code in eclipse: public class A { List<Integer> intList
I am trying the following code to AutoComplete contact details as the user types
I was trying learn lex and yacc using the oreilly book. I tried following
I was trying following code in which I defined copy c'tor explicitly to solve
I'm trying to implement CSS transition on the hyperlinks, I'm trying following code, but
I am trying the following code to make a UIButton look Disabled : btnYourButton.alpha
I am using Android 2.2. I am trying following code to run: Uri uri=Uri.parse(http://bluediamondring-s.com/wp-content/uploads/2010/08/gold-ring.jpg);

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.