1. package com.sun.java_cup.internal;
  2. import com.sun.java_cup.internal.assoc;
  3. import java.util.Hashtable;
  4. import java.util.Enumeration;
  5. /** This class represents a terminal symbol in the grammar. Each terminal
  6. * has a textual name, an index, and a string which indicates the type of
  7. * object it will be implemented with at runtime (i.e. the class of object
  8. * that will be returned by the scanner and pushed on the parse stack to
  9. * represent it).
  10. *
  11. * @version last updated: 7/3/96
  12. * @author Frank Flannery
  13. */
  14. public class terminal extends symbol {
  15. /*-----------------------------------------------------------*/
  16. /*--- Constructor(s) ----------------------------------------*/
  17. /*-----------------------------------------------------------*/
  18. /** Full constructor.
  19. * @param nm the name of the terminal.
  20. * @param tp the type of the terminal.
  21. */
  22. public terminal(String nm, String tp, int precedence_side, int precedence_num)
  23. {
  24. /* superclass does most of the work */
  25. super(nm, tp);
  26. /* add to set of all terminals and check for duplicates */
  27. Object conflict = _all.put(nm,this);
  28. if (conflict != null)
  29. // can't throw an execption here because this is used in static
  30. // initializers, so we do a crash instead
  31. // was:
  32. // throw new internal_error("Duplicate terminal (" + nm + ") created");
  33. (new internal_error("Duplicate terminal (" + nm + ") created")).crash();
  34. /* assign a unique index */
  35. _index = next_index++;
  36. /* set the precedence */
  37. _precedence_num = precedence_num;
  38. _precedence_side = precedence_side;
  39. /* add to by_index set */
  40. _all_by_index.put(new Integer(_index), this);
  41. }
  42. /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
  43. /** Constructor for non-precedented terminal
  44. */
  45. public terminal(String nm, String tp)
  46. {
  47. this(nm, tp, assoc.no_prec, -1);
  48. }
  49. /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
  50. /** Constructor with default type.
  51. * @param nm the name of the terminal.
  52. */
  53. public terminal(String nm)
  54. {
  55. this(nm, null);
  56. }
  57. /*-----------------------------------------------------------*/
  58. /*------------------- Class Variables ---------------------*/
  59. /*-----------------------------------------------------------*/
  60. private int _precedence_num;
  61. private int _precedence_side;
  62. /*-----------------------------------------------------------*/
  63. /*--- (Access to) Static (Class) Variables ------------------*/
  64. /*-----------------------------------------------------------*/
  65. /** Table of all terminals. Elements are stored using name strings as
  66. * the key
  67. */
  68. protected static Hashtable _all = new Hashtable();
  69. /** Access to all terminals. */
  70. public static Enumeration all() {return _all.elements();}
  71. /** Lookup a terminal by name string. */
  72. public static terminal find(String with_name)
  73. {
  74. if (with_name == null)
  75. return null;
  76. else
  77. return (terminal)_all.get(with_name);
  78. }
  79. /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
  80. /** Table of all terminals indexed by their index number. */
  81. protected static Hashtable _all_by_index = new Hashtable();
  82. /** Lookup a terminal by index. */
  83. public static terminal find(int indx)
  84. {
  85. Integer the_indx = new Integer(indx);
  86. return (terminal)_all_by_index.get(the_indx);
  87. }
  88. /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
  89. /** Total number of terminals. */
  90. public static int number() {return _all.size();}
  91. /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
  92. /** Static counter to assign unique index. */
  93. protected static int next_index = 0;
  94. /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
  95. /** Special terminal for end of input. */
  96. public static final terminal EOF = new terminal("EOF");
  97. /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
  98. /** special terminal used for error recovery */
  99. public static final terminal error = new terminal("error");
  100. /*-----------------------------------------------------------*/
  101. /*--- General Methods ---------------------------------------*/
  102. /*-----------------------------------------------------------*/
  103. /** Report this symbol as not being a non-terminal. */
  104. public boolean is_non_term()
  105. {
  106. return false;
  107. }
  108. /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
  109. /** Convert to a string. */
  110. public String toString()
  111. {
  112. return super.toString() + "[" + index() + "]";
  113. }
  114. /*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .*/
  115. /** get the precedence of a terminal */
  116. public int precedence_num() {
  117. return _precedence_num;
  118. }
  119. public int precedence_side() {
  120. return _precedence_side;
  121. }
  122. /** set the precedence of a terminal */
  123. public void set_precedence(int p, int new_prec) {
  124. _precedence_side = p;
  125. _precedence_num = new_prec;
  126. }
  127. /*-----------------------------------------------------------*/
  128. }