How can I handover an open file e.g. read by another scanner to the next scanner - and give it to the parser ?
相关问题
- How do I generate different yyparse functions from
- Error when attempting to use a type from GNU's
- Getting CMake to find flex on Windows
- Syntax error using empty production rule in python
- How can I send the yyleng of a matched string from
相关文章
- How to fix a missing ld library for -lfl while com
- semantic type checking analysis in bison
- Flex / Lex Encoding Strings with Escaped Character
- How to get string value of token in flex and bison
- Why does my Mac (OS X 10.7.3) have an old version
- Simple yacc grammars give an error
- What is the regex expression for CDATA
- Can Bison parse UTF-8 characters?
Flex buffers cannot easily be transferred from one scanner to another. Many details are private to the scanner and would need to be reverse-engineered, with the consequent loss of maintainability.
However, it is not difficult to combine two (or more) scanner definitions into a single scanner, provided that the semantic types are compatible. It is simply necessary to give them different start conditions. Since the start condition can be set even outside of a scanner action, it is trivial to switch from one scanner definition to the other.
Since Flex scanners are table-based, there is no real inefficiency in combining the two scanners; indeed, there may be some value in not duplicating the code. The combined table may be slightly larger than the sum of the individual tables, because there are likely to be more character equivalence classes, but on the other hand the larger table may allow better table compression. Neither of these effects is likely to be noticeable.
Here's a simple but possibly useful example. This parser reads a file and substitutes
${arithmetic expressions}
with the evaluated expression. (Since its just an example, only very basic expressions are allowed but it should be easy to extend.)Since the lexical scanner needs to start in start condition
SC_ECHO
, it needs to be initialized. Personally, I'd prefer to start inINITIAL
to avoid this initialization in this simple case, but sometimes scanners need to be able to handle various start conditions, so I left the code in. The error handling could be improved, but it's functional.The parser uses a very simple
error
rule to resynchronize and keep track of substitution errors. The semantic value of the non-terminalssubst
,file
andstart
is the error count for the file; the semantic value forexpr
is the value of the expression. In this simple case, they are both just integers so the default type foryylval
works.Unterminated substitutions are not handled gracefully; in particular, if EOF is read during the lexical scan for a substitution, no indication is inserted into the output. I leave fixing that as an exercise. :)
Here's the lexer:
The parser exports a single interface:
which returns 0 if all went well, and otherwise the number of incorrect substitutions (modulo the issue mentioned above with unterminated substitutions). Here's the file:
And a simple driver: