options { JAVA_UNICODE_ESCAPE = true; } PARSER_BEGIN(Exp2Parser) package le2.plp.expressions2.parser; import le2.plp.expressions2.*; import le2.plp.expressions2.memory.*; import le2.plp.expressions2.declaration.*; import le2.plp.expressions2.expression.*; import java.util.List; import java.util.LinkedList; public class Exp2Parser { public static void main(String args []) { Exp2Parser parser; if (args.length == 0) { System.out.println("Expressoes 2 PLP Parser Version 0.0.1: Reading from standard input . . ."); parser = new Exp2Parser(System.in); } else if (args.length == 1) { System.out.println("Expressoes 2 PLP Parser Version 0.0.1: Reading from file " + args [0] + " . . ."); try { parser = new Exp2Parser(new java.io.FileInputStream(args [0])); } catch (java.io.FileNotFoundException e) { System.out.println("Expressoes 2 PLP Parser Version 0.0.1: File " + args [0] + " not found."); return; } } else { System.out.println("Expressoes 2 PLP Parser Version 0.0.1: Usage is one of:"); System.out.println(" java Exp2Parser < inputfile"); System.out.println("OR"); System.out.println(" java Exp2Parser inputfile"); return; } try { Programa programa = parser.Input(); System.out.println("Expressoes 2 PLP Parser Version 0.0.1: Expressoes2 program parsed successfully."); try { if (!programa.checaTipo()) { System.out.println("Erro de tipo."); } else { System.out.println(programa.executar()); } } catch (VariavelJaDeclaradaException ee) { System.out.println("Erro: " + ee); ee.printStackTrace(); } catch (VariavelNaoDeclaradaException ee) { System.out.println("Erro: " + ee); ee.printStackTrace(); } } catch (ParseException e) { e.printStackTrace(); System.out.println("Expressoes 2 PLP Parser Version 0.0.1: Encountered errors during parse."); } } } PARSER_END(Exp2Parser) SKIP : /* WHITE SPACE */ { " " | "\t" | "\n" | "\r" | "\f" } SPECIAL_TOKEN : /* COMMENTS */ { < SINGLE_LINE_COMMENT : "//" (~[ "\n", "\r" ])* ( "\n" | "\r" | "\r\n" ) > | < FORMAL_COMMENT : "/**" (~[ "*" ])* "*" ( "*" | ( ~[ "*", "/" ] (~[ "*" ])* "*" ) )* "/" > | < MULTI_LINE_COMMENT : "/*" (~[ "*" ])* "*" ( "*" | ( ~[ "*", "/" ] (~[ "*" ])* "*" ) )* "/" > } TOKEN : /* TOKENS DE EXPRESSOES 2 */ { < AND : "and" > | < OR : "or" > | < NOT : "not" > | < LENGTH : "length" > | < TRUE : "true" > | < FALSE : "false" > | < LET : "let" > | < VAR : "var" > | < IN : "in" > } TOKEN : /* LITERALS */ { < INTEGER_LITERAL : < DECIMAL_LITERAL > ([ "l", "L" ])? | < HEX_LITERAL > ([ "l", "L" ])? | < OCTAL_LITERAL > ([ "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 > ( < LETTER > | < DIGIT > )* > | < #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 */ { < 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() < EOF > { return retorno; } } Valor PValorInteiro() : { Token token; } { token = < INTEGER_LITERAL > { return new ValorInteiro(Integer.parseInt(token.toString())); } } Valor PValorBooleano() : {} { < FALSE > { return new ValorBooleano(false); } | < TRUE > { return new ValorBooleano(true); } } Valor PValorString() : { Token token; } { token = < STRING_LITERAL > { 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; } } Id PId() : { Token token; } { token = < IDENTIFIER > { String tokenStr = token.toString(); // tokenStr = tokenStr.substring(1,tokenStr.length()-1); return new Id(tokenStr); } } Expressao PExpMenos() : { Expressao retorno; } { < MINUS > retorno = PExpPrimaria() { return new ExpMenos(retorno); } } Expressao PExpNot() : { Expressao retorno; } { < NOT > retorno = PExpPrimaria() { return new ExpNot(retorno); } } Expressao PExpLength() : { Expressao retorno; } { < LENGTH > retorno = PExpPrimaria() { if (retorno instanceof ValorString) { ValorString val = (ValorString) retorno; } return new ExpLength(retorno); } } Expressao PExpPrimaria() : { Expressao retorno; } { ( retorno = PValor() | retorno = PId() | < LPAREN > retorno = PExpressao() < RPAREN > ) { return retorno; } } DecVariavel PDecVariavel() : { Id id; Expressao exp; DecVariavel retorno; } { id = PId() exp = PExpressao() {retorno = new DecVariavel(id, exp);} { return retorno; } } DecComposta PDecComposta(): { Declaracao d1; Declaracao d2; } { d1 = PDecVariavel() d2 = PDeclaracao() {return new DecComposta(d1, d2);} } Declaracao PDeclaracao(): { Declaracao retorno; } { ( LOOKAHEAD(PDecVariavel() ) retorno = PDecComposta() | retorno = PDecVariavel() | retorno = PDeclaracao() ) {return retorno;} } Expressao PExpDeclaracao() : { Declaracao declaracao; Expressao expressao; } { < LET > declaracao = PDeclaracao() < IN > expressao = PExpressao() { return new ExpDeclaracao(declaracao, expressao); } } Expressao PExpUnaria() : { Expressao retorno; } { ( retorno = PExpMenos() | retorno = PExpNot() | retorno = PExpLength() | retorno = PExpPrimaria() | retorno = PExpDeclaracao() ) { return retorno; } } Expressao PExpBinaria() : { Expressao retorno, param2; } { ( retorno = PExpBinaria2() ( < EQ > param2 = PExpBinaria2() { retorno = new ExpEquals(retorno, param2); } )* ) { return retorno; } } Expressao PExpBinaria2() : { Expressao retorno, param2; } { ( retorno = PExpBinaria3() ( < PLUS > param2 = PExpBinaria3() { retorno = new ExpSoma(retorno, param2); } | < MINUS > param2 = PExpBinaria3() { retorno = new ExpSub(retorno, param2); } | < OR > param2 = PExpBinaria3() { retorno = new ExpOr(retorno, param2); } | < CONCAT > param2 = PExpBinaria3() { retorno = new ExpConcat(retorno, param2); } )* ) { return retorno; } } Expressao PExpBinaria3() : { Expressao retorno, param2; } { ( retorno = PExpUnaria() ( < AND > param2 = PExpUnaria() { retorno = new ExpAnd(retorno, param2); } )* ) { return retorno; } } Expressao PExpressao() : { Expressao retorno; } { retorno = PExpBinaria() { return retorno; } } Programa PPrograma() : { Expressao retorno; } { retorno = PExpressao() { return new Programa(retorno); } }