Skip to content

Commit 05dd1a7

Browse files
committed
[ADD] Arithmetic Expression Parser
1 parent 3daec49 commit 05dd1a7

39 files changed

+195
-118
lines changed

src/main/java/ParserOperations/ArithmeticParser.java

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 174 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,137 +1,227 @@
11
package ParserOperations;
22

3+
import java.util.ArrayList;
4+
5+
import Tokens.Token;
36
import Tokens.TokenType;
47

58
public class Assignments {
69
private Utilities tool;
7-
private ArithmeticParser ap;
10+
private ArrayList<Integer> openParent, closeParent;
811

912
public Assignments() {
1013
tool = new Utilities();
11-
ap = new ArithmeticParser();
14+
openParent = new ArrayList<>();
15+
closeParent = new ArrayList<>();
1216
}
1317

1418
public boolean isAssignment() {
15-
if (ap.isArithmeticOperation() || isValueAssign() /* || isDataCollection() */) {
16-
return true;
17-
} else {
18-
return false;
19-
}
19+
return isAssignExpression();
2020
}
2121

22-
public boolean isValueAssign() {
22+
public boolean isAssignExpression() {
2323
boolean flag = false;
2424
if (tool.verifyToken(TokenType.IDENTIFICADOR)) {
2525
tool.incrementIndex();
2626
if (tool.verifyToken(TokenType.ASIGNACION)) {
2727
tool.incrementIndex();
28-
if (tool.isValueToken(tool.getCurrentToken())) {
28+
if (isExpression()) {
2929
flag = true;
30-
// System.out.println("VARIABLE DECLARADA");
31-
tool.incrementIndex();
3230
} else {
33-
tool.showError("Se esperaba valor");
31+
if (tool.isValueToken(tool.getCurrentToken())) {
32+
flag = true;
33+
// System.out.println("VARIABLE DECLARADA");
34+
tool.incrementIndex();
35+
} else {
36+
tool.showError("[NORMAL] Se esperaba valor");
37+
}
3438
}
3539
} else if (tool.isAssignment(tool.getCurrentToken())) {
3640
tool.incrementIndex();
37-
if (tool.isValueToken(tool.getCurrentToken())) {
38-
// System.out.println("VARIABLE AUGASSIGN");
41+
if (isExpression()) {
3942
flag = true;
40-
tool.incrementIndex();
4143
} else {
42-
tool.showError("Se esperaba valor");
44+
if (tool.isValueToken(tool.getCurrentToken())) {
45+
flag = true;
46+
// System.out.println("VARIABLE AUGGASING");
47+
tool.incrementIndex();
48+
} else {
49+
tool.showError("[AUG] Se esperaba valor");
50+
}
4351
}
4452
} else {
45-
tool.showError("Se esperaba operador");
53+
tool.showError("[MAIN] Se esperaba operador");
54+
}
55+
}
56+
57+
for (Token tkn : tool.getTokenList()) {
58+
if (tkn.getRow() == tool.getCurrentToken().getRow()
59+
&& tkn.getColumn() <= tool.getCurrentToken().getColumn()) {
60+
System.out.println(tkn.toString());
61+
if (tkn.getToken() == TokenType.PARENTESIS_APERTURA) {
62+
openParent.add(tkn.getColumn());
63+
} else if (tkn.getToken() == TokenType.PARENTESIS_CIERRE) {
64+
closeParent.add(tkn.getColumn());
65+
}
4666
}
4767
}
68+
69+
if (openParent.size() < closeParent.size()) {
70+
tool.showError("Se esperaba '('");
71+
} else if (openParent.size() > closeParent.size()) {
72+
tool.showError("Se esperaba ')'");
73+
}
74+
4875
return flag;
4976
}
5077

51-
public boolean isDataCollection() {
78+
public boolean isFactor() {
5279
boolean flag = false;
53-
if (tool.verifyToken(TokenType.IDENTIFICADOR_TUPLA)) {
80+
System.out.println(tool.getCurrentToken().getLexeme());
81+
if (tool.isValueFactor(tool.getCurrentToken())) {
5482
tool.incrementIndex();
55-
if (tool.verifyToken(TokenType.ASIGNACION)) {
83+
flag = true;
84+
} else if (tool.verifyToken(TokenType.RESTA)) {
85+
tool.incrementIndex();
86+
if (tool.verifyToken(TokenType.ENTERO) || tool.verifyToken(TokenType.DECIMAL)) {
5687
tool.incrementIndex();
57-
if (tool.verifyToken(TokenType.PARENTESIS_APERTURA)) {
58-
tool.incrementIndex();
59-
if (tool.isValueToken(tool.getCurrentToken())) {
60-
tool.incrementIndex();
61-
if (checkValuesList()) {
62-
// tool.incrementIndex();
63-
if (tool.verifyToken(TokenType.PARENTESIS_CIERRE)) {
64-
flag = true;
65-
System.out.println("ESTRUCTURA DE DATOS DECLARADA");
66-
} else {
67-
tool.showError("Se esperaba ')'");
68-
}
69-
}
70-
} else {
71-
tool.showError("Se esperaba valor");
72-
}
73-
}
88+
flag = true;
7489
}
75-
} else if (tool.verifyToken(TokenType.IDENTIFICADOR_CONJUNTO)) {
90+
} else if (tool.verifyToken(TokenType.PARENTESIS_APERTURA)) {
7691
tool.incrementIndex();
77-
if (tool.verifyToken(TokenType.ASIGNACION)) {
78-
tool.incrementIndex();
79-
if (tool.verifyToken(TokenType.LLAVE_APERTURA)) {
92+
if (isExpression()) {
93+
if (tool.verifyToken(TokenType.PARENTESIS_CIERRE)) {
8094
tool.incrementIndex();
81-
if (tool.isValueToken(tool.getCurrentToken())) {
82-
tool.incrementIndex();
83-
if (checkValuesList()) {
84-
// tool.incrementIndex();
85-
if (tool.verifyToken(TokenType.LLAVE_CIERRE)) {
86-
flag = true;
87-
System.out.println("ESTRUCTURA DE DATOS DECLARADA");
88-
} else {
89-
tool.showError("Se esperaba '}'");
90-
}
91-
}
92-
} else {
93-
tool.showError("Se esperaba valor");
94-
}
95+
flag = true;
9596
}
9697
}
97-
} else if (tool.verifyToken(TokenType.IDENTIFICADOR_LISTA)) {
98-
tool.incrementIndex();
99-
if (tool.verifyToken(TokenType.ASIGNACION)) {
100-
tool.incrementIndex();
101-
if (tool.verifyToken(TokenType.CORCHETE_APERTURA)) {
102-
tool.incrementIndex();
103-
if (tool.isValueToken(tool.getCurrentToken())) {
104-
tool.incrementIndex();
105-
if (checkValuesList()) {
106-
// tool.incrementIndex();
107-
if (tool.verifyToken(TokenType.CORCHETE_CIERRE)) {
108-
flag = true;
109-
System.out.println("ESTRUCTURA DE DATOS DECLARADA");
110-
} else {
111-
tool.showError("Se esperaba ']'");
112-
}
113-
}
114-
} else {
115-
tool.showError("Se esperaba valor");
116-
}
98+
}
99+
return flag;
100+
}
101+
102+
public boolean isExpression() {
103+
boolean flag = false;
104+
// Verificar el primer término
105+
if (isTerm()) {
106+
flag = true;
107+
// Verificar operadores de suma o resta
108+
while (tool.verifyToken(TokenType.SUMA) || tool.verifyToken(TokenType.RESTA)) {
109+
tool.incrementIndex(); // Avanzar el operador
110+
if (!isTerm()) {
111+
tool.showError("[EXP] Se esperaba un término después del operador aritmético");
112+
flag = false;
113+
break;
117114
}
118115
}
116+
} else {
117+
tool.showError("[EXP] Se esperaba un término");
119118
}
120119
return flag;
121120
}
122121

123-
private boolean checkValuesList() {
124-
boolean flag = true;
125-
while (tool.verifyToken(TokenType.COMA)) {
126-
tool.incrementIndex();
127-
if (tool.isValueToken(tool.getCurrentToken())) {
128-
tool.incrementIndex();
129-
} else {
130-
tool.showError("Se esperaba valor después de ','");
131-
flag = false;
122+
public boolean isTerm() {
123+
boolean flag = false;
124+
// Verificar el primer factor
125+
if (isFactor()) {
126+
flag = true;
127+
// Verificar operadores de multiplicación o división
128+
while (tool.verifyToken(TokenType.MULTIPLICACION) || tool.verifyToken(TokenType.DIVISION)) {
129+
tool.incrementIndex(); // Avanzar el operador
130+
if (!isFactor()) {
131+
tool.showError("[TERM] Se esperaba valor después del operador aritmético");
132+
flag = false;
133+
break;
134+
}
132135
}
136+
} else {
137+
tool.showError("[TERM] Se esperaba un valor");
133138
}
134139
return flag;
135140
}
141+
// public boolean isDataCollection() {
142+
// boolean flag = false;
143+
// if (tool.verifyToken(TokenType.IDENTIFICADOR_TUPLA)) {
144+
// tool.incrementIndex();
145+
// if (tool.verifyToken(TokenType.ASIGNACION)) {
146+
// tool.incrementIndex();
147+
// if (tool.verifyToken(TokenType.PARENTESIS_APERTURA)) {
148+
// tool.incrementIndex();
149+
// if (tool.isValueToken(tool.getCurrentToken())) {
150+
// tool.incrementIndex();
151+
// if (checkValuesList()) {
152+
// // tool.incrementIndex();
153+
// if (tool.verifyToken(TokenType.PARENTESIS_CIERRE)) {
154+
// flag = true;
155+
// System.out.println("ESTRUCTURA DE DATOS DECLARADA");
156+
// } else {
157+
// tool.showError("Se esperaba ')'");
158+
// }
159+
// }
160+
// } else {
161+
// tool.showError("Se esperaba valor");
162+
// }
163+
// }
164+
// }
165+
// } else if (tool.verifyToken(TokenType.IDENTIFICADOR_CONJUNTO)) {
166+
// tool.incrementIndex();
167+
// if (tool.verifyToken(TokenType.ASIGNACION)) {
168+
// tool.incrementIndex();
169+
// if (tool.verifyToken(TokenType.LLAVE_APERTURA)) {
170+
// tool.incrementIndex();
171+
// if (tool.isValueToken(tool.getCurrentToken())) {
172+
// tool.incrementIndex();
173+
// if (checkValuesList()) {
174+
// // tool.incrementIndex();
175+
// if (tool.verifyToken(TokenType.LLAVE_CIERRE)) {
176+
// flag = true;
177+
// System.out.println("ESTRUCTURA DE DATOS DECLARADA");
178+
// } else {
179+
// tool.showError("Se esperaba '}'");
180+
// }
181+
// }
182+
// } else {
183+
// tool.showError("Se esperaba valor");
184+
// }
185+
// }
186+
// }
187+
// } else if (tool.verifyToken(TokenType.IDENTIFICADOR_LISTA)) {
188+
// tool.incrementIndex();
189+
// if (tool.verifyToken(TokenType.ASIGNACION)) {
190+
// tool.incrementIndex();
191+
// if (tool.verifyToken(TokenType.CORCHETE_APERTURA)) {
192+
// tool.incrementIndex();
193+
// if (tool.isValueToken(tool.getCurrentToken())) {
194+
// tool.incrementIndex();
195+
// if (checkValuesList()) {
196+
// // tool.incrementIndex();
197+
// if (tool.verifyToken(TokenType.CORCHETE_CIERRE)) {
198+
// flag = true;
199+
// System.out.println("ESTRUCTURA DE DATOS DECLARADA");
200+
// } else {
201+
// tool.showError("Se esperaba ']'");
202+
// }
203+
// }
204+
// } else {
205+
// tool.showError("Se esperaba valor");
206+
// }
207+
// }
208+
// }
209+
// }
210+
// return flag;
211+
// }
212+
213+
// private boolean checkValuesList() {
214+
// boolean flag = true;
215+
// while (tool.verifyToken(TokenType.COMA)) {
216+
// tool.incrementIndex();
217+
// if (tool.isValueToken(tool.getCurrentToken())) {
218+
// tool.incrementIndex();
219+
// } else {
220+
// tool.showError("Se esperaba valor después de ','");
221+
// flag = false;
222+
// }
223+
// }
224+
// return flag;
225+
// }
136226

137227
}

src/main/java/ParserOperations/CompoundStatements.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,12 @@ public CompoundStatements() {
1313
}
1414

1515
public boolean isCompoundStatement() {
16-
boolean flag = false;
1716
tool.setCurrentRow(tool.getCurrentToken().getRow());
18-
if (isClassDeclaration()
17+
return isClassDeclaration()
1918
|| isIfStatement() || isElifStatement() || isElseStatement()
2019
|| isForStatement()
2120
|| isWhileStatement()
22-
|| isMatchStatement() || isCaseStatement()) {
23-
flag = true;
24-
}
25-
return flag;
21+
|| isMatchStatement() || isCaseStatement();
2622
}
2723

2824
public boolean isClassDeclaration() {

src/main/java/ParserOperations/Parser.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package ParserOperations;
22

33
import Tokens.Token;
4+
import Tokens.TokenType;
5+
46
import java.util.ArrayList;
57
import javax.swing.JTextArea;
68

@@ -42,10 +44,12 @@ public void statements() {
4244
} else if (ss.isSimpleStatement()) {
4345
// SimpleStatement correcto
4446
statements();
47+
} else if (tool.verifyToken(TokenType.DESCONOCIDO)) {
48+
tool.showError("No se reconoce '" + tool.getCurrentToken().getLexeme() + "'");
4549
}
46-
}
50+
}
4751
// else {
48-
// System.out.println("Fin de Ejecución");
52+
// System.out.println("Fin de Ejecución");
4953
// }
5054
}
5155
}

src/main/java/ParserOperations/SimpleStatements.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,16 @@
55
public class SimpleStatements {
66

77
private Utilities tool;
8-
private Assignments assigns;
8+
private Assignments as;
99

1010
public SimpleStatements() {
1111
tool = new Utilities();
12-
assigns = new Assignments();
12+
as = new Assignments();
1313
}
1414

1515
public boolean isSimpleStatement() {
16-
boolean flag = false;
1716
tool.setCurrentRow(tool.getCurrentToken().getRow());
18-
if (assigns.isAssignment() || isImportStatement() || isPrint() || isControlSentence()) {
19-
flag = true;
20-
}
21-
return flag;
17+
return as.isAssignment() || isImportStatement() || isPrint() || isControlSentence();
2218
}
2319

2420
public boolean isControlSentence() {

0 commit comments

Comments
 (0)