Created
November 29, 2017 17:10
-
-
Save expalmer/f6c68243ba1da3db033da3a9a8e0790e to your computer and use it in GitHub Desktop.
Parser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
%% | |
%byaccj | |
%{ | |
// Armazena uma referencia para o parser | |
private Parser yyparser; | |
// Construtor recebendo o parser como parametro adicional | |
public Yylex(Reader r, Parser yyparser){ | |
this(r); | |
this.yyparser = yyparser; | |
} | |
%} | |
NL = \n | \r | \r\n | |
%% | |
incluir { return Parser.INCLUDE; } | |
para { return Parser.FOR; } | |
se { return Parser.IF; } | |
senao { return Parser.ELSE; } | |
retornar { return Parser.RETURN; } | |
\<.*\> { | |
yyparser.yylval = new ParserVal(yytext()); | |
return Parser.LIB; | |
} | |
\/\/.* { | |
yyparser.yylval = new ParserVal(yytext()); | |
return Parser.COMMENT; | |
} | |
\/\*.*\*\/ { | |
yyparser.yylval = new ParserVal(yytext()); | |
return Parser.COMMENT; | |
} | |
inteiro { return Parser.INT; } | |
real { return Parser.DOUBLE; } | |
caracter { return Parser.CHAR; } | |
funcao_principal { return Parser.MAIN; } | |
funcao { return Parser.FUNCTION; } | |
enquanto { return Parser.WHILE; } | |
faca { return Parser.DO; } | |
ate { return Parser.DOWHILE; } | |
caso { return Parser.SWITCH; } | |
opcao { return Parser.CASE; } | |
fim_opcao { return Parser.BREAK; } | |
imprima { return Parser.PRINT; } | |
";" { return Parser.PV; } | |
":" { return Parser.PP; } | |
"," { yyparser.yylval = new ParserVal(yytext()); return Parser.V; } | |
// valores | |
[a-zA-Z][a-zA-Z0-9]* { | |
yyparser.yylval = new ParserVal(yytext()); | |
return Parser.ID; | |
} | |
[0-9]+ { | |
yyparser.yylval = new ParserVal(yytext()); | |
return Parser.INT_VALUE; | |
} | |
\".*\"|\'.*\' { | |
yyparser.yylval = new ParserVal(yytext()); | |
return Parser.CHAR_VALUE; | |
} | |
":=" { yyparser.yylval = new ParserVal(yytext()); return Parser.EQUALS; } | |
"(" { yyparser.yylval = new ParserVal(yytext()); return Parser.PAR_INI; } | |
")" { yyparser.yylval = new ParserVal(yytext()); return Parser.PAR_END; } | |
"[" { yyparser.yylval = new ParserVal(yytext()); return Parser.BRA_INI; } | |
"]" { yyparser.yylval = new ParserVal(yytext()); return Parser.BRA_END; } | |
"{" { yyparser.yylval = new ParserVal(yytext()); return Parser.KEY_INI; } | |
"}" { yyparser.yylval = new ParserVal(yytext()); return Parser.KEY_END; } | |
"++" { yyparser.yylval = new ParserVal(yytext()); return Parser.INCR; } | |
"--" { yyparser.yylval = new ParserVal(yytext()); return Parser.DECR; } | |
"&&" { yyparser.yylval = new ParserVal(yytext()); return Parser.OP_AND; } | |
"||" { yyparser.yylval = new ParserVal(yytext()); return Parser.OP_OR; } | |
"<=" { yyparser.yylval = new ParserVal(yytext()); return Parser.OP_LE; } | |
">=" { yyparser.yylval = new ParserVal(yytext()); return Parser.OP_GE; } | |
"==" { yyparser.yylval = new ParserVal(yytext()); return Parser.OP_EQ; } | |
"!=" { yyparser.yylval = new ParserVal(yytext()); return Parser.OP_NE; } | |
"-" { yyparser.yylval = new ParserVal(yytext()); return Parser.OP_SUB; } | |
"+" { yyparser.yylval = new ParserVal(yytext()); return Parser.OP_SUM; } | |
"*" { yyparser.yylval = new ParserVal(yytext()); return Parser.OP_MUL; } | |
"/" { yyparser.yylval = new ParserVal(yytext()); return Parser.OP_DIV; } | |
"%" { yyparser.yylval = new ParserVal(yytext()); return Parser.OP_PER; } | |
"<" { yyparser.yylval = new ParserVal(yytext()); return Parser.OP_LT; } | |
">" { yyparser.yylval = new ParserVal(yytext()); return Parser.OP_GT; } | |
/* New Line */ | |
{NL}|" "|\t { } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%{ | |
import java.io.*; | |
import java.util.*; | |
%} | |
/* BYACC Declarations */ | |
%token <sval> ID | |
%token <sval> LIB | |
%token INCLUDE | |
%token MAIN | |
%token FUNCTION | |
%token FOR | |
%token IF | |
%token ELSE | |
%token WHILE | |
%token DO | |
%token DOWHILE | |
%token SWITCH | |
%token CASE | |
%token BREAK | |
%token PRINT | |
%token <sval> COMMENT | |
%token <sval> INCR | |
%token <sval> DECR | |
%token <sval> INT_VALUE | |
%token <sval> CHAR_VALUE | |
%token EQUALS | |
%token INT | |
%token DOUBLE | |
%token CHAR | |
%token RETURN | |
%token V | |
%token PV | |
%token PP | |
%token OP_AND | |
%token OP_OR | |
%token OP_LE | |
%token OP_GE | |
%token OP_EQ | |
%token OP_NE | |
%token OP_SUB | |
%token OP_SUM | |
%token OP_MUL | |
%token OP_DIV | |
%token OP_PER | |
%token OP_LT | |
%token OP_GT | |
%token PAR_INI | |
%token PAR_END | |
%token BRA_INI | |
%token BRA_END | |
%token KEY_INI | |
%token KEY_END | |
%type <sval> program | |
%type <sval> include | |
%type <sval> main | |
%type <sval> declaration | |
%type <sval> attribuition | |
%type <sval> expression | |
%type <sval> operator | |
%type <sval> comment | |
%type <sval> type | |
%type <sval> array | |
%type <sval> var | |
%type <sval> value | |
%type <sval> increment | |
%type <sval> print | |
%type <sval> function | |
%type <sval> function_args | |
%type <sval> function_param | |
%type <sval> function_return | |
%type <sval> function_call | |
%type <sval> it_for | |
%type <sval> it_if | |
%type <sval> it_else | |
%type <sval> it_while | |
%type <sval> it_dowhile | |
%type <sval> it_switch | |
%type <sval> it_case | |
%% | |
init | |
: program { System.out.println($1); } | |
program | |
: { $$ = ""; } | |
| include program { $$ = $1 + "\n" + $2; } | |
| main program { $$ = $1 + "\n" + $2; } | |
| comment program { $$ = $1 + "\n" + $2; } | |
| function program { $$ = $1 + "\n" + $2; } | |
| function_return program { $$ = $1 + "\n" + $2; } | |
| declaration program { $$ = $1 + "\n" + $2; } | |
| attribuition program { $$ = $1 + "\n" + $2; } | |
| print program { $$ = $1 + "\n" + $2; } | |
| it_for program { $$ = $1 + "\n" + $2; } | |
| it_if program { $$ = $1 + "\n" + $2; } | |
| it_while program { $$ = $1 + "\n" + $2; } | |
| it_dowhile program { $$ = $1 + "\n" + $2; } | |
| it_switch program { $$ = $1 + "\n" + $2; } | |
include | |
: INCLUDE LIB { $$ = "#include " + $2; } | |
comment | |
: COMMENT { $$ = $1; } | |
declaration | |
: type ID array { $$ = $1 + $2 + $3 + ";"; } | |
attribuition | |
: increment { $$ = $1 + ";"; } | |
| var EQUALS expression { $$ = $1 + " = " + $3 + ";"; } | |
increment | |
: var INCR { $$ = $1 + "++"; } | |
| var DECR { $$ = $1 + "--"; } | |
expression | |
: value { $$ = $1; } | |
| value operator expression { $$ = $1 + " " + $2 + " " + $3; } | |
| PAR_INI expression PAR_END { $$ = "(" + $2 + ")"; } | |
operator | |
: OP_AND { $$ = "&&"; } | |
| OP_OR { $$ = "||"; } | |
| OP_LE { $$ = "<="; } | |
| OP_GE { $$ = ">="; } | |
| OP_LT { $$ = "<"; } | |
| OP_GT { $$ = ">"; } | |
| OP_EQ { $$ = "=="; } | |
| OP_NE { $$ = "!="; } | |
| OP_SUB { $$ = "-"; } | |
| OP_SUM { $$ = "+"; } | |
| OP_MUL { $$ = "*"; } | |
| OP_DIV { $$ = "/"; } | |
| OP_PER { $$ = "%"; } | |
type | |
: INT { $$ = "int "; } | |
| DOUBLE { $$ = "double "; } | |
| CHAR { $$ = "char "; } | |
array | |
: { $$ = ""; } | |
| BRA_INI ID BRA_END { $$ = "[" + $2 +"]"; } | |
value | |
: var { $$ = $1; } | |
| INT_VALUE { $$ = $1; }; | |
| CHAR_VALUE { $$ = $1; }; | |
| function_call | |
var | |
: ID array { $$ = $1 + $2; } | |
: PRINT PAR_INI CHAR_VALUE V value PAR_END { $$ = "printf(" + $3 + ", " + $5 + ");"; } | |
main | |
: MAIN KEY_INI program KEY_END { $$ = "int main() {\n" + $3 + "\n}"; } | |
function | |
: FUNCTION type ID PAR_INI function_param PAR_END KEY_INI program KEY_END { $$ = $2 + $3 + "(" + $5 + "){\n" + $8 + "\n}"; } | |
function_param | |
: { $$ = ""; } | |
| type ID array { $$ = $1 + $2 + $3; } | |
function_call | |
: ID PAR_INI function_args PAR_END { $$ = $1 + "(" + $3 + ")"; } | |
function_args | |
: { $$ = ""; } | |
| value { $$ = $1; } | |
function_return | |
: { $$ = ""; } | |
| RETURN expression { $$ = "return " + $2 + ";"; } | |
it_for | |
: FOR PAR_INI attribuition PV expression PV increment PAR_END KEY_INI program KEY_END { $$ = "for (" + $3 + $5 + ";" + $7 + ") {\n " + $10 + "\n}"; } | |
it_if | |
: IF PAR_INI expression PAR_END KEY_INI program KEY_END it_else { $$ = "if (" + $3 + ") {\n" + $6 + "\n}" + $8; } | |
it_else | |
: { $$ = ""; } | |
| ELSE KEY_INI program KEY_END { $$ = " else {\n" + $3 + "\n}"; } | |
it_while | |
: WHILE PAR_INI expression PAR_END KEY_INI program KEY_END { $$ = "while (" + $3 + ") {\n" + $6 + "\n}"; } | |
it_dowhile | |
: DO KEY_INI program KEY_END DOWHILE PAR_INI expression PAR_END { $$ = "do {\n" + $3 + "\n} while(" + $7 + ");"; } | |
it_switch | |
: SWITCH PAR_INI var PAR_END KEY_INI it_case KEY_END { $$ = "switch(" + $3 + "){\n" + $6 + "\n}"; } | |
it_case | |
: { $$ = ""; } | |
| CASE value PP program BREAK it_case { $$ = "case " + $2 + ":\n" + $4 + "\nbreak;\n" + $6; } | |
%% | |
// Referencia ao JFlex | |
private Yylex lexer; | |
/* Interface com o JFlex */ | |
private int yylex(){ | |
int yyl_return = -1; | |
try { | |
yyl_return = lexer.yylex(); | |
} catch (IOException e) { | |
System.err.println("Erro de IO: " + e); | |
} | |
return yyl_return; | |
} | |
/* Reporte de erro */ | |
public void yyerror(String error){ | |
System.err.println("Error: " + error); | |
} | |
// Interface com o JFlex eh criado no construtor | |
public Parser(Reader r){ | |
lexer = new Yylex(r, this); | |
} | |
// Main | |
public static void main(String[] args){ | |
try{ | |
Parser yyparser = new Parser(new FileReader(args[0])); | |
yyparser.yyparse(); | |
} catch (IOException ex) { | |
System.err.println("Error: " + ex); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment