Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Palabras Reservadas.
Operadores Relacionales:
< <= > >= == !=
Operadores Lógicos:
! && ||
Constantes Enteras. Son como regularmente se conocen, pueden o no llevar signo, una
constante entera se ubica en el intervalo de -32768 a 32767, si se encuentra fuera de ese rango
automáticamente se convierte en número real.
Constantes Reales: Pueden o no llevar signo. Pueden iniciar con punto decimal pero no pueden
terminar con punto decimal. No permiten notación científica.
Constante string: Van limitadas por “ al inicio y al final. Un string debe terminar en la misma
línea, no hay manera de continuarlo en otra línea.
Comentarios: El lenguaje permite comentarios inician con // y debe terminar en la misma línea.
Ejemplo de código:
int n=5;
23.56
= -33
== -34
!= -35
! -36
&& -37
|| -38
; -39
[ -40
] -41
, -42
: -43
( -44
) -45
{ -46
} -47
CÓDIGO
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication6;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
try {
BufferedReader reader = new BufferedReader(new FileReader("codigo.txt"));
String linea;
while ((linea = reader.readLine()) != null) {
if (linea.trim().startsWith("//")) {
continue;
}
if (esPalabraReservada(palabra)) {
tokens.add(palabra + "\t" + getToken(palabra) + "\t-1\t" + numeroLinea);
} else if (palabra.matches("[a-zA-Z][a-zA-Z0-9]*")) {
tokens.add(palabra + "\t-22\t-2\t" + numeroLinea);
} else if (palabra.matches("-?\\d+")) {
if (esEnteroValido(palabra)) {
tokens.add(palabra + "\t-21\t-1\t" + numeroLinea);
} else {
tokens.add(palabra + "\t-23\t-1\t" + numeroLinea);
}
} else if (palabra.matches("-?\\d+(\\.\\d+)?")) {
tokens.add(palabra + "\t-23\t-1\t" + numeroLinea);
} else if (palabra.matches("\"[^\"]*\"")) {
tokens.add(palabra + "\t-20\t-1\t" + numeroLinea);
} else if (palabra.matches(OPERADORES_ARITMETICOS)) {
tokens.add(palabra + "\t" + "oparit" + "\t-1\t" + numeroLinea);
} else if (palabra.matches(OPERADORES_RELACIONALES)) {
tokens.add(palabra + "\t" + palabra + "\t-1\t" + numeroLinea);
} else if (palabra.matches(OPERADORES_LOGICOS)) {
tokens.add(palabra + "\t" + palabra + "\t-1\t" + numeroLinea);
} else if (palabra.matches(CARACTERES_TOKEN)) {
tokens.add(palabra + "\t" + palabra + "\t-1\t" + numeroLinea);
} else if (palabra.matches("[\\d]+\\)")) {
// Separar los caracteres '9' y ')' para analizarlos por separado
tokens.add(palabra.substring(0, palabra.length() - 1) + "\t-21\t-1\t" +
numeroLinea);
tokens.add(")" + "\t" + ")" + "\t-1\t" + numeroLinea);
} else {
// Tratar de dividir los caracteres de operadores
Matcher matcher = Pattern.compile("([&|])").matcher(palabra);
while (matcher.find()) {
tokens.add(matcher.group() + "\t" + matcher.group() + "\t-1\t" + numeroLinea);
}
tokens.add(palabra + "\tERROR\t-1\t" + numeroLinea);
}
}
numeroLinea++;
}
reader.close();
} catch (IOException e) {
System.out.println("Error al leer el archivo.");
e.printStackTrace();
}
}
}
return "-8";
case "println":
return "-12";
case "while":
return "-16";
default:
return "ERROR";
}
}