CD Additional
CD Additional
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*/
%{
#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.
%{
#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.
%{
#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
%{
#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>
%}
%%
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;
}
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;
}
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