CD Notes
Complete guide to the Lex tool for automatic generation of lexical analyzers from regular expression specifications, including syntax, rules, and examples.
Introduction
Lex (and its modern successor Flex) is a tool that automatically generates lexical analyzers from a specification file. Instead of manually coding a scanner, you write regular expression patterns paired with actions, and Lex produces a C program that implements the scanner. This dramatically simplifies lexical analyzer development.
How Lex Works
| Lex Source | Lex | lex.yy.c | ||
|---|---|---|---|---|
| (spec.l) | ──> | Tool | ──> | (C source) |
Lex Specification Format
A Lex specification has three sections:
Key Variables and Functions
| Name | Type | Description |
|---|---|---|
yytext | char* | Pointer to matched text |
yyleng | int | Length of matched text |
yyin | FILE* | Input file (default: stdin) |
yyout | FILE* | Output file (default: stdout) |
yylex() | function | Main scanning function |
yywrap() | function | Called at EOF |
ECHO | macro | Print matched text |
Complete Example: Simple Scanner
Building and Running
# Generate scanner C code
flex scanner.l # produces lex.yy.c
# Compile
gcc lex.yy.c -o scanner -lfl
# Run
echo "int x = 42;" | ./scannerPattern Matching Rules in Lex
Rule Priorities
- Longest match: Lex always matches the longest possible string
- First match: If two rules match the same length, the first rule wins
Start Conditions
For context-dependent scanning:
%x COMMENT
%%
"/*" { BEGIN(COMMENT); }
<COMMENT>"*/" { BEGIN(INITIAL); }
<COMMENT>. { /* ignore content */ }
<COMMENT>\n { /* ignore newlines */ }Integration with Yacc/Bison
Lex typically provides tokens to a Yacc/Bison parser:
Advantages and Limitations
Advantages
- Rapid development of scanners
- Mathematically proven correctness
- Automatic optimization (DFA minimization)
- Easy to modify and maintain
- Well-tested code generation
Limitations
- Generated code can be larger than hand-coded
- Sometimes slower than hand-optimized scanners
- Limited to regular languages
- Debugging generated code is difficult
- Less control over error handling
Interview Questions
- What is Lex and what does it produce?
Lex is a lexical analyzer generator that takes a specification file (containing regex patterns with actions) and produces a C source file implementing a scanner. The specification uses .l extension, and the output is lex.yy.c.
- What are the three sections of a Lex specification?
(1) Declarations section (between %{ and %}) for C includes and variable declarations, (2) Rules section (between %% delimiters) containing pattern-action pairs, and (3) User code section for additional C functions like main().
- How does Lex resolve conflicts between multiple matching patterns?
Using two rules: longest match (the pattern matching the most characters wins) and first match (if multiple patterns match the same length string, the pattern listed first in the specification wins).
- What are yytext and yyleng?
yytext is a character pointer to the text of the current matched lexeme. yyleng is an integer giving the length of the matched text. Both are set automatically before each action is executed.
- What is a start condition in Lex?
A start condition is a named state that enables context-dependent scanning. Rules prefixed with <STATE> only match when that state is active. They're useful for handling multi-line comments, string literals, and other context-sensitive constructs.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Lex Tool - Lexical Analyzer Generator.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Compiler Design topic.
Search Terms
compiler-design, compiler design, compiler, design, lexical, analysis, lex, tool
Related Compiler Design Topics