0

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

Phantom
  • 833
  • 1
  • 9
  • 26
  • 1
    possible duplicate of [in lex how to make yyin point to a file with the main function in yacc?](http://stackoverflow.com/questions/1796520/in-lex-how-to-make-yyin-point-to-a-file-with-the-main-function-in-yacc) – Sami Kuhmonen Aug 16 '15 at 13:29
  • OK, now that I can see your command line, I edited the answer to explain why that command doesn't work. – rici Aug 18 '15 at 15:26

1 Answers1

1

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.

rici
  • 234,347
  • 28
  • 237
  • 341
  • My version of flex is 2.5.39, and it seems to be the latest. – Phantom Aug 16 '15 at 13:46
  • @phantom: how are you invoking it, then? Are you sure that `lex` on your system invokes `flex`? – rici Aug 16 '15 at 14:01
  • @phantom: Also, I clarified my answer. You declare `yyin` *where you use it*. – rici Aug 16 '15 at 14:05
  • What can invoke lex else ? And how can I know if lex invokes something else ? – Phantom Aug 16 '15 at 14:07
  • @phantom: You could start with `lex --version` and see if it says 2.5.39. Other useful tools: `which lex` and `readlink` (to resolve the likely symlink). If all that checks out, put the *exact* command you used to invoke flex *into your question*. – rici Aug 16 '15 at 14:10
  • lex call `/usr/bin/` who is an alias to `/usr/bin/flex`. So, yes I think lex invokes flex. – Phantom Aug 16 '15 at 14:17
  • @phantom: then you are invoking it incorrectly. But if you don't show your command line, no-one can help you. – rici Aug 16 '15 at 15:34