Skip to content

Instantly share code, notes, and snippets.

@diegozr1
Last active September 14, 2017 07:56
Show Gist options
  • Save diegozr1/df43abcf97a12dd303a2ff36a856619d to your computer and use it in GitHub Desktop.
Save diegozr1/df43abcf97a12dd303a2ff36a856619d to your computer and use it in GitHub Desktop.
A new language written in JavaCC for kids
PARSER_BEGIN(analizadorPESP)
class analizadorPESP {
public static void main(String[] args) throws ParseException, TokenMgrError
{
try {
analizadorPESP ae = new analizadorPESP(System.in);
ae.boot();
}catch(ParseException e){
System.out.println(" === Errores lexicos encontrados en codigo fuente PESP === \n "+e.getMessage());
}catch(TokenMgrError e){
System.out.println (" ERROR EN TOKEN.");
System.out.println (e.getMessage());
}
}
}
PARSER_END(analizadorPESP)
/* DEFINICION DE ESTRUCTURAS */
TOKEN: {
<PROGRAMA: "clase">{System.out.println("CLASE PRINCIPAL> "+image+"\n");}
|<FUNCION: "func" >{ System.out.println("\nFUNCION -> "+image+"\n");}
|<IF: "si" > { System.out.println("\nDECISION \t-> "+image);}
|<ELSE: "sino" > { System.out.println("\nDECISION NEGADO -> "+image);}
|<WHILE: "mientras" > {System.out.println("\nCICLO -> "+image);}
|<RETURN: "retorna" > {System.out.println("\nDEVUELVE VALOR -> "+image);}
}
/* DEFINICION DE AGRUPACIONES */
TOKEN: {
<PARENIZQ: "(" >{ System.out.println("ABRE GRUPO -> " +image);}
|<PARENDER: ")">{ System.out.println("CIERRA GRUPO -> " + image);}
|<LLAVEIZQ: "{">{ System.out.println("\nABRE BLOQUE -> " + image+"\n");}
|<LLAVEDER: "}">{ System.out.println("\nCIERRA BLOQUE -> " + image+"\n");}
}
/* DEFINICION DE ASIGNACION Y COMPARACIONES*/
TOKEN: {
<ASIGNACION: "=" >{ System.out.println("ASIGNACION -> "+image);}
|<MENOR: "<">{ System.out.println("MENOR QUE -> "+image);}
|<MAYOR: ">">{ System.out.println("MAYOR QUE -> "+image);}
|<NEGACION: "!">{ System.out.println("NEGACION -> "+image);}
|<IGUALDAD: "==">{ System.out.println("IGUALDAD -> "+image);}
|<COMA: ",">{ System.out.println("COMA -> "+image);}
|<SEMICOLON: ";">{ System.out.println("CIERRE DE SENTENCIA -> " + image+"\n");}
}
/* DEFINICION DE IDENTIFICADORES */
TOKEN: {
<INT: "entero" >{ System.out.println("ENTERO -> "+image);}
|<STRING: "cadena" >{ System.out.println("CADENA -> "+image);}
|<BOOLEAN: "booleano" > { System.out.println("BOOLEANO - >"+image);}
|<NUMBER: (["0" - "9"])+>{ System.out.println("NUMERO -> "+image);}
|<TEXT: "\"" ( ~["\"","\\","\n","\r"] | "\\" ( ["n","t","b","r","f","\\","\'","\""] | ( ["\n","\r"] | "\r\n")))* "\""> { System.out.println("CADENA DE TEXTO -> "+image); }
|<VALBOOL: "true" | "false" | "-1" > { System.out.println("VALOR BOOLEANO - >"+image);}
|<IDENTIFIER: (["a"-"z", "A"-"Z", "0"-"9"])+ >{ System.out.println("IDENTIFICADOR -> "+image);}
}
/* CARACTERES A IGNORAR */
SKIP: { " " |"\t" |"\n" |"\r" | <"//" (~["\n","\r"])* ("\n" | "\r" | "\r\n")> {System.out.println("= COMENTARIO IGNORADO = ");} }
void boot () : {}
{
<PROGRAMA><IDENTIFIER>
<LLAVEIZQ>
funcionNormal()
<LLAVEDER><EOF>
}
void funcionNormal(): { }
{
<FUNCION> <IDENTIFIER> <PARENIZQ> <PARENDER > < LLAVEIZQ >sentencias()<LLAVEDER >
}
void funcionGenerica(): {}
{
<FUNCION><IDENTIFIER><PARENIZQ> parametros() <PARENDER > < LLAVEIZQ >sentencias()<LLAVEDER >
}
void parametros(): { }
{
< IDENTIFIER > pr()
}
void pr(): { } {
<COMA > < IDENTIFIER >
}
void sentencias(): { }
{
(
declaraciones()
| estructuraDecision()
| estructuraRepetitiva()
| retornaValor()
)*
}
void declaraciones():{}
{
<INT> <IDENTIFIER> <ASIGNACION> <NUMBER> DR() |
<STRING> <IDENTIFIER> <ASIGNACION> <TEXT> DR() |
<BOOLEAN> <IDENTIFIER> <ASIGNACION> <VALBOOL> DR()
}
void DR(): { }
{
declaraciones() | sentencias() | estructuraRepetitiva() | estructuraDecision()
}
void estructuraDecision(): { }
{
< IF ><PARENIZQ > comparaciones() <PARENDER ><LLAVEIZQ> <LLAVEDER >
}
void estructuraRepetitiva() : { }
{
< WHILE ><PARENIZQ > comparaciones() < PARENDER ><LLAVEIZQ > <LLAVEDER >
}
void retornaValor():{}
{
<RETURN><IDENTIFIER>
}
void comparaciones(): { }
{
valor() operadores() valor()
}
void valor(): { }
{
< IDENTIFIER > | < NUMBER > | < TEXT > | < BOOLEAN >
}
void operadores(): {}{
< MENOR > | < MAYOR > | < NEGACION > | < IGUALDAD >
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment