17

Other people have had the following problem that I am having but I can't find anyone that has reported a solution.. getting Flex to spot the EOF (end of file). I need Flex to find EOF and return a token indicating that it has found it so it can tell Yacc/Bison that it has reached the end of an input source file and can report a successful parse.

Note that this question is not the same as this one because this is about Lex/Flex.

Any help would be awesome. Thank you.

Community
  • 1
  • 1
ale
  • 11,636
  • 27
  • 92
  • 149
  • 1
    Something wrong here. You normally don't need to do this. Just write a target symbol into your grammar and it's all taken care of. – user207421 May 06 '13 at 13:28
  • @EJP: Could you please make a little example? I am a newbie in Flex/Bison. How to achieve this as a simple grammar rule? Thankyou – Andry Sep 26 '13 at 22:03
  • @EJP, when entering from a real keyboard, Ctrl-Z is passed to the program, causing a syntax error. I am unable to catch it using things like /x1a but <> works. – Sam Hobbs Apr 20 '17 at 06:51

1 Answers1

34

Flex has <<EOF>> pattern which matches only end-of-file. Probably you can return some token indicating end-of-file to the parser as the following.

<<EOF>>     return END_OF_FILE;
rici
  • 234,347
  • 28
  • 237
  • 341
Ise Wisteria
  • 11,259
  • 2
  • 43
  • 26