Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

PRACTICAL FILE COMPILER DESIGN

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>

// Function to recognize strings under 'a*'


bool recognizeAStar(char *str) {
int len = strlen(str);
for (int i = 0; i < len; i++)
{ if (str[i] != 'a')
return false;
}
return true;
}

// Function to recognize strings under 'a*b+'


bool recognizeAStarBPlus(char *str) {
int len = strlen(str);
int a_count = 0, b_count = 0;
bool a_seen = false;

for (int i = 0; i < len; i++) {

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

if (a_count > 0 && b_count > 0)


return true;
else
return false;
}

// Function to recognize strings under 'abb'


bool recognizeABB(char *str) {
return strcmp(str, "abb") == 0;
}

int main()
{ char
str[100];

printf("Enter a string: ");


scanf("%s", str);

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

printf("%s is recognized under 'abb'\n", str);


else
printf("%s is not recognized under any pattern\n", str);

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.4 PROGRAM LOGIC:


A string is called identifier when they follows these rules:
1. The first letter must be alphabet(both capital or small i.e. A-Z,a-z) or underscore(_).
2. After first letter it contains sequence of alphabet or digits(0-9) or underscore(_)
but not contain any special symbol(#,$,%,^,& etc.) and space( ).

4.5 Program:
#include <stdio.h>
#include <stdbool.h>
#include <ctype.h>
#include <string.h>

bool isValidIdentifier(char *identifier) {


// Check if the first character is alphabetic or underscore
if (!isalpha(identifier[0]) && identifier[0] != '_')
return false;

// Check the rest of the characters


for (int i = 1; i < strlen(identifier); i++) {
if (!isalnum(identifier[i]) && identifier[i] != '_')
return false;
}

6
PRACTICAL FILE COMPILER DESIGN

return true;
}

int main() {
char identifier[50];

printf("Enter an identifier: ");


scanf("%s", identifier);
if (isValidIdentifier(identifier))
printf("%s is a valid identifier.\n", identifier);
else
printf("%s is not a valid identifier.\n", identifier);
return 0;
}
Output-

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

You might also like