Skip to content

Commit f28c964

Browse files
committed
First commit and adding very old code from about 10 years ago
0 parents  commit f28c964

16 files changed

+1416
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.vscode
2+
.DS_Store

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019 Aftab Hussain Miranda <[email protected]>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Analizador de Lógica Proposicional
2+
3+
> Código que realice en la universidad Marzo-2009 para la matería
4+
de inteligencia artificial, para analizat la lógica proposicional.
5+
6+
# Descripción
7+
Este pequeño programa analiza una cadena, y a partir de esta
8+
primero extrae los tokens de la cadena y de las proposiciones
9+
pide un valor verdadero o falso. Luego a partir de una
10+
gramatica procede a evaluar y regresar un valor verdadero o
11+
falso segun sea el caso.
12+
13+
la manera mas facil para compilarlo es tener todos los
14+
archivos en una carpeta (no mezclado con otros archivos) y en
15+
la consola ejecutar:
16+
17+
```
18+
g++ *.cpp -o <nombre-del-programa>
19+
```
20+
21+
## License
22+
23+
This code is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT).

analizadorLexico.cpp

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
#include "analizadorLexico.h"
2+
3+
4+
AnalizadorLexico::AnalizadorLexico(){
5+
creaAutomatas();
6+
}
7+
8+
void AnalizadorLexico::creaAutomatas(){
9+
Afn[0] = conjuncion();
10+
Afn[1] = disjuncion();
11+
Afn[2] = negacion();
12+
Afn[3] = condicional();
13+
Afn[4] = bicondicional();
14+
Afn[5] = parentesisIzq();
15+
Afn[6] = parentesisDer();
16+
Afn[7] = alfabeto();
17+
Afn[8] = espacioBlanco();
18+
}
19+
20+
Automata *AnalizadorLexico::conjuncion(){
21+
return new Automata('&');
22+
}
23+
24+
Automata *AnalizadorLexico::disjuncion(){
25+
return new Automata('|');
26+
}
27+
28+
Automata *AnalizadorLexico::negacion(){
29+
return new Automata('!');
30+
}
31+
32+
Automata *AnalizadorLexico::condicional(){
33+
Automata *linea = new Automata('-');
34+
Automata *mayorQue = new Automata('>');
35+
return linea->concatenar(mayorQue);
36+
}
37+
38+
Automata *AnalizadorLexico::bicondicional(){
39+
Automata *menorQue = new Automata('<');
40+
Automata *cond = condicional();
41+
return menorQue->concatenar(cond);
42+
}
43+
44+
Automata *AnalizadorLexico::parentesisIzq(){
45+
return new Automata('(');
46+
}
47+
48+
Automata *AnalizadorLexico::parentesisDer(){
49+
return new Automata(')');
50+
}
51+
52+
Automata *AnalizadorLexico::alfabeto(){
53+
Automata *A[26];
54+
const int abc = 65;
55+
for(int i = 0; i < 26; i++){
56+
A[i] = new Automata((char)abc+i);
57+
}
58+
for(int i = 1; i < 26; i++)
59+
A[0]->unir(A[i]);
60+
return A[0];
61+
}
62+
63+
Automata *AnalizadorLexico::espacioBlanco(){
64+
return new Automata(' ');
65+
}
66+
67+
Lista<Token*> AnalizadorLexico::getTokens(string cadena){
68+
string cadAux = cadena;
69+
string cadStack;
70+
Lista<Token*> Tokens;
71+
Token *tok;
72+
int pCad = 0;
73+
for(int i=0; i<cadAux.size(); i++){
74+
cadStack += cadAux[pCad];
75+
tok = clasificaToken(cadStack);
76+
if(tok->getTipo()!=0){
77+
cadStack.clear();
78+
if(tok->getTipo()!=Token::EspacioBlanco)
79+
Tokens.pushFin(tok);
80+
}
81+
pCad++;
82+
}
83+
return Tokens;
84+
}
85+
86+
Token *AnalizadorLexico::clasificaToken(string cadena){
87+
Token *tok = new Token();
88+
if(Afn[0]->evaluaCadena(cadena)){
89+
tok->setTipo(Token::Conjuncion);
90+
tok->setNombre(cadena);
91+
}
92+
if(Afn[1]->evaluaCadena(cadena)){
93+
tok->setTipo(Token::Disjuncion);
94+
tok->setNombre(cadena);
95+
}
96+
if(Afn[2]->evaluaCadena(cadena)){
97+
tok->setTipo(Token::Negacion);
98+
tok->setNombre(cadena);
99+
}
100+
if(Afn[3]->evaluaCadena(cadena)){
101+
tok->setTipo(Token::Condicional);
102+
tok->setNombre(cadena);
103+
}
104+
if(Afn[4]->evaluaCadena(cadena)){
105+
tok->setTipo(Token::Bicondicional);
106+
tok->setNombre(cadena);
107+
}
108+
if(Afn[5]->evaluaCadena(cadena)){
109+
tok->setTipo(Token::ParentesisIzq);
110+
tok->setNombre(cadena);
111+
}
112+
if(Afn[6]->evaluaCadena(cadena)){
113+
tok->setTipo(Token::ParentesisDer);
114+
tok->setNombre(cadena);
115+
}
116+
if(Afn[7]->evaluaCadena(cadena)){
117+
tok->setTipo(Token::Proposicion);
118+
tok->setNombre(cadena);
119+
}
120+
if(Afn[8]->evaluaCadena(cadena)){
121+
tok->setTipo(Token::EspacioBlanco);
122+
tok->setNombre(cadena);
123+
}
124+
return tok;
125+
}
126+
127+
128+
129+
130+
131+

