Sei sulla pagina 1di 8

INSTITUTO TECNOLOGICO DE PACHUCA

Pgina |1

JavaCC
Instalacin y Compilacin

- INSTALACIN
Lo primero que tenemos que hacer para poder ocupar la herramienta javaCC es descargarlo e instalarlo. Dado que es software libre lo podemos descargar sin costo alguno. 1. Lo primero que debemos hacer es buscar la opcin javacc 5.0. rar ya que existe tambin el .tar pero ese trabaja bajo la plataforma Linux en la pagina: http://java.net/project/javacc/downloads

2. Una vez descargado lo abrimos y descomprimimos en una carpeta que crearemos en el disco local C llamada phhp

PROGRAMACIN DE SISTEMAS

INSTITUTO TECNOLOGICO DE PACHUCA

Pgina |2

3. Una vez creada nuestra carpeta solo tenemos que agregar esta herramienta copiado la direccin del javaCC y agregando la en nuestro path:

4. Listo, con esto ya tenemos agregada nuestra herramienta JavaCC

- COMPILACIN
Como nuestro ejemplo realizaremos una calculadora lo que tenemos que hacer es abrir un block de notas y escribir nuestro cdigo en java a continuacin se mostrara un ejemplo que nos creara dicha calculadora:
options { LOOKAHEAD=1; } PARSER_BEGIN(Calc3i) public class Calc3i { static double total; static java.util.Stack argStack = new java.util.Stack(); public static void main(String args[]) throws ParseException { Calc3i parser = new Calc3i(System.in); while (true) { System.out.print("Enter Expression: "); System.out.flush(); try { switch (parser.one_line()) { case -1: System.exit(0);

PROGRAMACIN DE SISTEMAS

INSTITUTO TECNOLOGICO DE PACHUCA


case 0: break; case 1: double x = ((Double) argStack.pop()).doubleValue(); System.out.println("Total = " + x); break; } } catch (ParseException x) { System.out.println("Exiting."); throw x; } } } } PARSER_END(Calc3i) SKIP : { | | } "" "\r" "\t"

Pgina |3

