0% found this document useful (0 votes)
40 views38 pages

CD LabManual

The document contains details of experiments conducted on lexical analysis. Experiment 1 involves writing programs to count characters in a string, validate usernames and passwords, and identify comments. Experiment 2 focuses on programs that recognize strings ending in "ab", simulate a lexical analyzer to validate operators, and design a lexical analyzer to identify keywords and identifiers. Experiment 3 studies Lexical Analyzer Generators like LEX and Flex.

Uploaded by

Youtube GsG
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)
40 views38 pages

CD LabManual

The document contains details of experiments conducted on lexical analysis. Experiment 1 involves writing programs to count characters in a string, validate usernames and passwords, and identify comments. Experiment 2 focuses on programs that recognize strings ending in "ab", simulate a lexical analyzer to validate operators, and design a lexical analyzer to identify keywords and identifiers. Experiment 3 studies Lexical Analyzer Generators like LEX and Flex.

Uploaded by

Youtube GsG
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/ 38

Index

Srno Experiment Title Date of


Performance

1 a) Program to count digits, vowels, consonant and symbols in


C.
b) Program to check validation of User Name and Password*
in C.
c) Write a program to identify whether a given line is a
comment or not.
2 a) Write a C program to recognize strings end with ‘ab’
b) Write a C program to simulate lexical analyzer for
validating operators
c) Design a lexical analyzer to identify from given pattern is
keyword or identifier.
d)Program to implement Lexical Analyzer.
3 To Study about Lexical Analyzer Generator(LEX) and
Flex(Fast Lexical Analyzer)
4 Implement following programs using Lex.
a) Create a Lexer to take input from text file and count no of
characters, no. of lines & no. of words.
b) Write a Lex program to count number of vowels and
consonants in a given input string.
c)Write a Lex program to print out all numbers from the given
file.
d) Write a Lex program which adds line numbers to the given
file and display the same onto the standard output.
e) Write a Lex program to printout all HTML tags in file.
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.

b) Write a Lex program to print keywords, identifiers,


operators, numbers in a given C program.
6 Program to implement Recursive Descent Parsing in C.

7 a) To Study about Yet Another Compiler-Compiler(YACC).


b) Create Yacc and Lex specification files to recognizes
arithmetic expressions involving +, -, * and / .
c) Create Yacc and Lex specification files are used to
generate a calculator which accepts integer and float type
arguments.

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;

// initialize all variables to 0


vowels = consonant = digit = symbol = 0;

// get full line of string input


printf("Enter a line of string: ");
fgets(line, sizeof(line), stdin);

// loop through each character of the string


for (int i = 0; line[i] != '\0'; ++i) {

// convert character to lowercase


line[i] = tolower(line[i]);

// check if the character is a vowel


if (line[i] == 'a' || line[i] == 'e' || line[i] == 'i' ||
line[i] == 'o' || line[i] == 'u') {

// increment value of vowels by 1


++vowels;
}

// if it is not a vowel and if it is an alphabet, it is a consonant


else if ((line[i] >= 'a' && line[i] <= 'z')) {
++consonant;
}

// check if the character is a digit


else if (line[i] >= '0' && line[i] <= '9') {
++digit;
}

else {
++symbol;
}
}

printf("Vowels: %d", vowels);


printf("\nConsonants: %d", consonant);
printf("\nDigits: %d", digit);
printf("\n symbol: %d", symbol);

return 0;
}

Output:-

b) Program to check validation of User Name and Password* in C.

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;
}

void checkUsername(char b[])


{
char c;
int len,i,flag1=0,flag2=0,flag3=0,flag4=0;
len=strlen(b);
if(len<4)
flag1=1;
else
{
for(i=0;i<len;i++)
if((b[i]>=48&&b[i]<=58))
{
flag2=0;
break;
}
else
flag2=1;

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:-

c) Write a program to identify whether a given line is a comment or not.


Code:-
#include<stdio.h>
void main()
{
char com[30];
int i=2,a=0;
printf("\n Enter comment:");
gets(com);

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:-

b) Write a C program to simulate lexical analyzer for validating operators


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'<': 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("\n Substraction");
break;
case'*': printf("\nMultiplication");
break;
case'/': printf("\nDivision");
break;
case'%': printf("Modulus");
break;
default: printf("\n Not a operator");
}
getch();
}
Output:-

c) Design a lexical analyzer to identify from given pattern is keyword or identifier.

