Professional Documents
Culture Documents
CD Prac-2
CD Prac-2
Practical-2
Aim-Write a C Program to identify whether a given lines is Comment or Not
SOURCE CODE-
#include <stdio.h>
#include <string.h>
int main()
{
char str[100];
int i=2,a=0;
printf("enter the string:\
n"); scanf("%[^\n]s",&str);
printf("\n Output:\n");
if(str[0]=='/')
{
if(str[1]=='/')
{
printf("it is a single line comment \n");
}
else if(str[1]=='*')
{
for(i=2;i<=100;i++)
{
if(str[i]=='*'&& str[i+1]=='/')
{
printf("It is a Multi-Line Comment \n");
a=1;
break;
}
else
{
continue;
}
if(a==0)
{
printf("It is Not a Comment \n");
}
}
else
{
printf("It is not comment \n");
}
}
}
else
{
printf("it is not a comment");
}
}
OUTPUT-
1
PRACTICAL FILE COMPILER DESIGN
Practical-3
3.1 Aim
Write a C program to recognize strings under 'a*', 'a*b+', 'abb'.
3.2 RESOURCE:
Turbo C, Onilne Compiler
3.3 PROGRAM LOGIC:
By using transition diagram we verify input of the state. If the state recognize the given
pattern rule. Then print string is accepted under a*/ a*b+/ abb. Else print string not
accepted.
3.4 PROCEDURE:
Go to debug -> run or press CTRL + F9 to run the program.
3.5 PROGRAM:
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
2
PRACTICAL FILE COMPILER DESIGN
if (str[i] == 'a') {
a_count++;
a_seen = true;
} else if (str[i] == 'b') {
b_count++;
if (!a_seen) // 'b' should not appear before 'a'
return false;
} else {
return false; // Invalid character
}
}
int main()
{ char
str[100];
if (recognizeAStar(str))
printf("%s is recognized under 'a*'\n", str);
else if (recognizeAStarBPlus(str))
printf("%s is recognized under 'a*b+'\n", str);
3
PRACTICAL FILE COMPILER DESIGN
else if (recognizeABB(str))
4
PRACTICAL FILE COMPILER DESIGN
return 0;
}
3.6
OUTPUT:
5
PRACTICAL FILE COMPILER DESIGN
Practical-4
4.1 AIM :-
Write a C program to identify whether a given identifier is valid or not.
4.2 RESOURCE:
Turbo C, Online Compiler
4.3 INTRODUCTION:
Introduction of identifier
4.5 Program:
#include <stdio.h>
#include <stdbool.h>
#include <ctype.h>
#include <string.h>
6
PRACTICAL FILE COMPILER DESIGN
return true;
}
int main() {
char identifier[50];
7
PRACTICAL FILE COMPILER DESIGN
Practical No:5
1.1 OBJECTIVE:
Write a C program to simulate lexical analyzer for validating operators.
1.2 RESOURCE:
Turbo C
1.2 INTRODUCTION
PROGRAM LOGIC:
1. Read the given input.
2. If the given input matches with any operator symbol.
3. Then display in terms of words of the particular symbol.
4. Else print not a operator.
1.4 PROCEDURE:
Go to debug -> run or press CTRL + F9 to run the program
PROGRAM:
#include<stdio.h>
int main() {
char s[3]; // Make sure it can hold at least two characters and a null terminator
printf("Enter any operator: ");
fgets(s, sizeof(s), stdin); // Use fgets to avoid buffer overflow and handle multi-character input
switch(s[0])
{ case '>':
if(s[1] == '=')
printf("Greater than or equal\n");
else
printf("Greater than\n");
break;
case '<':
if(s[1] == '=')
printf("Less than or equal\n");
else
printf("Less than\n");
break;
case '=':
if(s[1] == '=')
printf("Equal to\n");
8
PRACTICAL FILE COMPILER DESIGN
else
printf("Assignment\n");
break;
case '!':
if(s[1] == '=')
printf("Not Equal\n");
else
printf("Bit Not\n");
break;
case '&':
if(s[1] == '&')
printf("Logical AND\n");
else
printf("Bitwise AND\n");
break;
case '|':
if(s[1] == '|')
printf("Logical OR\n");
else
printf("Bitwise OR\n");
break;
case '+':
printf("Addition\n");
break;
case '-':
printf("Subtraction\n");
break;
case '*':
printf("Multiplication\n");
break;
case '/':
printf("Division\n");
break;
case '%': printf("Modulus\
n"); break;
default:
printf("Not an operator\n");
}
return 0;
}
9
PRACTICAL FILE COMPILER DESIGN
OUTPUT:
1
0