1 %{ 2 #include <stdlib.h> 3 4 typedef enum {cGLOBAL, cLOCAL} class; 5 typedef enum {tREAL, tINTEGER} type; 6 typedef char * name; 7 8 struct symbol { class c; type t; name id; }; 9 typedef struct symbol symbol; 10 11 struct namelist { symbol *s; struct namelist *next; }; 12 typedef struct namelist namelist; 13 14 extern symbol *mksymbol(type t, class c, name id); 15 16 #ifdef YYBISON 17 #define YYLEX_DECL() yylex(void) 18 #define YYERROR_DECL() yyerror(const char *s) 19 #endif 20 %} 21 22 %token <cval> GLOBAL LOCAL 23 %token <tval> REAL INTEGER 24 %token <id> NAME 25 26 %type <nlist> declaration namelist(<cval>, <tval>) locnamelist(<tval>) 27 %type <cval> class 28 %type <tval> type 29 30 %destructor { 31 namelist *p = $$; 32 while (p != NULL) 33 { namelist *pp = p; 34 p = p->next; 35 free(pp->s); free(pp); 36 } 37 } <nlist> 38 39 %union 40 { 41 class cval; 42 type tval; 43 namelist * nlist; 44 name id; 45 } 46 47 %start declaration 48 49 %% 50 declaration: class type namelist($1, $2) 51 { $$ = $3; } 52 | type locnamelist($1) 53 { $$ = $2; } 54 ; 55 56 class : GLOBAL { $$ = cGLOBAL; } 57 | LOCAL { $$ = cLOCAL; } 58 ; 59 60 type : REAL { $$ = tREAL; } 61 | INTEGER { $$ = tINTEGER; } 62 ; 63 64 namelist($<tval>c, $t, extra): namelist NAME 65 { $$->s = mksymbol($<tval>t, $<cval>c, $2); 66 $$->next = $1; 67 } 68 | NAME 69 { $$->s = mksymbol($t, $c, $1); 70 $$->next = NULL; 71 } 72 ; 73 74 locnamelist($t): namelist(cLOCAL, $t) 75 { $$ = $1; } 76 ; 77 %% 78 79 extern int YYLEX_DECL(); 80 extern void YYERROR_DECL(); 81