(root)/
bison-3.8.2/
src/
symtab.h
       1  /* Definitions for symtab.c and callers, part of Bison.
       2  
       3     Copyright (C) 1984, 1989, 1992, 2000-2002, 2004-2015, 2018-2021 Free
       4     Software Foundation, Inc.
       5  
       6     This file is part of Bison, the GNU Compiler Compiler.
       7  
       8     This program is free software: you can redistribute it and/or modify
       9     it under the terms of the GNU General Public License as published by
      10     the Free Software Foundation, either version 3 of the License, or
      11     (at your option) any later version.
      12  
      13     This program is distributed in the hope that it will be useful,
      14     but WITHOUT ANY WARRANTY; without even the implied warranty of
      15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      16     GNU General Public License for more details.
      17  
      18     You should have received a copy of the GNU General Public License
      19     along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
      20  
      21  /**
      22   * \file symtab.h
      23   * \brief Manipulating ::symbol.
      24   */
      25  
      26  #ifndef SYMTAB_H_
      27  # define SYMTAB_H_
      28  
      29  # include "assoc.h"
      30  # include "location.h"
      31  # include "scan-code.h"
      32  # include "uniqstr.h"
      33  
      34  /*----------.
      35  | Symbols.  |
      36  `----------*/
      37  
      38  /** Symbol classes.  */
      39  typedef enum
      40  {
      41    /** Undefined.  */
      42    unknown_sym,
      43    /** Declared with %type: same as Undefined, but triggered a Wyacc if
      44        applied to a terminal. */
      45    pct_type_sym,
      46    /** Terminal. */
      47    token_sym,
      48    /** Nonterminal. */
      49    nterm_sym
      50  } symbol_class;
      51  
      52  
      53  /** Internal token numbers. */
      54  typedef int symbol_number;
      55  # define SYMBOL_NUMBER_MAXIMUM INT_MAX
      56  
      57  
      58  typedef struct symbol symbol;
      59  typedef struct sym_content sym_content;
      60  
      61  /* Declaration status of a symbol.
      62  
      63     First, it is "undeclared".  Then, if "undeclared" and used in a
      64     %printer/%destructor, it is "used".  If not "declared" but used in
      65     a rule, it is "needed".  Finally, if declared (via a rule for
      66     nonterminals, or %token), it is "declared".
      67  
      68     When status are checked at the end, "declared" symbols are fine,
      69     "used" symbols trigger warnings, otherwise it's an error.  */
      70  
      71  typedef enum
      72    {
      73      /** Used in the input file for an unknown reason (error).  */
      74      undeclared,
      75      /** Used by %destructor/%printer but not defined (warning).  */
      76      used,
      77      /** Used in the grammar (rules) but not defined (error).  */
      78      needed,
      79      /** Defined with %type or %token (good).  */
      80      declared,
      81    } declaration_status;
      82  
      83  enum code_props_type
      84    {
      85      destructor = 0,
      86      printer = 1,
      87    };
      88  typedef enum code_props_type code_props_type;
      89  
      90  enum { CODE_PROPS_SIZE = 2 };
      91  
      92  struct symbol
      93  {
      94    /** The key, name of the symbol.  */
      95    uniqstr tag;
      96  
      97    /** The "defining" location.  */
      98    location location;
      99  
     100    /** Whether this symbol is translatable. */
     101    bool translatable;
     102  
     103    /** Whether \a location is about the first uses as left-hand side
     104        symbol of a rule (true), or simply the first occurrence (e.g.,
     105        in a %type, or as a rhs symbol of a rule).  The former type of
     106        location is more natural in error messages.  This Boolean helps
     107        moving from location of the first occurrence to first use as
     108        lhs. */
     109    bool location_of_lhs;
     110  
     111    /** Points to the other in the symbol-string pair for an alias. */
     112    symbol *alias;
     113  
     114    /** Whether this symbol is the alias of another or not. */
     115    bool is_alias;
     116  
     117    /** All the info about the pointed symbol is there. */
     118    sym_content *content;
     119  };
     120  
     121  struct sym_content
     122  {
     123    /** The main symbol that denotes this content (it contains the
     124        possible alias). */
     125    symbol *symbol;
     126  
     127    /** Its \c \%type.
     128  
     129        Beware that this is the type_name as was entered by the user,
     130        including silly things such as "]" if she entered "%token <]> t".
     131        Therefore, when outputting type_name to M4, be sure to escape it
     132        into "@}".  See quoted_output for instance.  */
     133    uniqstr type_name;
     134  
     135    /** Its \c \%type's location.  */
     136    location type_loc;
     137  
     138    /** Any \c \%destructor (resp. \%printer) declared specifically for this
     139        symbol.
     140  
     141        Access this field only through <tt>symbol</tt>'s interface functions. For
     142        example, if <tt>symbol::destructor = NULL</tt> (resp. <tt>symbol::printer
     143        = NULL</tt>), a default \c \%destructor (resp. \%printer) or a per-type
     144        \c symbol_destructor_printer_get will compute the correct one. */
     145    code_props props[CODE_PROPS_SIZE];
     146  
     147    symbol_number number;
     148    location prec_loc;
     149    int prec;
     150    assoc assoc;
     151  
     152    /** Token code, possibly specified by the user (%token FOO 42).  */
     153    int code;
     154  
     155    symbol_class class;
     156    declaration_status status;
     157  };
     158  
     159  /** Fetch (or create) the symbol associated to KEY.  */
     160  symbol *symbol_from_uniqstr (const uniqstr key, location loc);
     161  
     162  /** Fetch (or create) the symbol associated to KEY.  */
     163  symbol *symbol_get (const char *key, location loc);
     164  
     165  /** Generate a dummy nonterminal.
     166  
     167     Its name cannot conflict with the user's names.  */
     168  symbol *dummy_symbol_get (location loc);
     169  
     170  
     171  /*--------------------.
     172  | Methods on symbol.  |
     173  `--------------------*/
     174  
     175  /** Print a symbol (for debugging). */
     176  void symbol_print (symbol const *s, FILE *f);
     177  
     178  /** Is this a dummy nonterminal?  */
     179  bool symbol_is_dummy (symbol const *sym);
     180  
     181  /** The name of the code_props type: "\%destructor" or "\%printer".  */
     182  char const *code_props_type_string (code_props_type kind);
     183  
     184  /** The name of the symbol that can be used as an identifier.
     185   ** Consider the alias if needed.
     186   ** Return 0 if there is none (e.g., the symbol is only defined as
     187   ** a string). */
     188  uniqstr symbol_id_get (symbol const *sym);
     189  
     190  /**
     191   * Make \c str the literal string alias of \c sym.  Copy token number,
     192   * symbol number, and type from \c sym to \c str.
     193   */
     194  void symbol_make_alias (symbol *sym, symbol *str, location loc);
     195  
     196  /**
     197   * This symbol is used as the lhs of a rule.  Record this location
     198   * as definition point, if not already done.
     199   */
     200  void symbol_location_as_lhs_set (symbol *sym, location loc);
     201  
     202  /** Set the \c type_name associated with \c sym.
     203  
     204      Do nothing if passed 0 as \c type_name.  */
     205  void symbol_type_set (symbol *sym, uniqstr type_name, location loc);
     206  
     207  /** Set the \c \%destructor or \c \%printer associated with \c sym.  */
     208  void symbol_code_props_set (symbol *sym, code_props_type kind,
     209                              code_props const *destructor);
     210  
     211  /** Get the computed \c \%destructor or \c %printer for \c sym, which was
     212      initialized with \c code_props_none_init if there's no \c \%destructor or
     213      \c %printer.  */
     214  code_props *symbol_code_props_get (symbol *sym, code_props_type kind);
     215  
     216  /** Set the \c precedence associated with \c sym.
     217  
     218      Ensure that \a symbol is a terminal.
     219      Do nothing if invoked with \c undef_assoc as \c assoc.  */
     220  void symbol_precedence_set (symbol *sym, int prec, assoc a, location loc);
     221  
     222  /** Set the \c class associated with \c sym.
     223  
     224      Whether \c declaring means whether this class definition comes
     225      from %nterm or %token (but not %type, prec/assoc, etc.).  A symbol
     226      can have "declaring" set only at most once.  */
     227  void symbol_class_set (symbol *sym, symbol_class class, location loc,
     228                         bool declaring);
     229  
     230  /** Set the token \c code of \c sym, specified by the user at \c loc.  */
     231  void symbol_code_set (symbol *sym, int code, location loc);
     232  
     233  
     234  
     235  /*------------------.
     236  | Special symbols.  |
     237  `------------------*/
     238  
     239  /** The error token. */
     240  extern symbol *errtoken;
     241  /** The token for unknown tokens.  */
     242  extern symbol *undeftoken;
     243  /** The end of input token.  */
     244  extern symbol *eoftoken;
     245  /** The genuine start symbol.
     246  
     247     $accept: start-symbol $end */
     248  extern symbol *acceptsymbol;
     249  
     250  /** Whether a symbol declared with a type tag.  */
     251  extern bool tag_seen;
     252  
     253  
     254  /*-------------------.
     255  | Symbol Relations.  |
     256  `-------------------*/
     257  
     258  /* The symbol relations are represented by a directed graph. */
     259  
     260  /* The id of a node */
     261  typedef int graphid;
     262  
     263  typedef struct symgraphlink symgraphlink;
     264  
     265  struct symgraphlink
     266  {
     267    /** The second \c symbol or group of a precedence relation.
     268     * See \c symgraph. */
     269    graphid id;
     270  
     271    symgraphlink *next;
     272  };
     273  
     274  /* Symbol precedence graph, to store the used precedence relations between
     275   * symbols. */
     276  
     277  typedef struct symgraph symgraph;
     278  
     279  struct symgraph
     280  {
     281    /** Identifier for the node: equal to the number of the symbol. */
     282    graphid id;
     283  
     284    /** The list of related symbols that have a smaller precedence. */
     285    symgraphlink *succ;
     286  
     287    /** The list of related symbols that have a greater precedence. */
     288    symgraphlink *pred;
     289  };
     290  
     291  /** Register a new precedence relation as used. */
     292  
     293  void register_precedence (graphid first, graphid snd);
     294  
     295  /** Print a warning for each symbol whose precedence and/or associativity
     296   * is useless. */
     297  
     298  void print_precedence_warnings (void);
     299  
     300  /*----------------------.
     301  | Symbol associativity  |
     302  `----------------------*/
     303  
     304  void register_assoc (graphid i, graphid j);
     305  
     306  /*-----------------.
     307  | Semantic types.  |
     308  `-----------------*/
     309  
     310  /** A semantic type and its associated \c \%destructor and \c \%printer.
     311  
     312     Access the fields of this struct only through the interface functions in
     313     this file.  \sa symbol::destructor  */
     314  typedef struct {
     315    /** The key, name of the semantic type.  */
     316    uniqstr tag;
     317  
     318    /** The location of its first occurrence.  */
     319    location location;
     320  
     321    /** Its status : "undeclared", "used" or "declared".
     322        It cannot be "needed".  */
     323    declaration_status status;
     324  
     325    /** Any \c %destructor and %printer declared for this
     326        semantic type.  */
     327    code_props props[CODE_PROPS_SIZE];
     328  
     329  } semantic_type;
     330  
     331  /** Fetch (or create) the semantic type associated to KEY.  */
     332  semantic_type *semantic_type_from_uniqstr (const uniqstr key,
     333                                             const location *loc);
     334  
     335  /** Fetch (or create) the semantic type associated to KEY.  */
     336  semantic_type *semantic_type_get (const char *key, const location *loc);
     337  
     338  /** Set the \c destructor or \c printer associated with \c type.  */
     339  void semantic_type_code_props_set (semantic_type *type,
     340                                     code_props_type kind,
     341                                     code_props const *code);
     342  
     343  /*----------------------------------.
     344  | Symbol and semantic type tables.  |
     345  `----------------------------------*/
     346  
     347  /** Create the symbol and semantic type tables, and the built-in
     348      symbols.  */
     349  void symbols_new (void);
     350  
     351  /** Free all the memory allocated for symbols and semantic types.  */
     352  void symbols_free (void);
     353  
     354  /** Check that all the symbols are defined.
     355  
     356      Report any undefined symbols and consider them nonterminals.  */
     357  void symbols_check_defined (void);
     358  
     359  /** Sanity checks and #token_translations construction.
     360  
     361     Perform various sanity checks, assign symbol numbers, and set up
     362     #token_translations.  */
     363  void symbols_pack (void);
     364  
     365  #endif /* !SYMTAB_H_ */