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