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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:52:50+00:00 2026-05-16T07:52:50+00:00

I have tried something like this in my Bison file… ReturnS: RETURN expression {printf(;)}

  • 0

I have tried something like this in my Bison file…

ReturnS: RETURN expression {printf(";")}

…but the semicolon gets printed AFTER the next token, past this rule, instead of right after the expression. This rule was made as we’re required to convert the input file to a c-like form and the original language doesn’t require a semicolon after the expression in the return statement, but C does, so I thought I’d add it manually to the output with printf. That doesn’t seem to work, as the semicolon gets added but for some reason, it gets added after the next token is parsed (outside the ReturnS rule) instead of right when the expression rule returns to ReturnS.

This rule also causes the same result:

loop_for: FOR var_name COLONEQUALS expression TO {printf("%s<=", $<chartype>2);} expression STEP {printf("%s+=", $<chartype>2);} expression {printf(")\n");} Code ENDFOR

Besides the first two printf’s not working right (I’ll post another question regarding that), the last printf is actually called AFTER the first token/literal of the “Code” rule has been parsed, resulting in something like this:

for (i=0; i<=5; i+=1
a)
=a+1;

instead of

for (i=0; i<=5; i+=1)
a=a+1;

Any ideas what I’m doing wrong?

  • 1 1 Answer
  • 1 View
  • 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-16T07:52:50+00:00Added an answer on May 16, 2026 at 7:52 am

    Probably because the grammar has to look-ahead one token to decide to reduce by the rule you show.

    The action is executed when the rule is reduced, and it is very typical that the grammar has to read one more token before it knows that it can/should reduce the previous rule.

    For example, if an expression can consist of an indefinite sequence of added terms, it has to read beyond the last term to know there isn’t another ‘+’ to continue the expression.


    After seeing the Yacc/Bison grammar and Lex/Flex analyzer, some of the problems became obvious, and others took a little more sorting out.

    • Having the lexical analyzer do much of the printing meant that the grammar was not properly in control of what appeared when. The analyzer was doing too much.
    • The analyzer was also not doing enough work – making the grammar process strings and numbers one character at a time is possible, but unnecessarily hard work.
    • Handling comments is tricky if they need to be preserved. In a regular C compiler, the lexical analyzer throws the comments away; in this case, the comments had to be preserved. The rule handling this was moved from the grammar (where it was causing shift/reduce and reduce/reduce conflicts because of empty strings matching comments) to the lexical analyzer. This may not always be optimal, but it seemed to work OK in this context.
    • The lexical analyzer needed to ensure that it returned a suitable value for yylval when a value was needed.
    • The grammar needed to propagate suitable values in $$ to ensure that rules had the necessary information. Keywords for the most part did not need a value; things like variable names and numbers do.
    • The grammar had to do the printing in the appropriate places.

    The prototype solution returned had a major memory leak because it used strdup() liberally and didn’t use free() at all. Making sure that the leaks are fixed – possibly by using a char array rather than a char pointer for YYSTYPE – is left to the OP.

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

Sidebar

Related Questions

I have tried something like this: select PREPRO = case when (isnumeric(PREPRO) = 1
i have never tried something like this so i need your help. i have
I have tried searching over the internet about this problem but not able to
Last night I tried to put together something that I have had working since
Have tried to find solutions for this and can't really come up with anything.
I have tried this: #define format(f, ...) \ int size = strlen(f) + (sizeof((int[]){__VA_ARGS__})/sizeof(int))
I have tried many permutations but they all don't seems to work well. Am
I have one menu list like this.. </div> <ul id=menu> <li id=About>@Html.ActionLink(@MvcWebRole1.Resources.Shared.Layout.About, About, Home)</li>
I have this xml-file: <objects> <object> <value>test</value> </object> <object> <value>foo</value> </object> <object> <value>bar</value> </object>
I have this ini file. day=Mon time=01:00:00 occurence=weekly taskname=monitorschedule taskrun=C:\monitor.bat I want to have

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.