1
ERROR while doing integer printf When I try to enter the text : WRITE 10; TERMINATE; The Segmentation Failure error (saved core image) appears, since the list is string type and I cannot print integers.
Follow the code YACC/BISON:
%{
#include <stdio.h>
int yylex(void);
int yyerror(const char *s);
char stR[20];
%}
%union{
char *str; /* para strings*/
int num; /* para inteiros */
}
%token TERMINAR ESCREVER
%token SUBTRACAO
%token MULTIPLICACAO
%token DIVISAO
%token SOMA
%token<num> NUM
%token<str> TEXTO
%type<str> elemento
%type<num> expr
%type<str> lista
%start s
%%
s:linha s
|TERMINAR ';' {return 0;}
;
linha: ESCREVER lista ';' {printf("%s",$2);}
| VARS
;
lista: elemento { $$=$1; }
|lista ',' elemento
;
elemento:TEXTO { $$=$1; }
|expr
;
VARS :
| NUM
| TEXTO
| expr
| TEXTO '=' VARS ';' /* para delcaracoes */
;
expr : NUM SOMA expr {$$=$1+$3;}
| NUM SUBTRACAO expr {$$=$1-$3;}
| NUM MULTIPLICACAO expr {$$=$1*$3;}
| NUM DIVISAO expr {$$=$1/$3;}
| NUM '+' expr {$$=$1+$3;}
| NUM '=' expr {$$=$1=$3;}
| NUM '-' expr {$$=$1-$3;}
| NUM '*' expr {$$=$1*$3;}
| NUM '/' expr {$$=$1/$3;}
| NUM {$$=$1; }
;
%%
Lex code
%{
#include <stdio.h>
#include <stdlib.h>
#include "gram.h"
int yyerror(const char *s);
%}
%%
"ESCREVER" {return ESCREVER; }
"TERMINAR" {return TERMINAR; }
[0-9]+ { yylval.num =atoi(yytext);
return NUM; }
[A-Za-z0-9]* { yylval.str=strdup(yytext);
return TEXTO;}
"/" |
"-" |
"+" |
"*" |
"=" |
"." |
";" |
"," {return yytext[0]; }
[ \n\t] { }
. { return yytext[0]; }
%%
int yywrap(){ return 1; }
Missing include your lexer (and which commands you use to compile).
– hugomg
submeti. the name of my program is pro gcc -o pro lex. c gram. c, gram. c gram. h: pro. y, Bison -d -o gram. c pro. y, lex. c: pro. l gram. h, flex -o lex. c pro. l
– Dubosky dumont