Compiler Design and Construction - Old Questions

2.  Design a lexical analyzer generator and explain it.

6 marks | Asked in 2071-II

Lexical Analyzer Generator (Lex/Flex) systematically translate regular definitions into C source code for efficient scanning. Generated code is easy to integrate in C applications. Flex is a latter version of lex.

  • Firstly, specification of a lexical analyzer is prepared by creating a program lex.l in Lex/Flex
  • lex.l is run into lex compiler to produce a C program lex.yy.c
  • lex.yy.c consists of the tabular representation of lex.l, together with a standard routine that uses the table to recognize lexeme.
  • Lex.yy.c is run through C compiler to produce the object program a.out which is a lexical analyzer that input into tokens.

flex specification consists of three parts:

1. regular definitions, C declarations in %{ %}


2. translation rules


3. user-defined auxiliary procedures

The translation rules are of the form:

    p1     { action1 }

    p2     { action2 }


    pn     { actionn }

Lex/flex regular definitions are of the form:

       name definition

E.g. Digit   [0-9]

Example of lex specification


    #include <stdio.h>



[0-9]+   { printf(“%s\\n”, yytext); }

| \\n     {  }



{ yylex();