19.1.3
Problem #3: Field Declaration versus Method Declaration
LALR(1) GRAMMAR
public static int maddie(String art) { return art.length(); }
Therefore, the parser cannot tell with only one token lookahead whether
static
(or, similarly,
public
) should be reduced to
FieldModifier
or
MethodModifier
.
Therefore, the productions shown above result in a grammar that is not LALR(1).
There are also other problems with drawing distinctions among different kinds of
modifiers in the grammar.
While not all contexts provoke the problem, the simplest solution is to com
bine all contexts in which such modifiers are used, eliminating all six of the
nonterminals
ClassModifiers
( 8.1.2),
FieldModifiers
( 8.3.1),
MethodModifiers
( 8.4.3),
ConstructorModifiers
( 8.6.3),
InterfaceModifiers
( 9.1.2), and
ConstantModifiers
( 9.3) from the grammar, replacing them all with a single non
terminal
Modifiers
:
Modifiers:
Modifier
Modifiers Modifier
Modifier: one of
public protected private
static
abstract final native synchronized transient volatile
A later stage of compiler analysis then sorts out the precise role of each modifier
and whether it is permitted in a given context.
19.1.3 Problem #3: Field Declaration versus Method Declaration
Consider the two productions (shown after problem #2 has been corrected):
FieldDeclaration:
Modifiers
opt
Type VariableDeclarators
;
and:
MethodHeader:
Modifiers
opt
ResultType MethodDeclarator Throws
opt
where
ResultType
is defined as:
436
footer
Our partners:
PHP: Hypertext Preprocessor Best Web Hosting
Java Web Hosting
Inexpensive Web Hosting
Jsp Web Hosting
Cheapest Web Hosting
Jsp Hosting
Cheap Hosting
Visionwebhosting.net Business web hosting division of Web
Design Plus. All rights reserved