Paste: Grammar

Author: Max
Mode: javacc
Date: Wed, 7 Oct 2009 23:35: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)


/*********************************************************************
 * 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_())*
<CLK>
}

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> ( (<ATRI>) | (<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>)
)
}

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

Summary:
Author:
Mode:
Body: