package rebelsky.compiler.pascal; import java.util.Vector; /** * A collection of constants that define the nonterminals in Pascal. * (Not yet complete.) * * @author Samuel A. Rebelsky * @version 0.5 of October 2002 */ public class PascalNonterminals { // +---------+----------------------------------------------------------- // | Fields | // +---------+ /** The number of nonterminals. */ private static int NUM_NONTERMINALS = 0; /** The names of the various nonterminals. */ private static String[] names; // +-----------+----------------------------------------------------------- // | Constants | // +-----------+ /** * An addition operator, such as plus or minus. */ public static final int ADDING_OPERATOR = NUM_NONTERMINALS++; /** * An assignment statement. */ public static final int ASSIGNMENT = NUM_NONTERMINALS++; /** * A list of assignment statements. Not used in the standard * Pascal grammar, but useful for some variant grammars that * I like to create. * * ::= TSEMI { TSEMI } */ public static final int ASSIGNMENT_LIST = NUM_NONTERMINALS++; /** * A block of code. In Pascal, lots of declarations followed by * a group of statements enclosed in begin and end. */ public static final int BLOCK = NUM_NONTERMINALS++; /** * A constant. */ public static final int CONSTANT = NUM_NONTERMINALS++; /** * A single constant definition. */ public static final int CONSTANT_DEFINITION = NUM_NONTERMINALS++; /** * Zero or more constant definitions. */ public static final int CONSTANT_DEFINITIONS = NUM_NONTERMINALS++; /** * The famous expression. */ public static final int EXPRESSION = NUM_NONTERMINALS++; /** * Part of an expression. Factors contain no unparenthesized * addition and multiplication symbols. */ public static final int FACTOR = NUM_NONTERMINALS++; /** * An identifier that names a file. Used in the program heading. */ public static final int FILE_IDENTIFIER = NUM_NONTERMINALS++; /** * A non-empty list of identifiers. */ public static final int IDENTIFIER_LIST = NUM_NONTERMINALS++; /** * A label, typically used as the destination of a (blech) goto * statement. */ public static final int LABEL = NUM_NONTERMINALS++; /** * The declaration of zero or more labels that are to be used * within a block. */ public static final int LABEL_DECLARATION = NUM_NONTERMINALS++; /** * An operator with the precedence of multiplication, such as * multiplication, division, or modulus. */ public static final int MULTIPLYING_OPERATOR = NUM_NONTERMINALS++; /** * The heading of a program. Gives name, input, and output. */ public static final int PROGRAM_HEADING = NUM_NONTERMINALS++; /** * Part of an expression. Terms are expression with no unparenthesized * additive operations. */ public static final int TERM = NUM_NONTERMINALS++; /** * The name of a type. */ public static final int TYPE_IDENTIFIER = NUM_NONTERMINALS++; /** * A non-negative number. */ public static final int UNSIGNED_NUMBER = NUM_NONTERMINALS++; /** * A variable (something that can be assigned to). */ public static final int VARIABLE = NUM_NONTERMINALS++; /** * Declare the type of one or more variables. */ public static final int VARIABLE_DECLARATION = NUM_NONTERMINALS++; /** * Zero or more variable declarations. */ public static final int VARIABLE_DECLARATIONS = NUM_NONTERMINALS++; // +---------+----------------------------------------------------------- // | Methods | // +---------+ /** * Get the number of nonterminal symbols. */ public static int numNonterminals() { return NUM_NONTERMINALS; } // numNonterminals() /** * Get the name of a nonterminal. * * Pre: 0 <= ntnum < numNonterminals */ public static String getName(int ntnum) { return (String) names[ntnum]; } // getName(int) public static void init() { names = new String[NUM_NONTERMINALS]; names[ADDING_OPERATOR] = ""; names[ASSIGNMENT] = ""; names[ASSIGNMENT_LIST] = ""; names[VARIABLE] = ""; names[VARIABLE_DECLARATION] = ""; names[VARIABLE_DECLARATIONS] = ""; } // init() } // class PascalNonterminals