Ejemplo de reconocedor de expresiones aritméticas, con árbol de parsing /*Opciones de personalización*/ options { LOOKAHEAD = 1; CHOICE_AMBIGUITY_CHECK = 2; OTHER_AMBIGUITY_CHECK = 1; STATIC = true; DEBUG_PARSER = false; DEBUG_LOOKAHEAD = false; DEBUG_TOKEN_MANAGER = false; ERROR_REPORTING = true; JAVA_UNICODE_ESCAPE = false; UNICODE_INPUT = false; IGNORE_CASE = false; USER_TOKEN_MANAGER = false; USER_CHAR_STREAM = false; BUILD_PARSER = true; BUILD_TOKEN_MANAGER = true; SANITY_CHECK = true; FORCE_LA_CHECK = false; } /*unidad de compilación*/ PARSER_BEGIN(eajjt) public class eajjt { public static void main(String args[]) { eajjt t = new eajjt(System.in); try { SimpleNode n = t.Start(); /*método para indicar el nivel del árbol*/ n.dump(">"); System.out.println("Muchas Gracias."); } catch (Exception e) { System.out.println("Oops."); System.out.println(e.getMessage()); e.printStackTrace(); } } } PARSER_END(eajjt) /*Definición de tokens*/ TOKEN : { } /*Métodos correspondientes a las producciones*/ SimpleNode Start() : {} { Exp() ("\n"|"\r")* {return jjtThis;} /*acción para mostrar el árbol*/ } void Exp() : {} { Term() ["+" Exp()] } void Term() : {} { Fact() TT() } void TT() : {} { ["*" Fact() TT()] } void Fact() : {} { ("(" Exp() ")") | }