0% found this document useful (0 votes)
1K views6 pages

CD Expt 3 Implementation of A Lexical Analyzer Using Lex Tool

The document describes implementing a lexical analyzer for a subset of the C language using the lex tool. It outlines the program logic which starts the program, declares variables, prints tokens by analyzing the input program text, checks for arguments, opens a file for reading, identifies tokens by matching regular expressions, and prints the identified tokens. The program section shows the lex code with rules to recognize preprocessor directives, keywords, identifiers, strings, numbers, operators, and other tokens. It also includes a sample input and output showing the tokens identified in a C program.

Uploaded by

kannambi
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
Download as doc, pdf, or txt
0% found this document useful (0 votes)
1K views6 pages

CD Expt 3 Implementation of A Lexical Analyzer Using Lex Tool

The document describes implementing a lexical analyzer for a subset of the C language using the lex tool. It outlines the program logic which starts the program, declares variables, prints tokens by analyzing the input program text, checks for arguments, opens a file for reading, identifies tokens by matching regular expressions, and prints the identified tokens. The program section shows the lex code with rules to recognize preprocessor directives, keywords, identifiers, strings, numbers, operators, and other tokens. It also includes a sample input and output showing the tokens identified in a C program.

Uploaded by

kannambi
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1/ 6

EX.NO.

3 IMPLEMENTATION OF A LEXICAL ANALYZER USING LEX TOOL

OBJECTIVE::

To implement the lexical analyzer using lex tool for a subset of C language.

PROGRAM LOGIC:

Start the program.


Declare necessary variables and creates token representation using Regular.
Print the pre processor or directives, keywords by analysis of the input program.
In the program check whether there are arguments.
Declare a file and open it as read mode.
Read the file and if any taken in source program matches with RE that all returned as integer value.
Print the token identified using YYdex() function.
Stop the program.

PROGRAM

%{

%}

identifier[a-zA-Z][a-zA-Z0-9]*

%%

#.* {printf("\n%s is a preprocessor directive",yytext);}

int |

float |
char |

double |

while |

do |

if |

break |

continue |

void |

switch |

return |

else |

goto {printf("\n%s is a keyword",yytext);}

{identifier}\( {printf("\n function %s",yytext);}

\{ {printf("\nblock begins");}

\} {printf("\nblock ends");}

\( {printf("\n");ECHO;}

{identifier}(\[[0-9]*\])* {printf("\n%s is an identifier",yytext);}

\".*\" {printf("\n %s is a string ",yytext);}

[0-9]+ {printf("\n%s is a number",yytext);

\<= |

\>= |
\< |

\> |

\== {printf("\n %s is a relational operator",yytext);}

\= |

\+ |

\- |

\/ |

\& |

% {printf("\n %s is a operator",yytext);}

.|

\n;

%%

int main(int argc,char **argv)

FILE *file;

file=fopen("inp.c","r");

if(!file)

printf("could not open the file!!!");

exit(0);

}
yyin=file;

yylex();

printf("\n\n");

return(0);

int yywrap()

return 1;

INPUT FILE:

#include<stdio.h>

void main()

int a,b,c;

printf("enter the value for a,b");

scanf("%d%d",&a,&b)';

c=a+b;

printf("the value of c:%d",&c);

}
OUTPUT:

[3cse01@localhost ~]$ lex ex3.l

[3cse01@localhost ~]$ cc lex.yy.c

[3cse01@localhost ~]$ ./a.out

#include<stdio.h> is a preprocessor directive

void is a keyword

function main(

block begins

int is a keyword

a is an identifier

b is an identifier

c is an identifier

function printf(

"enter the value for a,b" is a string

function scanf(

"%d%d" is a string

& is a operator

a is an identifier

& is a operator

b is an identifier

c is an identifier

= is a operator

a is an identifier
+ is a operator

b is an identifier

function printf(

"the value of c:%d" is a string

& is a operator

c is an identifier

block ends

RESULT:

Thus the program to implement the lexical analyzer using lex tool for a subset of C language was
implemented and verifiedverified.

You might also like