#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);
}

// Returns 'true' if the character is an OPERATOR.


bool isOperator(char ch)
{
if (ch == '+' || ch == '-' || ch == '*' ||
ch == '/' || ch == '>' || ch == '<' ||
ch == '=')
return (true);
return (false);
}

// Returns 'true' if the string is a VALID IDENTIFIER.


bool validIdentifier(char* str)
{
if (str[0] == '0' || str[0] == '1' || str[0] == '2' ||
str[0] == '3' || str[0] == '4' || str[0] == '5' ||
str[0] == '6' || str[0] == '7' || str[0] == '8' ||
str[0] == '9' || isDelimiter(str[0]) == true)
return (false);
return (true);
}

// Returns 'true' if the string is a KEYWORD.


bool isKeyword(char* str)
{
if (!strcmp(str, "if") || !strcmp(str, "else") ||
!strcmp(str, "while") || !strcmp(str, "do") ||
!strcmp(str, "break") ||
!strcmp(str, "continue") || !strcmp(str, "int")
|| !strcmp(str, "double") || !strcmp(str, "float")
|| !strcmp(str, "return") || !strcmp(str, "char")
|| !strcmp(str, "case") || !strcmp(str, "char")
|| !strcmp(str, "sizeof") || !strcmp(str, "long")
|| !strcmp(str, "short") || !strcmp(str, "typedef")
|| !strcmp(str, "switch") || !strcmp(str, "unsigned")
|| !strcmp(str, "void") || !strcmp(str, "static")
|| !strcmp(str, "struct") || !strcmp(str, "goto"))
return (true);
return (false);
}

// Returns 'true' if the string is an INTEGER.


bool isInteger(char* str)
{
int i, len = strlen(str);
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);
}

// Returns 'true' if the string is a REAL NUMBER.


bool isRealNumber(char* str)
{
int i, len = strlen(str);
bool hasDecimal = 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);
}

// Extracts the SUBSTRING.


char* subString(char* str, int left, int right)
{
int i;
char* subStr = (char*)malloc(
sizeof(char) * (right - left + 2));

for (i = left; i<= right; i++)


subStr[i - left] = str[i];
subStr[right - left + 1] = '\0';
return (subStr);
}

// Parsing the input STRING.


void parse(char* str)
{
int left = 0, right = 0;
int len = strlen(str);

while (right <= len&& left <= right) {


if (isDelimiter(str[right]) == false)
right++;
if (isDelimiter(str[right]) == true && left == right) {
if (isOperator(str[right]) == true)
printf("");

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);

else if (isInteger(subStr) == true)


printf("");

else if (isRealNumber(subStr) == true)


printf("");

else if (validIdentifier(subStr) == true


&&isDelimiter(str[right - 1]) == false)
printf("'%s' IS AN IDENTIFIER\n", subStr);

else if (validIdentifier(subStr) == false


&&isDelimiter(str[right - 1]) == false)
printf("");
left = right;
}
}
return;
}

// DRIVER FUNCTION
int main()
{
// maximum length of string is 100 here
char str[100] = "char x = y + 1z; ";

parse(str); // calling the parse function

return (0);
}
Output:-

d) Program to implement Lexical Analyzer.


