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