/* Compute look-ahead criteria for Bison.
Copyright (C) 1984, 1986, 1989, 2000, 2001, 2002, 2003, 2004, 2005,
2006 Free Software Foundation, Inc.
*/
#include <config.h>
#include "system.h"
#include <bitset.h>
#include <bitsetv.h>
#include <quotearg.h>
#include "LR0.h"
#include "complain.h"
#include "derives.h"
#include "getargs.h"
#include "gram.h"
#include "lalr.h"
#include "nullable.h"
#include "reader.h"
#include "relation.h"
#include "symtab.h"
goto_number *goto_map;
static goto_number ngotos;
state_number *from_state;
state_number *to_state;
/* Linked list of goto numbers. */
typedef struct goto_list
{
struct goto_list *next;
goto_number value;
} goto_list;
/* LA is a LR by NTOKENS matrix of bits. LA[l, i] is 1 if the rule
LArule[l] is applicable in the appropriate state when the next
token is symbol i. If LA[l, i] and LA[l, j] are both 1 for i != j,
it is a conflict. */
static bitsetv LA = NULL;
size_t nLA;
/* And for the famous F variable, which name is so descriptive that a
comment is hardly needed. <grin>. */
static bitsetv F = NULL;
static goto_number **includes;
static goto_list **lookback;
/* 计算GOTO关系矩阵,压缩存储到一维数组中
GOTO关系就是状态之间通过非终结符进行转移
代表了非终结符在不同位置的出现
后面说的"非终结符(区分出现位置)"GOTO关系是一一对应的 */
static void
set_goto_map (void)
{
state_number s;
goto_number *temp_map;
goto_map = xcalloc (nvars + 1, sizeof *goto_map);
temp_map = xnmalloc (nvars + 1, sizeof *temp_map);
ngotos = 0;
for (s = 0; s < nstates; ++s)
{
transitions *sp = states[s]->transitions;
int i;
for (i = sp->num - 1; i >= 0 && TRANSITION_IS_GOTO (sp, i); --i)
{
ngotos++;
/* Abort if (ngotos + 1) would overflow. */
assert (ngotos != GOTO_NUMBER_MAXIMUM);
goto_map[TRANSITION_SYMBOL (sp, i) - ntokens]++;
}
}
{
goto_number k = 0;
int i;
for (i = ntokens; i < nsyms; i++)
{
temp_map[i - ntokens] = k;
k += goto_map[i - ntokens];
}
for (i = ntokens; i < nsyms; i++)
goto_map[i - ntokens] = temp_map[i - ntokens];
goto_map[nsyms - ntokens] = ngotos;
temp_map[nsyms - ntokens] = ngotos;
}
from_state = xcalloc (ng