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 c�digo do analisador l�xico) * *********************************************************************/ SKIP: {"\r"|"\n"|"\r\n"|"\t"|" "} SPECIAL_TOKEN : /* COMMENTS */ { // Escreva as ER's referentes aos tr�s tipos de coment�rios. | | } TOKEN : { /* Escreva as ER's referentes �s palavras reservadas, operadores, * pontuadores e separadores. */ | | | | | | | | | | | | | | | | | | | | | // | | | | | | | | | | | | | | | } // // TOKEN : /* NUMBERS */ { /* Escreva as ER's referentes aos n�meros inteiros. */ } TOKEN : /* IDENTIFIERS */ { /* Escreva as ER's referentes aos identificadores. */ (["0"-"9"] | )*)> | < #LETTER: ("$" | "_" | (["a"-"z","A"-"Z"]))> } /********************************************************************* * A gram�tica da linguagem MiniJava come�a aqui * *********************************************************************/ void Goal() : {} { MainClass() (ClassDeclaration())* } void MainClass(): {} { /* MainClass ::= "class" Identifier "{" "public" "static" "void" "main" "(" "String" "[" "]" Identifier ")" "{" Statement "}" "}" */ } void ClassDeclaration(): {} { /* ClassDeclaration ::= "class" Identifier ( "extends" Identifier )? "{" ( VarDeclaration )* ( MethodDeclaration )* "}" */ (( )?) (VarDeclaration())* (MethodDeclaration())* } void VarDeclaration(): {} { /* VarDeclaration ::= Type Identifier ";" */ TypeCh() ( )* } void TypeCh(): {} { /* Type ::= "int" "[" "]" | "boolean" | "int" | Identifier */ ( (()?) | | ) } void MethodDeclaration(): {} { /* MethodDeclaration ::= "public" Type Identifier "(" ( Type Identifier ( "," Type Identifier )* )? ")" "{" ( VarDeclaration )* ( Statement )* "return" Expression ";" "}" */ TypeCh() ( (TypeCh() ( TypeCh() )*)? ) //(VarDeclaration())* //(Statement_())* (StatementWithVarDec()) Expression_() } void StatementWithVarDec(): {} { ( ( BeginWithID()) | ((( | ) (()*)) (StatementWithVarDec())?) ) } void BeginWithID(): {} { ( ((( ( Expression_() )? Expression_())) (Statement_())*) | ( (()*) (StatementWithVarDec())?) ) } void Statement_(): {} { /* Statement ::= "{" ( Statement )* "}" | "if" "(" Expression ")" Statement "else" Statement | "while" "(" Expression ")" Statement | "System.out.println" "(" Expression ")" ";" | Identifier "=" Expression ";" | Identifier "[" Expression "]" "=" Expression ";"*/ ( ( ( (Statement_())* ) ) | ( Expression_() Statement_() Statement_()) | ( Expression_() Statement_()) | ( Expression_() ) | (( ( Expression_() )? ) Expression_() ) ) } void Expression_(): {} { /* Expression ::= Expression ( "&&" | "<" | "+" | "-" | "*" ) Expression | Expression "[" Expression "]" | Expression "." "length" | Expression "." Identifier "(" ( Expression ( "," Expression )* )? ")" | | "true" | "false" | Identifier | "this" | "new" "int" "[" Expression "]" | "new" Identifier "(" ")" | "!" Expression | "(" Expression ")" ( ( (Expression_1())?) | ( (Expression_1())?) | ( (Expression_1())?) | ( (Expression_1())?) | ( (Expression_1())?) | ( (( (Expression_()) ) | ( ))) | ( Expression_()) | ( Expression_() ) )*/ AddExpr() } void AddExpr(): {} { MultExpr() AddExpr1() } void AddExpr1(): {} { (AddExpr())? } void MultExpr(): {} { LessExpr() MultExpr1() } void MultExpr1(): {} { ( MultExpr())? } void LessExpr(): {} { AndExpr() LessExpr1() } void LessExpr1(): {} { ( LessExpr())? } void AndExpr(): {} { Prim() AndExpr1() } void AndExpr1(): {} { ( AndExpr())? } void Prim(): {} { ()? ( | | | | | (( Expression_() ) | ( )) | Expression_() ) }