C言語の自作コンパイラを作成したいと思っています。以下のlex, yacc, cのコードを用いてwhile文を表現したいのですが、うまくいきません。test.c の T_WHILEの部分がおかしいのかなと思うのですが、どのように変更したらよいか教えていただきたいです。
test.c
1#include <stdio.h> 2struct stab { 3 int val; 4 char name[20]; } stab[100]; 5int stabuse = 0; 6struct node { 7 int type, left, right; } ntab[400]; 8int ntabuse = 1; 9#define T_STLIST 1 10#define T_ASSIGN 2 11#define T_READ 3 12#define T_PRINT 4 13#define T_ADD 5 14#define T_SUB 6 15#define T_MUL 7 16#define T_DIV 8 17#define T_REM 9 18#define T_NUM 10 19#define T_VAR 11 20#define T_WHILE 12 21#define T_IF 13 22#define T_LT 14 23#define T_GT 15 24int lookup(char*); 25int node(int, int, int); 26void dotree(int); 27extern char *yytext; 28#include "y.tab.c" 29#include "lex.yy.c" 30 31int main() { 32 yyparse(); 33 return 0; 34} 35int lookup(char *s) { 36 int i; 37 for(i = 0; i < stabuse; ++i) 38 if(strcmp(stab[i].name, s) == 0) return i; 39 if(stabuse >= 99) { 40 printf("table overflow.\n"); exit(1); } 41 strcpy(stab[stabuse].name, s); return stabuse++; 42} 43int node(int t, int l, int r) { 44 int i = ntabuse++; 45 ntab[i].type = t; 46 ntab[i].left = l; 47 ntab[i].right = r; 48 return i; 49} 50void dotree(int i) { 51 int stk; 52 printf(" .section .rodata\n"); 53 printf(".Lprompt: .string\"> \"\n"); /* プロンプト */ 54 printf(".Lread: .string\"%%ld\"\n"); /* 読み取り用書式 */ 55 printf(".Lprint: .string\"%%ld\\n\"\n"); /* 書き出し用書式 */ 56 printf(" .text\n"); 57 printf(".global main\n"); 58 printf("main:\n"); 59 printf(" pushq %%rbp\n"); 60 printf(" movq %%rsp,%%rbp\n"); 61 stk = (8*stabuse + 15) / 16; /* 変数の個数*8 で 16 の倍数へ切り上げ */ 62 stk *= 16; 63 printf(" subq $%d,%%rsp\n", stk); 64 emittree(i); 65 printf(" leave\n"); 66 printf(" ret\n"); 67} 68 69void emittree(int i) 70{ 71 static int labelno = 1; 72 int l; 73 switch(ntab[i].type) { 74 case T_STLIST: if(ntab[i].left) emittree(ntab[i].left); 75 emittree(ntab[i].right); 76 break; 77 case T_READ: printf(" movq $.Lprompt,%%rdi\n"); 78 printf(" movq $0,%%rax\n"); /* 浮動小数点レジスタを使わない */ 79 printf(" call printf\n"); 80 printf(" leaq %d(%%rbp),%%rsi\n", -(ntab[i].left+1)*8); 81 printf(" movq $.Lread,%%rdi\n"); 82 printf(" movq $0,%%rax\n"); /* 浮動小数点レジスタを使わない */ 83 printf(" call scanf\n"); 84 break; 85 case T_PRINT: emittree(ntab[i].left); 86 printf(" popq %%rsi\n"); 87 printf(" movq $.Lprint,%%rdi\n"); 88 printf(" movq $0,%%rax\n"); /* 浮動小数点レジスタを使わない */ 89 printf(" call printf\n"); 90 break; 91 case T_NUM: printf(" pushq $%d\n", ntab[i].left); 92 break; 93 case T_VAR: printf(" pushq %d(%%rbp)\n", -(ntab[i].left+1)*8); 94 break; 95 default: printf("NotImplemented: %d\n", ntab[i].type); 96 break; 97 case T_ASSIGN: emittree(ntab[i].right); 98 printf(" popq %d(%%rbp)\n", -(ntab[i].left+1)*8); 99 break; 100 case T_ADD: emittree(ntab[i].left); 101 emittree(ntab[i].right); 102 printf(" popq %%rdx\n"); 103 printf(" popq %%rax\n"); 104 printf(" addq %%rdx,%%rax\n"); 105 printf(" pushq %%rax\n"); 106 break; 107 case T_SUB: emittree(ntab[i].left); 108 emittree(ntab[i].right); 109 printf(" popq %%rdx\n"); 110 printf(" popq %%rax\n"); 111 printf(" subq %%rdx, %%rax\n"); 112 printf(" pushq %%rax\n"); 113 break; 114 case T_MUL: emittree(ntab[i].left); 115 emittree(ntab[i].right); 116 printf(" popq %%rdx\n"); 117 printf(" popq %%rax\n"); 118 printf(" imulq %%rdx\n"); 119 printf(" pushq %%rax\n"); 120 break; 121 case T_DIV: emittree(ntab[i].left); 122 emittree(ntab[i].right); 123 printf(" popq %%rsp\n"); 124 printf(" popq %%rax\n"); 125 printf(" movq $0,%%rdx\n"); 126 printf(" idivq %%rsp\n"); 127 printf(" pushq %%rax\n"); 128 break; 129 case T_REM: emittree(ntab[i].left); 130 emittree(ntab[i].right); 131 printf(" popq %%rsp\n"); 132 printf(" popq %%rax\n"); 133 printf(" movq $0,%%rdx\n"); 134 printf(" idivq %%rsp\n"); 135 printf(" pushq %%rdx\n"); 136 case T_LT: emittree(ntab[i].left); 137 emittree(ntab[i].right); 138 printf(" popq %%rcx\n"); 139 printf(" popq %%rax\n"); 140 printf(" cmp %%rcx,%%rax\n"); 141 printf(" jge "); 142 break; 143 case T_GT: emittree(ntab[i].left); 144 emittree(ntab[i].right); 145 printf(" popq %%rcx\n"); 146 printf(" popq %%rax\n"); 147 printf(" cmp %%rcx,%%rax\n"); 148 printf(" jle "); 149 break; 150 case T_IF: l = labelno++; 151 emittree(ntab[i].left); 152 printf(".L%d\n", l); 153 emittree(ntab[i].right); 154 printf(".L%d:\n", l); 155 break; 156 case T_WHILE: // この部分 157 l = labelno++; // ラベル番号更新 158 printf(".L%d:\n", l); // ラベル l 生成 159 emittree(ntab[i].left); // 分岐条件判定 160 printf(".L%d\n", l+1); // 不成立なら l+1に移動 161 emittree(ntab[i].right); // 成立していたら、中身を実行 162 printf(".L%d\n", l); // 前のラベル l に戻る 163 printf(".L%d:\n", l+1); // ラベル l+1 生成 164 break; 165 } 166}
test.yacc
1%token NUM; 2%token IDENT; 3%token READ; 4%token PRINT; 5%token WHILE; 6%token IF; 7%left '+' '-'; 8%left '*' '/'; 9%% 10prog : IDENT '{' stlist'}' { dotree($3); return 0; } 11; 12stlist : { $$ = 0; } 13| stlist stat { $$ = node(T_STLIST, $1, $2); } 14; 15stat : var '=' expr ';' { $$ = node(T_ASSIGN, $1, $3); } 16| READ var ';' { $$ = node(T_READ, $2, 0); } 17| PRINT expr ';' { $$ = node(T_PRINT, $2, 0); } 18| WHILE '(' cond ')' stat { $$ = node(T_WHILE, $3, $5); } 19| IF '(' cond ')' stat { $$ = node(T_IF, $3, $5); } 20| '{' stlist '}' { $$ = $2; } 21; 22 23cond : expr '<' expr { $$ = node(T_LT, $1, $3); } 24| expr '>' expr { $$ = node(T_GT, $1, $3); } 25; 26expr : term { $$ = $1; } 27| expr '+' term { $$ = node(T_ADD, $1, $3); } 28| expr '-' term { $$ = node(T_SUB, $1, $3); } 29; 30term: prim{ $$ = $1; } 31| term '*' prim { $$ = node(T_MUL, $1, $3); } 32| term '/' prim { $$ = node(T_DIV, $1, $3); } 33| term '%' prim { $$ = node(T_REM, $1, $3); } 34; 35 36prim : NUM { $$ = node(T_NUM, atoi(yytext), 0); } 37| var { $$ = node(T_VAR, $1, 0); } 38| '(' expr ')' { $$ = $2; } 39; 40var : IDENT { $$ = lookup(yytext); } 41;
test.lex
1alpha [a-zA-Z] 2digit [0-9] 3white [\n\t ] 4%% 5while { return WHILE; } 6if { return IF; } 7read { return READ; } 8print { return PRINT; } 9{alpha}({alpha}|{digit})* { return IDENT; } 10{digit}+ { return NUM; } 11[-+()=;{}<>*/%] { return yytext[0]; } 12{white} { ; }

回答4件
あなたの回答
tips
プレビュー