Professional Documents
Culture Documents
LAB Manual: School of Engineering and Technology
LAB Manual: School of Engineering and Technology
LAB Manual
Navrachana University
School of Engineering and Technology
Department
of
Computer Science and Engineering
Semester – VI
TABLE OF CONTENT
Practical 1
Code:
#include <stdio.h>
void main()
{
char str[100];
int i, vow_count = 0, conso_count = 0, digi_count = 0, spaces = 0,
sym_count = 0;
Output:
Code:
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
void main()
{
char pass[20], ch;
int i, number, special, capital, small, length;
length = 1;
}
if (number == 0 || special == 0 || capital == 0 || small == 0 || length ==
0)
{
Output:
Code:
#include <stdio.h>
void main()
{
char str[100];
int i = 2, flag = 0;
{
if (str[i] == '*' && str[i + 1] == '/')
{
printf("\n Multiline comment");
flag = 1;
break;
}
else
continue;
}
if (flag == 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:
CD Lab Manual Kalp Adhwaryu - 19124065
Practical 2
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void main()
{
char str[100], c;
int state = 0, i = 0;
printf("Enter a string :");
gets(str);
while (str[i] != '\0')
{
switch (state)
{
case 0:
c = str[i++];
if (c == 'a')
state = 1;
else if (c == 'b')
state = 0;
else
state = 3;
break;
case 1:
c = str[i++];
if (c == 'a')
state = 1;
else if (c == 'b')
state = 2;
else
state = 3;
break;
case 2:
c = str[i++];
if (c == 'a')
state = 1;
else if (c == 'b')
state = 0;
else
state = 3;
i++;
CD Lab Manual Kalp Adhwaryu - 19124065
break;
case 3:
printf("String not acceptable");
exit(0);
default:
break;
}
}
if (state == 2)
printf("String accepted and ends with ab");
else
printf("String accepted but doesn't end with ab");
}
Output:
Code:
#include <stdio.h>
#include <conio.h>
void main()
{
char s[5];
printf("\n Enter any operator:");
gets(s);
switch (s[0])
{
case '>':
if (s[1] == '=')
printf("\n Greater than or equal");
else
printf("\n Greater than");
break;
case '<':
CD Lab Manual Kalp Adhwaryu - 19124065
if (s[1] == '=')
printf("\n Less than or equal");
else
printf("\nLess than");
break;
case '=':
if (s[1] == '=')
printf("\nEqual to");
else
printf("\nAssignment");
break;
case '!':
if (s[1] == '=')
printf("\nNot Equal");
else
printf("\n Bit Not");
break;
case '&':
if (s[1] == '&')
printf("\nLogical AND");
else
printf("\n Bitwise AND");
break;
case '|':
if (s[1] == '|')
printf("\nLogical OR");
else
printf("\nBitwise OR");
break;
case '+':
printf("\n Addition");
break;
case '-':
printf("\nSubstraction");
break;
case '*':
printf("\nMultiplication");
break;
case '/':
printf("\nDivision");
break;
case '%':
printf("Modulus");
break;
default:
printf("\n Not a operator");
}
}
CD Lab Manual Kalp Adhwaryu - 19124065
Output:
Aim 3: Design a lexical analyzer to identify from given pattern is keyword or identifier.
Code:
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
void main()
{
char str[10];
int flag, i = 1;
printf("\n Enter a string:");
gets(str);
if (isalpha(str[0]))
flag = 1;
else
printf("\n Not a valid identifier");
while (str[i] != '\0')
{
if (!isdigit(str[i]) && !isalpha(str[i]))
{
flag = 0;
break;
}
else if (strcmp("for", str) == 0 || strcmp("while", str) == 0 ||
strcmp("do", str) == 0 || strcmp("int", str) == 0 || strcmp("float", str) == 0
|| strcmp("char", str) == 0 || strcmp("double", str) == 0 || strcmp("static",
str) == 0 || strcmp("switch", str) == 0 || strcmp("case", str) == 0)
flag = 3;
i++;
CD Lab Manual Kalp Adhwaryu - 19124065
}
if (flag == 1)
printf("\n Valid identifier");
else if (flag == 3)
printf("\nIt's a keyword");
}
Output:
Code:
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
{
if (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] == '-' && i > 0))
return (false);
}
return (true);
CD Lab Manual Kalp Adhwaryu - 19124065
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);
int main()
{
Output:
CD Lab Manual Kalp Adhwaryu - 19124065
Practical 3
Aim: To Study about Lexical Analyzer Generator(LEX) and Flex(Fast Lexical Analyzer)
Code:
Sample.l:
%{
#include<stdio.h>
%}
%%
[a-z] s_letters++;
[A-Z] c_letters++;
[0-9] num++;
%%
main(void)
yyin= fopen("myfile.txt","r");
yylex();
printf("\nThis File contains ...");
printf("\n%d small letters", s_letters);
printf("\n%d cap letters", c_letters);
printf("\n%d digits", num);
int yywrap()
return(1);
Myfile.txt:
CD Lab Manual Kalp Adhwaryu - 19124065
Output:
Practical 4
CD Lab Manual Kalp Adhwaryu - 19124065
Aim 1: Create a Lexer to take input from text file and count no of characters, no. of lines &
no. of words.
Code:
Sample.l file:
%{
#include<stdio.h>
%}
%%
[a-zA-Z] chars++;
\n { lines++; words++;}
[\t ' '] words++;
%%
main(void)
{
yyin= fopen("myfile.txt","r");
yylex();
int yywrap()
{
return(1);
Myfile.txt:
CD Lab Manual Kalp Adhwaryu - 19124065
Output:
Aim 2: Write a Lex program to count number of vowels and consonants in a given input
string.
Code:
Sample.l:
%{
int vow_count=0;
int const_count =0;
%}
%%
[aeiouAEIOU] {vow_count++;}
[a-zA-Z] {const_count++;}
%%
int yywrap(){}
int main()
{
printf("Enter the string of vowels and consonents:");
yylex();
printf("Number of vowels are: %d\n", vow_count);
printf("Number of consonants are: %d\n", const_count);
return 0;
}
Output:
Aim 3: Write a Lex program to count number of vowels and consonants in a given input
string.
CD Lab Manual Kalp Adhwaryu - 19124065
Code:
Sample.l:
%{
#include <stdio.h>
%}
%%
[0-9]+ { printf("%s\n", yytext); }
.|\n ;
%%
int yywrap(){}
main()
{
yyin= fopen("myfile.txt","r");
printf("Numbers in the file are \n");
yylex();
}
Myfile.txt:
Output:
Aim 4: Write a Lex program which adds line numbers to the given file and display the same
onto the standard output.
Code:
Sample.l:
%{
int line_number = 1; // initializing line number to 1
%}
line .*\n
CD Lab Manual Kalp Adhwaryu - 19124065
%%
{line} { fprintf(yyout,"%10d %s", line_number++, yytext); }
%%
int yywrap(){}
yyin = fopen("input.txt","r");
yylex();
return 0;
}
Input.txt:
Output:
Code:
Sample.l:
%{
#include <stdio.h>
%}
%%
"<"[^>]*> { printf("HTML Tag: %s\n", yytext); }
.|\n ;
%%
int yywrap(){}
main()
{
yyin= fopen("myfile.txt","r");
yylex();
}
Myfile.txt:
Output:
Practical 5
CD Lab Manual Kalp Adhwaryu - 19124065
Aim 1: 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:
Sample.l file:
%{
#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.txt:
Output:
CD Lab Manual Kalp Adhwaryu - 19124065
Output.txt:
Aim 2: Write a Lex program to print keywords, identifiers, operators, numbers in a given C
program.
Code:
Sample.l file:
%{
#include<stdio.h>
%}
%%
auto|double|int|struct|break|else|long|switch|case|enum|register|typedef|char|
extern|return|union|continue|for|signed|void|do|if|static|while|default|goto|sizeof|
volatile|const|float|short|printf|scanf {ECHO; printf(" => KEYWORD\t");}
.|;
%%
int yywrap()
{
return 1;
}
CD Lab Manual Kalp Adhwaryu - 19124065
int main(void)
{
yylex();
return 0;
}
Input:
Output:
Practical 7
CD Lab Manual Kalp Adhwaryu - 19124065
Code:
#include <stdio.h>
#include <conio.h>
#include <string.h>
char input[100];
int i, l;
int E();
int EP();
int T();
int TP();
int F();
int match(char t);
void main()
{
printf("\nRecursive descent parsing for the following grammar\n");
printf("\nE->TE'\nE'->+TE'/@\nT->FT'\nT'->*FT'/@\nF->(E)/ID\n");
printf("\nEnter the string to be checked:");
gets(input);
if (E())
{
if (input[i + 1] == '\0')
printf("\nString is accepted");
else
printf("\nString is not accepted");
}
else
printf("\nString not accepted");
getch();
}
int E()
{
if (T())
{
if (EP())
return (1);
else
return (0);
}
else
return (0);
CD Lab Manual Kalp Adhwaryu - 19124065
int EP()
{
if (input[i] == '+')
{
i++;
if (T())
{
if (EP())
return (1);
else
return (0);
}
else
return (0);
}
else
return (1);
}
int T()
{
if (F())
{
if (TP())
return (1);
else
return (0);
}
else
return (0);
}
int TP()
{
if (input[i] == '*')
{
i++;
if (F())
{
if (TP())
return (1);
else
return (0);
}
else
return (0);
CD Lab Manual Kalp Adhwaryu - 19124065
}
else
return (1);
}
int F()
{
if (input[i] == '(')
{
i++;
if (E())
{
if (input[i] == ')')
{
i++;
return (1);
}
else
return (0);
}
else
return (0);
}
else if (input[i] >= 'a' && input[i] <= 'z' || input[i] >= 'A' && input[i]
<= 'Z')
{
i++;
return (1);
}
else
return (0);
}
int E2()
{
if (l == '+')
{
match('+');
match('i');
E2();
}
}
// Match function
int match(char t)
{
if (l == t)
{
l = getchar();
CD Lab Manual Kalp Adhwaryu - 19124065
}
else
printf("Error");
}
Output:
Practical 8
CD Lab Manual Kalp Adhwaryu - 19124065
Input: A .y file
Aim 2: Create Yacc and Lex specification files to recognizes arithmetic expressions involving
+, -, * and /.
Code:
b.l file:
%{
#include <stdlib.h>
void yyerror(char *);
#include "b.tab.h"
%}
%%
[0-9]+ {
yylval = atoi(yytext);
return INTEGER;
}
[+-/*\n] return *yytext;
[ \t] ; /* skip whitespace */
. yyerror("invalid character");
%%
int yywrap() {
return 0;
}
CD Lab Manual Kalp Adhwaryu - 19124065
b.y file:
%{
#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 ;
%%
void yyerror(char *s) {
fprintf(stderr, "%s\n", s);
}
int main() {
yyparse();
return 0;
}
Output:
CD Lab Manual Kalp Adhwaryu - 19124065
Aim 3: Create Yacc and Lex specification files are used to generate a calculator which
accepts integer and float type arguments.
Code:
b.l file:
%{
#include <stdlib.h>
void yyerror(char *);
#include "b.tab.h"
%}
%%
[0-9]+ {
yylval = atoi(yytext);
return INTEGER;
}
[+-/*\n] return *yytext;
[ \t] ; /* skip whitespace */
. yyerror("invalid character");
%%
int yywrap() {
return 0;
}
b.y file:
%{
#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);
}
CD Lab Manual Kalp Adhwaryu - 19124065
int main() {
yyparse();
return 0;
}
Output: