/*
Este ejemplo es una forma diferente del clasico y facilon.
(Si encuentro un parentesis entonces leo hasta el parentesis contrario.)
se procesan los datos seguidos sin ningun While ( hasta el otro parentesis)
Se procesna los datos en conjunto al encontrar el terminador de linea
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
char str[] = "#comentario\n"
"#comentario\n"
" ( (3(33+4(2(23+3)) (18+4(2+2( 3 + 3))) #Linea con parentesis sin cerrar\n"
" (3) (4) (5) #comentario\n"
" ( ((6))(44+55 (34+5) ))(88 +99(99 + 88((((99))))) ) (6- 6 / 6) #comentario\n"
"#comentario\n"
"#comentario\n"
" (1(2(3(4(5)))))\n";
typedef struct Token{
char *ptr;
char *end;
char *sep;
int orderparent;
}Token;
#define IsOperator(n) ( (n) == '*' || (n) == '+' || (n) == '/' || (n) == '-')
#define IsSeparator(n) ( (n) == '(' || (n) == ')' )
#define IsValidChar(n) ( isdigit(n) || IsOperator(n) )
#define IsNoValidChar(n) ( !IsValidChar(n) && !IsSeparator(n) && !isspace(n) )
int ReadToken(char *start, char *end){
char *raw;
char *ptr;
char *praw;
char *c;
int i;
int line = 0;
int parent = 0;
int orderparent = 0;
int COMMENT = 0;
int tkindex = 0;
int maxtoken = 50;
Token tk[50];
ptr = raw = start;
while ( start < end){
c = start++;
if ( *c == '#'){
COMMENT = 1;
} else if ( *c == '\n') {
COMMENT = 0;
}
if ( COMMENT == 0){
if ( IsValidChar(*c) ){
if ( raw == ptr)
ptr = raw = c;
raw++;
}else{
if ( IsNoValidChar(*c)){
printf("\nCaracter no permitido Linea: = %d caracter: = %c", line,*c);
return 1;
}
if ( (raw - ptr > 0) && !IsSeparator(*c))
raw++;
}
if (IsSeparator(*c) ){
*c == '(' ?parent++ : parent--;
if ( raw - ptr > 0){
if ( *c == '(')
orderparent++;
tk[tkindex].orderparent = orderparent;
tk[tkindex].ptr = ptr;
tk[tkindex].end = raw;
tkindex++;
if ( tkindex > maxtoken){
return 1;
}
}
ptr = raw = c;
} else if ( *c == '\n'){
line++;
if (tkindex > 0){ /* salta los comentarios de una linea completa*/
if ( parent != 0) {
printf("\nParentesis sin cerrrar Linea: = %d\n",line);
}
for ( i = 0; i < tkindex; i++){
ptr = tk[i].ptr ;
praw = tk[i].end ;
while ( ptr < praw){
/* con los datos recopilados, debiera ser suficiente para
*aplicar las reglas de precendencia de operadores*/
if (IsOperator(*ptr)){
//printf(" Orden = %d",tk[i].orderparent);
}
printf("%c",*ptr);
ptr++;
}
printf("\n");
}
printf("--------\n");
parent = orderparent = tkindex = 0;
ptr = raw = c;
}
}
}
}
return 0;
}
int main(void){
int size;
char *p;
size = strlen(str);
p = str;
ReadToken(p, p+size);
return 0;
}