Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing $x to grab string from rule
    primarykey
    data
    text
    <p>I'm trying to do something like this in Bison...</p> <pre><code>loop_for: FOR var_name COLONEQUALS expression TO {printf("%s&lt;=", $2);} expression STEP {printf("%s+=", $2);} expression {printf(")\n");} Code ENDFOR </code></pre> <p>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?</p> <p>I even tried adding a union and using char* for a new type, but I still get (null) from the above actions.</p> <p><b>EDIT:</b><br> I tried to fix it using the hints from the questions that were provided, but I still can't get it perfect.</p> <p>FLEX rules in question: </p> <pre><code>"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;} </code></pre> <p>BISON rules:</p> <pre><code>loop_for: FOR var_name {strcpy(myvar, $&lt;strV&gt;2);} COLONEQUALS expression TO {printf("%s&lt;=", 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 </code></pre> <p>Thing is, for input:</p> <pre><code>FOR i := 0 TO 10 STEP 1 </code></pre> <p>I get as output:</p> <pre><code>for ( i = 0 ; i :=&lt;= 10 ; i :=+= 1 </code></pre> <p>Can't I avoid having the := symbols getting in the strV?</p> <p><b>EDIT 2:</b><br> I tried again, this time with</p> <pre><code>var_name: name_first_is_char {memset(myvar, '\0', BUFFER_LENGTH); sprintf(myvar, "%s", $&lt;strV&gt;1);} </code></pre> <p>But still the next few characters get in myvar.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload