Write a java program lex specification to implement lexical analysis phase of compiler to count number of words,lines and characters of given input file
Answers
Answered by
22
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
..........
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
Math,
7 months ago
Math,
7 months ago
English,
7 months ago
Environmental Sciences,
1 year ago
Math,
1 year ago