Skip to content

Commit 457d328

Browse files
committed
Algo de trabajo en el parser.
TODO: Pruebas
1 parent aaabe51 commit 457d328

File tree

8 files changed

+233
-3
lines changed

8 files changed

+233
-3
lines changed

src/correctorcodigodoc/Comparador.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.ArrayList;
55
import java.util.HashMap;
66
import javax.swing.JOptionPane;
7+
import parser.ParserStatement;
78

89
/**
910
* @author Sebastian
@@ -148,6 +149,17 @@ public Comparador(String patron, String texto) {
148149
public ArrayList<Error> CompararLexer() {
149150
ArrayList<Error> errores_detectados = new ArrayList<Error>();
150151

152+
Parser parser = new Parser(lexer_texto);
153+
try {
154+
ParserStatement st = parser.parseStatement();
155+
while (st != null)
156+
st = parser.parseStatement();
157+
} catch (Parser.ParseException ex) {
158+
JOptionPane.showMessageDialog(null, ex.message);
159+
}
160+
161+
return errores_detectados;
162+
/*
151163
ArrayList<Token> tokens_patron = new ArrayList<Token>();
152164
ArrayList<Token> tokens_texto = new ArrayList<Token>();
153165
@@ -180,7 +192,7 @@ public ArrayList<Error> CompararLexer() {
180192
}
181193
}
182194
183-
return errores_detectados;
195+
return errores_detectados;*/
184196
}
185197

