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 extern int YYLEX_DECL(); 20 extern void YYERROR_DECL(); 21 #endif 22 %} 23 24 %token <cval> GLOBAL LOCAL 25 %token <tval> REAL INTEGER 26 %token <id> NAME 27 28 %type <nlist> declaration namelist locnamelist 29 %type <cval> class 30 %type <tval> type 31 32 %union 33 { 34 class cval; 35 type tval; 36 namelist * nlist; 37 name id; 38 } 39 40 %start declaration 41 42 %% 43 declaration: class type namelist 44 { $$ = $3; } 45 | type locnamelist 46 { $$ = $2; } 47 ; 48 49 class : GLOBAL { $$ = cGLOBAL; } 50 | LOCAL { $$ = cLOCAL; } 51 ; 52 53 type : REAL { $$ = tREAL; } 54 | INTEGER { $$ = tINTEGER; } 55 ; 56 57 namelist: namelist NAME 58 { $$->s = mksymbol($<tval>0, $<cval>-1, $2); 59 $$->next = $1; 60 } 61 | NAME 62 { $$->s = mksymbol($<tval>0, $<cval>-1, $1); 63 $$->next = NULL; 64 } 65 ; 66 67 locnamelist: 68 { $<cval>$ = cLOCAL; } /* set up semantic stack for <class> = LOCAL */ 69 { $<tval>$ = $<tval>-1; } /* copy <type> to where <namelist> expects it */ 70 namelist 71 { $$ = $3; } 72 ; 73 %% 74 75 extern int YYLEX_DECL(); 76 extern void YYERROR_DECL(); 77