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