1 char rcsid_fe
[] = "$Id$";
15 static void doBinding
ARGS((Binding
));
16 static void doDecl
ARGS((Arity
));
17 static NonTerminal lookup
ARGS((Pattern
));
18 static NonTerminal normalize
ARGS((PatternAST
, NonTerminal
, Pattern
*));
19 static void doEnterNonTerm
ARGS((RuleAST
));
20 static void doRule
ARGS((RuleAST
));
21 static void doTable
ARGS((Operator
));
24 doBinding(b
) Binding b
;
29 s
= enter(b
->name
, &new);
31 fprintf(stderr
, "Non-unique name: %s\n", b
->name
);
35 s
->u
.op
= newOperator(b
->name
, b
->opnum
, arity
);
37 leaves
= newList(s
->u
.op
, leaves
);
48 foreachList((ListFn
) doBinding
, a
->bindings
);
52 static List xpatterns
;
64 for (l
= xpatterns
; l
; l
= l
->next
) {
65 Pattern x
= (Pattern
) l
->x
;
67 && x
->children
[0] == p
->children
[0]
68 && x
->children
[1] == p
->children
[1]) {
72 sprintf(buf
, "n%%%d", tcount
++);
73 s
= (char *) zalloc(strlen(buf
)+1);
75 n
= newNonTerminal(s
);
77 xpatterns
= newList(p
, xpatterns
);
79 (void) newRule(dummy
, 0, n
, p
);
84 normalize(ast
, nt
, patt
) PatternAST ast
; NonTerminal nt
; Pattern
*patt
;
90 s
= enter(ast
->op
, &new);
93 fprintf(stderr
, "Illegal use of %s --- undefined symbol\n", s
->name
);
95 return 0; /* shut up compilers */
96 } else if (s
->tag
== NONTERMINAL
) {
98 fprintf(stderr
, "Illegal use of %s, a non-terminal, as a terminal\n", s
->name
);
101 *patt
= newPattern(0);
102 (*patt
)->children
[0] = s
->u
.nt
;
106 *patt
= newPattern(s
->u
.op
);
107 if (s
->u
.op
->arity
== -1) {
108 if (!ast
->children
) {
110 leaves
= newList(s
->u
.op
, leaves
);
111 } else if (!ast
->children
->next
) {
113 } else if (!ast
->children
->next
->next
) {
116 fprintf(stderr
, "ERROR: Too many children (max = 2) for \"%s\"\n", s
->name
);
119 if (s
->u
.op
->arity
> max_arity
) {
120 max_arity
= s
->u
.op
->arity
;
123 switch (s
->u
.op
->arity
) {
129 fprintf(stderr
, "ERROR: Incorrect number of children for leaf operator, \"%s\"\n", s
->name
);
134 if (!ast
->children
|| ast
->children
->next
) {
135 fprintf(stderr
, "ERROR: Incorrect number of children for unary operator, \"%s\"\n", s
->name
);
138 (*patt
)->children
[0] = normalize((PatternAST
) ast
->children
->x
, 0, &dummy
);
141 if (!ast
->children
|| !ast
->children
->next
) {
142 fprintf(stderr
, "ERROR: Incorrect number of children for binary operator, \"%s\"\n", s
->name
);
145 (*patt
)->children
[0] = normalize((PatternAST
) ast
->children
->x
, 0, &dummy
);
146 (*patt
)->children
[1] = normalize((PatternAST
) ast
->children
->next
->x
, 0, &dummy
);
150 (*patt
)->normalizer
= nt
;
153 return lookup(*patt
);
159 doEnterNonTerm(ast
) RuleAST ast
;
168 s
= enter(ast
->lhs
, &new);
170 s
->u
.nt
= newNonTerminal(s
->name
);
171 s
->tag
= NONTERMINAL
;
173 if (s
->tag
!= NONTERMINAL
) {
174 fprintf(stderr
, "Illegal use of %s as a non-terminal\n", s
->name
);
179 for (p
= ast
->cost
, i
= 0; p
; p
= p
->next
, i
++) {
183 if (i
< DELTAWIDTH
) {
189 if (i
== principleCost
) {
190 PRINCIPLECOST(delta
) = x
;
194 ast
->rule
= newRule(delta
, ast
->erulenum
, s
->u
.nt
, 0);
198 doRule(ast
) RuleAST ast
;
202 (void) normalize(ast
->pat
, ast
->rule
->lhs
, &pat
);
203 ast
->rule
->pat
= pat
;
207 doTable(op
) Operator op
;
209 op
->table
= newTable(op
);
213 doSpec(decls
, rules
) List decls
; List rules
;
215 foreachList((ListFn
) doDecl
, decls
);
216 debug(debugTables
, foreachList((ListFn
) dumpOperator_l
, operators
));
219 reveachList((ListFn
) doEnterNonTerm
, rules
);
221 last_user_nonterminal
= max_nonterminal
;
223 reveachList((ListFn
) doRule
, rules
);
224 debug(debugTables
, foreachList((ListFn
) dumpRule
, rules
));
226 foreachList((ListFn
) doTable
, operators
);
230 doStart(name
) char *name
;
236 yyerror1("Redeclaration of start symbol to be ");
237 fprintf(stderr
, "\"%s\"\n", name
);
240 s
= enter(name
, &new);
242 s
->u
.nt
= newNonTerminal(s
->name
);
243 s
->tag
= NONTERMINAL
;
245 if (s
->tag
!= NONTERMINAL
) {
246 fprintf(stderr
, "Illegal use of %s as a non-terminal\n", s
->name
);
258 for (l
= grammarNts
; l
; l
= l
->next
) {
259 char *n
= (char*) l
->x
;
264 fprintf(stderr
, "ERROR: %%gram, unused non-terminal: \"%s\"\n", n
);
267 if (s
->tag
!= NONTERMINAL
) {
268 fprintf(stderr
, "ERROR: %%gram, Not a non-terminal: \"%s\"\n", n
);
276 doGram(nts
) List nts
;
279 yyerror1("Redeclaration of %%gram\n");
286 newArity(ar
, b
) int ar
; List b
;
288 Arity a
= (Arity
) zalloc(sizeof(struct arity
));
295 newBinding(name
, opnum
) char *name
; int opnum
;
297 Binding b
= (Binding
) zalloc(sizeof(struct binding
));
299 yyerror1("ERROR: Non-positive external symbol number, ");
300 fprintf(stderr
, "%d", opnum
);
309 newPatternAST(op
, children
) char *op
; List children
;
311 PatternAST p
= (PatternAST
) zalloc(sizeof(struct patternAST
));
313 p
->children
= children
;
320 newRuleAST(lhs
, pat
, erulenum
, cost
) char *lhs
; PatternAST pat
; int erulenum
; IntList cost
;
322 RuleAST p
= (RuleAST
) zalloc(sizeof(struct ruleAST
));
326 yyerror1("External Rulenumber ");
327 fprintf(stderr
, "(%d) <= 0\n", erulenum
);
330 p
->erulenum
= erulenum
;
337 dumpBinding(b
) Binding b
;
339 printf("%s=%d ", b
->name
, b
->opnum
);
343 dumpArity(a
) Arity a
;
347 printf("Arity(%d) ", a
->arity
);
348 for (l
= a
->bindings
; l
; l
= l
->next
) {
349 Binding b
= (Binding
) l
->x
;
356 dumpPatternAST(p
) PatternAST p
;
363 for (l
= p
->children
; l
; l
= l
->next
) {
364 PatternAST past
= (PatternAST
) l
->x
;
365 dumpPatternAST(past
);
375 dumpRuleAST(p
) RuleAST p
;
377 printf("%s : ", p
->lhs
);
378 dumpPatternAST(p
->pat
);
379 printf(" = %d (%ld)\n", p
->erulenum
, (long) p
->cost
);
383 dumpDecls(decls
) List decls
;
387 for (l
= decls
; l
; l
= l
->next
) {
388 Arity a
= (Arity
) l
->x
;
394 dumpRules(rules
) List rules
;
398 for (l
= rules
; l
; l
= l
->next
) {
399 RuleAST p
= (RuleAST
) l
->x
;