Paste: Grammar
Author: | Max |
Mode: | javacc |
Date: | Thu, 8 Oct 2009 00:50:50 |
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> (BeginWithID())?)
)
}
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> ( (<ATRI>) | (<OPC> Expression_() <CLC><ATRI>) ) Expression_() <DELIM_PV>)
)
}
void Expression_():
{}
{
(
(<NUM> (Expression_1())?) |
(<TRUE_RES> (Expression_1())?) |
(<FALSE_RES> (Expression_1())?) |
(<ID> (Expression_1())?) |
(<THIS_RES> (Expression_1())?) |
(<NEW_RES> ((<INT_RES> <OPC> (Expression_()) <CLC>) | (<ID> <OPPAR> <CLPAR>))) |
(<NOT> Expression_()) |
(<OPPAR> Expression_() <CLPAR>)
)
}
void Expression_1():
{}
{
(
((<ADD>|<SUB>|<MULT>|<CONJ>|<LESS>)) |
(<OPC>Expression_()<CLC>) |
(<DOT>(<LENGTH_RES>|(<ID><OPPAR>(Expression_()(<SEP_VIR>Expression_())*)?<CLPAR>)))
)
(Expression_1())?
}
New Annotation