1 /* $NetBSD: lalr.c,v 1.6 2015/01/03 23:22:52 christos Exp $ */
4 /* Id: lalr.c,v 1.11 2014/09/18 00:26:39 tom Exp */
7 __RCSID("$NetBSD: lalr.c,v 1.6 2015/01/03 23:22:52 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
;
46 static Value_t infinity
;
50 static Value_t
**includes
;
51 static shorts
**lookback
;
53 static Value_t
*INDEX
;
54 static Value_t
*VERTICES
;
60 tokensetsize
= WORDSIZE(ntokens
);
63 set_accessing_symbol();
65 set_reduction_table();
80 state_table
= NEW2(nstates
, core
*);
81 for (sp
= first_state
; sp
; sp
= sp
->next
)
82 state_table
[sp
->number
] = sp
;
86 set_accessing_symbol(void)
90 accessing_symbol
= NEW2(nstates
, Value_t
);
91 for (sp
= first_state
; sp
; sp
= sp
->next
)
92 accessing_symbol
[sp
->number
] = sp
->accessing_symbol
;
100 shift_table
= NEW2(nstates
, shifts
*);
101 for (sp
= first_shift
; sp
; sp
= sp
->next
)
102 shift_table
[sp
->number
] = sp
;
106 set_reduction_table(void)
110 reduction_table
= NEW2(nstates
, reductions
*);
111 for (rp
= first_reduction
; rp
; rp
= rp
->next
)
112 reduction_table
[rp
->number
] = rp
;
125 item_end
= ritem
+ nitems
;
126 for (itemp
= ritem
; itemp
< item_end
; itemp
++)
149 lookaheads
= NEW2(nstates
+ 1, Value_t
);
152 for (i
= 0; i
< nstates
; i
++)
154 lookaheads
[i
] = (Value_t
) k
;
155 rp
= reduction_table
[i
];
159 lookaheads
[nstates
] = (Value_t
) k
;
161 LA
= NEW2(k
* tokensetsize
, unsigned);
162 LAruleno
= NEW2(k
, Value_t
);
163 lookback
= NEW2(k
, shorts
*);
166 for (i
= 0; i
< nstates
; i
++)
168 rp
= reduction_table
[i
];
171 for (j
= 0; j
< rp
->nreds
; j
++)
173 LAruleno
[k
] = rp
->rules
[j
];
192 goto_base
= NEW2(nvars
+ 1, Value_t
);
193 temp_base
= NEW2(nvars
+ 1, Value_t
);
195 goto_map
= goto_base
- ntokens
;
196 temp_map
= temp_base
- ntokens
;
199 for (sp
= first_shift
; sp
; sp
= sp
->next
)
201 for (i
= sp
->nshifts
- 1; i
>= 0; i
--)
203 symbol
= accessing_symbol
[sp
->shift
[i
]];
208 if (ngotos
== MAXYYINT
)
209 fatal("too many gotos");
217 for (i
= ntokens
; i
< nsyms
; i
++)
219 temp_map
[i
] = (Value_t
) k
;
223 for (i
= ntokens
; i
< nsyms
; i
++)
224 goto_map
[i
] = temp_map
[i
];
226 goto_map
[nsyms
] = (Value_t
) ngotos
;
227 temp_map
[nsyms
] = (Value_t
) ngotos
;
229 from_state
= NEW2(ngotos
, Value_t
);
230 to_state
= NEW2(ngotos
, Value_t
);
232 for (sp
= first_shift
; sp
; sp
= sp
->next
)
235 for (i
= sp
->nshifts
- 1; i
>= 0; i
--)
237 state2
= sp
->shift
[i
];
238 symbol
= accessing_symbol
[state2
];
243 k
= temp_map
[symbol
]++;
244 from_state
[k
] = state1
;
245 to_state
[k
] = state2
;
252 /* Map_goto maps a state/symbol pair into its numeric representation. */
255 map_goto(int state
, int symbol
)
262 low
= goto_map
[symbol
];
263 high
= goto_map
[symbol
+ 1];
268 middle
= (low
+ high
) >> 1;
269 s
= from_state
[middle
];
271 return (Value_t
) (middle
);
295 nwords
= ngotos
* tokensetsize
;
296 F
= NEW2(nwords
, unsigned);
298 reads
= NEW2(ngotos
, Value_t
*);
299 edge
= NEW2(ngotos
+ 1, Value_t
);
303 for (i
= 0; i
< ngotos
; i
++)
305 stateno
= to_state
[i
];
306 sp
= shift_table
[stateno
];
312 for (j
= 0; j
< k
; j
++)
314 symbol
= accessing_symbol
[sp
->shift
[j
]];
317 SETBIT(rowp
, symbol
);
322 symbol
= accessing_symbol
[sp
->shift
[j
]];
323 if (nullable
[symbol
])
324 edge
[nedges
++] = map_goto(stateno
, symbol
);
329 reads
[i
] = rp
= NEW2(nedges
+ 1, Value_t
);
331 for (j
= 0; j
< nedges
; j
++)
339 rowp
+= tokensetsize
;
345 for (i
= 0; i
< ngotos
; i
++)
356 build_relations(void)
374 Value_t
**new_includes
;
376 includes
= NEW2(ngotos
, Value_t
*);
377 edge
= NEW2(ngotos
+ 1, Value_t
);
378 states
= NEW2(maxrhs
+ 1, Value_t
);
380 for (i
= 0; i
< ngotos
; i
++)
383 state1
= from_state
[i
];
384 symbol1
= accessing_symbol
[to_state
[i
]];
386 for (rulep
= derives
[symbol1
]; *rulep
>= 0; rulep
++)
392 for (rp
= ritem
+ rrhs
[*rulep
]; *rp
>= 0; rp
++)
395 sp
= shift_table
[stateno
];
398 for (j
= 0; j
< k
; j
++)
400 stateno
= sp
->shift
[j
];
401 if (accessing_symbol
[stateno
] == symbol2
)
405 states
[length
++] = stateno
;
408 add_lookback_edge(stateno
, *rulep
, i
);
418 stateno
= states
[--length
];
419 edge
[nedges
++] = map_goto(stateno
, *rp
);
420 if (nullable
[*rp
] && length
> 0)
428 includes
[i
] = shortp
= NEW2(nedges
+ 1, Value_t
);
429 for (j
= 0; j
< nedges
; j
++)
435 new_includes
= transpose(includes
, ngotos
);
437 for (i
= 0; i
< ngotos
; i
++)
443 includes
= new_includes
;
450 add_lookback_edge(int stateno
, int ruleno
, int gotono
)
456 i
= lookaheads
[stateno
];
457 k
= lookaheads
[stateno
+ 1];
459 while (!found
&& i
< k
)
461 if (LAruleno
[i
] == ruleno
)
469 sp
->next
= lookback
[i
];
470 sp
->value
= (Value_t
) gotono
;
475 transpose(Value_t
** R2
, int n
)
484 nedges
= NEW2(n
, Value_t
);
486 for (i
= 0; i
< n
; i
++)
496 new_R
= NEW2(n
, Value_t
*);
497 temp_R
= NEW2(n
, Value_t
*);
499 for (i
= 0; i
< n
; i
++)
504 sp
= NEW2(k
+ 1, Value_t
);
513 for (i
= 0; i
< n
; i
++)
519 *temp_R
[*sp
++]++ = (Value_t
) i
;
529 compute_FOLLOWS(void)
535 compute_lookaheads(void)
538 unsigned *fp1
, *fp2
, *fp3
;
543 n
= lookaheads
[nstates
];
544 for (i
= 0; i
< n
; i
++)
546 fp3
= rowp
+ tokensetsize
;
547 for (sp
= lookback
[i
]; sp
; sp
= sp
->next
)
550 fp2
= F
+ tokensetsize
* sp
->value
;
557 for (i
= 0; i
< n
; i
++)
558 for (sp
= lookback
[i
]; sp
; sp
= next
)
569 digraph(Value_t
** relation
)
573 infinity
= (Value_t
) (ngotos
+ 2);
574 INDEX
= NEW2(ngotos
+ 1, Value_t
);
575 VERTICES
= NEW2(ngotos
+ 1, Value_t
);
580 for (i
= 0; i
< ngotos
; i
++)
583 for (i
= 0; i
< ngotos
; i
++)
585 if (INDEX
[i
] == 0 && R
[i
])
605 VERTICES
[++top
] = (Value_t
) i
;
606 INDEX
[i
] = height
= top
;
608 base
= F
+ i
* tokensetsize
;
609 fp3
= base
+ tokensetsize
;
614 while ((j
= *rp
++) >= 0)
619 if (INDEX
[i
] > INDEX
[j
])
623 fp2
= F
+ j
* tokensetsize
;
630 if (INDEX
[i
] == height
)
641 fp2
= F
+ j
* tokensetsize
;
657 for (i
= 0; i
< ngotos
; i
++)