Code:-
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
bool isValidDelimiter(char ch) {
if (ch == ' ' || ch == '+' || ch == '-' || ch == '*' ||
ch == '/' || ch == ',' || ch == ';' || ch == '>' ||
ch == '<' || ch == '=' || ch == '(' || ch == ')' ||
ch == '[' || ch == ']' || ch == '{' || ch == '}')
return (true);
return (false);
}
bool isValidOperator(char ch){
if (ch == '+' || ch == '-' || ch == '*' ||
ch == '/' || ch == '>' || ch == '<' ||
ch == '=')
return (true);
return (false);
}
// Returns 'true' if the string is a VALID IDENTIFIER.
bool isvalidIdentifier(char* str){
if (str[0] == '0' || str[0] == '1' || str[0] == '2' ||
str[0] == '3' || str[0] == '4' || str[0] == '5' ||
str[0] == '6' || str[0] == '7' || str[0] == '8' ||
str[0] == '9' || isValidDelimiter(str[0]) == true)
return (false);
return (true);
}
bool isValidKeyword(char* str) {
if (!strcmp(str, "if") || !strcmp(str, "else") || !strcmp(str, "while") || !strcmp(str, "do") || !
strcmp(str, "break") || !strcmp(str, "continue") || !strcmp(str, "int")
|| !strcmp(str, "double") || !strcmp(str, "float") || !strcmp(str, "return") || !strcmp(str, "char") || !
strcmp(str, "case") || !strcmp(str, "char")
|| !strcmp(str, "sizeof") || !strcmp(str, "long") || !strcmp(str, "short") || !strcmp(str, "typedef") || !
strcmp(str, "switch") || !strcmp(str, "unsigned")
|| !strcmp(str, "void") || !strcmp(str, "static") || !strcmp(str, "struct") || !strcmp(str, "goto"))
return (true);
return (false);
}
bool isValidInteger(char* str) {
int i, len = strlen(str);
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);
}
bool isRealNumber(char* str) {
int i, len = strlen(str);
bool hasDecimal = 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);
}
char* subString(char* str, int left, int right) {
int i;
char* subStr = (char*)malloc( sizeof(char) * (right - left + 2));
for (i = left; i<= right; i++)
subStr[i - left] = str[i];
subStr[right - left + 1] = '\0';
return (subStr);
}
void detectTokens(char* str) {
int left = 0, right = 0;
int length = strlen(str);
while (right <= length && left <= right) {
if (isValidDelimiter(str[right]) == false)
right++;
if (isValidDelimiter(str[right]) == true && left == right) {
if (isValidOperator(str[right]) == true)
printf("Valid operator : '%c'\n", str[right]);
right++;
left = right;
} else if (isValidDelimiter(str[right]) == true && left != right || (right == length && left !=
right)) {
char* subStr = subString(str, left, right - 1);
if (isValidKeyword(subStr) == true)
printf("Valid keyword : '%s'\n", subStr);
else if (isValidInteger(subStr) == true)
printf("Valid Integer : '%s'\n", subStr);
else if (isRealNumber(subStr) == true)
printf("Real Number : '%s'\n", subStr);
else if (isvalidIdentifier(subStr) == true
&&isValidDelimiter(str[right - 1]) == false)
printf("Valid Identifier : '%s'\n", subStr);
else if (isvalidIdentifier(subStr) == false
&&isValidDelimiter(str[right - 1]) == false)
printf("Invalid Identifier : '%s'\n", subStr);
left = right;
}
}
return;
}
int main(){
char str[100] = "float x = a + 1b; ";
printf("The Program is : '%s' \n", str);
printf("All Tokens are : \n");
detectTokens(str);
return (0);
}
Output:-
Practical-4
a) Create a Lexer to take input from text file and count no of characters, no. of lines & no.
of words.
Code:-
%{

#include<stdio.h>

int words=0, lines=0, chars=0;

%}

%%

[a-zA-Z] chars++;
\n {++lines;++words;};
[\t' '] ++words;
%%

main(void)

{
yyin= fopen("input.txt","r");
yylex();

printf(" This File contains ...");


printf("\n\t%d chars", chars);

printf("\n\t%d lines", lines);

printf("\n\t%d words", (words+1));


}

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>

int vowels=0, consonants=0;

%}

%%

[aeiouAEIOU] vowels++;
[a-zA-Z] consonants++;

%%

main(void)

{
yyin= fopen("input1.txt","r");
yylex();

printf(" This File contains ...");

printf("\n\t%d vowels", vowels);

printf("\n\t%d consonants", consonants);

}
int yywrap()
{

return(1);

}
}
Output:-

c)Write a Lex program to print out all numbers from the given file.
Code:-
%{

#include<stdio.h>

%}

%%

([0-9]+)((.([0-9]+))[Ee][+-]?[0-9]+) { printf("%s\n", yytext); }


.|\n ;
%%

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(){}

int main(int argc, char*argv[])


{
extern FILE *yyin;

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:

b) Write a Lex program to print keywords, identifiers, operators, numbers in a given C


program.
Input:-

%{

#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);}

[0-9]* {printf("\n%s: Numbers",yytext);}

\"[^\"]\" {printf("\n%s: string constant",yytext);}

"<"|">"|"+"|"="|"-"|")"|"("|"*"|"^"|"$"|"#"|"@"|"!"|"~" {printf("\n%s: Special Character",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 ;
%%

void yyerror(char *s) {


fprintf(stderr, "%s\n", s);
}
int main() {
yyparse();
return 0;
}

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;
}

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