Tuesday, 4 February 2014

Lex Analyzer for Windows

Lex Analyzer for Windows:


 It's very complex to run operating system environment into one system and no one wants to use of Linux for ever for the Lex Analyzer.so Here is a tool Which will work same as Lex Analyzer.
Download this from Link Below:

   1.Flex
    2.Bason




How To Compile the Lex Programs

1. Firstly, make the lex file according to the given format and then save the file with extension ".l" such as: "file_name.l".
2. Lex files can be compiled using any Lex Analyzers like 'flex' Analyzer for Windows.
3.Use the lex program to turn into a C language program. The resulting program is in the lex.yy.c file.
4. Use the C compiler with the -ll flag to compile and link the program with a library of lex subroutines. The resulting executable program is in the a.out file.

For Example,

$$ lex file_name.l
$$ gcc lex.yy.c -ll
$$ ./a.out 

                                       OR
  
$$ lex -t file_name.l > file_name.c 
$$ cc -o file_name file_name.c -ll
 

Lex program to convert lowercase to uppercase

Lex program to convert lowercase to uppercase:
This program takes a string from the text file and convert every letter into the reverse case of it and display every character.

Program:
lower [a-z]
upper [ A-Z]
%%

{lower} { printf(" % c",yytext[0]-32);
{upper} {printf("%c",yytext[0]+32);
[\t\n]+  echo;
. echo;

%%
main( )
{
yylex();
}

Output: 
$ lex ex1.l
$ gcc ex1.c -ll
$ ./a.out<untitled.txt
mY NAME IS kHAN
gaurav singh pundir
$

and the untitled.txt file contains:
My name is Khan
GAURAV SINGH PUNDIR

 
 
 
 

LEX Program to count the number of vowels and consonants in a given string

LEX Program to count the number of vowels and consonants in a given string:

%{
int vow_count=0;
int const_count =0;
%}

%%

[aeiouAEIOU] {vow_count++;}
[a-zA-Z] {const_count++;}
%%

main()
{
printf("Enter the string of vowels and consonents:");
yylex();
printf("The number of vowels are:  %d\n",vow);
printf("The number of consonants are:  %d\n",cons);
return 0;
}


Output:
$$ lex kk.l
$$ gcc kk.c -ll
$$ ./a.out
Enter the string of vowels and consonents
My name is Khan
The number of vowels are: 4
The number of consonants are: 8
^D
$$

Lex Program to Identify the Keywords of C Language

Lex Program to Identify the Keywords of C Language:

%{
int count;
/* -----you can write any comments here-------
*  program to recognize the keywords
*/
%}

%%
 [%\t ] +         /* "+" indicates zero or more and this pattern use for ignoring the whitespaces*/

auto|
double|
if|
static|
break|
else|
int|
struct|
case|
enum|
long|
switch|
char|
extern|
near|
typedef|
const|
float|
continue|
register|

union|
unsigned|
void|
while|
default|
do|
goto|
signed|
while|signed|unsigned  {printf("C keyWord(%d) :\t %s",count,yytext);
[a-zA-Z]+ { printf("%s: is not the Keyword\n", yytext);
 %%
 main( )
{
yylex();
}

Output:

$$ lex example.l
$$ gcc example.c -ll
$$ ./a.out
while asd goto char for
C keyWord(1):        while
asd: is not the keyword
C keyWord(2):       goto
C keyWord(3):      char
C keyWord(4):       for
auto
C keyWord(5):        auto
^D
$$ 

 
 

First Lex Program

First Lex Program

%{ 
/*     */
%}
%%
is printf(" 'is' has entered");
%%

OutPut:
$$ lex first.l        
$$ gcc 1ex.yy.c -11
$$ ./a.out
is
'is' has entered
^D
$$

Explanation: whenever you will enter the is keyword it automatically display the line written into the printf statement i.e. 'is' has entered

Program Structure in Lex Analyzer

Program Structure in Lex Analyzer:

To write a program in lexical analyzer we need to aware of program structure:

%{
/* definition Section */
%}
%%
/*Rules section : Pattern and Action
%%
main( )
{
/* your program*/
}

Let's go into Detail :

Definition Section: This is especially important when we  have header files that must be included for code later in the file to work.You can include the header files and can initialize the variables that can use in any section of the entire program.

e.g.   #include<stdio.h>  /*you can include stdio.h header file for the further use.*/

Rules section : Each rule is made up of two parts: a pattern and an action, separated by whitespace. The lexer that lex generates will execute the action when it recognizes the pattern which are made up regular expressions.

e.g.  @|#|$|%   printf("Special character occured %s",yytext);

 If any special character you write at the time of execution then it will print the statement written into the printf statement and value or that character will be store in the predefined yytext variable via yylex.

User Subroutine Section: It can consist of any Legal C code. Lex copies it to the C file after the end of the lex generated code.
%%
main ( )
{
yylex();
}
The lexer produced by lex is a C routine called yylex(). Unless the actions contain explicit return statements, yylex() won't return until it has processed the entire input.