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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:54:13+00:00 2026-06-13T13:54:13+00:00

Hello I have just started doing some parsing in bison/yacc. Now my first program

  • 0

Hello I have just started doing some parsing in bison/yacc. Now my first program already fails. What went wrong? I am using an example from:
original source of tutorial

%{
    #include <stdio.h>
    int yylex(void);
    void yyerror(char *);
%}


%token INTEGER

%%

program:
        program expr '\n'         { printf("%d\n", $2); }
        | 
        ;

expr:
        INTEGER                   { $$ = $1; }
        | expr '+' expr           { $$ = $1 + $3; }
        | expr '-' expr           { $$ = $1 - $3; }
        ;

%%

void yyerror(char *s) {
    fprintf(stderr, "%s\n", s);
}

int main(void) {
    yyparse();
    return 0;
}

Using version 2.4.1 of bison I get this error:

conflicts: 4 shift/reduce
  • 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-13T13:54:14+00:00Added an answer on June 13, 2026 at 1:54 pm

    Try that:

    expr:
        INTEGER                   { $$ = $1; }
        | expr '+' INTEGER           { $$ = $1 + $3; }
        | expr '-' INTEGER           { $$ = $1 - $3; }
    

    bison/yacc don’t like right recursion. If the input is 1+2+3, when the parser reaches 1+2+ it can’t decide either to reduce 1+2 to expr or to shift another token in order to reduce 2+3 to expr. By specifying INTEGER on the right, it can decide to reduce as soon as it sees 1+2 leaving only expr + 3 then reducing again.

    You can also solve the issue by specifying that + and - are left associative, thus giving a higher priority to reducing 1+2 over shifting new tokens. Add that line in your preamble:

    %left '+' '-'
    

    Hope it helps.

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

Sidebar

Related Questions

I have just put the first step in android,and just started with hello android
I just started my Hello World program in objective C on windows.I have installed
I have written a small program just like hello the world , everything was
I have just started the Hello World App using Blackberry Eclipse Plugin. I have
I have just started with Android with the usual Hello World project template in
I have just started learning Javascript. I want Hello World! to be written to
Hello there people of stack overflow, I have just started developing with android and
I have just started learning how to program Windows GUI's using the low level
Hello! I have just started playing with android layouts and i wonder if there
I just started programming J. Now I want to type text=: 'hello world' text

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.