Package beaver

Class Symbol

  • All Implemented Interfaces:
    Cloneable
    Direct Known Subclasses:
    ASTNode

    public class Symbol
    extends Object
    implements Cloneable
    Represents a symbol of a grammar.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected int end
      Line and column where this symbol ends.
      protected short id
      Numeric symbol ID.
      protected int start
      Line and column where this symbol begins.
      Object value
      Value assigned to this symbol.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected Symbol()
      Special case constructor that allows creation of explicitly Symbol-ized nonterminals.
        Symbol​(short id)  
        Symbol​(short id, int start, int end)  
        Symbol​(short id, int start_line, int start_column, int length)  
        Symbol​(short id, int start_line, int start_column, int length, Object value)  
        Symbol​(short id, int left, int right, Object value)  
        Symbol​(short id, Object value)  
        Symbol​(Object value)
      Creates Symbol for non-symbolic results of action routines
    • Field Detail

      • value

        public Object value
        Value assigned to this symbol.
      • id

        protected short id
        Numeric symbol ID.
      • start

        protected int start
        Line and column where this symbol begins.
      • end

        protected int end
        Line and column where this symbol ends.
    • Constructor Detail

      • Symbol

        public Symbol​(short id)
      • Symbol

        public Symbol​(short id,
                      Object value)
      • Symbol

        public Symbol​(short id,
                      int start,
                      int end)
      • Symbol

        public Symbol​(short id,
                      int left,
                      int right,
                      Object value)
      • Symbol

        public Symbol​(short id,
                      int start_line,
                      int start_column,
                      int length)
      • Symbol

        public Symbol​(short id,
                      int start_line,
                      int start_column,
                      int length,
                      Object value)
      • Symbol

        public Symbol​(Object value)
        Creates Symbol for non-symbolic results of action routines
        Parameters:
        value - attached Symbol's value
      • Symbol

        protected Symbol()
        Special case constructor that allows creation of explicitly Symbol-ized nonterminals.

        Used by classes descending from Symbol and which instances are returned by reduce actions. In this case ID and symbol position will be assigned by the parser when reduce action code returns this symbol.

    • Method Detail

      • makePosition

        public static int makePosition​(int line,
                                       int column)
        Packs symbol "coordinates" into a single number.
      • getLine

        public static int getLine​(int position)
        Extracts line number from a packed position.
      • getColumn

        public static int getColumn​(int position)
        Extracts column number from a packed position.
      • getId

        public short getId()
        Returns an ID of this symbol.

        This ID typically is, depending on a symbol type, either a terminal ID if a Symbol is a token created and returned by a Scanner, or a nonterminal ID if a symbol was created by parser based on that nonterminal definition. In the former case the ID is one of IDs generated by Beaver for terminal symbols. The latter keeps IDs of nonterminal symbols.

        Returns:
        symbol's ID
      • getStart

        public int getStart()
        Returns a position in a source where this symbol starts.
        Returns:
        packed line and column numbers
      • getEnd

        public int getEnd()
        Returns a position in a source where this symbol ends.
        Returns:
        packed line and column numbers