Re: [Flex-help] how to use flex for two different purposes in one main()
flex is a tool for generating scanners
Brought to you by:
wlestes
From: Rui M. <rui...@gm...> - 2010-01-09 15:10:00
|
Michael Chen wrote: > My user has the freedom to input data in two different formats, > either a single file *.x or a single file *.y. I wrote a parser using > flex for each format separately. Now I want to combine them under one > main(), which identifies the input format and choose the right parser. Is it possible to identify the file formats through the file extension or do you need to parse some sort of header section in order to tell them apart? > Using start state seems not a good idea to me as it complicate many > things. I guess it is a compiling and linking thing, but not sure how > to do it. Thanks in advance for your idea. You will certainly experience a compiler problem if you haven't configured your flex source code files to generate the code for the different lexers using different prefixes. If you haven't done that then you will end up with multiple functions carrying the same function name, which ends up causing all sorts of compiler errors. In order to avoid that you only need to configure lex to use different prefixes for each lexer, which you get through flex's prefix option. So, for example, if you add the following: %option prefix="my_x" ...to your .x format lexer then I believe this problem will go away. And it would be good (symmetry and all) if you also reconfigured the prefix for your .y format, just so that your source code tree looks prettier. Rui Maciel |