Introduction · Compiler Design

Scanner Generator (Lex, Flex)

Watch lectures

Scanner Generator (Lex, Flex)

1. What It Does

Lex / Flex generates a scanner in C from declarative RE rules — you write patterns + actions, it builds the DFA and emits lex.yy.c with yylex().

2. Lex Program Structure

%{  C headers/declarations  %}
%%  definitions (name  RE)
%%
rule1   { action1 }
rule2   { action2 }
%%
user code (main, yywrap)

Three sections separated by %%.

3. Example

%{
#include <stdio.h>
%}
digit   [0-9]
letter  [a-zA-Z]
%%
{letter}({letter}|{digit})*  { printf("ID:%s", yytext); }
{digit}+                     { printf("NUM:%s", yytext); }
[ \t
]+                    { /* skip */ }
.                            { printf("ERR:%c", yytext[0]); }
%%
int main(){ yylex(); return 0; }
int yywrap(){ return 1; }

Key vars: yytext (matched lexeme), yyleng, yylex() returns next token.

4. Conflict Resolution (Lex rules)

  1. Longest match wins.
  2. On tie, earlier rule wins (so list keywords before identifier).
  3. Default: copy unmatched char to output.

Build: flex scan.l → lex.yy.c → gcc lex.yy.c -lfl → ./a.out < input.c

5. Exam Points

  • Lex program structure + example for identifier/number/keyword — 7 marks, very frequent.
  • yytext, yylex, yywrap roles — 3 marks, frequent.
  • Longest-match + rule-priority resolution — 3-5 marks, frequent.
Student FinanceWeb Platform

Pocketly — Personal Finance for Students

Keep your hostel and college expenses under control. Track accounts, log spending in seconds, and stay within your monthly budget.

← All Compiler Design modules