Comparison with Flex/Bison
This chapter compares OpenLexer with GNU Flex and Bison.
Feature Comparison
| Feature | Flex | Bison | OpenLexer |
|---|---|---|---|
| Lexer generation | Yes | No | Yes |
| Parser generation | No | Yes | Yes |
| C output | Yes | Yes | Yes |
| Java output | JFlex | CUP/ANTLR | Yes |
| Python output | PLY | PLY | Yes |
| GLR parsing | No | Yes | Yes |
| LALR(1) | N/A | Yes | Yes |
| Start conditions | Yes | N/A | Yes |
| Precedence | N/A | Yes | Yes |
| Unicode | Limited | N/A | Planned |
| Web (WASM) | No | No | Yes |
File Format Compatibility
Lexer Files (.l)
OpenLexer supports most Flex syntax:
Supported:
- Definitions section with
%{ %}code blocks - Named patterns
- Rules with actions
- Start conditions (
%x,%s) - Character classes
[a-z] - Predefined classes
[:alpha:] - Quantifiers
*,+,?,{n,m} - Alternation
| - Grouping
() - Literal strings
"..."
Not supported:
%optiondirectivesyymore(),yyless()REJECT- Multiple input buffers
- Interactive mode
Grammar Files (.y)
OpenLexer supports most Bison syntax:
Supported:
%tokendeclarations%uniontype declarations%typenon-terminal types%left,%right,%nonassocprecedence%startsymbol%preccontextual precedence%glr-parserGLR mode- Semantic actions with
$$,$1, etc. errortoken for recovery
Not supported:
%defineoptions%codeblocks (use%{ %}instead)%destructor%printer- Lookahead correction (
%define lr.type ielr) - Named references (
$nameinstead of$1)
Migration Guide
From Flex
- Remove
%optionlines - Replace
yymore()with manual buffer handling - Replace
REJECTwith explicit rules - Test start condition behavior
From Bison
- Replace
%definewith equivalent features - Move
%codeblocks to%{ %} - Test GLR behavior if used
- Verify error recovery patterns
Performance
OpenLexer generates table-driven lexers and parsers similar to Flex/Bison. Performance characteristics:
- Lexer: O(n) where n is input length
- LALR Parser: O(n) for unambiguous grammars
- GLR Parser: O(n) to O(n^3) depending on ambiguity
The generated code size is comparable to Flex/Bison output.
When to Use OpenLexer
Use OpenLexer when:
- You need multi-language output
- You want a single cross-platform tool
- You need integrated lexer + parser generation
- You want GLR parsing
Use Flex/Bison when:
- You need maximum C performance optimization
- You require specific Flex/Bison features not in OpenLexer
- You have existing build infrastructure for Flex/Bison