Ejemplo - norman-ipn/Errores GitHub Wiki

/* Codigo C*/ %{ #include "diccionario.h" /* definiciones de insertar_tabla(), buscar_tabla()*/ DICCIONARIO *diccionario; %} //DECLARACION EN BISON

%union { int valor_entero; double valor_real; char * texto; } %token <valor_real> CONSTANTE_REAL %token <valor_entero> CONSTANTE_ENTERA %token IDENTIFICADOR

%left ’-’ ’+’ %left ’’ ’/’ %type <valor_real> expresion %% / Gramatica / input: / cadena vacia / | input linea ; linea: ’\n’ | IDENTIFICADOR ’=’ expresion ’\n’ { insertar_tabla(diccionario, $1, $3); } | expresion ’\n’ { printf ("\t%d\n", $1); } ; expresion: CONSTANTE_REAL { $$ = $1; } | CONSTANTE_ENTERA { $$ = (double) $1; } | IDENTIFICADOR { $$ = buscar_tabla(diccionario,$1); } | expresion ’+’ expresion { $$ = $1 + $3; } | expresion ’-’ expresion { $$ = $1 - $3; } | expresion ’’ expresion { $$ = $1 * $3; } | expresion ’/’ expresion { $$ = $1 / $3; } ; %% int main() { inicializar(diccionario); yyparse(); } yyerror (char *s) { printf ("%s\n", s); } int yywrap() { return 1; }

/ejemplo.l ESTO CODI9FICADO EN FLEX/

%{ #include <stdlib.h> #include <malloc.h> #include <string.h> #include "ejemplo.tab.h" %} NUM [0-9]+ %% {NUM} { yylval.valor_entero = atoi(yytext); return (CONSTANTE_ENTERA); } {NUM}.{NUM} { yylval.valor_real = atof(yytext); return (CONSTANTE_REAL); } "+"|"-"|""|"/" { return (yytext[0]); } "\n" { return (yytext[0]); } [A-Za-z] { yylval.texto = (char *) malloc (strlen(yytext) + 1); strcpy(yytext, yylval.texto); return (IDENTIFICADOR); } . ; %%

//un sencillo ejemplo en la pagina adjunta para comprender un poco el post de algunos compañeros y tambn para la compilacion

Compilacion $ bison -d ejemplo.y $ flex ejemplo.l $ gcc -o ejemplo ejemplo.tab.c lex.yy.c diccionario.c

⚠️ **GitHub.com Fallback** ⚠️