views:

46

answers:

3

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.

A: 

No, the $x notation is not restricted solely to integer values (well, the x has to be an integer, but the value represented by $x does not).

See whether this question helps at all:

Jonathan Leffler
I read that question and also the one that was linked in it and they were very helpful. I guess that in FLEX I have to use a rule that parses all characters together and does something like "yylval.strV = yytext;". However, I find that this way I can't have separate FLEX rules for "AND" and "OR" which are required by the exercise. Oh gods.
Lefteris Aslanoglou
Wait, no. After moving those rules above the character parsing rule I get it compile. I'll come back with an update.
Lefteris Aslanoglou
I updated the question.
Lefteris Aslanoglou
+1  A: 
Lefteris Aslanoglou
It looks like you're trying to write an interpreter? You might want to consider some sort of symbol table to prevent memory leaks.
Kizaru
+1  A: 

What you need to consider is that yytext is a pointer to a character. That sequence of characters changes all the time in flex, but yytext (the pointer) is constant. Your original flex rule simply sets that pointer in yylval. So when the parser continues, it engulfs the := from input as well. This is why in flex, you should copy the string and store it somewhere as soon as it matches. That somewhere could be a field in yylval union. I always malloc space for all constants and variable names before using them in Bison.

Kizaru
Thanks for the info, this actually explains the theory behind the solution I've provided above.
Lefteris Aslanoglou