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

  • Home
  • SEARCH
  • 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 1015285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:22:44+00:00 2026-05-16T10:22:44+00:00

I’m trying to do something like this in Bison… loop_for: FOR var_name COLONEQUALS expression

  • 0

I’m trying to do something like this in Bison…

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

What I’m trying to do is convert a for statement from the fake language’s syntax to C’s. However, the $2 I’ve used to grab var_name doesn’t seem to work as the program crashes when it reaches there. Is $x supposed to work only for integers?

I even tried adding a union and using char* for a new type, but I still get (null) from the above actions.

EDIT:
I tried to fix it using the hints from the questions that were provided, but I still can’t get it perfect.

FLEX rules in question:

"FOR"   {printf("for ("); lisnew=0; return FOR;}
"TO"    {printf("; "); return TO;}
"STEP"  {printf("; "); return STEP;}
"ENDFOR"    {printf("\n"); t--; instabs(); printf("}\n"); instabs(); lisnew=1; return ENDFOR;}
[a-zA-Z]+   {printf("%s",yytext); lisnew=0; yylval.strV = yytext; return CHARACTERS;}
":="    {printf("="); lisnew=0; return COLONEQUALS;}

BISON rules:

loop_for:   FOR var_name {strcpy(myvar, $<strV>2);} COLONEQUALS expression TO {printf("%s<=", myvar);} expression STEP {printf("%s+=", myvar);} expression {printf(")\n");} Code ENDFOR
var_name:   name_first_is_char
name_first_is_char: character | character name2
name2:  | character name2 | digit name2

Thing is, for input:

FOR i := 0 TO 10 STEP 1

I get as output:

for ( i = 0 ; i :=<= 10 ; i :=+= 1

Can’t I avoid having the := symbols getting in the strV?

EDIT 2:
I tried again, this time with

var_name:   name_first_is_char {memset(myvar, '\0', BUFFER_LENGTH); sprintf(myvar, "%s", $<strV>1);}

But still the next few characters get in myvar.

  • 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-16T10:22:44+00:00Added an answer on May 16, 2026 at 10:22 am

    I managed to get around working with the Bison’s $x by identifying variable names with a regular expression in flex and passing them to a string which is then extern’ed from Bison, and available for use anytime.

    So some of the changes made are…

    FLEX

    char tempvar[1000];
    [a-zA-Z][a-zA-Z0-9]*    {printf("%s",yytext); strcpy(tempvar,yytext); lisnew=0; yylval.strV = yytext; return VARNAME;}
    

    BISON

    extern char tempvar[];
    var_name:    VARNAME {strcpy(myvar, tempvar);}
    loop_for:   FOR var_name COLONEQUALS number TO {printf("%s<=", myvar);} number STEP {printf("%s+=", myvar);} number {printf(")\n"); instabs(); printf("{\n"); t++; instabs();} Code ENDFOR
    

    EDIT: With credit due to Jonathan Leffler, this (below) should also work, allowing access via the usual $x in Bison.

    Partial code

    FLEX

    [a-zA-Z][a-zA-Z0-9]*    { yylval = strdup(yytext); return VARNAME;}
    [1-9][0-9]*|0           { yylval = strdup(yytext); return NUMBER; }
    

    BISON

    loop_for:   FOR var_name COLONEQUALS NUMBER TO NUMBER STEP NUMBER
        { printf("for (%s = %s; %s <= %s; %s += %s)\n", $2, $4, $2, $6, $2, $8); }
    

    Jonathan reports that although this works, it should be used carefully as strdup() used in this manner without free() statements should result in major memory leaks.

    See also SO 3539416 for more info.

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

Sidebar

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.