Write a program lex specification to implement lexical anasysis phase of compiler to count number of wors,lines and characters of given input file in java
Answers
Answered by
23
ANSWER
............
int ch=0,
bl=0,
ln=0,
wr=0;
%}
%%
[\n] {ln++;wr++;}
[\t] {bl++;wr++;}
[" "
] {bl++;wr++;
}
[^\n\t] {ch++;}
%%
int main()
{
FILE *fp;
char file[10];
printf
("Enter the filename: ");
scanf("%s", file);
yyin=fp;
yylex();
printf(
"Character=%d\nBlank=%d\nLines=%d\nWords=%d", ch, bl, ln, wr);
return 0;
}
Output ->
$cat > input
Girish rao salanke
$lex p1a.l
$cc lex.yy.c –ll
$./a.out
Enter the filename: input
Character=16
Blank=2
Lines=1
Word=3
Similar questions
English,
8 months ago
Hindi,
8 months ago
India Languages,
1 year ago
India Languages,
1 year ago
English,
1 year ago
Math,
1 year ago
Math,
1 year ago