0% found this document useful (0 votes)
8 views7 pages

Wa0093.

The document contains multiple programs written in C using Lex and Yacc for various text processing tasks. These include counting lines, words, and characters, converting substrings, counting vowels and consonants, validating identifiers, and evaluating arithmetic expressions. Each program is accompanied by example input and output demonstrating its functionality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views7 pages

Wa0093.

The document contains multiple programs written in C using Lex and Yacc for various text processing tasks. These include counting lines, words, and characters, converting substrings, counting vowels and consonants, validating identifiers, and evaluating arithmetic expressions. Each program is accompanied by example input and output demonstrating its functionality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

PROGRAM-3​

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

HAI ALL WELCOME TO THE WORLD


I HOPE ALL ARE FINE

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

Enter the string:abcaaaaabc


ABCaaaaABC

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

Enter the arithmetic expression:4*6/2 ​


RESULT=12

Enter the arithmetic expression:9/0


Floating point exception (core dumped)
PROGRAM-9

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy