0

I'm making a lexical analyzer for C language, a less-powered version.

I want to process different kinds of regular expressions at different times for example, for the first time the input character stream from the source program and then the second time, an intermediate representation form of the program generated after first processing.

So is it possible to create 2 or more yylex() functions using FLEX and use it in a same C/C++ program, the lexical analyzer?

PalashV
  • 759
  • 3
  • 8
  • 25

2 Answers2

4

You can use the %prefix declaration to change the yy in yylex (and a variety of other global names) to something different, which allows you to have multiple scanners in the same project. You will probably also want to use the -o option to set the name of the generated file; otherwise, the build procedure gets ugly.

But they will be completely separate scanners, each with their own input stream. That might not be what you want.

If you want a scanner whose lexical definitions can be changed to another set, you need to use start conditions. That will let you change scanner behaviour in different contexts, and has the advantage that you can share common lexical features.

rici
  • 234,347
  • 28
  • 237
  • 341
1

flex has a similar parameter:

‘-PPREFIX, --prefix=PREFIX, %option prefix="PREFIX"’changes the default ‘yy’ prefix used by flex for all globally-visible variable and function names to instead be ‘PREFIX’. For example, ‘--prefix=foo’ changes the name of yytext to footext. It also changes the name of the default output file from lex.yy.c to lex.foo.c.

So you can rename the second function and its variables. Unfortunately POSIX lex has not such parameter.

rici
  • 234,347
  • 28
  • 237
  • 341
Mark Shevchenko
  • 7,937
  • 1
  • 25
  • 29