Java Reference
In-Depth Information
actions, such as creating an AST node, are java code embedded within blocks. JavaCC turns
the specification for each non-terminal into a java method within the generated parser.
As an example, let us look at how we specify the following rule:
qualiedIdentier ::= <identifier> f .<identifier> g
for parsing a qualified identifier using JavaCC.
privateTypeNamequalifiedIdentifier():{
intline=0;
StringqualifiedIdentifier="";
}
{
try{
<IDENTIFIER>
{
line=token.beginLine;
qualifiedIdentifier=token.image;
}
(
<DOT><IDENTIFIER>
{qualifiedIdentifier+="."+token.image;}
)*
}
catch(ParseExceptione){
recoverFromError(newint[]{SEMI,EOF},e);
}
{return
newTypeName(line,qualifiedIdentifier);}
}
Let us walk through the above method in order to make sense out of it.
The qualifiedIdentifier non-terminal, as in the case of the hand-written parser,
is private method and returns an instance of TypeName . The method does not take
any arguments.
The local variable block defines two variables, line and qualifiedIdentifier ; the
former is for tracking the line number in the source file, and the latter is for accumu-
lating the individual identifiers ( x , y , and z in x.y.z , for example) into a qualified
identifier ( x.y.z , for example). The variables line and qualifiedIdentifier are used
within the body that actually parses a qualified identifier.
In the body, all parsing is done within the try-catch block. When an identifier token 8
<IDENTIFIER> is encountered, its line number in the source file and its image are
recorded in the respective variables; this is an action and hence is within a block. We
then look for zero or more occurrences of the tokens <DOT><IDENTIFIER> within the
()* EBNF construct. For each such occurrence, we append the image of the identifier
to the qualifiedIdentifier variable; this is also an action and hence is java code
within a block. Once a qualified identifer has been parsed, we return (again an action
and hence is java code within a block) an instance of TypeName .
JavaCC raises a ParseException when encountering a parsing error. The instance of
ParseException stores information about the token that was found and the token
that was sought. When such an exception occurs, we invoke our own error recovery
8 The token variable stores the current token information: token.beginLine stores the line number
in which the token occurs in the source file and token.image stores the token's image (for example, the
identifier name in case of the <IDENTIFIER> token).
 
Search WWH ::




Custom Search