Common tokens for bending and bison

I have one file with declarations of my tokens declarations.h:

#define ID 257 #define NUM 258 ... 

In my flex code, I return one of these values ​​or characters (e.g. '+', '-', '*'). And everything works.

The problem is in the bison file. If I write something like this: exp: ID '+' ID I will get an error because the bison knows nothing about the ID. Adding the% token line id will not help, because in this case I will have a compilation error (the preprocessor will change the ID to 257, and I will get 257 = 257)

+4
source share
1 answer

You get Bison to create a list of tokens; your lexer uses the list created by Bison.

 bison -d grammar.y # Generates grammar.tab.c and grammar.tab.h 

Then your lexer uses grammar.tab.h :

 $ cat grammar.y %token ID %% program: /* Nothing */ | program ID ; %% $ cat lexer.l %{ #include "grammar.tab.h" %} %% [a-zA-Z][A-Za-z_0-9]+ { return ID; } [ \t\n] { /* Nothing */ } . { return *yytext; } %% $ bison -d grammar.y $ flex lexer.l $ gcc -o testgrammar grammar.tab.c lex.yy.c -ly -lfl $ ./testgrammar id est quod erat demonstrandum $ 

Bison 2.4.3 on MacOS X 10.7.2 generates token numbers as enum , not as a series of #define values ​​— to get the names of tokens in the symbol table for debuggers (a very good idea!).

+7
source

Source: https://habr.com/ru/post/1384573/


All Articles