A. Dubosky
A. Dubosky

Reputation: 53

Bison Parser: trouble in print token

I already do some change , i try to change $$ to $2 but i still with error becuase i dont recognize T_NUM and T_STR , and it showing that error: $$ linha' Does not have a declared type

bison -d -o gram.c pro.y:

pro.y:45.32-33: erro: $$ linha Does not have a declared type
 linha: ESCREVER lista ';' { if($$.u.type==T_STR){
                                ^^
pro.y:46.48-49: erro: $$ de linha Does not have a declared type
                                printf("[%s]\n",$$.u.str);
                                                ^^
pro.y:49.43-44: erro: $$  linha Does not have a declared type
                                    assert($$.u.type==T_NUM);
                                           ^^
pro.y:50.50-51: erro: $$ linha Does not have a declared type
                                    printf("%d\n",$$.u.num);
                                                  ^^
Makefile:9: recipe for target 'gram.c' failed
make: *** [gram.c] Error 1

bison/yacc code(update)

%{

  #include <stdio.h>
  #include "pro.h"
  int yylex(void);
  int yyerror(const char *s);
%}

%code requires
{
enum TokenType{ T_STR ,T_NUM };

}

%union{

  struct token{

    enum TokenType type;
    union{ char *str;
          int num;
         };
               }u;
      }


%token TERMINAR ESCREVER
%token SUBTRACAO 
%token MULTIPLICACAO 
%token DIVISAO 
%token SOMA
%token<u.num> NUM 
%token<u.str> TEXTO 
%type<u> elemento
%type<u.num> expr 
%type<u> lista
%start s

%%

s:linha s
 |TERMINAR ';' {return 0;}
 ;

linha: ESCREVER lista ';' { if($$.u.type==T_STR){
                               printf("[%s]\n",$$.u.str);
                                                } 
                            else{
                                   assert($$.u.type==T_NUM);
                                   printf("%d\n",$$.u.num);


                                        }}
     | VARS
     ;

lista: elemento { $$=$1; }
     | lista ',' elemento 
     ;

elemento:TEXTO { $<u.str>$=$1;}
        |expr  { $<u.num>$=$1;}
        ;





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;   }
         ;             


%%

Upvotes: 2

Views: 972

Answers (1)

Jonathan Leffler
Jonathan Leffler

Reputation: 753725

You need to be able to tell whether you've got a u.str or u.num value to work with — this is normally done by adding a flag to indicate which you have. So, you might have a u.type element in the structure, and you'd set it to T_STR or T_NUM (values from an enumeration, perhaps), and then you can do:

if ($$.u.type == T_STR)
    printf("str: [%s]\n", $$.u.str);
else
{
    assert($$.u.type == T_NUM);  // Make sure it gets changed with more types
    printf("num: %d\n", $$.u.num);
}

You might well factor that into a function, in fact; it is often useful to be able to dump structures on demand. You'd need to think about whether the structure should have a tag so you can reference the type more easily.

%{
enum TokenType { T_STR, T_NUM };
%}

%union {

   struct token
   {
       enum TokenType type;
       union
       {
           char *str;
           int   num;
       };
   } u;
}

This uses the anonymous union types added in C11. You can now pass a struct token * into a function that prints the data appropriately.

Upvotes: 2

Related Questions