Hi! I've been working for hours and I can't figure out how to print the token from the bison file, the bison file is this: (it's a short simple file) This is the modified version: the solution to the problem:
%{
#include <stdio.h>
#include <stdlib.h>
void yyerror(const char *);
int yylex(void);
int id;
%}
%union {
int d;
}
%error-verbose
%token <d> ID
%%
instruction: ID { //yylval.d is set in the FLEX file, this prints the ID entered
printf("The id is:%d\n",yylval.d);};
%%
int main(){
if (yyparse()==0)
printf("Finished.\n");
else
printf("Error\n");
}
void yyerror(char const *s)
{
fprintf(stderr,"err %s\n",s);
}
And the Flex file is this:
%{
#include "sample.tab.h"
#include <math.h>
%}
ID [0-9]
ENTER "\n"
SPACE [ \t\n]+
%%
{ID} {//the "d" is from the union of the bison file, it connects to it
yylval.d=atoi(yytext);return(ID);}
{ENTER} {}
{SPACE} {}
. {printf("Strange character: %s\n", yytext);}
%%
What I want to do after I get this, is to store the value of ID in a table and when I read another ID if it is repeated I would mark an error and say: repeated id! For now this codes tells me: "error request for member 'd' in something not a structure or union".
Please help!!! I've looked on internet and haven't found anything! =(