Question

I am programming a lexer in C and I read somewhere about the header file tokens.h. Is it there? If so, what is its use?

Was it helpful?

Solution

tokens.h is a file generated by yacc or bison that contains a list of tokens within your grammar.

Your yacc/bison input file may contain token declarations like:

%token INTEGER
%token ID
%token STRING
%token SPACE

Running this file through yacc/bison will result in a tokens.h file that contains preprocessor definitions for these tokens:

/* Something like this... */
#define INTEGER (1)
#define ID      (2)
#define STRING  (3)

OTHER TIPS

Probably, tokens.h is a file generated by the parser generator (Yacc/Bison) containing token definitions so you can return tokens from the lexer to the parser.

With Lex/Flex and Yacc/Bison, it works like this:

parser.y:

%token FOO
%token BAR

%%

start: FOO BAR;

%%

lexer.l:

%{
#include "tokens.h"
%}

%%

foo {return FOO;}
bar {return BAR;}

%%
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top