Paste: Grammar
Author: | Max |
Mode: | javacc |
Date: | Thu, 8 Oct 2009 02:10:41 |
Plain Text |
PARSER_BEGIN(Parser)
public class Parser {
public static void main(String args[]) throws ParseException {
Parser parser = new Parser( System.in ) ;
try {
parser.Goal() ;
}
catch ( ParseException e ) {
System.out.println( "Exiting." ) ;
throw e ;
}
}
}
PARSER_END(Parser)
SKIP: {"\r"|"\n"|"\r\n"|"\t"|" "}
SPECIAL_TOKEN :
{
<COMMENTS_FORMAL: (("/**")(~["/"](("*")(~["/"]) | (~["*"])(("/")?))*)("*/"))> |
<COMMENTS_LINE: ("//")(~["\r","\n"])*("\n" | "\r\n" | ("\r"))> |
<COMMENTS_MLINE: ("/*")(("*")~["/"] | ~["*"](["/"])?)*("*/")>
}
TOKEN :
{
<BOOLEAN_RES: "boolean"> |
<CLASS_RES: "class"> |
<INTERFACE_RES: "interface"> |
<ELSE_RES: "else"> |
<EXTENDS_RES: "extends"> |
<FALSE_RES: "false"> |
<IF_RES: "if"> |
<WHILE_RES: "while"> |
<INT_RES: "int"> |
<LENGTH_RES: "length"> |
<MAIN_RES: "main"> |
<NEW_RES: "new"> |
<NULL_RES: "null"> |
<PUBLIC_RES: "public"> |
<RETURN_RES: "return"> |
<STATIC_RES: "static"> |
<STRING_RES: "String"> |
<TRUE_RES: "true"> |
<THIS_RES: "this"> |
<PRINTLN_RES: "System.out.println"> |
<VOID_RES: "void"> |
<ADD: "+"> |
<SUB: "-"> |
<MULT: "*"> |
<CONJ: "&&"> |
<NOT: "!"> |
<LESS: "<"> |
<ATRI: "="> |
<DOT: "."> |
<OPPAR: "("> |
<CLPAR: ")"> |
<OPC: "["> |
<CLC: "]"> |
<OPK: "{"> |
<CLK: "}"> |
<DELIM_PV: ";"> |
<SEP_VIR: ","> |
<SEP_DOT: ["."]>
}
TOKEN :
{
<NUM: ("0"|(["1"-"9"]((["0"-"9"])*)?))>
}
TOKEN :
{
<ID: (<LETTER>(["0"-"9"] | <LETTER>)*)> |
< #LETTER: ("$" | "_" | (["a"-"z","A"-"Z"]))>
}
void Goal() :
{}
{
MainClass()
(ClassDeclaration())*
<EOF>
}
void MainClass():
{}
{
<CLASS_RES> <ID> <OPK> <PUBLIC_RES> <STATIC_RES> <VOID_RES> <MAIN_RES>
<OPPAR> <STRING_RES> <OPC> <CLC> <ID> <CLPAR> <OPK> <CLK> <CLK>
}
void ClassDeclaration():
{}
{
<CLASS_RES> <ID> ((<EXTENDS_RES> <ID>)?) <OPK>
(VarDeclaration())*
(MethodDeclaration())*
<CLK>
}
void VarDeclaration():
{}
{
TypeCh() <ID>(<SEP_VIR> <ID>)* <DELIM_PV>
}
void TypeCh():
{}
{
(
<INT_RES>((<OPC><CLC>)?) |
<BOOLEAN_RES> |
<ID>
)
}
void MethodDeclaration():
{}
{
<PUBLIC_RES> TypeCh() <ID> <OPPAR> ( (TypeCh() <ID> (<SEP_VIR> TypeCh() <ID>)*)? ) <CLPAR>
<OPK>
(StatementWithVarDec())
<RETURN_RES> Expression_() <DELIM_PV>
<CLK>
}
void StatementWithVarDec():
{}
{
(
(<ID> BeginWithID()) |
(((<BOOLEAN_RES> | <INT_RES>) <ID>((<SEP_VIR><ID>)*)<DELIM_PV>) (StatementWithVarDec())?)
)
}
void BeginWithID():
{}
{
(
((( (<OPC> Expression_() <CLC>)? <ATRI> Expression_())) <DELIM_PV> (Statement_())*) |
(<ID> ((<SEP_VIR><ID>)*) <DELIM_PV> (StatementWithVarDec())?)
)
}
void Statement_():
{}
{
(
(<OPK> ( (Statement_())* ) <CLK>) |
(<IF_RES> <OPPAR> Expression_() <CLPAR> Statement_() <ELSE_RES> Statement_()) |
(<WHILE_RES> <OPPAR> Expression_() <CLPAR> Statement_()) |
(<PRINTLN_RES> <OPPAR> Expression_() <CLPAR> <DELIM_PV>) |
((<ID> (<OPC> Expression_() <CLC>)? <ATRI> ) Expression_() <DELIM_PV>)
)
}
void Expression_():
{}
{
AddExpr()
}
void AddExpr():
{}
{
MultExpr() AddExpr1()
}
void AddExpr1():
{}
{
(<ADD>AddExpr())?
}
void MultExpr():
{}
{
LessExpr() MultExpr1()
}
void MultExpr1():
{}
{
(<MULT> MultExpr())?
}
void LessExpr():
{}
{
AndExpr() LessExpr1()
}
void LessExpr1():
{}
{
(<LESS> LessExpr())?
}
void AndExpr():
{}
{
NotExpr() AndExpr1()
}
void AndExpr1():
{}
{
(<CONJ> AndExpr())?
}
void NotExpr():
{}
{
(<NOT>)?(CallExpr())
}
void CallExpr():
{}
{
Prim() CallExpr1()
}
void CallExpr1():
{}
{
((<DOT>(<LENGTH_RES> | (<ID> <OPPAR> (Expression_() (<SEP_VIR> Expression_())*)? <CLPAR>))) | <OPC> Expression_() <CLC> )?
}
void Prim():
{}
{
(<ID> |
<NUM> |
<TRUE_RES> |
<FALSE_RES> |
<THIS_RES> |
<NEW_RES> ((<INT_RES> <OPC> Expression_() <CLC>) | (<ID> <OPPAR> <CLPAR>)) |
<OPPAR> Expression_() <CLPAR>
)
}
New Annotation