-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsyntax_val.c
More file actions
106 lines (80 loc) · 1.89 KB
/
syntax_val.c
File metadata and controls
106 lines (80 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include "pila.h"
char caracter_cerrado(char signo){
if(signo == '(') return ')';
if(signo == '[') return ']';
if(signo == '{') return '}';
return '\0';
}
bool es_caracter_cerrado(char signo){
if(signo == ')') return true;
if(signo == ']') return true;
if(signo == '}') return true;
return false;
}
bool es_caracter_abierto(char signo){
if(signo == '(') return true;
if(signo == '[') return true;
if(signo == '{') return true;
return false;
}
bool es_par(char abierto, char cerrado){
if(abierto == '(' && cerrado == ')') return true;
if(abierto == '[' && cerrado == ']') return true;
if(abierto == '{' && cerrado == '}') return true;
return false;
}
bool validar_linea(char* linea){
size_t i = 0, comilla_simple = 0;
char buscado = '\0';
pila_t* pila = pila_crear();
if(!pila) return false;
while(linea[i] != '\n'){
if(es_caracter_abierto(linea[i]) && comilla_simple == 0){
pila_apilar(pila, &linea[i]);
buscado = caracter_cerrado(pila_ver_tope(pila));
}else if(linea[i] == '\''){
comilla_simple++;
if(comilla_simple == 2){
comilla_simple = 0;
buscado = caracter_cerrado(pila_ver_tope(pila));
}else{
buscado = '\'';
}
}else if(es_caracter_cerrado(linea[i]) && comilla_simple == 0){
if(!es_par(pila_desapilar(pila), linea[i])){
pila_destruir(pila);
return false;
}
buscado = caracter_cerrado(pila_ver_tope(pila));
}
i++;
}
if(buscado != '\0'){
pila_destruir(pila);
return false;
}
pila_destruir(pila);
return true;
}
void syntax_val(){
char* linea = NULL;
size_t cant = 0;
ssize_t leidos;
while((leidos = getline(&linea, &cant, stdin) > 0)){
if(validar_linea(linea)){
fprintf(stdout, "%s\n", "OK");
}else{
fprintf(stdout, "%s\n", "ERROR");
}
}
free(linea);
}
int main(){
syntax_val();
return 0;
}