Professional Documents
Culture Documents
1. Overview of Lex:
Lex is a program that generates lexical analyzer. It is used with YACC parser generator. The lexical analyzer is
a program that transforms an input stream into a sequence of tokens. It reads the input stream and produces the
source code as output through implementing the lexical analyzer in the C program
2. Tasks to be done:
3. Steps for practical: (Mention the steps for each and every task)
C File
Code
#include<stdio.h>
int word=0,space=0,line=0,chr=0;
%}
%%
([a-zA-Z0-9])* {word++;}
"\n" {line++;}
. {chr++;}
%%
main()
yyin=fopen("myfile.c","r");
yylex();
printf("Words: %d \n",word);
printf("Lines: %d \n",line);
printf("Characters %d \n",chr);
int yywrap()
return 1;
6. Result/Output/Writing Summary:
Department of Computer Science and Engineering, AIT
We successfully counted the number of Characters, white spaces and lines in a C
program file using Lex tool.
Evaluation Grid (To be created as per the SOP and Assessment guidelines by the faculty):