Wa0093.
Wa0093.
LINES,WORDS,CHARACTER
%{
#include<stdio.h>
int lc=0,ch=0,wc=0;
%}
%%
[\n] { lc++;ch+=yyleng;}
[^\t\n ]+ { wc++;ch+=yyleng;}
. {ch+=yyleng;}
%%
int yywrap(){ return 1; }
void main(){
yyin=fopen("input.txt","r");
yylex();
printf("Number of lines : %d\n",lc);
printf("Number of words: %d \n",wc);
printf("Number of character: %d \n",ch);
}
INPUT.TXT
Output
Number of lines : 2
Number of words: 11
Number of character: 50
PROGRAM 4
//SUBSTRING CONVERSION
%{
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
int i;
%}
%%
[a-zA-Z]* {
for (i = 0; i < yyleng; i++) {
if ((yytext[i] == 'a') && (yytext[i+1] == 'b') && (yytext[i+2] == 'c'))
{
yytext[i] = toupper(yytext[i]);
yytext[i+1] = toupper(yytext[i+1]);
yytext[i+2] = toupper(yytext[i+2]);
}
}
printf("%s", yytext);
}
%%
void main()
{
printf("Enter the string:");
yylex();
}
OUTPUT
PROGRAM 5
Input.txt
Machine learning is a type of artificial intelligence (AI) that enables computers to learn from data
and make decisions or predictions without being explicitly programmed for every task.
%{
#include<stdio.h>
#include<stdlib.h>
int vc= 0;
int cc= 0;
%}
%%
[aeiouAEIOU] { vc++; }
[a-zA-Z] { cc++; }
%%
int main()
{
yyin=fopen("input.txt","r");
yylex();
printf("VOWELS: %d\n", vc);
printf("CONSONANTS: %d\n", cc);
return 0;
}
Output
VOWELS: 62
CONSONANTS: 94
PROGRAM 6
Vi.l
%{
#include "y.tab.h"
%}
%%
[0-9]+ {return NUMBER;}
[a-zA-Z]+ {return ID;}
. {return yytext[0];}
\n {exit(0);}
%%
Vi.y
%{
#include <stdio.h>
#include <stdlib.h>
#include "lex.yy.c"
int yyerror();
%}
%token NUMBER ID;
%%
var: ID {printf("\nvalid identifier\n");}
| var ID
| var NUMBER
;
%%
int yyerror(){
printf("\ninvalid identifier\n");
exit(0);
}
int main(){
printf("Enter a string:");
yyparse();
return 0;
}
OUTPUT
Enter a string:B24
valid identifier
Enter a string:12C
invalid identifier
PROGRAM-7
Ae.l
%{
#include "y.tab.h"
extern int yylval;
%}
%%
[0-9]+ { return NUMBER; }
[a-zA-Z_] { return CH; }
. { return yytext[0]; }
\n { return 0; }
%%
Ae.y
%{
#include <stdio.h>
#include "lex.yy.c"
int yyerror();
int f = 0;
%}
%token NUMBER CH
%left '+' '-'
%left '*' '/'
%%
E : E '+' E { f = 1; }
| E '-' E { f = 1; }
| E '*' E { f = 1; }
| E '/' E { f = 1; }
| '(' E ')' { f = 1; }
| NUMBER
| CH
;
%%
int main() {
printf("Enter the expression:");
yyparse();
if (f == 1) {
printf("\nValid expression\n");
}
else {
printf("Invalid expression\n");
}
return 0;
}
int yyerror() {
printf("Invalid expression\n");
exit(0);
}
PROGRAM-8
Calc.l
%{
#include <stdlib.h>
#include "y.tab.h"
extern int yylval;
%}
%%
[0-9]+ { yylval = atoi(yytext); return NUMBER; }
. { return yytext[0]; }
\n { return 0; }
%%
Calc.y
%{
#include <stdio.h>
#include "lex.yy.c"
int yyerror();
%}
%token NUMBER
%left '+''-'
%left '*''/'
%%
AE : exp { printf("RESULT=%d\n", $$); }
exp : exp '+' exp { $$ = $1 + $3; }
| exp '-' exp { $$ = $1 - $3; }
| exp '*' exp { $$ = $1 * $3; }
| exp '/' exp { $$ = $1 / $3; }
| '(' exp ')' { $$ = $2; }
| NUMBER
;
%%
int main() {
printf("Enter the arithmetic expression:");
yyparse();
return 0;
}
int yyerror() {
printf("Invalid expression\n");
exit(0);
}
OUTPUT