Skip to content

Instantly share code, notes, and snippets.

@thpoiani
Last active March 4, 2016 22:03
Show Gist options
  • Save thpoiani/1aa0400de7b6ba132b59 to your computer and use it in GitHub Desktop.
Save thpoiani/1aa0400de7b6ba132b59 to your computer and use it in GitHub Desktop.
options
{
static = false;
LOOKAHEAD = 2;
JDK_VERSION = "1.7";
IGNORE_CASE = false;
ERROR_REPORTING = true;
}
PARSER_BEGIN(AnalisadorLexico)
import java.io.*;
public class AnalisadorLexico{
public static void main(String[] args) throws ParseException, TokenMgrError {
AnalisadorLexico analisadorLexico = new AnalisadorLexico(System.in);
while (true) {
try {
// analisadorLexico.bc();
analisadorLexico.parser();
}
// catch (ParseException e) {
// System.out.println("> Erro Sintático");
// System.out.println(e.getMessage());
// System.exit(-1);
// }
catch (TokenMgrError e) {
System.out.println("> Erro Léxico");
System.out.println(e.getMessage());
System.exit(-1);
}
}
}
}
PARSER_END(AnalisadorLexico)
////////////////////////////////////////////////////////////////////////////////
TOKEN_MGR_DECLS :
{
static int commentNesting = 0;
}
SKIP :
{
" " | "\t"
}
SKIP : /* COMMENTS */
{
"/*" { commentNesting++; } : IN_COMMENT
| "#" : IN_SINGLE_LINE_COMMENT
}
<IN_SINGLE_LINE_COMMENT> SPECIAL_TOKEN :
{
<SINGLE_LINE_COMMENT: "\n" | "\r" | "\r\n" > : DEFAULT
}
<IN_SINGLE_LINE_COMMENT> MORE :
{
< ~[] >
}
<IN_COMMENT> SKIP :
{
"/*" { commentNesting++; }
| "*/" { commentNesting--;
if (commentNesting == 0)
SwitchTo(DEFAULT);
}
| <~[]>
}
TOKEN : /* Palavras Reservadas */
{
< IBASE : "ibase" >
| < OBASE : "obase" >
| < SCALE : "scale" >
| < LAST : "last" >
| < LENGTH : "length" >
| < SQRT : "sqrt" >
| < READ : "read" >
| < DEFINE : "define" >
| < AUTO : "auto" >
| < PRINT : "print" >
| < WHILE : "while" >
| < FOR : "for" >
| < IF : "if" >
| < ELSE : "else" >
| < WARRANTY : "warranty" >
| < LIMITS : "limits" >
| < BREAK : "break" >
| < CONTINUE : "continue" >
| < QUIT : "quit" >
| < HALT : "halt" >
| < RETURN : "return" >
| < LPAREN : "(" >
| < RPAREN : ")" >
| < LBRACKET : "[" >
| < RBRACKET : "]" >
| < LBRACE : "{" >
| < RBRACE : "}" >
| < DOT : "." >
| < COMMA : "," >
| < SEMICOLON : ";" >
| < EOL : ( "\n" | "\r" | "\r\n" ) >
}
TOKEN : /* Operadores */
{
< REL_OP : ( ">" | "<" | "==" | "<=" | ">=" | "!=" ) >
| < ASSIGN_OP : ( "=" | "+=" | "-=" | "*=" | "/=" | "%=" | "^=" ) >
| < PLUS : ( "+" ) >
| < MINUS : "-" >
| < TIMES : "*" >
| < DIVIDE : "/" >
| < MODULE : "%" >
| < EXP_OP : "^" >
| < PLUS_PLUS : "++" >
| < MINUS_MINUS : "--" >
| < AND_OP : "&&" >
| < OR_OP : "||" >
| < NOT_OP : "!" >
}
TOKEN : /* Caracteres, Números, Strings e Identificadores */
{
< STRING : "\"" ( <CHAR> )* "\"" >
| < NUMBER : (( <DIGITS> )( <DIGITS> )*( "." ( <DIGITS> )* )? | "." <DIGITS> ( <DIGITS> )*) >
| < NAME : ( <LETTER> )( <DIGIT> | <LETTER> | "_" )* >
| < #DIGIT : [ "0"-"9" ] >
| < #DIGITS : [ "0"-"9", "A"-"F" ] >
| < #LETTER: [ "a"-"z", "A"-"Z" ] >
| < #CHAR : ~["\""] >
}
////////////////////////////////////////////////////////////////////////////////
void parser() throws TokenMgrError, ParseException :
{
Token t;
}
{
t=< IBASE > { System.out.printf("@(%3d,%3d)\t"+String.format("%-15s - ", "IBASE")+t.toString()+"\n", t.beginLine, t.beginColumn); }
| t=< OBASE > { System.out.printf("@(%3d,%3d)\t"+String.format("%-15s - ", "OBASE")+t.toString()+"\n", t.beginLine, t.beginColumn); }
| t=< SCALE > { System.out.printf("@(%3d,%3d)\t"+String.format("%-15s - ", "SCALE")+t.toString()+"\n", t.beginLine, t.beginColumn); }
| t=< LAST > { System.out.printf("@(%3d,%3d)\t"+String.format("%-15s - ", "LAST")+t.toString()+"\n", t.beginLine, t.beginColumn); }
| t=< LENGTH > { System.out.printf("@(%3d,%3d)\t"+String.format("%-15s - ", "LENGTH")+t.toString()+"\n", t.beginLine, t.beginColumn); }
| t=< SQRT > { System.out.printf("@(%3d,%3d)\t"+String.format("%-15s - ", "SQRT")+t.toString()+"\n", t.beginLine, t.beginColumn); }
| t=< READ > { System.out.printf("@(%3d,%3d)\t"+String.format("%-15s - ", "READ")+t.toString()+"\n", t.beginLine, t.beginColumn); }
| t=< DEFINE > { System.out.printf("@(%3d,%3d)\t"+String.format("%-15s - ", "DEFINE")+t.toString()+"\n", t.beginLine, t.beginColumn); }
| t=< AUTO > { System.out.printf("@(%3d,%3d)\t"+String.format("%-15s - ", "AUTO")+t.toString()+"\n", t.beginLine, t.beginColumn); }
| t=< PRINT > { System.out.printf("@(%3d,%3d)\t"+String.format("%-15s - ", "PRINT")+t.toString()+"\n", t.beginLine, t.beginColumn); }
| t=< WHILE > { System.out.printf("@(%3d,%3d)\t"+String.format("%-15s - ", "WHILE")+t.toString()+"\n", t.beginLine, t.beginColumn); }
| t=< FOR > { System.out.printf("@(%3d,%3d)\t"+String.format("%-15s - ", "FOR")+t.toString()+"\n", t.beginLine, t.beginColumn); }
| t=< IF > { System.out.printf("@(%3d,%3d)\t"+String.format("%-15s - ", "IF")+t.toString()+"\n", t.beginLine, t.beginColumn); }
| t=< ELSE > { System.out.printf("@(%3d,%3d)\t"+String.format("%-15s - ", "ELSE")+t.toString()+"\n", t.beginLine, t.beginColumn); }
| t=< WARRANTY > { System.out.printf("@(%3d,%3d)\t"+String.format("%-15s - ", "WARRANTY")+t.toString()+"\n", t.beginLine, t.beginColumn); }
| t=< LIMITS > { System.out.printf("@(%3d,%3d)\t"+String.format("%-15s - ", "LIMITS")+t.toString()+"\n", t.beginLine, t.beginColumn); }
| t=< BREAK > { System.out.printf("@(%3d,%3d)\t"+String.format("%-15s - ", "BREAK")+t.toString()+"\n", t.beginLine, t.beginColumn); }
| t=< CONTINUE > { System.out.printf("@(%3d,%3d)\t"+String.format("%-15s - ", "CONTINUE")+t.toString()+"\n", t.beginLine, t.beginColumn); }
| t=< QUIT > { System.out.printf("@(%3d,%3d)\t"+String.format("%-15s - ", "QUIT")+t.toString()+"\n", t.beginLine, t.beginColumn); }
| t=< HALT > { System.out.printf("@(%3d,%3d)\t"+String.format("%-15s - ", "HALT")+t.toString()+"\n", t.beginLine, t.beginColumn); }
| t=< RETURN > { System.out.printf("@(%3d,%3d)\t"+String.format("%-15s - ", "RETURN")+t.toString()+"\n", t.beginLine, t.beginColumn); }
| t=< LPAREN > { System.out.printf("@(%3d,%3d)\t"+String.format("%-15s - ", "LPAREN")+t.toString()+"\n", t.beginLine, t.beginColumn); }
| t=< RPAREN > { System.out.printf("@(%3d,%3d)\t"+String.format("%-15s - ", "RPAREN")+t.toString()+"\n", t.beginLine, t.beginColumn); }
| t=< LBRACKET > { System.out.printf("@(%3d,%3d)\t"+String.format("%-15s - ", "LBRACKET")+t.toString()+"\n", t.beginLine, t.beginColumn); }
| t=< RBRACKET > { System.out.printf("@(%3d,%3d)\t"+String.format("%-15s - ", "RBRACKET")+t.toString()+"\n", t.beginLine, t.beginColumn); }
| t=< LBRACE > { System.out.printf("@(%3d,%3d)\t"+String.format("%-15s - ", "LBRACE")+t.toString()+"\n", t.beginLine, t.beginColumn); }
| t=< RBRACE > { System.out.printf("@(%3d,%3d)\t"+String.format("%-15s - ", "RBRACE")+t.toString()+"\n", t.beginLine, t.beginColumn); }
| t=< COMMA > { System.out.printf("@(%3d,%3d)\t"+String.format("%-15s - ", "COMMA")+t.toString()+"\n", t.beginLine, t.beginColumn); }
| t=< SEMICOLON > { System.out.printf("@(%3d,%3d)\t"+String.format("%-15s - ", "SEMICOLON")+t.toString()+"\n", t.beginLine, t.beginColumn); }
| t=< TIMES > { System.out.printf("@(%3d,%3d)\t"+String.format("%-15s - ", "TIMES")+t.toString()+"\n", t.beginLine, t.beginColumn); }
| t=< EOL > { System.out.printf("@(%3d,%3d)\t"+String.format("%-15s - ", "EOL")+"\\n\n", t.beginLine, t.beginColumn); }
| t=< REL_OP > { System.out.printf("@(%3d,%3d)\t"+String.format("%-15s - ", "REL_OP")+t.toString()+"\n", t.beginLine, t.beginColumn); }
| t=< ASSIGN_OP > { System.out.printf("@(%3d,%3d)\t"+String.format("%-15s - ", "ASSIGN_OP")+t.toString()+"\n", t.beginLine, t.beginColumn); }
| t=< MINUS > { System.out.printf("@(%3d,%3d)\t"+String.format("%-15s - ", "MINUS")+t.toString()+"\n", t.beginLine, t.beginColumn); }
| t=< PLUS > { System.out.printf("@(%3d,%3d)\t"+String.format("%-15s - ", "PLUS")+t.toString()+"\n", t.beginLine, t.beginColumn); }
| t=< DIVIDE > { System.out.printf("@(%3d,%3d)\t"+String.format("%-15s - ", "DIVIDE")+t.toString()+"\n", t.beginLine, t.beginColumn); }
| t=< MODULE > { System.out.printf("@(%3d,%3d)\t"+String.format("%-15s - ", "MODULE")+t.toString()+"\n", t.beginLine, t.beginColumn); }
| t=< EXP_OP > { System.out.printf("@(%3d,%3d)\t"+String.format("%-15s - ", "EXP_OP")+t.toString()+"\n", t.beginLine, t.beginColumn); }
| t=< PLUS_PLUS > { System.out.printf("@(%3d,%3d)\t"+String.format("%-15s - ", "PLUS_PLUS")+t.toString()+"\n", t.beginLine, t.beginColumn); }
| t=< MINUS_MINUS > { System.out.printf("@(%3d,%3d)\t"+String.format("%-15s - ", "MINUS_MINUS")+t.toString()+"\n", t.beginLine, t.beginColumn); }
| t=< AND_OP > { System.out.printf("@(%3d,%3d)\t"+String.format("%-15s - ", "AND_OP")+t.toString()+"\n", t.beginLine, t.beginColumn); }
| t=< OR_OP > { System.out.printf("@(%3d,%3d)\t"+String.format("%-15s - ", "OR_OP")+t.toString()+"\n", t.beginLine, t.beginColumn); }
| t=< NOT_OP > { System.out.printf("@(%3d,%3d)\t"+String.format("%-15s - ", "NOT_OP")+t.toString()+"\n", t.beginLine, t.beginColumn); }
| t=< STRING > { System.out.printf("@(%3d,%3d)\t"+String.format("%-15s - ", "STRING")+t.toString()+"\n", t.beginLine, t.beginColumn); }
| t=< NUMBER > { System.out.printf("@(%3d,%3d)\t"+String.format("%-15s - ", "NUMBER")+t.toString()+"\n", t.beginLine, t.beginColumn); }
| t=< NAME > { System.out.printf("@(%3d,%3d)\t"+String.format("%-15s - ", "NAME")+t.toString()+"\n", t.beginLine, t.beginColumn); }
| t=< EOF > { System.exit(0); }
}
////////////////////////////////////////////////////////////////////////////////
void bc():{}{
(comando() | definicao_de_funcao())+
{ System.exit(0); }
}
void comando():{}{
e()
|
comando_composto()
|
IF()
|
WHILE()
|
FOR()
|
NULL()
|
BREAK()
|
CONTINUE()
|
QUIT()
|
PRINT()
|
STRING()
}
void comando_composto():{}{
<LBRACE> lista_comandos() <RBRACE>
}
void lista_comandos():{}{
comando()(separador() comando())*
}
void separador():{}{
< SEMICOLON > | < EOL >
}
void IF():{}{
< IF > < LPAREN > e() < RPAREN > comando() [ < ELSE > comando() ]
}
void WHILE():{}{
< WHILE > < LPAREN > e() < RPAREN > comando()
}
void FOR():{}{
< FOR > < LPAREN > [e()] < SEMICOLON > [e()] < SEMICOLON > [e()] < RPAREN > comando()
}
void NULL():{}{
< SEMICOLON >
}
void BREAK():{}{
< BREAK >
}
void CONTINUE():{}{
< CONTINUE >
}
void QUIT():{}{
< QUIT >
}
void PRINT():{}{
< PRINT > lista_expressoes()
}
void lista_expressoes():{}{
e() ( < COMMA > e() )*
}
void e():{}{
e1() ( < OR_OP > e1() )*
}
void e1():{}{
e2() ( < AND_OP > e2() )*
}
void e2():{}{
[ < NOT_OP > ] e3()
}
void e3():{}{
e4() [ < REL_OP > e4() ] // TODO - desmembrar
}
void e4():{}{
e5() [ < ASSIGN_OP > e4() ] // TODO - desmembrar
}
void e5():{}{
e6() ( ( < PLUS > | < MINUS > ) e6() )*
}
void e6():{}{
e7() ( ( < TIMES > | < DIVIDE > | < MODULE > ) e7() )*
}
void e7():{}{
e8() [ < EXP_OP > e7()]
}
void e8():{}{
[ < MINUS > ] e9()
}
void e9():{}{
[ < PLUS_PLUS > | < MINUS_MINUS > ] e10() | e10() [ < PLUS_PLUS > | < MINUS_MINUS > ]
}
void e10():{}{
< NUMBER >
| < LPAREN > e() < RPAREN >
| < SQRT > < LPAREN > e() < RPAREN >
| < LENGTH > < LPAREN > e() < RPAREN >
| < SCALE > < LPAREN > e() < RPAREN >
| chamada_funcao()
| id()
| id() < LBRACKET > e() < RBRACKET >
| < IBASE >
| < OBASE >
| < SCALE >
| < LAST >
| < DOT >
}
void chamada_funcao():{}{
id() < LPAREN > [ lista_expressoes() ] < RPAREN >
}
void definicao_de_funcao():{}{
< DEFINE > id() < LPAREN > [ lista_var() ] < RPAREN > < LBRACE > < AUTO > lista_var() lista_comandos() [ retorno() ] < RBRACE >
}
void retorno():{}{
< RETURN > < LPAREN > [ e() ] < RPAREN > | < RETURN > < LPAREN > e() < RPAREN >
}
void lista_var():{}{
id() [ < LBRACKET > < RBRACKET > ]( < COMMA > id() [ < LBRACKET > < RBRACKET > ] )*
}
void STRING():{}{
< STRING >
}
void id():{}{
< NAME >
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment