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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:09:57+00:00 2026-05-18T20:09:57+00:00

How does this tiny yacc programe work? What I know so far: %{…%} is

  • 0

How does this tiny yacc programe work?

What I know so far:

%{...%} is definition
%% ... %% is rule,but how to interpret the rule?
and stuff after %% is function definition.
What’s the section %token INTEGER between %} and %% ?

%{
#include <stdlib.h>
int yylex(void);
void yyerror(char *);
%}
%token INTEGER
%left '+' '-'
%left '*' '/'
%%
program:
program expr '\n' { printf("%d\n", $2); }
|
;
expr:
INTEGER { $$ = $1; }
| expr '*' expr { $$ = $1 * $3; }
| expr '/' expr { $$ = $1 / $3; }
| expr '+' expr { $$ = $1 + $3; }
| expr '-' expr { $$ = $1 - $3; }
;
%%
void yyerror(char *s) {
printf("%s\n", s);
}
int main(void) {
yyparse();
return 0;
}

UPDATE

What I don’t understand:

program:
program expr '\n' { printf("%d\n", $2); }
|
;
  • 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-05-18T20:09:58+00:00Added an answer on May 18, 2026 at 8:09 pm

    The expr: means an expr is one of the following options, which are separated by | below. If it can be seen as an INTEGER token, then it takes the first one. If it can be seen as an expr followed by the * character followed by an expr then it takes the second option and so on. $$ is the default return value, and $1 is the first token, $2 the second on and on.

    So if it were parsing 5 + 6, it sees it as expr ‘+’ expr, so it takes the 4th definition. It returns 11 as an expr, so then it matches 11 as an INTEGER token and assigns 11 as the return value.

    If we were parsing a program token followed by 5 + 6, it would do the same thing to get program 11, then take the program expr rule and call the c code which would print to the screen.

    Left means that operator is left associative. As in a + b + c = (a + b) + c. The operators on the same line have the same precedence, and those below it have lower precedence.

    I admittedly haven’t used yacc in a while so feel free to tell me I’m totally wrong.

    UPDATE:

    yacc generates c code, so you can put your own code directly into it. So as it’s parsing, if it sees a “program expr”, then it can directly input the code in the { } into the generated code.

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

Sidebar

Related Questions

How does this tiny yacc programe work? What I know so far: %{...%} is
Does this pattern: setTimeout(function(){ // do stuff }, 0); Actually return control to the
Does this look like it should work? I'm wanting to generate directions from one
I know $() is the jQuery function... but what does $({}) reference/do? I'm looking
Does anyone here happen to know how to accomplish this effect from Espresso.app? http://f.cl.ly/items/1j1V1q322f2K202v2T2s/ScreenFlow.mov
I have a question very similar to this one but the answer does not
This would seem to be super simple but I cannot get it to work
I realize this isn't strictly speaking a programming question, but I'd appreciate a tiny
Does this smell? I have a few properties you can only set once. They
Does this code cause a memory leak: int main(){ int * a = new

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.