CD LabManual
CD LabManual
Pratical-1
a) Program to count digits, vowels, consonant and symbols in C.
Code:-
#include <stdio.h>
int main() {
char line[150];
int vowels, consonant, digit, symbol;
else {
++symbol;
}
}
return 0;
}
Output:-
Code:-
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void checkPass(char a[]);
void checkUsername(char b[]);
main()
{
char a[100], b[100];
printf("Enter your username \n");
scanf("%s",b);
printf( "Enter your password \n");
scanf("%s",a);
checkUsername(b);
checkPass(a);
}
void checkPass(char a[])
{
char c;
int len,i,flag1=0,flag2=0,flag3=0,flag4=0;
len=strlen(a);
if(len<6)
flag1=1;
else
{
for(i=0;i<len;i++)
if((a[i]>=48&&a[i]<=58))
{
flag2=0;
break;
}
else
flag2=1;
for(i=0;i<len;i++)
if((a[i]>=65&&a[i]<=90) || (a[i]>= 97 && a[i]<=122))
{
flag3=0;
break;
}
else
flag3=1;
for(i=0;i<len;i++)
if(a[i]=='@'||a[i]=='_'||a[i]=='*'||a[i]=='&'||a[i]=='$')
{
flag4=0;
break;
}
else
flag4=1;
}
if(flag1==1||flag2==1||flag3==1||flag4==1)
printf("\nWrong password\n ");
else
printf("your password is successfully validate\n");
return;
}
for(i=0;i<len;i++)
if((b[i]>=65&&b[i]<=90) || (b[i]>= 97 && b[i]<=122))
{
flag3=0;
break;
}
else
flag3=1;
for(i=0;i<len;i++)
if(b[i]=='@'||b[i]=='_'||b[i]=='*'||b[i]=='&'||b[i]=='$')
{
flag4=0;
break;
}
else
flag4=1;
}
if(flag1==1||flag2==1||flag3==1||flag4==1)
printf("\nWrong username\n");
else
printf("your username is successfully validate\n");
return;
}
Output:-
if(com[0]=='/')
{
if(com[1]=='/')
printf("\n It is a comment");
else if(com[1]=='*')
{
for(i=2;i<=30;i++)
{
if(com[i]=='*'&&com[i+1]=='/')
{
printf("\n It is a comment");
a=1;
break;
}
else
continue;
}
if(a==0)
printf("\n It is not a comment");
}
else
printf("\n It is not a comment");
}
else
printf("\n It is not a comment");
} Output:-
Pratical-2
a) Write a C program to recognize strings end with ‘ab’
Code:-
#include<stdio.h>
#include<string.h>
int main() {
char com[30];
int i=2,a=0, len = 0;
printf("\n Enter comment:");
gets(com);
len = strlen(com);
if((com[len-2]=='a' || com[len-2]=='A') && (com[len-1]=='b' || com[len-1]=='B'))
{
printf("\n recognize strings end with ab");
}
else{
printf("\n not recognize strings end with ab");
}
return 0;
}
Output:-
#include<stdio.h>
#include<conio.h>
void main()
{
char s[5];
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// Returns 'true' if the character is a DELIMITER.
bool isDelimiter(char ch)
{
if (ch == ' ' || ch == '+' || ch == '-' || ch == '*' ||
ch == '/' || ch == ',' || ch == ';' || ch == '>' ||
ch == '<' || ch == '=' || ch == '(' || ch == ')' ||
ch == '[' || ch == ']' || ch == '{' || ch == '}')
return (true);
return (false);
}
if (len == 0)
return (false);
for (i = 0; i<len; i++) {
if (str[i] != '0' && str[i] != '1' && str[i] != '2'
&& str[i] != '3' && str[i] != '4' && str[i] != '5'
&& str[i] != '6' && str[i] != '7' && str[i] != '8'
&& str[i] != '9' && str[i] != '.' ||
(str[i] == '-' &&i> 0))
return (false);
if (str[i] == '.')
hasDecimal = true;
}
return (hasDecimal);
}
right++;
left = right;
} else if (isDelimiter(str[right]) == true && left != right
|| (right == len&& left != right)) {
char* subStr = subString(str, left, right - 1);
if (isKeyword(subStr) == true)
printf("'%s' IS A KEYWORD\n", subStr);
// DRIVER FUNCTION
int main()
{
// maximum length of string is 100 here
char str[100] = "char x = y + 1z; ";
return (0);
}
Output:-
#include<stdio.h>
%}
%%
[a-zA-Z] chars++;
\n {++lines;++words;};
[\t' '] ++words;
%%
main(void)
{
yyin= fopen("input.txt","r");
yylex();
int yywrap()
{
return(1);
}
Output:-
b) Write a Lex program to count number of vowels and consonants in a given input
string.
Code:-
%{
#include<stdio.h>
%}
%%
[aeiouAEIOU] vowels++;
[a-zA-Z] consonants++;
%%
main(void)
{
yyin= fopen("input1.txt","r");
yylex();
}
int yywrap()
{
return(1);
}
}
Output:-
c)Write a Lex program to print out all numbers from the given file.
Code:-
%{
#include<stdio.h>
%}
%%
main(void)
{
yyin= fopen("input.txt","r");
yylex();
int yywrap()
{
return(1);
Output:-
d) Write a Lex program which adds line numbers to the given file and display the same onto the
standard output.
Code:-
%{
int line_number = 1;
%}
line .*
%%
{line} { fprintf(yyout,"%10d %s", line_number++, yytext); }
%%
int yywrap(){}
yyin = fopen("input.txt","r");
yyout = fopen("output.txt","w");
yylex();
return 0;
}
Output:-
e) Write a Lex program to printout all HTML tags in file.
Code:-
%{
%}
%%
"<"[^>]*> {printf("%s\n", yytext); }
.;
%%
int yywrap(){}
int main(int argc, char*argv[])
{
yyin=fopen("input.txt","r");
yylex();
return 0;
}
Output:-
Practical 5
a) Write a Lex program to count the number of comment lines in a given C program. Also
eliminate them and copy that program into separate file.
Code:
%{
#include<stdio.h>
int single=0;
int multi=0;
%}
%%
"//".*\n { ++single; fprintf(yyout,"%s", " ");}
"/*"[^*/]*"*/" { ++multi; fprintf(yyout,"%s", " ");}
. { fprintf(yyout,"%s", yytext);}
%%
int yywrap(){}
int main(int argc, int **argv)
{
extern FILE *yyin;
yyin=fopen("input.txt","r");
yyout =fopen("output.txt", "w");
yylex();
printf("\nNo of single line comment = %d ", single);
printf("\nNo of multi line comment = %d ", multi);
return 0;
}
Input File:
Output:
%{
#include<stdio.h>
int con=0,num=0,c=0,space=0,vowels=0,w=0,line=0;
%}
%%
"if"|"else"|"break"|"continue"|"main"|"void"|"float"|"int"|"double"|"do"|"switch"|"for" {printf("\n
%s: Keyword",yytext);}
[A-za-z_][A-Za-z0-9_]* {printf("\n%s: Identifier",yytext);}
[ \t\n] ;
"//". num++;
"/*"+[^"*/"]+"*/" con++;
.;
%%
main(void)
{yyin= fopen("input.txt","r");
yylex();
printf("Single Line Comment %d",num);
printf("\nMultiline comment %d",con);}
int yywrap()
{return(1);}
Output:-
Practical 6
a. Program to implement Recursive Descent Parsing in C.
Code:
#include<stdio.h>
#include<string.h>
#include<ctype.h>
char input[10];
int i,error;
void E();
void T();
void Eprime();
void Tprime();
void F();
main()
{
i=0;
error=0;
printf("Enter an arithmetic expression : "); // Eg: a+a*a
gets(input);
E();
if(strlen(input)==i&&error==0)
printf("\nAccepted..!!!\n");
else printf("\nRejected..!!!\n");
}
void E()
{
T();
Eprime();
}
void Eprime()
{
if(input[i]=='+')
{
i++;
T();
Eprime();
}
}
void T()
{
F();
Tprime();
}
void Tprime()
{
if(input[i]=='*')
{
i++;
F();
Tprime();
}
}
void F()
{
if(isalnum(input[i]))i++;
else if(input[i]=='(')
{
i++;
E();
if(input[i]==')')
i++;
else error=1;
}
else error=1;
}
Output:
Practical 7.
a. Create Yacc and Lex specification files to recognizes arithmetic expressions involving
+, -, * and /.
Code:
%{
#include <stdio.h>
int yylex(void);
void yyerror(char *);
%}
%token INTEGER
%%
S: S E '\n' { printf("valid \n"); }
|;
E:E '+' E ;
| E '-' E ;
|E '*' E ;
|E '/' E ;
| INTEGER ;
%%
Output:
b. Create Yacc and Lex specification files are used to generate a calculator which accepts
integer and float type arguments.
Code: (b.y)
%{
#include <stdio.h>
int yylex(void);
void yyerror(char *);
%}
%token INTEGER
%%
S:
S E '\n' { printf("%d\n", $2); }
|;
E:E '+' E { $$ = $1 + $3; }
| E '-' E { $$ = $1 - $3; }
|E '*' E { $$ = $1 * $3; }
|E '/' E { $$ = $1 / $3; }
| INTEGER { $$ = $1; }
%%
void yyerror(char *s) {
fprintf(stderr, "%s\n", s);
}
int main() {
yyparse();
return 0;
}