options { JAVA_UNICODE_ESCAPE = true; DEBUG_PARSER = true; } PARSER_BEGIN(Imp2Parser) package li2.plp.imperative2.parser; import li2.plp.expressions1.util.*; import li2.plp.expressions2.expression.*; import li2.plp.imperative1.command.*; import li2.plp.imperative1.declaration.*; import li2.plp.imperative1.memory.*; import li2.plp.imperative2.*; import li2.plp.imperative2.command.*; import li2.plp.imperative2.declaration.*; import li2.plp.imperative2.memory.*; import java.util.*; public class Imp2Parser { public static void main(String args[]) { Imp2Parser parser; ListaValor entrada = new ListaValor(); if (args.length == 0) { System.out.println("Imperativa 2 PLP Parser Version 0.0.1: Reading from standard input . . ."); parser = new Imp2Parser(System.in); } else{ System.out.println("Imperativa 2 PLP Parser Version 0.0.1: Reading from file " + args[0] + " . . ."); try { parser = new Imp2Parser(new java.io.FileInputStream(args[0])); } catch (java.io.FileNotFoundException e) { System.out.println("Java Parser Version 1.0.2: File " + args[0] + " not found."); return; } List valores = new LinkedList(); for(int i=1;i | | } TOKEN : /* TOKENS DE EXPRESSOES 1 */ { < AND: "and" > | < OR : "or" > | < NOT : "not" > | < LENGTH : "length" > | < TRUE : "true" > | < FALSE : "false" > } TOKEN : /* TOKENS DE IMPERATIVA 1 */ { < VAR: "var" > | < COMAND_SKIP: "skip" > | < WHILE: "while" > | < DO: "do" > | < READ: "read" > | < WRITE: "write" > | < IF: "if" > | < THEN: "then" > | < ELSE: "else" > } TOKEN : /* TOKENS DE IMPERATIVA 2 */ { < PROC: "proc" > | < CALL: "call" > } TOKEN : /* TOKENS DOS POSS�VEIS TIPOS */ { < INT : "int" > | < BOOLEAN : "boolean" > | < STRING : "string" > } TOKEN : /* LITERALS */ { < INTEGER_LITERAL: (["l","L"])? | (["l","L"])? | (["l","L"])? > | < #DECIMAL_LITERAL: ["1"-"9"] (["0"-"9"])* > | < #HEX_LITERAL: "0" ["x","X"] (["0"-"9","a"-"f","A"-"F"])+ > | < #OCTAL_LITERAL: "0" (["0"-"7"])* > | < STRING_LITERAL: "\"" ( (~["\"","\\","\n","\r"]) | ("\\" ( ["n","t","b","r","f","\\","'","\""] | ["0"-"7"] ( ["0"-"7"] )? | ["0"-"3"] ["0"-"7"] ["0"-"7"] ) ) )* "\"" > } TOKEN : /* IDENTIFIERS */ { < IDENTIFIER: (|)* > | < #LETTER: [ "\u0024", "\u0041"-"\u005a", "\u005f", "\u0061"-"\u007a", "\u00c0"-"\u00d6", "\u00d8"-"\u00f6", "\u00f8"-"\u00ff", "\u0100"-"\u1fff", "\u3040"-"\u318f", "\u3300"-"\u337f", "\u3400"-"\u3d2d", "\u4e00"-"\u9fff", "\uf900"-"\ufaff" ] > | < #DIGIT: [ "\u0030"-"\u0039", "\u0660"-"\u0669", "\u06f0"-"\u06f9", "\u0966"-"\u096f", "\u09e6"-"\u09ef", "\u0a66"-"\u0a6f", "\u0ae6"-"\u0aef", "\u0b66"-"\u0b6f", "\u0be7"-"\u0bef", "\u0c66"-"\u0c6f", "\u0ce6"-"\u0cef", "\u0d66"-"\u0d6f", "\u0e50"-"\u0e59", "\u0ed0"-"\u0ed9", "\u1040"-"\u1049" ] > } TOKEN : /* SEPARATORS */ { < LPAREN: "(" > | < RPAREN: ")" > | < LBRACE: "{" > | < RBRACE: "}" > | < LBRACKET: "[" > | < RBRACKET: "]" > | < SEMICOLON: ";" > | < COMMA: "," > | < DOT: "." > } TOKEN : /* OPERATORS */ { < ATTRIB: ":=" > | < ASSIGN: "=" > | < GT: ">" > | < LT: "<" > | < BANG: "!" > | < TILDE: "~" > | < HOOK: "?" > | < COLON: ":" > | < EQ: "==" > | < LE: "<=" > | < GE: ">=" > | < NE: "!=" > | < SC_OR: "||" > | < SC_AND: "&&" > | < CONCAT: "++" > | < PLUS: "+" > | < MINUS: "-" > | < STAR: "*" > | < SLASH: "/" > | < BIT_AND: "&" > | < BIT_OR: "|" > | < XOR: "^" > | < REM: "%" > } Programa Input() : { Programa retorno; } { retorno = PPrograma() { return retorno; } } Programa PPrograma() : { Comando retorno; } { retorno = PComando() { return new Programa(retorno); } } Comando PComando() : { Comando retorno; } { ( LOOKAHEAD (PComandoSimples() ) retorno = PSequenciaComando() | retorno = PComandoSimples() ) { return retorno; } } SequenciaComando PSequenciaComando() : { Comando c1; Comando c2; } { c1 = PComandoSimples() c2 = PComando() {return new SequenciaComando(c1, c2);} } IO PIO() : { IO retorno; } { ( retorno = PRead() | retorno = PWrite() ) {return retorno;} } Read PRead() : { Id id; } { id = PId() {return new Read (id);} } Write PWrite() : { Expressao exp; } { exp = PExpressao() {return new Write(exp);} } IfThenElse PIfThenElse() : { Expressao expressao; Comando comandoThen; Comando comandoElse; } { expressao = PExpressao() comandoThen = PComando() comandoElse = PComando() {return new IfThenElse (expressao, comandoThen, comandoElse);} } While PWhile() : { Expressao expressao; Comando comando; } { expressao = PExpressao() comando = PComando() {return new While(expressao, comando);} } Skip PSkip() : { } { {return new Skip();} } Atribuicao PAtribuicao() : { Id id; Expressao exp; } { id = PId() exp = PExpressao() { return new Atribuicao(id, exp); } } Id PId() : { Token token; } { token = { return new Id(token.toString()); } } Valor PValorInteiro() : { Token token; } { token = { return new ValorInteiro(Integer.parseInt(token.toString())); } } Valor PValorBooleano() : { } { { return new ValorBooleano(false); } | { return new ValorBooleano(true); } } Valor PValorString() : { Token token; } { token = { String tokenStr = token.toString(); tokenStr = tokenStr.substring(1,tokenStr.length()-1); return new ValorString(tokenStr); } } Valor PValor() : { Valor retorno; } { ( retorno = PValorInteiro() | retorno = PValorBooleano() | retorno = PValorString() ) { return retorno; } } Expressao PExpMenos() : { Expressao retorno; } { retorno = PExpressao() { return new ExpMenos(retorno); } } Expressao PExpNot() : { Expressao retorno; } { retorno = PExpressao() { return new ExpNot(retorno); } } Expressao PExpLength() : { Expressao retorno; } { retorno = PExpressao() { return new ExpLength(retorno); } } Expressao PExpPrimaria() : { Expressao retorno; } { ( retorno = PId() | retorno = PValor() | retorno = PExpressao() ) { return retorno; } } Expressao PExpUnaria() : { Expressao retorno; } { ( retorno = PExpMenos() | retorno = PExpNot() | retorno = PExpLength() ) { return retorno; } } Expressao PExpBinaria() : { Expressao retorno, param2; } { ( LOOKAHEAD (PExpPrimaria() ) retorno = PExpConcat() | LOOKAHEAD (PExpPrimaria() ) retorno = PExpSub() | LOOKAHEAD (PExpPrimaria() ) retorno = PExpAnd() | LOOKAHEAD (PExpPrimaria() ) retorno = PExpOr() | LOOKAHEAD (PExpPrimaria() ) retorno = PExpEquals() | LOOKAHEAD (PExpPrimaria() ) retorno = PExpSoma() ) { return retorno; } } ExpSoma PExpSoma() : { Expressao esq; Expressao dir; } { esq = PExpPrimaria() dir = PExpressao() {return new ExpSoma(esq, dir);} } ExpSub PExpSub() : { Expressao esq; Expressao dir; } { esq = PExpPrimaria() dir = PExpressao() {return new ExpSub(esq, dir);} } ExpAnd PExpAnd() : { Expressao esq; Expressao dir; } { esq = PExpPrimaria() dir = PExpressao() {return new ExpAnd(esq, dir);} } ExpOr PExpOr() : { Expressao esq; Expressao dir; } { esq = PExpPrimaria() dir = PExpressao() {return new ExpOr(esq, dir);} } ExpEquals PExpEquals() : { Expressao esq; Expressao dir; } { esq = PExpPrimaria() dir = PExpressao() {return new ExpEquals(esq, dir);} } ExpConcat PExpConcat() : { Expressao esq; Expressao dir; } { esq = PExpPrimaria() dir = PExpressao() {return new ExpConcat(esq, dir);} } Expressao PExpressao() : { Expressao retorno; } { ( LOOKAHEAD (2) retorno = PExpUnaria() | LOOKAHEAD (PExpPrimaria() ( | | | | | )) retorno = PExpBinaria() | retorno = PExpPrimaria() ) { return retorno; } } /* ADICIONADO OU MODIFICADO DA Imperativa1 PARA A Imperativa2 */ Comando PComandoSimples() : { Comando retorno; } { ( retorno = PSkip() | retorno = PAtribuicao() | retorno = PComandoDeclaracao() | retorno = PWhile() | retorno = PIfThenElse() | retorno = PIO() | retorno = PComando() | retorno = PChamadaProcedimento() ) { return retorno; } } ChamadaProcedimento PChamadaProcedimento(): { Id nomeProcedimento; ListaExpressao parametrosAtuais; } { nomeProcedimento = PId() parametrosAtuais = PListaExpressao() { return new ChamadaProcedimento(nomeProcedimento, parametrosAtuais); } } ComandoDeclaracao PComandoDeclaracao() : { Declaracao dec; Comando comando; } { dec = PDeclaracao() comando = PComando() { return new ComandoDeclaracao(dec, comando); } } Declaracao PDeclaracao(): { Declaracao retorno; } { ( LOOKAHEAD(PDeclaracaoVariavel() ) retorno = PDeclaracaoComposta() |LOOKAHEAD(PDeclaracaoProcedimento() ) retorno = PDeclaracaoComposta() | retorno = PDeclaracaoVariavel() | retorno = PDeclaracaoProcedimento() | retorno = PDeclaracao() ) {return retorno;} } DeclaracaoComposta PDeclaracaoComposta(): { Declaracao d1; Declaracao d2; } { (LOOKAHEAD(PDeclaracaoVariavel() ) d1 = PDeclaracaoVariavel() d2 = PDeclaracao() | LOOKAHEAD(PDeclaracaoProcedimento() ) d1 = PDeclaracaoProcedimento() d2 = PDeclaracao() ) {return new DeclaracaoComposta(d1, d2);} } DeclaracaoProcedimento PDeclaracaoProcedimento(): { Id nome; DefProcedimento defProcedimento; } { ( LOOKAHEAD( PId() ) nome = PId() defProcedimento = PDefProcedimento() | nome = PId() defProcedimento = PDefProcedimento() ) { return new DeclaracaoProcedimento (nome, defProcedimento); } } DefProcedimento PDefProcedimento(): { ListaDeclaracaoParametro listaPar = null; Comando comando; } { ( comando = PComando() | listaPar = PListaDeclaracaoParametro() comando = PComando() ) { if(listaPar == null) { listaPar = new ListaDeclaracaoParametro(); } return new DefProcedimento (listaPar, comando); } } Tipo PTipo(): { Tipo tipo; } { ( {tipo = TipoPrimitivo.INTEIRO;} | {tipo = TipoPrimitivo.BOOLEANO;} | {tipo = TipoPrimitivo.STRING;} ) { return tipo; } } ListaDeclaracaoParametro PListaDeclaracaoParametro(): { Id id; Tipo tipo; ListaDeclaracaoParametro lista = null; } { [ tipo = PTipo() id = PId() { lista = new ListaDeclaracaoParametro(new DeclaracaoParametro(id, tipo));} ] ( tipo = PTipo() id = PId() {lista = new ListaDeclaracaoParametro(new DeclaracaoParametro(id, tipo), lista);} )* { if (lista == null) { lista = new ListaDeclaracaoParametro(); } return lista; } } ListaExpressao PListaExpressao(): { Expressao exp; ListaExpressao lista = null; } { [ exp = PExpressao() { lista = new ListaExpressao(exp); } ] ( exp = PExpressao() {lista = new ListaExpressao(exp, lista);} )* { if (lista == null) { lista = new ListaExpressao(); } return lista; } } DeclaracaoVariavel PDeclaracaoVariavel() : { Id id; Expressao exp; DeclaracaoVariavel retorno; } { id = PId() exp = PExpressao() {retorno = new DeclaracaoVariavel(id, exp);} { return retorno; } }