186198
public ArrayList<Error> CompararCaracteres() {

src/correctorcodigodoc/Lexer.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ public class Lexer {
1313
private char ultimo_caracter;
1414
private Token ultimo_token;
1515
private int lex_index;
16-
private int lex_linea;
1716

1817
private final static HashMap<String, Token.Ids> TiposToken;
1918
private final static HashMap<Character, Token.Ids> TokensAtomicos;
@@ -72,7 +71,6 @@ public Lexer(String program) {
7271
this.ultimo_caracter = ' ';
7372
this.lex_index = 0;
7473
this.ultimo_token = null;
75-
this.lex_linea = 0;
7674
}
7775

7876
private Token nextTokenInternal() {
@@ -163,6 +161,25 @@ public Token nextToken() {
163161
return next_token;
164162
}
165163

164+
public Token peekToken() {
165+
// Guarda el índice y el caracter actual
166+
int lex_idx = lex_index;
167+
char ultimo_caract = ultimo_caracter;
168+
169+
// Lexea un token
170+
Token token = nextTokenInternal();
171+
172+
// Restaura el índice y el caracter
173+
lex_index = lex_idx;
174+
ultimo_caracter = ultimo_caract;
175+
176+
return token;
177+
}
178+
179+
public Token prevToken() {
180+
return ultimo_token;
181+
}
182+
166183
private char getChar() {
167184
return codigo[lex_index++];
168185
}

src/correctorcodigodoc/Parser.java

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,128 @@
55
*/
66
package correctorcodigodoc;
77

8+
import parser.ParserStatement;
9+
import parser.DeclarationStatement;
10+
import parser.NullStatement;
11+
import compiladorpseu.Tokens.Token;
12+
import java.util.ArrayList;
13+
import java.util.List;
14+
import parser.Expression;
15+
import parser.Identifier;
16+
817
/**
918
*
1019
* @author Sebastian
1120
*/
1221
public class Parser {
22+
public static class ParseException extends Exception {
23+
public String message;
24+
25+
public ParseException(String what) {
26+
this.message = what;
27+
}
28+
}
29+
1330
private Lexer lexer;
31+
private Token current_token;
1432

1533
public Parser(Lexer lexer) {
1634
this.lexer = lexer;
35+
this.current_token = lexer.nextToken();
36+
}
37+
38+
public ParserStatement parseStatement() throws ParseException {
39+
if (current_token == null)
40+
return null;
41+
42+
Token next_token = lexer.nextToken();
43+
ParserStatement statement = null;
44+
45+
switch (current_token.id) {
46+
case INICIO: // Nada que hacer
47+
case PROC: // Nada que hacer
48+
case FIN: // Nada que hacer
49+
case NUEVA_LINEA: // Nada que hacer
50+
case DOS_PUNTOS:
51+
statement = new NullStatement();
52+
break;
53+
54+
case TIPO_DATO:
55+
// El siguiente token debe ser un identificador o una lista
56+
List<Identifier> tokens = parseIdentifierList(next_token);
57+
if (tokens.isEmpty()) {
58+
throw new ParseException("Error de sintaxis en la lista de identificadores");
59+
}
60+
statement = new DeclarationStatement(tokens);
61+
62+
// Ya nos comimos el último token, por tanto tenemos que retroceder una posición
63+
next_token = lexer.prevToken();
64+
break;
65+
}
66+
67+
current_token = next_token;
68+
return statement;
69+
}
70+
71+
/*
72+
* Parsea una lista de identificadores de la forma I1,I2,I3,I4...In
73+
* Tira una excepción si la lista está malformada
74+
* @param token_inicial El token que inicia la lista
75+
* @returns Lista de identificadores parseada
76+
*/
77+
public List<Identifier> parseIdentifierList(Token token_inicial) throws ParseException {
78+
List<Identifier> ret = new ArrayList<Identifier>();
79+
80+
Token token = token_inicial;
81+
82+
while (token.id == Token.Ids.IDENTIFIER) {
83+
ret.add(parseIdentifier(token));
84+
85+
token = lexer.nextToken();
86+
87+
// La lista termina con una nueva linea
88+
if (token.id == Token.Ids.NUEVA_LINEA)
89+
break;
90+
91+
// Si lo que sigue no es una coma, tenemos un error de sintaxis
92+
if (token.id != Token.Ids.COMA) {
93+
throw new ParseException("Error de sintaxis en la lista de identificadores");
94+
}
95+
96+
token = lexer.nextToken();
97+
}
98+
99+
return ret;
100+
}
101+
102+
/*
103+
* Parsea un identifier a partir de un token
104+
* el identifier puede ser un array o una variable normal
105+
* @param token_inicial El token por el que comienza el identifier
106+
* @returns Identifier parseado
107+
*/
108+
public Identifier parseIdentifier(Token token_inicial) throws ParseException {
109+
Token next_token = lexer.peekToken();
110+
111+
if (next_token.id == Token.Ids.PARENTESIS_ABRE) {
112+
// El identificador es un arreglo, parsea los parametros hasta el (
113+
// Comete el (
114+
lexer.nextToken();
115+
List<Expression> parametros = parseExpressionList(lexer.nextToken(), Token.Ids.PARENTESIS_CIERRA);
116+
if (parametros.isEmpty()) {
117+
throw new ParseException("Error de sintaxis en la lista de identificadores");
118+
}
119+
return new Identifier(parametros);
120+
}
121+
122+
return new Identifier(null);
123+
}
124+
125+
public List<Expression> parseExpressionList(Token token_inicial, Token.Ids terminador) {
126+
return null;
127+
}
128+
129+
public Expression parseExpression() {
130+
return null;
17131
}
18132
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package parser;
7+
8+
import java.util.List;
9+
10+
/**
11+
*
12+
* @author Sebastian
13+
*/
14+
public class DeclarationStatement extends ParserStatement {
15+
private List<Identifier> identifiers;
16+
17+
public DeclarationStatement(List<Identifier> identifiers) {
18+
this.identifiers = identifiers;
19+
}
20+
}

src/parser/Expression.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package parser;
7+
8+
/**
9+
*
10+
* @author Sebastian
11+
*/
12+
public class Expression {
13+
14+
}

src/parser/Identifier.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package parser;
7+
8+
import java.util.List;
9+
10+
/**
11+
*
12+
* @author Sebastian
13+
*/
14+
public class Identifier {
15+
private List<Expression> parameters;
16+
17+
public Identifier(List<Expression> parameters) {
18+
this.parameters = parameters;
19+
}
20+
}

src/parser/NullStatement.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package parser;
7+
8+
import compiladorpseu.Tokens.Token;
9+
import java.util.List;
10+
11+
/**
12+
*
13+
* @author Sebastian
14+
*/
15+
public class NullStatement extends ParserStatement {
16+
public NullStatement() {
17+
18+
}
19+
}

src/parser/ParserStatement.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package parser;
7+
8+
/**
9+
*
10+
* @author Sebastian
11+
*/
12+
public abstract class ParserStatement {
13+
14+
}

0 commit comments

Comments
 (0)