In: Computer Science
The following grammar for a program has a problem with semicolons:
Use JavaCC to update
When an if statement has a block after it, the block sometimes must end with a semicolon sometimes but not all the time.
The Main purpose of semicolen here is to separate independent statements.
Example : Statement 1 : statement 2 : statement 3;
semicolens are used where the comma and full stops can not be used and are inappropriate.
Javacc 1 .A program that creates parsers
2.The source code(input) is a definition file ◦ Grammar definition ◦ Inline Java code
3 The target(output) is java-based parser
BASICS of JavaCC need to know for better understanding of program
javacc_input ::= javacc_options
"PARSER_BEGIN" "(" identifier ")"
CompilationUnit
"PARSER_END" "(" identifier ")"
( production )+
<EOF>
javacc_options ::= ( <IDENTIFIER> "{" ( option_binding )* "}" )?
option_binding ::= ( <IDENTIFIER> | "LOOKAHEAD" | "IGNORE_CASE" | "static" | "PARSER_BEGIN" )
"="
( IntegerLiteral | BooleanLiteral | StringLiteral | StringList )
";"
option_binding ::= ( <IDENTIFIER> | "LOOKAHEAD" | "IGNORE_CASE" | "static" | "PARSER_BEGIN" )
"="
( IntegerLiteral | BooleanLiteral | StringLiteral | StringList )
";"
StringList ::= "(" StringLiteral ( "," StringLiteral )* ")"
production ::= javacode_production
| cppcode_production
| regular_expr_production
| token_manager_decls
| bnf_production
javacode_production ::= "JAVACODE"
AccessModifier
ResultType
identifier
FormalParameters ( "throws" Name ( "," Name )* )?
Block
cppcode_production ::= "CPPCODE"
AccessModifier
ResultType
identifier
FormalParameters ( "throws" Name ( "," Name )* )?
Block
bnf_production ::= AccessModifier
ResultType
identifier
FormalParameters ( "throws" Name ( "," Name )* )?
":"
Block
"{" expansion_choices "}"
AccessModifier ::= ( "public" | "protected" | "private" )?
regular_expr_production ::= ( "<" "*" ">"
| "<" <IDENTIFIER> ( "," <IDENTIFIER> )* ">"
)?
regexpr_kind ( "[" "IGNORE_CASE" "]" )? ":" "{"
regexpr_spec ( "|" regexpr_spec )* "}"
token_manager_decls ::= "TOKEN_MGR_DECLS" ":" ( ClassOrInterfaceBody )?
regexpr_kind ::= "TOKEN"
| "SPECIAL_TOKEN"
| "SKIP"
| "MORE"
regexpr_spec ::= regular_expression
( Block )?
( ":" <IDENTIFIER> )?
expansion_choices ::= expansion ( "|" expansion )*
expansion ::= ( "LOOKAHEAD" "(" local_lookahead ")" )?
( expansion_unit )+
local_lookahead ::= ( IntegerLiteral )?
( "," )?
( expansion_choices )?
( "," )?
( "{" ( Expression )? "}" )?
expansion_unit ::= "LOOKAHEAD" "(" local_lookahead ")"
| Block
| "[" expansion_choices "]"
| "try" "{" expansion_choices "}"
( "catch" "(" ( Name <IDENTIFIER> )? ")" Block )*
( "finally" Block )?
| ( PrimaryExpression "=" )?
(
identifier ( TypeArguments )? Arguments
| regular_expression ( "." <IDENTIFIER> )?
)
| "(" expansion_choices ")"
( "+" | "*" | "?" )?
regular_expression ::= StringLiteral
| <LANGLE: "<">
( ( "#" )? identifier ":" )?
complex_regular_expression_choices <RANGLE: ">">
| "<" identifier ">"
| "<" "EOF" ">"
complex_regular_expression_choices ::= complex_regular_expression
( "|" complex_regular_expression )*
complex_regular_expression ::= ( complex_regular_expression_unit )+
complex_regular_expression_unit ::= StringLiteral
| "<" identifier ">"
| character_list
| "(" complex_regular_expression_choices ")"
(
"+" | "*" | "?" | "{"
IntegerLiteral
( "," ( IntegerLiteral )? )?
"}"
)?
character_list ::= ( "~" )? "[" ( character_descriptor ( "," character_descriptor )* )? "]"
character_descriptor ::= StringLiteral ( "-" StringLiteral )?
identifier ::= <IDENTIFIER>
Program structure
CompilationUnit ::= ( PackageDeclaration )?
( ImportDeclaration )*
( TypeDeclaration )*
PackageDeclaration ::= Modifiers "package" Name ";"
ImportDeclaration ::= "import"
( "static" )?
Name
( "." "*" )?
";"
Example Program :
PARSER_BEGIN(Example)
/**
* Simple brace matcher.
*/
public class Example{
/** Main entry point. */
public static void main(String args[]) throws ParseException {
Example3 parser = new Example(System.in);
parser.Input();
}
}
PARSER_END(Example)
SKIP :
{
" "
| "\t"
| "\n"
| "\r"
}
TOKEN :
{
<LBRACE: "{">
| <RBRACE: "}">
}
/** Root production. */
void Input() :
{ int count; }
{
count=MatchedBraces() <EOF>
{ System.out.println("The levels of nesting is " + count); }
}
/** Brace counting production. */
int MatchedBraces() :
{ int nested_count=0; }
{
<LBRACE> [ nested_count=MatchedBraces() ] <RBRACE>
{ return ++nested_count; }
}