0% found this document useful (0 votes)
14 views8 pages

CD Additional

1) The document describes 10 programs that use flex to perform various text processing tasks on input files. These include extracting comments from C code, removing whitespace, replacing strings, removing comments, and using yacc to validate strings based on certain patterns. Each program is accompanied by its flex/bison code and output. 2) The programs perform tasks such as extracting comments from C code, removing whitespace from text, replacing strings in a file, removing comments, and using yacc/flex to validate strings based on start/end characters and substring patterns. 3) A variety of text processing operations are demonstrated including comment extraction, whitespace removal, string replacement, comment removal, and validating strings using flex and yacc gramm

Uploaded by

snowbee07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views8 pages

CD Additional

1) The document describes 10 programs that use flex to perform various text processing tasks on input files. These include extracting comments from C code, removing whitespace, replacing strings, removing comments, and using yacc to validate strings based on certain patterns. Each program is accompanied by its flex/bison code and output. 2) The programs perform tasks such as extracting comments from C code, removing whitespace from text, replacing strings in a file, removing comments, and using yacc/flex to validate strings based on start/end characters and substring patterns. 3) A variety of text processing operations are demonstrated including comment extraction, whitespace removal, string replacement, comment removal, and validating strings using flex and yacc gramm

Uploaded by

snowbee07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

1) Extract only comments from a C program and display the same on

standard output.

%{
#include <stdio.h>
%}
%option noyywrap

%%
"//".* {
printf("%s\n", yytext);
}
"/*"([^*]|\*+[^*/])*\*+"/" {
printf("%s\n", yytext);
}
[(){}]+ {
}
\n {
}
.{
}
%%

int main() {
printf("Comments in this C code:\n");
yylex();
return 0;
}

OUTPUT:
D:\CD_LAB>flex q1.l
D:\CD_LAB>gcc lex.yy.c -o q1
D:\CD_LAB>q1 <prog.c
Comments in this C code:
//Greater number
/*If a is greater that b, print a>b*/
/*Otherwise, print b>a*/

2) Replace all nonnull sequences of white spaces in the given document


by the single blank character. Any other character is to be returned the
same.

%{
#include <stdio.h>
%}
%option noyywrap
%%
\n {
}
[ \t\r\f\v]+ {
printf(" ");
}
.{
printf("%c", yytext[0]);
}
%%
int main() {
printf("FILE AFTER REMOVING:\n");
yylex();
return 0;
}

OUTPUT:
D:\CD_LAB>flex q2.l
D:\CD_LAB>gcc lex.yy.c -o q2
D:\CD_LAB>q2 <sample1.txt
FILE AFTER REMOVING:
Hi I am Snowvita this is an example for removing occurences for whtie
spaces.

3) Replace all occurrences of “rama” with “RAMA” in the given document


file

%{
#include <stdio.h>
%}
%option noyywrap
%%
"rama" {
printf("RAMA");
}
.{
printf("%c", yytext[0]);
}
%%
int main() {
printf("FILE AFTER REPLACING:\n");
yylex();
return 0;
}

OUTPUT:
D:\CD_LAB>flex q3.l
D:\CD_LAB>gcc lex.yy.c -o q3
D:\CD_LAB>q3 <sample2.txt
FILE AFTER REPLACING:
This is RAMA.
RAMA is a computer science engineer.
RAMA lives in chennai.
4) Replace all occurrences of “rama” with “SITA” in the given document
file

%{
#include <stdio.h>
%}
%option noyywrap
%%
"rama" {
printf("SITA");
}
.{
printf("%c", yytext[0]);
}
%%
int main() {
printf("FILE AFTER REPLACING:\n");
yylex();
return 0;
}

OUTPUT:
D:\CD_LAB>flex q4.l
D:\CD_LAB>gcc lex.yy.c -o q4
D:\CD_LAB>q4 <sample2.txt
FILE AFTER REPLACING:
This is SITA.
SITA is a computer science engineer.
SITA lives in chennai.

5) Remove all the occurrences of “RAMA” and “SITA” from the file.

