How to Implement Type Checking in Yacc/Bison and C?

  • Thread starter Thread starter -EquinoX-
  • Start date Start date
Click For Summary
To implement type checking in Yacc/Bison with C, the user needs to ensure that the type information is correctly associated with the semantic values in the parser. The provided code attempts to access type information using `$1.type`, but it fails because the type is not properly defined in the `%union`. The user should define a union that includes a `Type` structure and ensure that the semantic actions correctly assign types to the values being processed. Additionally, type checks should be performed in arithmetic operations to validate that both operands are of the expected type, such as INTEGER. The discussion emphasizes the importance of correctly linking the parser's semantic values with the type information defined in the lexer.
-EquinoX-
Messages
561
Reaction score
1
Ok, so first I want to do a type checking to my yacc code. The partial code I have is below:

What must I add to the code so that I can access $1.type? I already defined:

typedef struct Type{
int type;
} Type

%union{
int typ;
}

%type <typ> expr

However it still doesn't work


Code:
expr   : '-' expr %prec UNARY {$$ = -$2}
       | '!' expr %prec UNARY {$$ = !$2 }
       | expr '+' expr { 
          if ($1.type != INTEGER || $3.type != INTEGER)
          {
                fprintf(stderr, "Invalid type for addition, must be an integer\n");
          }
          else
                 $$.type = INTEGER;
        }
       | expr '-' expr {
          if ($1.type != INTEGER || $3.type != INTEGER)
          {
                fprintf(stderr, "Invalid type for substraction, must be an integer\n");
          }
          else
                 $$.type = INTEGER;
        }
       | expr '*' expr { 
          if ($1.type != INTEGER || $3.type != INTEGER)
          {
                fprintf(stderr, "Invalid type for multiplication, must be an integer\n");
          }
          else
                 $$.type = INTEGER;
        }
       | expr '/' expr { 
          if ($1.type != INTEGER || $3.type != INTEGER)
          {
                fprintf(stderr, "Invalid type for division, must be an integer\n");
          }
          else
                 $$.type = INTEGER;
        }
       | expr EQ expr 
       | expr NE expr
       | expr LE expr
       | expr '<' expr
       | expr GE expr
       | expr '>' expr
       | expr AND_OP expr
       | expr OR_OP expr
       | ID opt_idexpr
       | '(' expr ')'
        | '(' error {fprintf(stderr, "Missing expression after '(' \n");}
       | INTCON {
                  $1.type = INTEGER;
                  }
       | CHARCON {
                  $1.type = CHARMARK;
                  }
       | STRINGCON {
                $1.type = STRINGMARK;
                  } 
        
;


All I want to do here is so that I can do $1.type or $2.type, so therefore I know what type is $1 or $2 and the type is basically something that I have defined in top of the file, something like:

#define INTEGER 1
#define CHAR 2
 
Technology news on Phys.org
When you create %union, it's your yylval variable in lex. Hence when your make actions for the regular expressions in lex you can access the type as yylval.type.
The $'s are the parts of your recursive tree, for i.e.
S:EXPR ',' EXPR2
$1 = EXPR
$2 = ,
$3 = EXPR2
$$ = S

you receive those from lex not from yacc.
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 31 ·
2
Replies
31
Views
7K
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 29 ·
Replies
29
Views
3K
Replies
1
Views
2K
Replies
12
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
3
Views
4K