analizadorLexico.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#ifndef ANALIZADORLEXICO_H
2+
#define ANALIZADORLEXICO_H
3+
4+
#include "automata.h"
5+
#include "token.h"
6+
7+
class AnalizadorLexico{
8+
private:
9+
Automata *Afn[9];
10+
void creaAutomatas();
11+
Automata *conjuncion();
12+
Automata *disjuncion();
13+
Automata *negacion();
14+
Automata *condicional();
15+
Automata *parentesisIzq();
16+
Automata *parentesisDer();
17+
Automata *bicondicional();
18+
Automata *alfabeto();
19+
Automata *espacioBlanco();
20+
public:
21+
AnalizadorLexico();
22+
Lista<Token*> getTokens(string cadena);
23+
Token *clasificaToken(string cadena);
24+
bool evalua(string cadena);
25+
};
26+
27+
#endif
28+

analizadorSintactico.cpp

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
#include "analizadorSintactico.h"
2+
3+
AnalizadorSintactico::AnalizadorSintactico(Lista<Token*> Tokens){
4+
for(int i=0; i<Tokens.numElem(); i++){
5+
Toks.pushFin(Tokens.getInfoAt(i));
6+
}
7+
aceptable = E(&resultado);
8+
}
9+
10+
int AnalizadorSintactico::Aceptable() const{
11+
return aceptable;
12+
}
13+
14+
bool AnalizadorSintactico::Resultado() const{
15+
return resultado;
16+
}
17+
18+
bool AnalizadorSintactico::E(bool *ApV){
19+
if(T(ApV)) return Ep(ApV);
20+
return 0;
21+
}
22+
23+
bool AnalizadorSintactico::Ep(bool *ApV){
24+
bool Aux;
25+
Token *Tok;
26+
Toks.popIni(Tok);
27+
if(Tok!=NULL){
28+
if(Tok->getTipo()==Token::Condicional){
29+
if(T(&Aux)){
30+
*ApV = (*ApV==true && Aux==false) ? false : true;
31+
return Ep(ApV);
32+
}
33+
return 0;
34+
}
35+
Toks.pushIni(Tok);
36+
}
37+
return 1;
38+
}
39+
40+
bool AnalizadorSintactico::T(bool *ApV){
41+
if(F(ApV)) return Tp(ApV);
42+
return 0;
43+
}
44+
45+
bool AnalizadorSintactico::Tp(bool *ApV){
46+
bool Aux;
47+
Token *Tok;
48+
Toks.popIni(Tok);
49+
if(Tok!=NULL){
50+
if(Tok->getTipo()==Token::Bicondicional){
51+
if(F(&Aux)){
52+
*ApV = ((*ApV)==Aux) ? true : false;
53+
return Tp(ApV);
54+
}
55+
return 0;
56+
}
57+
Toks.pushIni(Tok);
58+
}
59+
return 1;
60+
}
61+
62+
bool AnalizadorSintactico::F(bool *ApV){
63+
if(H(ApV)) return Fp(ApV);
64+
return 0;
65+
}
66+
67+
bool AnalizadorSintactico::Fp(bool *ApV){
68+
bool Aux;
69+
Token *Tok;
70+
Toks.popIni(Tok);
71+
if(Tok!=NULL){
72+
if(Tok->getTipo()==Token::Disjuncion){
73+
if(H(&Aux)){
74+
*ApV = ( *ApV || Aux );
75+
return Fp(ApV);
76+
}
77+
return 0;
78+
}
79+
Toks.pushIni(Tok);
80+
}
81+
return 1;
82+
}
83+
84+
bool AnalizadorSintactico::H(bool *ApV){
85+
if(I(ApV)) return Hp(ApV);
86+
return 0;
87+
}
88+
89+
bool AnalizadorSintactico::Hp(bool *ApV){
90+
bool Aux;
91+
Token *Tok;
92+
Toks.popIni(Tok);
93+
if(Tok!=NULL){
94+
if(Tok->getTipo()==Token::Conjuncion){
95+
if(I(&Aux)){
96+
*ApV = ( *ApV && Aux );
97+
return Hp(ApV);
98+
}
99+
return 0;
100+
}
101+
Toks.pushIni(Tok);
102+
}
103+
return 1;
104+
}
105+
106+
bool AnalizadorSintactico::I(bool *ApV){
107+
Token *Tok;
108+
Toks.popIni(Tok);
109+
if(Tok!=NULL){
110+
if(Tok->getTipo()==Token::Negacion){
111+
flags.push(false);
112+
}
113+
if(Tok->getTipo()!=Token::Negacion){
114+
Toks.pushIni(Tok);
115+
}
116+
if(K(ApV)){
117+
while(!flags.estaVacia()){
118+
bool aux;
119+
*ApV = !*ApV;
120+
flags.pop(aux);
121+
}
122+
return 1;
123+
}
124+
}
125+
return 0;
126+
}
127+
128+
bool AnalizadorSintactico::K(bool *ApV){
129+
Token *Tok;
130+
Toks.popIni(Tok);
131+
if(Tok!=NULL){
132+
if(Tok->getTipo()==Token::Proposicion){
133+
*ApV = Tok->getValor();
134+
return 1;
135+
}
136+
if(Tok->getTipo()==Token::ParentesisIzq){
137+
if(E(ApV)){
138+
Toks.popIni(Tok);
139+
if(Tok->getTipo()==Token::ParentesisDer){
140+
return 1;
141+
}
142+
}
143+
}
144+
}
145+
return 0;
146+
}
147+
148+
149+
150+
151+
152+
153+
154+
155+
156+
157+

analizadorSintactico.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#ifndef ANALIZADORSINTACTICO_H
2+
#define ANALIZADORSINTACTICO_H
3+
4+
#include "analizadorLexico.h"
5+
6+
7+
class AnalizadorSintactico{
8+
private:
9+
int aceptable;
10+
bool resultado;
11+
Lista<Token*> Toks;
12+
Pila<bool> flags;
13+
public:
14+
AnalizadorSintactico(Lista<Token*> Tokens);
15+
int Aceptable() const;
16+
bool Resultado() const;
17+
bool E(bool *ApV);
18+
bool Ep(bool *ApV);
19+
bool T(bool *ApV);
20+
bool Tp(bool *ApV);
21+
bool F(bool *ApV);
22+
bool Fp(bool *ApV);
23+
bool H(bool *ApV);
24+
bool Hp(bool *ApV);
25+
bool I(bool *ApV);
26+
bool K(bool *ApV);
27+
};
28+
29+
#endif
30+
31+

0 commit comments

Comments
 (0)