Está en la página 1de 3

package compiladores;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
/*Matcher es la encargada de que la cadena introducida concuerde con el
patron
* Pattern => Es la encargada de definir el patron
*/
/**
*
* @author edwin
*/
public class Compiladores {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
/*
* Lo primero que se hace es definir el patron
*/

String patron = ("(while\\b|for\\b)|([0-9]+)|([a-zA-Z\\d]+)|([>|<|=|+|-|*|/|)|(|


{|}]+)");
String texto = "while300 azz3 for (edwin = 0) 5+4-3*2/1 < {y=1000000} ";
/* compila nuestra expresion regular */
Pattern p = Pattern.compile(patron);

/* creamos el analizador, capturamos el texto *


Matcher matcher = p.matcher(texto);
/* creamos un while, para encontrar las coincidencias */
int cantidad = matcher.groupCount()

String instruccion;
while (matcher.find()){
instruccion = matcher.group(1);
if(instruccion != null){
System.out.println("Palabra reservada :" + instruccion );
}
instruccion = matcher.group(2);
if(instruccion != null){
System.out.println("Numero :" + instruccion);
}
// String identificador = matcher.group(2);
instruccion= matcher.group(3);
if(instruccion != null){
System.out.println("identificador :" + instruccion );
}
//String operador = matcher.group(3);
instruccion = matcher.group(4);
if(instruccion != null){
System.out.println("Operador :" + instruccion );
} }
}

También podría gustarte