⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tables.c

📁 bison 2.0 主要可以用来做语法分析用的
💻 C
📖 第 1 页 / 共 2 页
字号:
/* Output the generated parsing program for Bison.   Copyright (C) 1984, 1986, 1989, 1992, 2000, 2001, 2002, 2003, 2004   Free Software Foundation, Inc.   This file is part of Bison, the GNU Compiler Compiler.   Bison is free software; you can redistribute it and/or modify it   under the terms of the GNU General Public License as published by   the Free Software Foundation; either version 2, or (at your option)   any later version.   Bison is distributed in the hope that it will be useful, but   WITHOUT ANY WARRANTY; without even the implied warranty of   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU   General Public License for more details.   You should have received a copy of the GNU General Public License   along with Bison; see the file COPYING.  If not, write to the Free   Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA   02111-1307, USA.  */#include "system.h"#include <bitsetv.h>#include <quotearg.h>#include "complain.h"#include "conflicts.h"#include "files.h"#include "getargs.h"#include "gram.h"#include "lalr.h"#include "reader.h"#include "symtab.h"#include "tables.h"/* Several tables are indexed both by state and nonterminal numbers.   We call such an index a `vector'; i.e., a vector is either a state   or a nonterminal number.   Of course vector_number_t ought to be wide enough to contain   state_number and symbol_number.  */typedef int vector_number;static inline vector_numberstate_number_to_vector_number (state_number s){  return s;}static inline vector_numbersymbol_number_to_vector_number (symbol_number sym){  return state_number_as_int (nstates) + sym - ntokens;}int nvectors;/* FROMS and TOS are indexed by vector_number.   If VECTOR is a nonterminal, (FROMS[VECTOR], TOS[VECTOR]) form an   array of state numbers of the non defaulted GOTO on VECTOR.   If VECTOR is a state, TOS[VECTOR] is the array of actions to do on   the (array of) symbols FROMS[VECTOR].   In both cases, TALLY[VECTOR] is the size of the arrays   FROMS[VECTOR], TOS[VECTOR]; and WIDTH[VECTOR] =   (FROMS[VECTOR][SIZE] - FROMS[VECTOR][0] + 1) where SIZE =   TALLY[VECTOR].   FROMS therefore contains symbol_number and action_number,   TOS state_number and action_number,   TALLY sizes,   WIDTH differences of FROMS.   Let base_number be the type of FROMS, TOS, and WIDTH.  */#define BASE_MAXIMUM INT_MAX#define BASE_MINIMUM INT_MINstatic base_number **froms;static base_number **tos;static unsigned int **conflict_tos;static int *tally;static base_number *width;/* For a given state, N = ACTROW[SYMBOL]:   If N = 0, stands for `run the default action'.   If N = MIN, stands for `raise a syntax error'.   If N > 0, stands for `shift SYMBOL and go to n'.   If N < 0, stands for `reduce -N'.  */typedef int action_number;#define ACTION_NUMBER_MINIMUM INT_MINstatic action_number *actrow;/* FROMS and TOS are reordered to be compressed.  ORDER[VECTOR] is the   new vector number of VECTOR.  We skip `empty' vectors (i.e.,   TALLY[VECTOR] = 0), and call these `entries'.  */static vector_number *order;static int nentries;base_number *base = NULL;/* A distinguished value of BASE, negative infinite.  During the   computation equals to BASE_MINIMUM, later mapped to BASE_NINF to   keep parser tables small.  */base_number base_ninf = 0;static base_number *pos = NULL;static unsigned int *conflrow;unsigned int *conflict_table;unsigned int *conflict_list;int conflict_list_cnt;static int conflict_list_free;/* TABLE_SIZE is the allocated size of both TABLE and CHECK.  We start   with more or less the original hard-coded value (which was   SHRT_MAX).  */static int table_size = 32768;base_number *table;base_number *check;/* The value used in TABLE to denote explicit syntax errors   (%nonassoc), a negative infinite.  First defaults to ACTION_NUMBER_MININUM,   but in order to keep small tables, renumbered as TABLE_ERROR, which   is the smallest (non error) value minus 1.  */base_number table_ninf = 0;static int lowzero;int high;state_number *yydefgoto;rule_number *yydefact;/*----------------------------------------------------------------.| If TABLE (and CHECK) appear to be small to be addressed at      || DESIRED, grow them.  Note that TABLE[DESIRED] is to be used, so || the desired size is at least DESIRED + 1.                       |`----------------------------------------------------------------*/static voidtable_grow (int desired){  int old_size = table_size;  while (table_size <= desired)    table_size *= 2;  if (trace_flag & trace_resource)    fprintf (stderr, "growing table and check from: %d to %d\n",	     old_size, table_size);  table = xnrealloc (table, table_size, sizeof *table);  conflict_table = xnrealloc (conflict_table, table_size,			      sizeof *conflict_table);  check = xnrealloc (check, table_size, sizeof *check);  for (/* Nothing. */; old_size < table_size; ++old_size)    {      table[old_size] = 0;      conflict_table[old_size] = 0;      check[old_size] = -1;    }}/*-------------------------------------------------------------------.| For GLR parsers, for each conflicted token in S, as indicated      || by non-zero entries in CONFLROW, create a list of possible 	     || reductions that are alternatives to the shift or reduction	     || currently recorded for that token in S.  Store the alternative     || reductions followed by a 0 in CONFLICT_LIST, updating		     || CONFLICT_LIST_CNT, and storing an index to the start of the list   || back into CONFLROW.						     |`-------------------------------------------------------------------*/static voidconflict_row (state *s){  int i, j;  reductions *reds = s->reductions;  if (!nondeterministic_parser)    return;  for (j = 0; j < ntokens; j += 1)    if (conflrow[j])      {	conflrow[j] = conflict_list_cnt;	/* Find all reductions for token J, and record all that do not	   match ACTROW[J].  */	for (i = 0; i < reds->num; i += 1)	  if (bitset_test (reds->look_ahead_tokens[i], j)	      && (actrow[j]		  != rule_number_as_item_number (reds->rules[i]->number)))	    {	      if (conflict_list_free <= 0)		abort ();	      conflict_list[conflict_list_cnt] = reds->rules[i]->number + 1;	      conflict_list_cnt += 1;	      conflict_list_free -= 1;	    }	/* Leave a 0 at the end.  */	if (conflict_list_free <= 0)	  abort ();	conflict_list[conflict_list_cnt] = 0;	conflict_list_cnt += 1;	conflict_list_free -= 1;      }}/*------------------------------------------------------------------.| Decide what to do for each type of token if seen as the           || look-ahead in specified state.  The value returned is used as the || default action (yydefact) for the state.  In addition, ACTROW is  || filled with what to do for each kind of token, index by symbol    || number, with zero meaning do the default action.  The value       || ACTION_NUMBER_MINIMUM, a very negative number, means this	    || situation is an error.  The parser recognizes this value	    || specially.							    ||                                                                   || This is where conflicts are resolved.  The loop over look-ahead   || rules considered lower-numbered rules last, and the last rule     || considered that likes a token gets to handle it.                  ||                                                                   || For GLR parsers, also sets CONFLROW[SYM] to an index into         || CONFLICT_LIST iff there is an unresolved conflict (s/r or r/r)    || with symbol SYM. The default reduction is not used for a symbol   || that has any such conflicts.                                      |`------------------------------------------------------------------*/static rule *action_row (state *s){  int i;  rule *default_rule = NULL;  reductions *reds = s->reductions;  transitions *trans = s->transitions;  errs *errp = s->errs;  /* Set to nonzero to inhibit having any default reduction.  */  bool nodefault = false;  bool conflicted = false;  for (i = 0; i < ntokens; i++)    actrow[i] = conflrow[i] = 0;  if (reds->look_ahead_tokens)    {      int j;      bitset_iterator biter;      /* loop over all the rules available here which require	 look-ahead (in reverse order to give precedence to the first	 rule) */      for (i = reds->num - 1; i >= 0; --i)	/* and find each token which the rule finds acceptable	   to come next */	BITSET_FOR_EACH (biter, reds->look_ahead_tokens[i], j, 0)	{	  /* and record this rule as the rule to use if that	     token follows.  */	  if (actrow[j] != 0)	    {	      conflicted = true;	      conflrow[j] = 1;	    }	  actrow[j] = rule_number_as_item_number (reds->rules[i]->number);	}    }  /* Now see which tokens are allowed for shifts in this state.  For     them, record the shift as the thing to do.  So shift is preferred     to reduce.  */  FOR_EACH_SHIFT (trans, i)    {      symbol_number sym = TRANSITION_SYMBOL (trans, i);      state *shift_state = trans->states[i];      if (actrow[sym] != 0)	{	  conflicted = true;	  conflrow[sym] = 1;	}      actrow[sym] = state_number_as_int (shift_state->number);      /* Do not use any default reduction if there is a shift for	 error */      if (sym == errtoken->number)	nodefault = true;    }  /* See which tokens are an explicit error in this state (due to     %nonassoc).  For them, record ACTION_NUMBER_MINIMUM as the     action.  */  for (i = 0; i < errp->num; i++)    {      symbol *sym = errp->symbols[i];      actrow[sym->number] = ACTION_NUMBER_MINIMUM;    }  /* Now find the most common reduction and make it the default action     for this state.  */  if (reds->num >= 1 && !nodefault)    {      if (s->consistent)	default_rule = reds->rules[0];      else	{	  int max = 0;	  for (i = 0; i < reds->num; i++)	    {	      int count = 0;	      rule *r = reds->rules[i];	      symbol_number j;	      for (j = 0; j < ntokens; j++)		if (actrow[j] == rule_number_as_item_number (r->number))		  count++;	      if (count > max)		{		  max = count;		  default_rule = r;		}	    }	  /* GLR parsers need space for conflict lists, so we can't	     default conflicted entries.  For non-conflicted entries	     or as long as we are not building a GLR parser,	     actions that match the default are replaced with zero,	     which means "use the default". */	  if (max > 0)	    {	      int j;	      for (j = 0; j < ntokens; j++)		if (actrow[j] == rule_number_as_item_number (default_rule->number)		    && ! (nondeterministic_parser && conflrow[j]))		  actrow[j] = 0;	    }	}    }  /* If have no default rule, the default is an error.     So replace any action which says "error" with "use default".  */  if (!default_rule)    for (i = 0; i < ntokens; i++)      if (actrow[i] == ACTION_NUMBER_MINIMUM)	actrow[i] = 0;  if (conflicted)    conflict_row (s);  return default_rule;}/*----------------------------------------.| Set FROMS, TOS, TALLY and WIDTH for S.  |`----------------------------------------*/static voidsave_row (state_number s){  symbol_number i;  int count;  base_number *sp;  base_number *sp1;  base_number *sp2;  unsigned int *sp3;  /* Number of non default actions in S.  */  count = 0;  for (i = 0; i < ntokens; i++)    if (actrow[i] != 0)      count++;  if (count == 0)    return;  /* Allocate non defaulted actions.  */  froms[s] = sp = sp1 = xnmalloc (count, sizeof *sp1);  tos[s] = sp2 = xnmalloc (count, sizeof *sp2);  conflict_tos[s] = sp3 =    nondeterministic_parser ? xnmalloc (count, sizeof *sp3) : NULL;  /* Store non defaulted actions.  */  for (i = 0; i < ntokens; i++)    if (actrow[i] != 0)      {	*sp1++ = i;	*sp2++ = actrow[i];	if (nondeterministic_parser)	  *sp3++ = conflrow[i];      }  tally[s] = count;  width[s] = sp1[-1] - sp[0] + 1;}/*------------------------------------------------------------------.| Figure out the actions for the specified state, indexed by        || look-ahead token type.                                            ||                                                                   || The YYDEFACT table is output now.  The detailed info is saved for || putting into YYTABLE later.                                       |`------------------------------------------------------------------*/static voidtoken_actions (void){  state_number i;  symbol_number j;  rule_number r;  int nconflict = nondeterministic_parser ? conflicts_total_count () : 0;  yydefact = xnmalloc (nstates, sizeof *yydefact);  actrow = xnmalloc (ntokens, sizeof *actrow);  conflrow = xnmalloc (ntokens, sizeof *conflrow);  conflict_list = xnmalloc (1 + 2 * nconflict, sizeof *conflict_list);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -