I’m trying to write a yacc source file for a program that will convert a simple while statement from C language (let’s say ANSI 89) to assembly at&t.
The following is my grammar, the central part of yacc file.
%%
while_statement : 'w' 'h' 'i' 'l' 'e' '(' control_statement ')' '{' block '}'
{printing of the assembly code;}
control_statement : expression '>' expression { $$ = strcat(write exp jg back,) ;}
| expression '<' expression { $$ = strcat(write exp jl back,) ;}
| expression '==' expression { $$ = strcat(write exp je back,) ;}
| expression '<=' expression { $$ = strcat(write exp jle back,) ;}
| expression '>=' expression { $$ = strcat(write exp jge back,) ;}
| expression '!=' expression { $$ = strcat(write exp jne back,) ;}
| expression { $$ = $1;}
block : expression ';'
| block expression ';'
expression : expression '+' expression { $$ = $1 + $3;}
| expression '-' expression { $$ = $1 - $3;}
| expression '*' expression { $$ = $1 * $3;}
| expression '/' expression { if($3 == 0)
yyerror("divide by zero");
else
$$ = $1 / $3;}
| '-' expression { $$ = -$2;}
| '(' expression ')' { $$ = $2;}
| string '=' expression { create new variable called string with expression value }
| number { $$ = $1;}
string : letter {$$ = $1;}
| string letter {strcat($$, ??;}
letter : A {strcat($$, 'A');}
.........
number : digit { $$ = $$ + $1;}
| number digit { $$ = ($1 * 10) + $2;}
digit : '0' {$$ = 0;}
| '1' {$$ = 1;}
| '2' {$$ = 2;}
| '3' {$$ = 3;}
| '4' {$$ = 4;}
| '5' {$$ = 5;}
| '6' {$$ = 6;}
| '7' {$$ = 7;}
| '8' {$$ = 8;}
| '9' {$$ = 9;}
%%
My question is: wich kind of function I’m supposed to use to concatenate the values for the final string? Or which kind of data types, struct, union, etc. I should use?
Thanks in advance for the answers.
Well, for a while loop, what you want is something like:
So in your pseudo code, that would look something like:
where
concatenateis whatever you’re using to build strings out of a bunch of other strings.That’s assuming you’re building up your assembly code by concatenating strings together (which is what its seems like you’re trying to do from your pseudo-code.) Also, if you want to allow for more than a single loop, you’ll need to create unique labels to use for each, rather than using fixed strings.