-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 48d580f
Showing
11 changed files
with
314 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<classpath> | ||
<classpathentry kind="src" path="src"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/> | ||
<classpathentry kind="output" path="bin"/> | ||
</classpath> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<projectDescription> | ||
<name>CompiladorLexerParser</name> | ||
<comment></comment> | ||
<projects> | ||
</projects> | ||
<buildSpec> | ||
<buildCommand> | ||
<name>org.eclipse.jdt.core.javabuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
</buildSpec> | ||
<natures> | ||
<nature>org.eclipse.jdt.core.javanature</nature> | ||
</natures> | ||
</projectDescription> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
eclipse.preferences.version=1 | ||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled | ||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 | ||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve | ||
org.eclipse.jdt.core.compiler.compliance=1.7 | ||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate | ||
org.eclipse.jdt.core.compiler.debug.localVariable=generate | ||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate | ||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error | ||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error | ||
org.eclipse.jdt.core.compiler.source=1.7 |
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import java.io.BufferedReader; | ||
import java.io.File; | ||
import java.io.FileReader; | ||
import java.io.IOException; | ||
|
||
/* | ||
* To change this template, choose Tools | Templates | ||
* and open the template in the editor. | ||
*/ | ||
|
||
/** | ||
* | ||
* @author Emmanuel Valentín Granados López | ||
* | ||
*/ | ||
public class CompiladorSimple | ||
{ | ||
|
||
/** | ||
* @param args the command line arguments | ||
*/ | ||
public static void main(String[] args) | ||
{ | ||
|
||
|
||
String linea=""; | ||
|
||
File f = new File( "C:\\ab.txt" ); | ||
BufferedReader entrada=null; | ||
try { | ||
entrada = new BufferedReader( new FileReader( f ) ); | ||
|
||
while(entrada.ready()){ | ||
linea = entrada.readLine(); | ||
System.out.println(linea + " Esta linea se obtuvo desde un txt"); | ||
} | ||
}catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
finally | ||
{ | ||
try{ | ||
entrada.close(); | ||
}catch(IOException e1){} | ||
} | ||
|
||
|
||
// Prueba del lexer | ||
|
||
String palabra1 = "-1/1-2*3+4"; | ||
String palabra2 = "324+54+4567/4563-12345678900987654321*1"; | ||
String palabra3 = "1/11*11+11-1P"; | ||
|
||
|
||
Lexer lex = new Lexer(); | ||
|
||
Parser parser = new Parser(); | ||
|
||
// Prueba lexer para cadena obtenida en txt desde C:\\ | ||
if (linea.matches ("[0-9(\\+|\\/|\\-|\\*)*]*")) { | ||
lex.setCadena(linea); | ||
System.out.println("Lexer Para txt: " + linea + " Token devuelto: " + ((lex.edo0() == 0) ? "ACEPTADA" : "INVALIDA")); | ||
System.out.println("Parser: txt"); | ||
parser.parse(linea + ";",';'); | ||
|
||
} | ||
else { | ||
System.out.println("error : se encontraron caracteres distintos a los permitidos"); | ||
} | ||
|
||
if (palabra1.matches ("[0-9(\\+|\\/|\\-|\\*)*]*")) { | ||
lex.setCadena(palabra1); | ||
System.out.println("Lexer Para 1: " + palabra1 + " Token devuelto: " + ((lex.edo0() == 0) ? "ACEPTADA" : "INVALIDA")); | ||
System.out.println("Parser: palabra1"); | ||
parser.parse(palabra1 + ";",';'); | ||
} | ||
else { | ||
System.out.println("error : se encontraron caracteres distintos a los permitidos"); | ||
} | ||
|
||
|
||
if (palabra2.matches ("[0-9(\\+|\\/|\\-|\\*)*]*")) { | ||
lex.setCadena(palabra2); | ||
System.out.println("Lexer Para 2: " + palabra2 + " Token devuelto: " + ((lex.edo0() == 0) ? "ACEPTADA" : "INVALIDA")); | ||
System.out.println("Parser: palabra2"); | ||
parser.parse(palabra2 + ";",';'); | ||
} | ||
else { | ||
System.out.println("error : se encontraron caracteres distintos a los permitidos"); | ||
} | ||
|
||
if (palabra3.matches ("[0-9(\\+|\\/|\\-|\\*)*]*")) { | ||
lex.setCadena(palabra3); | ||
System.out.println("Lexer Para 3: " + palabra3 + " Token devuelto: " + ((lex.edo0() == 0) ? "ACEPTADA" : "INVALIDA")); | ||
System.out.println("Parser: palabra3"); | ||
parser.parse(palabra3 + ";",';'); | ||
} | ||
else { | ||
System.out.println("error : se encontraron caracteres distintos a los permitidos"); | ||
} | ||
|
||
|
||
// Prueba del parser | ||
|
||
|
||
System.out.println("---------Solo"); | ||
parser.parse("12+345*678/90;12++3432", ';'); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* | ||
* To change this template, choose Tools | Templates | ||
* and open the template in the editor. | ||
*/ | ||
|
||
/** | ||
* | ||
* @author Emmanuel Valentín Granados López | ||
* | ||
*/ | ||
public class Lexer | ||
{ | ||
static final int ERROR = -1; | ||
static final int ACEPTAR = 0; | ||
|
||
private String cadena = ""; | ||
private int cont = 0; | ||
|
||
|
||
public void setCadena(String cadena) | ||
{ | ||
this.cadena = cadena; | ||
cont = 0; | ||
} | ||
|
||
char obtenerCaracter() | ||
{ | ||
char c = (cont >= cadena.length()) ? 'z' : cadena.charAt(cont); | ||
cont++; | ||
return c; | ||
} | ||
|
||
int edo0() | ||
{ | ||
char c = obtenerCaracter(); | ||
switch(c) | ||
{ | ||
case '0': return edo1(); | ||
case '1': return edo1(); | ||
case '2': return edo1(); | ||
case '3': return edo1(); | ||
case '4': return edo1(); | ||
case '5': return edo1(); | ||
case '6': return edo1(); | ||
case '7': return edo1(); | ||
case '8': return edo1(); | ||
case '9': return edo1(); | ||
default: return ERROR; | ||
} | ||
} | ||
|
||
int edo1() | ||
{ | ||
|
||
char c = obtenerCaracter(); | ||
|
||
switch(c) | ||
{ | ||
|
||
case '0': return edo1(); | ||
case '1': return edo1(); | ||
case '2': return edo1(); | ||
case '3': return edo1(); | ||
case '4': return edo1(); | ||
case '5': return edo1(); | ||
case '6': return edo1(); | ||
case '7': return edo1(); | ||
case '8': return edo1(); | ||
case '9': return edo1(); | ||
|
||
case '+': return edo2(); | ||
case '-': return edo2(); | ||
case '*': return edo2(); | ||
case '/': return edo2(); | ||
|
||
|
||
} | ||
|
||
return ACEPTAR; | ||
|
||
|
||
} | ||
|
||
int edo2() | ||
{ | ||
char c = obtenerCaracter(); | ||
switch(c) | ||
{ | ||
case '0': return edo1(); | ||
case '1': return edo1(); | ||
case '2': return edo1(); | ||
case '3': return edo1(); | ||
case '4': return edo1(); | ||
case '5': return edo1(); | ||
case '6': return edo1(); | ||
case '7': return edo1(); | ||
case '8': return edo1(); | ||
case '9': return edo1(); | ||
default: return ERROR; | ||
} | ||
} | ||
|
||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* To change this template, choose Tools | Templates | ||
* and open the template in the editor. | ||
*/ | ||
|
||
/** | ||
* | ||
* @author Emmanuel Valentín Granados López | ||
* | ||
*/ | ||
public class Parser | ||
{ | ||
enum Tokens | ||
{ | ||
ACEPTADA, | ||
INVALIDA, | ||
PUNTOYCOMA, | ||
EOF | ||
} | ||
|
||
public void parse(String secuencia, char delimitador) | ||
{ | ||
Lexer lex = new Lexer(); | ||
Tokens token = Tokens.EOF; | ||
String buffer = ""; | ||
int pos = 0; | ||
|
||
do | ||
{ | ||
if (pos < secuencia.length()) | ||
{ | ||
while(secuencia.charAt(pos) != delimitador) | ||
{ | ||
buffer+= secuencia.charAt(pos); | ||
pos++; | ||
if (pos >= secuencia.length()) break; | ||
} | ||
|
||
if(!buffer.equals("")) | ||
{ | ||
lex.setCadena(buffer); | ||
if(lex.edo0() == 0) | ||
token = Tokens.ACEPTADA; | ||
else | ||
token = Tokens.INVALIDA; | ||
buffer = ""; | ||
|
||
} | ||
else if(secuencia.charAt(pos) == ';') | ||
{ | ||
token = Tokens.PUNTOYCOMA; | ||
pos++; | ||
} | ||
} | ||
else | ||
token = Tokens.EOF; | ||
|
||
System.out.println(token.name()); | ||
|
||
}while(token != Tokens.EOF); | ||
} | ||
|
||
} | ||
|