How Can I use struct in yacc file to implement parser

37 Views Asked by At

I am trying to implement parser with lexer and I need to use struct in yacc file. definition and usage of struct in the yacc file is below

%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void yyerror(const char *s);
extern int yylex();
%}

%token KW_AND KW_OR KW_NOT KW_EQUAL KW_LESS KW_NIL KW_LIST
%token KW_APPEND KW_CONCAT KW_SET KW_DEF KW_FOR KW_IF KW_EXIT
%token KW_LOAD KW_DISP KW_TRUE KW_FALSE OP_PLUS OP_MINUS OP_DIV
%token OP_MULT OP_OP OP_CP OP_COM IDENTIFIER

%left OP_PLUS OP_MINUS
%left OP_MULT OP_DIV

%union {
    struct {
        int intval;
        char frac[10];
    }num;
    
}

%token <num> VALUEF
%type <num> exp
%type <num> IDENTIFIER 

%%

start
  : exp { printf("Result: %d%s\n", $1.num.intval, $1.num.frac); }
  | function_def { printf("#function\n"); }
  | OP_OP KW_EXIT OP_CP { printf("(exit)\n"); exit(EXIT_SUCCESS); }
  ;
 //rest of  the code

but when I run this yacc file like below

bison -d gpp_interpreter.y
flex gpp_lexer.l
gcc lex.yy.c gpp_interpreter.tab.c -o parser 

I take error like this:

gpp_interpreter.y: In function ‘yyparse’:
gpp_interpreter.y:32:40: error: ‘struct <anonymous>’ has no member named ‘num’
   32 |   : exp { printf("Result: %d%s\n", $1.num.intval, $1.num.frac); }

how can I fix this error?

0

There are 0 best solutions below