Class Symbol


public class Symbol extends Object
Symbol objects represent a terminal or non-terminal symbol in the grammar.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final int
    The position of the symbol in the parsed file.
    private final int
    The position of the symbol in the parsed file.
    private static final Object
    No value attached to symbol, for terminals without value.
    private final LexicalUnit
    The LexicalUnit (terminal) attached to this token.
    private static final int
    Undefined line/column position of symbol.
    private final Object
    The value attached to the token.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a Symbol using the provided attributes, without position or value.
    Symbol(LexicalUnit unit, int line)
    Creates a Symbol using the provided attributes, without column nor value.
    Symbol(LexicalUnit unit, int line, int column)
    Creates a Symbol using the provided attributes and no value.
    Symbol(LexicalUnit unit, int line, int column, Object value)
    Creates a Symbol using the provided attributes.
    Symbol(LexicalUnit unit, Object value)
    Creates a Symbol using the provided attributes, without position.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Compares this symbol to another object for equality.
    private static String
    Escape LaTeX special characters in a string.
    int
    Returns the column where the symbol appeared.
    int
    Returns the line where the symbol appeared.
    Returns the type of the symbol.
    Returns the value of the symbol.
    int
    Returns a hash code value for the object.
    boolean
    Returns whether the symbol represents a non-terminal.
    boolean
    Returns whether the symbol represents a terminal.
    Returns a string representation of the symbol.
    Returns a LaTeX-formatted string representation of the symbol.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • UNDEFINED_POSITION

      private static final int UNDEFINED_POSITION
      Undefined line/column position of symbol.
      See Also:
    • NO_VALUE

      private static final Object NO_VALUE
      No value attached to symbol, for terminals without value.
    • type

      private final LexicalUnit type
      The LexicalUnit (terminal) attached to this token.
    • value

      private final Object value
      The value attached to the token. May be any Object. In fact, for terminals with value it is indeed the value attached to the terminal.
    • line

      private final int line
      The position of the symbol in the parsed file.
    • column

      private final int column
      The position of the symbol in the parsed file.
  • Constructor Details

    • Symbol

      public Symbol(LexicalUnit unit, int line, int column, Object value)
      Creates a Symbol using the provided attributes.
      Parameters:
      unit - the LexicalUnit (terminal) associated with the symbol.
      line - the line where the symbol appears in the file.
      column - the column where the symbol appears in the file.
      value - the value of the symbol.
    • Symbol

      public Symbol(LexicalUnit unit, int line, int column)
      Creates a Symbol using the provided attributes and no value.
      Parameters:
      unit - the LexicalUnit (terminal) associated with the symbol.
      line - the line where the symbol appears in the file.
      column - the column where the symbol appears in the file.
    • Symbol

      public Symbol(LexicalUnit unit, int line)
      Creates a Symbol using the provided attributes, without column nor value.
      Parameters:
      unit - the LexicalUnit (terminal) associated with the symbol.
      line - the line where the symbol appears in the file.
    • Symbol

      public Symbol(LexicalUnit unit)
      Creates a Symbol using the provided attributes, without position or value.
      Parameters:
      unit - the LexicalUnit (terminal) associated with the symbol.
    • Symbol

      public Symbol(LexicalUnit unit, Object value)
      Creates a Symbol using the provided attributes, without position.
      Parameters:
      unit - the LexicalUnit (terminal) associated with the symbol.
      value - the value of the symbol.
  • Method Details

    • isTerminal

      public boolean isTerminal()
      Returns whether the symbol represents a terminal. A terminal symbol must have a non-null LexicalUnit type.
      Returns:
      a boolean which is true iff the Symbol represents a terminal.
    • isNonTerminal

      public boolean isNonTerminal()
      Returns whether the symbol represents a non-terminal. A non-terminal symbol has no type.
      Returns:
      a boolean which is true iff the Symbol represents a non-terminal.
    • getType

      public LexicalUnit getType()
      Returns the type of the symbol. The type of a non-terminal is null.
      Returns:
      the value of attribute type.
    • getValue

      public Object getValue()
      Returns the value of the symbol.
      Returns:
      the value of attribute value.
    • getLine

      public int getLine()
      Returns the line where the symbol appeared.
      Returns:
      the value of attribute line.
    • getColumn

      public int getColumn()
      Returns the column where the symbol appeared.
      Returns:
      the value of attribute column.
    • hashCode

      public int hashCode()
      Returns a hash code value for the object.
      Overrides:
      hashCode in class Object
      Returns:
      a hash code based on the type and value of the Symbol.
    • equals

      public boolean equals(Object obj)
      Compares this symbol to another object for equality. Two symbols are equal if they have the same type and value.
      Overrides:
      equals in class Object
      Parameters:
      obj - the object to compare with this symbol.
      Returns:
      true if the object is a Symbol with the same type and value, false otherwise.
    • toString

      public String toString()
      Returns a string representation of the symbol. This method has been modified from the provided class to provide the value of the non-terminal symbols.
      Overrides:
      toString in class Object
      Returns:
      a string representation of the token's value and type.
    • escapeLaTeX

      private static String escapeLaTeX(String s)
      Escape LaTeX special characters in a string. If the input string is null, returns an empty string.
      Parameters:
      s - the input string.
      Returns:
      the escaped string.
    • toTexString

      public String toTexString()
      Returns a LaTeX-formatted string representation of the symbol.
      Returns:
      a LaTeX-formatted string representation of the symbol.