TOKEN : { < EOL: "\n" > } TOKEN : /* OPERATORS */ { < PLUS: "+" > | < MINUS: "-" > | < MULTIPLY: "*" > | < DIVIDE: "/" > | < EXP: "**" > | < AND: "&" > | < OR: "|" > | < XOR: "^" > } TOKEN : /* numeric constants */ { < CONSTANT: <FLOAT> | <FLOAT> ( ["e","E"] ([ "-","+"])? <INTEGER> )? > | < #FLOAT: <INTEGER> | <INTEGER> ( "." <INTEGER> )? | "." <INTEGER> > | < #INTEGER: ( <DIGIT> )+ > | < #DIGIT: ["0" - "9"] > } TOKEN : /* Function names */ { < ID: ( <LETTER> )+ ( <DIGIT> | <LETTER> )* >

PROGRAMACIN DE SISTEMAS

INSTITUTO TECNOLOGICO DE PACHUCA


| } int one_line() : {} { logical() <EOL> { return 1; } | <EOL> { return 0; } | <EOF> { return -1; } } void logical() : {Token x;} { sum() ( ( x = <AND> | x = <OR> | x = <XOR> ) sum() { long a = ((Double) argStack.pop()).longValue(); long b = ((Double) argStack.pop()).longValue(); switch (x.kind) { case AND: argStack.push(new Double(a & b)); break; case OR: argStack.push(new Double(a | b)); break; case XOR: argStack.push(new Double(a ^ b)); break; } } )* } void sum() : {Token x;} { term() ( ( x = <PLUS> | x = <MINUS> ) term() { double a = ((Double) argStack.pop()).doubleValue(); double b = ((Double) argStack.pop()).doubleValue(); if ( x.kind == PLUS ) argStack.push(new Double(b + a)); else argStack.push(new Double(b - a)); } )* } void term() : {Token x;} { exp() ( ( x = <MULTIPLY> | x = <DIVIDE> ) exp() < #LETTER: ["a"-"z", "A"-"Z"] >

Pgina |4

PROGRAMACIN DE SISTEMAS

INSTITUTO TECNOLOGICO DE PACHUCA


{ double a = ((Double) argStack.pop()).doubleValue(); double b = ((Double) argStack.pop()).doubleValue(); if ( x.kind == MULTIPLY ) argStack.push(new Double(b * a)); else argStack.push(new Double(b / a)); } )* } void exp() : {} { unary() ( LOOKAHEAD( <EXP> ) <EXP> exp() { double a = ((Double) argStack.pop()).doubleValue(); double b = ((Double) argStack.pop()).doubleValue(); argStack.push(new Double(Math.pow(b, a))); } )* } void unary() : {} { <MINUS> element() { double a = ((Double) argStack.pop()).doubleValue(); argStack.push(new Double(- a)); } | element() } void element() : {} { <CONSTANT> { try { argStack.push(Double.valueOf(token.image)); } catch (NumberFormatException ee) { argStack.push(new Double(Double.NaN)); } } | function() | "(" logical() ")" } void function() : { String funcname; double args = 0; } { <ID> {funcname = token.image;} "(" [ logical() {args++;} ( "," logical() {args++;})* ] ")" { double a, b; if (funcname.equalsIgnoreCase("sin")) {

Pgina |5

PROGRAMACIN DE SISTEMAS

INSTITUTO TECNOLOGICO DE PACHUCA


if (args != 1) { System.err.println("Too many arguments to sin()"); throw new ParseException(); } a = ((Double) argStack.pop()).doubleValue(); argStack.push(new Double(Math.sin(a))); } else if (funcname.equalsIgnoreCase("cos")) { if (args != 1) { System.err.println("Too many arguments to cos()"); throw new ParseException(); } a = ((Double) argStack.pop()).doubleValue(); argStack.push(new Double(Math.cos(a))); } else if (funcname.equalsIgnoreCase("tan")) { if (args != 1) { System.err.println("Too many arguments to tan()"); throw new ParseException(); } a = ((Double) argStack.pop()).doubleValue(); argStack.push(new Double(Math.tan(a))); } else if (funcname.equalsIgnoreCase("min")) { if (args != 2) { System.err.println("Wrong number of arguments to min()"); throw new ParseException(); } a = ((Double) argStack.pop()).doubleValue(); b = ((Double) argStack.pop()).doubleValue(); argStack.push(new Double(Math.min(a, b))); } else if (funcname.equalsIgnoreCase("max")) { if (args != 2) { System.err.println("Wrong number of arguments to max()"); throw new ParseException(); } a = ((Double) argStack.pop()).doubleValue(); b = ((Double) argStack.pop()).doubleValue(); argStack.push(new Double(Math.max(a, b))); } else { /* Note we have to reverse the order of the arguments */ StringBuffer sb = new StringBuffer(); System.out.print("Function "+funcname+"( "); for (int i = 0; i < args; i++) { a = ((Double) argStack.pop()).doubleValue(); if (i == 0) { sb.insert(0, " "+a); } else { sb.insert(0, " "+a+","); } } System.out.println(sb+")"); /* push back a bogus value */ argStack.push(new Double(-1)); } } }

Pgina |6

PROGRAMACIN DE SISTEMAS

INSTITUTO TECNOLOGICO DE PACHUCA

Pgina |7

Este lo copiamos en el block de notas y lo guardamos con el nombre que queramos pero la extencin .jj, en este ejemplo ocuparemos Calc3i.jj

Una vez hecho esto lo ejecutamos en el smbolo del sistema Para esto vamos a abrir la carpeta raz desde el smbolo de sistema, escribiremos las siguientes lneas Cd\ (enter) Cd php\ Javacc Calc3i.jj Con esto, java cc empezara la creacin de tokens

PROGRAMACIN DE SISTEMAS

INSTITUTO TECNOLOGICO DE PACHUCA Despus escribimos javac *.java Con eso tendremos los archivos. Class

Pgina |8

Por ultimo escribiremos: Java Calc3i Calc3i.txt y nos dar como resultado:

Listo, esto fue nuestra calculadora compilada desde JAVACC.

PROGRAMACIN DE SISTEMAS

Potrebbero piacerti anche