This script generates test code for the COS 341 Project. Simply run the script, follow the prompts to build your tree, and the code will be generated. When asked for a rule number, input the corresponding number for each production rule listed below.
PROG ::= main GLOBVARS ALGO FUNCTIONS
GLOBVARS ::= // nullable
GLOBVARS ::= VTYP VNAME , GLOBVARS // there can be as many global variables as we like
VTYP ::= num
VTYP ::= text
VNAME ::= a token of Token-Class V from the Lexer
(See the Appendix below)
ALGO ::= begin INSTRUC end
INSTRUC ::= // nullable
INSTRUC ::= COMMAND ; INSTRUC
COMMAND ::= skip // an empty algorithmic step where nothing happens
COMMAND ::= halt
COMMAND ::= print ATOMIC
COMMAND ::= ASSIGN
COMMAND ::= CALL // call to a void-function that only updates global variables
COMMAND ::= BRANCH
COMMAND ::= return ATOMIC
ATOMIC ::= VNAME
ATOMIC ::= CONST
CONST ::= a token of Token-Class N from the Lexer
CONST ::= a token of Token-Class T from the Lexer
ASSIGN ::= VNAME < input // user input during run-time
ASSIGN ::= VNAME = TERM // deep nesting of assignment terms is allowed
CALL ::= FNAME( ATOMIC , ATOMIC , ATOMIC ) // un-nested params only
BRANCH ::= if COND then ALGO else ALGO
TERM ::= ATOMIC
TERM ::= CALL
TERM ::= OP
OP ::= UNOP( ARG )
OP ::= BINOP( ARG , ARG )
ARG ::= ATOMIC
ARG ::= OP // allows deep-nesting of operations
COND ::= SIMPLE
COND ::= COMPOSIT
SIMPLE ::= BINOP( ATOMIC , ATOMIC )
COMPOSIT ::= BINOP( SIMPLE , SIMPLE )
COMPOSIT ::= UNOP( SIMPLE )
UNOP ::= not
UNOP ::= sqrt // square root of real numbers
BINOP ::= or
BINOP ::= and
BINOP ::= eq
BINOP ::= grt // greater than >
BINOP ::= add
BINOP ::= sub
BINOP ::= mul
BINOP ::= div
FNAME ::= a token of Token-Class F from the Lexer
FUNCTIONS ::= // nullable
FUNCTIONS ::= DECL FUNCTIONS
DECL ::= HEADER BODY
HEADER ::= FTYP FNAME( VNAME , VNAME , VNAME ) // functions have 3 parameters
FTYP ::= num
FTYP ::= void
BODY ::= PROLOG LOCVARS ALGO EPILOG SUBFUNCS end
PROLOG ::= { // the prolog
EPILOG ::= } // the epilog
LOCVARS ::= VTYP VNAME , VTYP VNAME , VTYP VNAME
SUBFUNCS ::= FUNCTIONS // functions can have sub-functions
- Token-Class V: Represents variable names, defined by the regular expression:
r'^V_[a-z]([a-z]|[0-9])*$'
- Token-Class F: Represents function names, defined by the regular expression:
r'^F_[a-z]([a-z]|[0-9])*$'
- Token-Class N: Represents numbers, defined by the regular expression:
r"^-?(0|[1-9][0-9]*)(\.[0-9]*[1-9])?$"
- Token-Class T: Represents strings, defined by the regular expression:
r"^\"[A-Z][a-z]{0,7}\"$"