Why does Bison put the entire match in the variable?

57 Views Asked by At

I am trying to use flex and bison to create a 6502 assembler (for learning purposes). My flex tokens are:

[a-zA-Z]{3} {
    yylval.a = yytext;
    return MNEMONIC;
}

\$[0-9a-fA-F]{2} {
    yylval.zp = strtol(yytext + 1, NULL, 16);
    return ZEROPAGE;
}

and the Bison grammar is this:

program:
    program expression '\n' { }
|   program '\n' { }
|   program error '\n'      { yyerrok; }
|   /* NOTHING */
;

expression:
     MNEMONIC { printf("I: %s\n", $1); }
|    MNEMONIC ZEROPAGE { printf("ZP: '%s' %d\n", $1, $2); }
;

For what I understand, when parsing LDA $99 the MNEMONIC ZEROPAGE rule should put LDA in $1 and $99 in $2, but when printing, I see that $1 is set to LDA $99.

What am I doing wrong?

0

There are 0 best solutions below