我正在尝试为一个程序编写一个 yacc 源文件,它将一个简单的 while 语句从 C 语言(比如说 ANSI 89)转换为汇编 at&t。以下是我的语法,yacc 文件的核心部分。
%%
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;}
%%
我的问题是:我应该使用哪种函数来连接最终字符串的值?或者我应该使用哪种数据类型、结构、联合等?提前感谢您的答案。