The Lexical- Analyzer Generator Lex , Structure of Lex Programs , Find out the Uppercase and lowercase of a string .
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.
%{
#include
int Upper=0;
int Lower=0;
%}
%%
[A-Z] {printf("Uppercase\t");Upper++;}
[a-z] {printf("Lowercase\t");Lower++;}
%%
int
yywrap()
{
return 1;
}
main()
{
printf("Enter a string\n");
yylex();
printf("Uppercase=%d and Lowercase=%d",Upper,Lower);
}