Phantom
Phantom

Reputation: 875

YACC/LEX call the parser from C file

For a project I use yacc and lex/flex to parse a file, and I'd like to call the parser from somewhere else than from the .y or the .l files.

I can call the yyparse() function, but if I just do that, it will read from stdin, so I have change yyin to make it refer to my file. But I can't access it somewhere else than in the .l file.

In the flex man page I saw the option --header-file=lex.h who seems to be like the -d for yacc, but when I use it I have an error:

lex: can not open --header-file=lex.h
/usr/bin/m4:stdin:2837: ERROR: end of file in string

So how can I access to yyin in my program, or is there an easier solution to call the parser ?

EDIT:

I tried to put extern FILE * yyin; at the top of the .l file, but it does not work.

EDIT 2:
It works when I add it in the C file.

EDIT 3:
My flex version is 2.5.39, and the command line I use is:
lex carnet.l --header-file=lex.h

Upvotes: 0

Views: 850

Answers (1)

rici
rici

Reputation: 241671

The easiest solution is to just declare it in the file where you need to use it:

extern FILE* yyin;

For the --header option to work, you need to put it before the source filename:

flex --header-file=lex.h carnet.l

because although flex implements long options, it does not conform to GNU style in which options may appear after positional arguments.

Upvotes: 1

Related Questions