Search code examples
bisonflex-lexer

Bison, difference between @1 and $1


I'm programming a Pascal compiler and already have working grammar. Now I want to start with semantic analysis but actually don't understand how it works in bison. Somehow I wrote a piece of working code by trying every option and I'm wondering what is the difference between @1 and $1.

uint_values_split_by_comma:
        UINT {
            pas::ic_label label = ctx->tab->new_label();
            ctx->tab->add_label_entry(@1, $1, label);
        }
        | uint_values_split_by_comma COMMA UINT {}
;

Also saw bison documentation, still don't get it.


Solution

  • When writing a semantic action, $n refer to a semantic value and @n to a location.

    Here is an example from the Bison documentation :

    expr: expr '+' expr   { $$ = $1 + $3; } ;
    

    $1 refer to the value of the left expression and $3 to the value of the right expression.

    About locations, in the documentation, we can read :

    Like semantic values, locations can be reached in actions using a dedicated set of constructs. In the example above, the location of the whole grouping is @$, while the locations of the subexpressions are @1 and @3.

    Here is an example of semantic location usage:

    exp:
      …
    | exp '/' exp
        {
          @$.first_column = @1.first_column;
          @$.first_line = @1.first_line;
          @$.last_column = @3.last_column;
          @$.last_line = @3.last_line;
          if ($3)
            $$ = $1 / $3;
          else
            {
              $$ = 1;
              fprintf (stderr, "%d.%d-%d.%d: division by zero",
                       @3.first_line, @3.first_column,
                       @3.last_line, @3.last_column);
            }
        }
    

    In this example, using locations allows to print a detailed error message with the precise location of the error.