The syntactic grammar for MiniJava is given here. This grammar has
Java tokens defined by the Java Language Specification's
lexical grammar as its terminal symbols. It defines a set of productions
starting from the goal symbol Goal
which describe how
sequences of tokens can form syntactically correct MiniJava programs. This is
an LALR(1) grammar (apart from ambiguities that can be resolved by precedence
and associativity). It is not an LL(1) grammar because it contains
left-recursion.
fixed
width
font in
the productions of the lexical and syntactic grammars, and throughout this
specification whenever the text is directly referring to such a terminal
symbol. These are to appear in a program exactly as written.
Nonterminal symbols are shown in italic
type. The
definition of a nonterminal is introduced by the name of the nonterminal being
defined followed by a colon. One or more alternative right-hand sides for the
nonterminal then follow on succeeding lines. For example, the syntactic
definition:
IfThenStatement: if ( Expression ) Statementstates that the nonterminal
IfThenStatement
represents the
token if
, followed by a left parenthesis token, followed
by an Expression
, followed by a right parenthesis
token, followed by a Statement
.
As another example, the syntactic definition:
ArgumentList: Argument ArgumentList , Argumentstates that an
ArgumentList
may represent either a single
Argument
or an ArgumentList
, followed
by a comma token, followed by an Argument
. This definition
of ArgumentList
is recursive, that is to say, it
is defined in terms of itself. The result is that an
ArgumentList
may contain any positive number of
arguments. Such recursive definitions of nonterminals are common. Moreover,
this definition is left-recursive, which means it cannot be used
directly with a top-down (LL) parser like JavaCC. Rather, the grammar will
need to be manipulated to eliminate left-recursion (and to left-factor common
prefixes).
The grammar below uses the following BNF-style conventions:
xopt
denotes an optional (zero or one)
occurrence of x
.x*
denotes zero or more occurrences of
x
.Goal: CompilationUnit
The
Java lexical definition describes tokens for
Identifier
, and the various literals:
Literal: IntegerLiteral BooleanLiteral CharacterLiteral StringLiteral NullLiteralIn MiniJava we accept identifiers consisting of ASCII characters only, integer literals including octal and hexadecimal, and character and string literals consisting of ASCII characters only, and including escapes.
Type: PrimitiveType ReferenceType PrimitiveType: int boolean ReferenceType: ClassType ArrayType ClassType: Name ArrayType: PrimitiveType [ ] ClassType [ ] ArrayType [ ]
Name: SimpleName QualifiedName SimpleName: Identifier QualifiedName: Name . Identifier
CompilationUnit: TypeDeclaration* TypeDeclaration: ClassDeclaration ;
Modifiers: Modifier* Modifier: public static native
ClassDeclaration: class Identifier Superopt ClassBody Super: extends ClassType ClassBody: { ClassMemberDeclaration* } ClassMemberDeclaration: FieldDeclaration MethodDeclaration
FieldDeclaration: Modifiers Type IdentifierList ; IdentifierList: Identifier IdentifierList , Identifier
MethodDeclaration: MethodHeader MethodBody MethodHeader: Modifiers Type MethodDeclarator Throwsopt Modifiers void MethodDeclarator Throwsopt MethodDeclarator: Identifier ( FormalParameterListopt ) FormalParameterList: FormalParameter FormalParameterList , FormalParameter FormalParameter: Type Identifier Throws: throws ClassType MethodBody: Block ;
Block: { BlockStatement* } BlockStatement: LocalVariableDeclarationStatement Statement LocalVariableDeclarationStatement: LocalVariableDeclaration ; LocalVariableDeclaration: Type VariableDeclarators VariableDeclarators: VariableDeclarator VariableDeclarators , VariableDeclarator VariableDeclarator: Identifier Identifier = Expression Statement: Block ExpressionStatement IfThenStatement IfThenElseStatement WhileStatement DoWhileStatement ReturnStatement BreakStatement ContinueStatement ForStatement ExpressionStatement: StatementExpressionopt ; StatementExpression: Expression IfThenStatement: if ( Expression ) Statement IfThenElseStatement: if ( Expression ) Statement else Statement WhileStatement: while ( Expression ) Statement DoWhileStatement: do Statement while ( Expression ) ; ReturnStatement: return Expressionopt ; BreakStatement: break ; ContinueStatement: continue ; ForStatement: for ( ForInitopt ; Expressionopt ; ForUpdateopt ) Statement ForInit: StatementExpressionList LocalVariableDeclaration ForUpdate: StatementExpressionList StatementExpressionList: StatementExpression StatementExpressionList , StatementExpression
Expression: ConditionalExpression AssignmentExpression AssignmentExpression: ConditionalExpression = Expression ConditionalExpression: InfixExpression InfixExpression ? Expression : ConditionalExpression InfixExpression: PrefixExpression InfixExpression InfixOp PrefixExpression InfixOp: || && == != < > <= >= + - * / PrefixExpression: PrefixOp PrefixExpression PostfixExpression PrefixOp: - ! PostfixExpression: Primary Suffix* Suffix: ArrayAccess FieldAccess MethodInvocation ArrayAccess: [ Expression ] Selector: . Identifier FieldAccess: Selector MethodInvocation: Selector ( ArgumentListopt ) ArgumentList: Expression ArgumentList , Expression Primary: ( Expression ) this Literal Identifier super FieldAccess super MethodInvocation ClassInstanceCreationExpression ArrayCreationExpression ClassInstanceCreationExpression: new ClassType ( ) ArrayCreationExpression: new PrimitiveType [ Expression ] Dimension* new ClassType [ Expression ] Dimension* Dimension: [ ]