%{
#include <stdio.h>
%}
%option noyywrap
%%
"RAMA" {
}
"SITA" {
}
.{
printf("%c", yytext[0]);
}
%%
int main() {
printf("FILE AFTER REPLACING:\n");
yylex();
return 0;
}
OUTPUT:
D:\CD_LAB>flex q5.l
D:\CD_LAB>gcc lex.yy.c -o q5
D:\CD_LAB>q5 <sample3.txt
FILE AFTER REPLACING:
This is .
She is a computer science engineer.
She lives in chennai.
and are friends.

6) Display a file’s content by replacing “:” with \t.

%{
#include <stdio.h>
%}
%option noyywrap
%%
":" {
printf("\t");
}
.{
printf("%c", yytext[0]);
}
%%
int main() {
printf("FILE AFTER REPLACING:\n");
yylex();
return 0;
}

OUTPUT:
D:\CD_LAB>flex q6.l
D:\CD_LAB>gcc lex.yy.c -o q6
D:\CD_LAB>q6 <sample4.txt
FILE AFTER REPLACING:
1 Hello
2 This is a sample text document

7) Change all the numbers to hexadecimal in the input file ignoring all
others

%{
#include <stdio.h>
%}
%option noyywrap

%%
[0-9]+ { printf("%X", atoi(yytext)); }
. { printf("%s", yytext); }
%%

int main() {
printf("FILE AFTER REPLACING:\n");
yylex();
return 0;
}

OUTPUT:
D:\CD_LAB>flex q7.l
D:\CD_LAB>gcc lex.yy.c -o q7
D:\CD_LAB>q7 <sample5.txt
FILE AFTER REPLACING:
Give a number: CC
Adding A to the given number = D6

8) Remove C Comments from the file.

%{
#include <stdio.h>
%}
%option noyywrap

%%
"//".* {
}
"/*"([^*]|\*+[^*/])*\*+"/" {
}
\n {
}
. { printf("%s", yytext); }
%%

int main() {
printf("FILE AFTER REMOVING:\n");
yylex();
return 0;
}

OUTPUT:
D:\CD_LAB>flex q8.l
D:\CD_LAB>gcc lex.yy.c -o q8
D:\CD_LAB>q8 <prog.c
FILE AFTER REMOVING:
if (a > b){
printf("a>b");
}
else{
printf("b>a");
}
9) YACC program which accept strings that starts and ends with 0 or 1.

q9.l
%{
#include "y.tab.h"
%}

%%
0 { return ZERO; }
1 { return ONE; }
\n {
return 0;
}
.{
}
%%

int yywrap() {
return 1;
}
q9.y
%{
#include <stdio.h>
%}

%token ZERO ONE

%%
program : input {printf("Valid String\n");exit(0);}
input : ZERO expression ZERO
| ONE expression ONE
;
expression : /* empty */
| expression ZERO
| expression ONE
;
%%

int main() {
printf("Enter a string: ");
yyparse();
return 0;
}

int yyerror(char *s) {


printf("Error");
return 0;
}
OUTPUT:
D:\CD_LAB>flex q9.l
D:\CD_LAB>bison -d -y q9.y
D:\CD_LAB>gcc lex.yy.c y.tab.c -w -o q9
D:\CD_LAB>q9
Enter a string: 1110000
Invalid String

D:\CD_LAB>q9
Enter a string: 01110
Valid String

10)To accept string that begin with 11 and contains 101 and ends with 10.

q10.l
%{
#include "y.tab.h"
%}

%%
0 { return ZERO; }
1 { return ONE; }
\n {
return 0;
}
.{
}
%%

int yywrap() {
return 1;
}
q10.y
%{
#include <stdio.h>
%}
%token ZERO ONE

%%
program : input { printf("Valid String\n"); exit(0); }
;

input : ONE ONE expression ONE ZERO ONE expression ONE ZERO
;

expression : /* empty */
| expression ZERO
| expression ONE
;
%%
int main() {
printf("Enter a string: ");
yyparse();
return 0;
}

int yyerror(char *s) {


printf("Invalid String\n");
return 0;
}

OUTPUT:
D:\CD_LAB>flex q9.l
D:\CD_LAB>bison -d -y q10.y
D:\CD_LAB>gcc lex.yy.c y.tab.c -w -o q10
D:\CD_LAB>q10
Enter a string: 11111101000010
Valid String
D:\CD_LAB>q10
Enter a string: 11000010
Invalid String
D:\CD_LAB>q10
Enter a string: 010010110
Invalid String

You might also like