summaryrefslogtreecommitdiff
path: root/utils/Burg/gram.yc
blob: 9d2f9f4e53f1413771d6ac24d06a1d41c26060b8 (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
81
82
83
84
85
86
87
88
89
90
91
%{
char rcsid_gram[] = "$Id$";

#include <stdio.h>
#include "b.h"
#include "fe.h"
int doGram(List);
%}

%union {
	int y_int;
	char *y_string;
	Arity y_arity;
	Binding y_binding;
	PatternAST y_patternAST;
	RuleAST y_ruleAST;
	List y_list;
	IntList y_intlist;
}

%start full

%term ERROR
%term K_TERM
%term K_GRAM
%term K_START
%term K_PPERCENT
%term INT
%term ID

%token <y_string> ID
%token <y_int> INT

%type <y_arity> decl
%type <y_binding> binding
%type <y_intlist> cost costtail
%type <y_ruleAST> rule
%type <y_patternAST> pattern
%type <y_list> decls rules bindinglist grammarlist
%%


full	: spec
	| spec K_PPERCENT
		{ yyfinished(); }
	;

spec	: decls K_PPERCENT rules
		{ doSpec($1, $3); }
	;

decls	: /* lambda */	{ $$ = 0; }
	| decls decl	{ $$ = newList($2, $1); }
	;

decl	: K_TERM bindinglist	{ $$ = newArity(-1, $2); }
	| K_GRAM grammarlist	{ $$ = 0; doGram($2); }
	| K_START ID		{ $$ = 0; doStart($2); }	/* kludge */
	;

grammarlist	: /* lambda */		{ $$ = 0; }
		| grammarlist ID	{ $$ = newList($2, $1); }
		;

bindinglist	: /* lambda */		{ $$ = 0; }
		| bindinglist binding	{ $$ = newList($2, $1); }
		;

binding	: ID '=' INT	{ $$ = newBinding($1, $3); }
	;

rules	: /* lambda */	{ $$ = 0; }
	| rules rule	{ $$ = newList($2, $1); }
	;

rule	: ID ':' pattern '=' INT cost ';'	{ $$ = newRuleAST($1, $3, $5, $6); }
		;

pattern	: ID 					{ $$ = newPatternAST($1, 0); }
	| ID '(' pattern ')'			{ $$ = newPatternAST($1, newList($3,0)); }
	| ID '(' pattern ',' pattern ')'	{ $$ = newPatternAST($1, newList($3, newList($5, 0))); }
	;

cost	: /* lambda */		{ $$ = 0; }
	| '(' INT costtail ')'	{ $$ = newIntList($2, $3); }
	;

costtail	: /* lambda */		{ $$ = 0; }
		| ',' INT costtail	{ $$ = newIntList($2, $3); }
		| INT costtail		{ $$ = newIntList($1, $2); }
		;