Paste: Grammar

Author: Max
Mode: javacc
Date: Thu, 8 Oct 2009 02:02:14
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)


/*********************************************************************
 * Defina os tokens  AQUI (copie o cdigo do  analisador lxico)     *
 *********************************************************************/


SKIP: {"\r"|"\n"|"\r\n"|"\t"|" "}

SPECIAL_TOKEN : /* COMMENTS */
{
// Escreva as ER's referentes aos tr�s tipos de coment�rios.
<COMMENTS_FORMAL: (("/**")(~["/"](("*")(~["/"]) | (~["*"])(("/")?))*)("*/"))> |
<COMMENTS_LINE: ("//")(~["\r","\n"])*("\n" | "\r\n" | ("\r"))> |
<COMMENTS_MLINE: ("/*")(("*")~["/"] | ~["*"](["/"])?)*("*/")>
}

TOKEN : 
{
/*  Escreva as  ER's  referentes �s  palavras reservadas,  operadores,
 *  pontuadores e separadores.
 */
<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 : /* NUMBERS */
{
/* Escreva as ER's referentes aos n�meros inteiros. */
<NUM: ("0"|(["1"-"9"]((["0"-"9"])*)?))>
}

TOKEN : /* IDENTIFIERS */
{
/* Escreva as ER's referentes aos identificadores. */
<ID: (<LETTER>(["0"-"9"] | <LETTER>)*)> |
< #LETTER: ("$" | "_" | (["a"-"z","A"-"Z"]))>
}


/*********************************************************************
 * A gramtica da linguagem MiniJava comea aqui                     *
 *********************************************************************/

void Goal() :
{}
{
	MainClass()
	(ClassDeclaration())*
  <EOF>
}

void MainClass():
{}
{
/*
  MainClass  ::=  "class" Identifier "{" "public" "static" "void" "main" "(" "String" "[" "]" Identifier ")" "{" Statement "}" "}"
*/
<CLASS_RES> <ID> <OPK> <PUBLIC_RES> <STATIC_RES> <VOID_RES> <MAIN_RES>
<OPPAR> <STRING_RES> <OPC> <CLC> <ID> <CLPAR> <OPK> <CLK> <CLK>
}

void ClassDeclaration():
{}
{
/*
ClassDeclaration  ::=  "class" Identifier ( "extends" Identifier )? "{" ( VarDeclaration )* ( MethodDeclaration )* "}"
*/
	<CLASS_RES> <ID> ((<EXTENDS_RES> <ID>)?) <OPK>
		(VarDeclaration())*
		(MethodDeclaration())*
	<CLK>
}

void VarDeclaration():
{}
{
/*
VarDeclaration  ::=  Type Identifier ";"
*/
	TypeCh() <ID>(<SEP_VIR> <ID>)* <DELIM_PV>
}

void TypeCh():
{}
{
/*
  Type  ::=  "int" "[" "]"
  |  "boolean"
  |  "int"
  |  Identifier
*/
 (
	<INT_RES>((<OPC><CLC>)?) |
	<BOOLEAN_RES> |
	<ID>
 )
}

void MethodDeclaration():
{}
{
/*
  MethodDeclaration  ::=  "public" Type Identifier "(" ( Type Identifier ( "," Type Identifier )* )? ")" "{" ( VarDeclaration )* ( Statement )* "return" Expression ";" "}"
*/
<PUBLIC_RES> TypeCh() <ID> <OPPAR> ( (TypeCh() <ID> (<SEP_VIR> TypeCh() <ID>)*)? ) <CLPAR>
<OPK>
 //(VarDeclaration())*
 //(Statement_())*
	(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_():
{}
{
/*
Statement  ::=  "{" ( Statement )* "}"
  |  "if" "(" Expression ")" Statement "else" Statement
  |  "while" "(" Expression ")" Statement
  |  "System.out.println" "(" Expression ")" ";"
  |  Identifier "=" Expression ";"
  |  Identifier "[" Expression "]" "=" Expression ";"*/
(
	(<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_():
{}
{
/*
Expression  ::=  Expression ( "&&" | "<" | "+" | "-" | "*" ) Expression
  |  Expression "[" Expression "]"
  |  Expression "." "length"
  |  Expression "." Identifier "(" ( Expression ( "," Expression )* )? ")"
  |  <NUMBER>
  |  "true"
  |  "false"
  |  Identifier
  |  "this"
  |  "new" "int" "[" Expression "]"
  |  "new" Identifier "(" ")"
  |  "!" Expression
  |  "(" 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>)
)*/
	
	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():
{}
{
	Prim() AndExpr1()
}

void AndExpr1():
{}
{
	(<CONJ> AndExpr())?
}

void Prim():
{}
{
	(<NOT>)?
	(<ID> |
	 <NUM> |
	 <TRUE_RES> |
	 <FALSE_RES> |
	 <THIS_RES> |
	 <NEW_RES> ((<INT_RES> <OPC> Expression_() <CLC>) | (<ID> <OPPAR> <CLPAR>)) |
	 <OPPAR> Expression_() <CLPAR>
	)
}

New Annotation

Summary:
Author:
Mode:
Body: