%{ #include typedef enum {cGLOBAL, cLOCAL} class; typedef enum {tREAL, tINTEGER} type; typedef char * name; struct symbol { class c; type t; name id; }; typedef struct symbol symbol; struct namelist { symbol *s; struct namelist *next; }; typedef struct namelist namelist; extern symbol *mksymbol(type t, class c, name id); #ifdef YYBISON #define YYLEX_DECL() yylex(void) #define YYERROR_DECL() yyerror(const char *s) extern int YYLEX_DECL(); extern void YYERROR_DECL(); #endif %} %token GLOBAL LOCAL %token REAL INTEGER %token NAME %type declaration namelist locnamelist %type class %type type %union { class cval; type tval; namelist * nlist; name id; } %start declaration %% declaration: class type namelist { $$ = $3; } | type locnamelist { $$ = $2; } ; class : GLOBAL { $$ = cGLOBAL; } | LOCAL { $$ = cLOCAL; } ; type : REAL { $$ = tREAL; } | INTEGER { $$ = tINTEGER; } ; namelist: namelist NAME { $$->s = mksymbol($0, $-1, $2); $$->next = $1; } | NAME { $$->s = mksymbol($0, $-1, $1); $$->next = NULL; } ; locnamelist: { $$ = cLOCAL; } /* set up semantic stack for = LOCAL */ { $$ = $-1; } /* copy to where expects it */ namelist { $$ = $3; } ; %% extern int YYLEX_DECL(); extern void YYERROR_DECL();