1. package com.sun.java_cup.internal;
  2. /** This abstract class serves as the base class for grammar symbols (i.e.,
  3. * both terminals and non-terminals). Each symbol has a name string, and
  4. * a string giving the type of object that the symbol will be represented by
  5. * on the runtime parse stack. In addition, each symbol maintains a use count
  6. * in order to detect symbols that are declared but never used, and an index
  7. * number that indicates where it appears in parse tables (index numbers are
  8. * unique within terminals or non terminals, but not across both).
  9. *
  10. * @see com.sun.java_cup.internal.terminal
  11. * @see com.sun.java_cup.internal.non_terminal
  12. * @version last updated: 7/3/96
  13. * @author Frank Flannery
  14. */
  15. public abstract class symbol {
  16. /*-----------------------------------------------------------*/
  17. /*--- Constructor(s) ----------------------------------------*/
  18. /*-----------------------------------------------------------*/
  19. /** Full constructor.
  20. * @param nm the name of the symbol.
  21. * @param tp a string with the type name.
  22. */
  23. public symbol(String nm, String tp)
  24. {
  25. /* sanity check */
  26. if (nm == null) nm = "";
  27. /* apply default if no type given */
  28. if (tp == null) tp = "Object";
  29. _name = nm;
  30. _stack_type = tp;
  31. }
  32. /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
  33. /** Constructor with default type.
  34. * @param nm the name of the symbol.
  35. */
  36. public symbol(String nm)
  37. {
  38. this(nm, null);
  39. }
  40. /*-----------------------------------------------------------*/
  41. /*--- (Access to) Instance Variables ------------------------*/
  42. /*-----------------------------------------------------------*/
  43. /** String for the human readable name of the symbol. */
  44. protected String _name;
  45. /** String for the human readable name of the symbol. */
  46. public String name() {return _name;}
  47. /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
  48. /** String for the type of object used for the symbol on the parse stack. */
  49. protected String _stack_type;
  50. /** String for the type of object used for the symbol on the parse stack. */
  51. public String stack_type() {return _stack_type;}
  52. /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
  53. /** Count of how many times the symbol appears in productions. */
  54. protected int _use_count = 0;
  55. /** Count of how many times the symbol appears in productions. */
  56. public int use_count() {return _use_count;}
  57. /** Increment the use count. */
  58. public void note_use() {_use_count++;}
  59. /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
  60. /** Index of this symbol (terminal or non terminal) in the parse tables.
  61. * Note: indexes are unique among terminals and unique among non terminals,
  62. * however, a terminal may have the same index as a non-terminal, etc.
  63. */
  64. protected int _index;
  65. /** Index of this symbol (terminal or non terminal) in the parse tables.
  66. * Note: indexes are unique among terminals and unique among non terminals,
  67. * however, a terminal may have the same index as a non-terminal, etc.
  68. */
  69. public int index() {return _index;}
  70. /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
  71. /** Indicate if this is a non-terminal. Here in the base class we
  72. * don't know, so this is abstract.
  73. */
  74. public abstract boolean is_non_term();
  75. /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
  76. /** Convert to a string. */
  77. public String toString()
  78. {
  79. return name();
  80. }
  81. /*-----------------------------------------------------------*/
  82. }