1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Yacc/bison and C problems

  1. Oct 13, 2008 #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 (Text):

    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
     
  2. jcsd
  3. Oct 16, 2008 #2
    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Yacc/bison and C problems
  1. C++ problem (Replies: 4)

  2. A C problem (Replies: 3)

  3. C++ problem (Replies: 11)

  4. Problems in C++ (Replies: 5)

  5. A problem with C (Replies: 4)

Loading...