1 /* $NetBSD: lalr.c,v 1.5 2011/09/10 21:29:04 christos Exp $ */
2 /* Id: lalr.c,v 1.9 2009/10/27 09:49:27 tom Exp */
7 __RCSID("$NetBSD: lalr.c,v 1.5 2011/09/10 21:29:04 christos Exp $");
16 static Value_t
map_goto(int state
, int symbol
);
17 static Value_t
**transpose(Value_t
** R
, int n
);
18 static void add_lookback_edge(int stateno
, int ruleno
, int gotono
);
19 static void build_relations(void);
20 static void compute_FOLLOWS(void);
21 static void compute_lookaheads(void);
22 static void digraph(Value_t
** relation
);
23 static void initialize_F(void);
24 static void initialize_LA(void);
25 static void set_accessing_symbol(void);
26 static void set_goto_map(void);
27 static void set_maxrhs(void);
28 static void set_reduction_table(void);
29 static void set_shift_table(void);
30 static void set_state_table(void);
31 static void traverse(int i
);
33 static int tokensetsize
;
37 Value_t
*accessing_symbol
;
40 reductions
**reduction_table
;
45 static Value_t infinity
;
49 static Value_t
**includes
;
50 static shorts
**lookback
;
52 static Value_t
*INDEX
;
53 static Value_t
*VERTICES
;
59 tokensetsize
= WORDSIZE(ntokens
);
62 set_accessing_symbol();
64 set_reduction_table();
79 state_table
= NEW2(nstates
, core
*);
80 for (sp
= first_state
; sp
; sp
= sp
->next
)
81 state_table
[sp
->number
] = sp
;
85 set_accessing_symbol(void)
89 accessing_symbol
= NEW2(nstates
, Value_t
);
90 for (sp
= first_state
; sp
; sp
= sp
->next
)
91 accessing_symbol
[sp
->number
] = sp
->accessing_symbol
;
99 shift_table
= NEW2(nstates
, shifts
*);
100 for (sp
= first_shift
; sp
; sp
= sp
->next
)
101 shift_table
[sp
->number
] = sp
;
105 set_reduction_table(void)
109 reduction_table
= NEW2(nstates
, reductions
*);
110 for (rp
= first_reduction
; rp
; rp
= rp
->next
)
111 reduction_table
[rp
->number
] = rp
;
124 item_end
= ritem
+ nitems
;
125 for (itemp
= ritem
; itemp
< item_end
; itemp
++)
148 lookaheads
= NEW2(nstates
+ 1, Value_t
);
151 for (i
= 0; i
< nstates
; i
++)
153 lookaheads
[i
] = (Value_t
) k
;
154 rp
= reduction_table
[i
];
158 lookaheads
[nstates
] = (Value_t
) k
;
160 LA
= NEW2(k
* tokensetsize
, unsigned);
161 LAruleno
= NEW2(k
, Value_t
);
162 lookback
= NEW2(k
, shorts
*);
165 for (i
= 0; i
< nstates
; i
++)
167 rp
= reduction_table
[i
];
170 for (j
= 0; j
< rp
->nreds
; j
++)
172 LAruleno
[k
] = rp
->rules
[j
];
190 goto_map
= NEW2(nvars
+ 1, Value_t
) - ntokens
;
191 temp_map
= NEW2(nvars
+ 1, Value_t
) - ntokens
;
194 for (sp
= first_shift
; sp
; sp
= sp
->next
)
196 for (i
= sp
->nshifts
- 1; i
>= 0; i
--)
198 symbol
= accessing_symbol
[sp
->shift
[i
]];
203 if (ngotos
== MAXSHORT
)
204 fatal("too many gotos");
212 for (i
= ntokens
; i
< nsyms
; i
++)
214 temp_map
[i
] = (Value_t
) k
;
218 for (i
= ntokens
; i
< nsyms
; i
++)
219 goto_map
[i
] = temp_map
[i
];
221 goto_map
[nsyms
] = (Value_t
) ngotos
;
222 temp_map
[nsyms
] = (Value_t
) ngotos
;
224 from_state
= NEW2(ngotos
, Value_t
);
225 to_state
= NEW2(ngotos
, Value_t
);
227 for (sp
= first_shift
; sp
; sp
= sp
->next
)
230 for (i
= sp
->nshifts
- 1; i
>= 0; i
--)
232 state2
= sp
->shift
[i
];
233 symbol
= accessing_symbol
[state2
];
238 k
= temp_map
[symbol
]++;
239 from_state
[k
] = state1
;
240 to_state
[k
] = state2
;
244 FREE(temp_map
+ ntokens
);
247 /* Map_goto maps a state/symbol pair into its numeric representation. */
250 map_goto(int state
, int symbol
)
257 low
= goto_map
[symbol
];
258 high
= goto_map
[symbol
+ 1];
263 middle
= (low
+ high
) >> 1;
264 s
= from_state
[middle
];
266 return (Value_t
) (middle
);
290 nwords
= ngotos
* tokensetsize
;
291 F
= NEW2(nwords
, unsigned);
293 reads
= NEW2(ngotos
, Value_t
*);
294 edge
= NEW2(ngotos
+ 1, Value_t
);
298 for (i
= 0; i
< ngotos
; i
++)
300 stateno
= to_state
[i
];
301 sp
= shift_table
[stateno
];
307 for (j
= 0; j
< k
; j
++)
309 symbol
= accessing_symbol
[sp
->shift
[j
]];
312 SETBIT(rowp
, symbol
);
317 symbol
= accessing_symbol
[sp
->shift
[j
]];
318 if (nullable
[symbol
])
319 edge
[nedges
++] = map_goto(stateno
, symbol
);
324 reads
[i
] = rp
= NEW2(nedges
+ 1, Value_t
);
326 for (j
= 0; j
< nedges
; j
++)
334 rowp
+= tokensetsize
;
340 for (i
= 0; i
< ngotos
; i
++)
351 build_relations(void)
369 Value_t
**new_includes
;
371 includes
= NEW2(ngotos
, Value_t
*);
372 edge
= NEW2(ngotos
+ 1, Value_t
);
373 states
= NEW2(maxrhs
+ 1, Value_t
);
375 for (i
= 0; i
< ngotos
; i
++)
378 state1
= from_state
[i
];
379 symbol1
= accessing_symbol
[to_state
[i
]];
381 for (rulep
= derives
[symbol1
]; *rulep
>= 0; rulep
++)
387 for (rp
= ritem
+ rrhs
[*rulep
]; *rp
>= 0; rp
++)
390 sp
= shift_table
[stateno
];
393 for (j
= 0; j
< k
; j
++)
395 stateno
= sp
->shift
[j
];
396 if (accessing_symbol
[stateno
] == symbol2
)
400 states
[length
++] = stateno
;
403 add_lookback_edge(stateno
, *rulep
, i
);
413 stateno
= states
[--length
];
414 edge
[nedges
++] = map_goto(stateno
, *rp
);
415 if (nullable
[*rp
] && length
> 0)
423 includes
[i
] = shortp
= NEW2(nedges
+ 1, Value_t
);
424 for (j
= 0; j
< nedges
; j
++)
430 new_includes
= transpose(includes
, ngotos
);
432 for (i
= 0; i
< ngotos
; i
++)
438 includes
= new_includes
;
445 add_lookback_edge(int stateno
, int ruleno
, int gotono
)
451 i
= lookaheads
[stateno
];
452 k
= lookaheads
[stateno
+ 1];
454 while (!found
&& i
< k
)
456 if (LAruleno
[i
] == ruleno
)
464 sp
->next
= lookback
[i
];
465 sp
->value
= (Value_t
) gotono
;
470 transpose(Value_t
** R2
, int n
)
479 nedges
= NEW2(n
, Value_t
);
481 for (i
= 0; i
< n
; i
++)
491 new_R
= NEW2(n
, Value_t
*);
492 temp_R
= NEW2(n
, Value_t
*);
494 for (i
= 0; i
< n
; i
++)
499 sp
= NEW2(k
+ 1, Value_t
);
508 for (i
= 0; i
< n
; i
++)
514 *temp_R
[*sp
++]++ = (Value_t
) i
;
524 compute_FOLLOWS(void)
530 compute_lookaheads(void)
533 unsigned *fp1
, *fp2
, *fp3
;
538 n
= lookaheads
[nstates
];
539 for (i
= 0; i
< n
; i
++)
541 fp3
= rowp
+ tokensetsize
;
542 for (sp
= lookback
[i
]; sp
; sp
= sp
->next
)
545 fp2
= F
+ tokensetsize
* sp
->value
;
552 for (i
= 0; i
< n
; i
++)
553 for (sp
= lookback
[i
]; sp
; sp
= next
)
564 digraph(Value_t
** relation
)
568 infinity
= (Value_t
) (ngotos
+ 2);
569 INDEX
= NEW2(ngotos
+ 1, Value_t
);
570 VERTICES
= NEW2(ngotos
+ 1, Value_t
);
575 for (i
= 0; i
< ngotos
; i
++)
578 for (i
= 0; i
< ngotos
; i
++)
580 if (INDEX
[i
] == 0 && R
[i
])
600 VERTICES
[++top
] = (Value_t
) i
;
601 INDEX
[i
] = height
= top
;
603 base
= F
+ i
* tokensetsize
;
604 fp3
= base
+ tokensetsize
;
609 while ((j
= *rp
++) >= 0)
614 if (INDEX
[i
] > INDEX
[j
])
618 fp2
= F
+ j
* tokensetsize
;
625 if (INDEX
[i
] == height
)
636 fp2
= F
+ j
* tokensetsize
;
652 for (i
= 0; i
< ngotos
; i
++)