Makefileの例 タブ文字 n YACC=bison YFLAGS= -d LEX=flex LFLAGS= OBJS = bar. tab. o lex. yy. o all: a. out bar. tab. c: bar. y $(YACC) $(YFLAGS) bar. y lex. yy. c: foo. l bar. tab. h $(LEX) $(LFLAGS) foo. l a. out: $(OBJS) $(CC) –o $@ $(OBJS) OBJS のリストの並び順によっては以下も必要 bar. tab. h: bar. y $(YACC) $(YFLAGS) bar. y
共用体 struct st 1 { … }; struct st 2 { … }; union un { struct st 1 s 1; struct st 2 s 2; }; n union un *u = (union un *) malloc(sizeof(union un)); u->st 1. ? ? ? = …; n sizeof(union un):s 1 または s 2 を格納するのに十分 n なサイズ u は st 2 として再利用可
yaccファイルでの%union指定 n %union { int i; char *str; tree n; } トークンIdentifierの値は • 型treeの宣言を持つヘッダファイルを用意してyaccファイルの先 文字列(char *)型 頭でインクルード n n n %token <str> Identifier %token <i> Constant %token IF ELSE WHILE… yylval. i = atoi(yytext); yylval. str = strdup(yytext); %type <n> program … 非終端記号programの値は tree型
opt の扱い 2 a: bopt c d ↓ a: c d |bcd ; a: bopt c d x: a |y ↓ a_aux: c d ; x: a_aux | b a_aux |y ;