compiler design

It's all about self learning.

First Lex Program of Complier Design

The Lexical- Analyzer Generator Lex , Structure of Lex Programs , Find out the Uppercase and lowercase of a string .
22 January, 2016     
 In this section, a tool called Lex, or in a more recent implementation Flex, That allows one to specify a lexical analyzer by specifying regular  expressions to describe patterns for tokens.The input language is referred as Lex Language and the tool itself is the Lex Complier

 How its work: The Lex complier transforms the input patterns into a transition diagram and generates code in a file called lex.yy.c, that simulates  this transition diagram.

Use of Lex:
  • Lex source program(Lex.l)  -->  LEX COMPLIER      -->   lex.yy.c
  • lex.yy.c                                   -->   C COMPLIER       -->   a.out
  • Input stream                         -->          a.out                          -->  Sequence of tokens

==>An input file lex.l is written in the Lex language and describe the lexical analyzer. The Lex complier transforms lex.l to C program , in a file that is always named lex.yy.c.
==>The C output is a.out that can take a stream of input characters and produce a stream of tokens.
To understand the complete concept , just go through this below example.
  • Install flex software(free available on Internet)
  • Write your complete code
  • Start from %{ to }
  • Save and execute the program.
  • Here is the source code of How to find out the lowercase and uppercase of a string??


int Upper=0;

int Lower=0;



[A-Z] {printf("Uppercase\t");Upper++;}

[a-z] {printf("Lowercase\t");Lower++;}




return 1;




printf("Enter a string\n");


printf("Uppercase=%d and Lowercase=%d",Upper,Lower);


Conflict resolution in LEX:
  • Always prefer a longer prefix to a shorter prefix.
  • If the longest possible prefix matches two or more patterns , prefer the pattern listed first in the lex Program.

Get solution for your websites