Experiment 1: D I T C: Tt1 Batch: B
Experiment 1: D I T C: Tt1 Batch: B
Experiment 1: D I T C: Tt1 Batch: B
DEPARTMENT OF INFORMATION
TECHNOLOGY CLASS: TT1 BATCH: B
Experiment 1
Title: Write a C program to identify whether a given line is a comment or not
Program:
#include<stdio.h>
void main() {
char str[30];
int i=2,a=0;
Output:
Experiment 2
Title: Write a C program to test that the given input is valid identifier or keyword.
Program:
#include <stdio.h>
void main()
{
int i=0,flag=0;
char
keyw[10][10]={"int","float","break","long","char","for","if","switch","else","while"},a[10];
printf("Enter Identifier : ");
gets(a);
for(i=0;i<10;i++)
{
if((strcmp(keyw[i],a)==0))
{
flag=1;
}
}
if(flag==1)
{
printf("\n%s is Keyword.",a);
}
else
{
flag=0;
if((a[0]=='_')||(isalpha(a[0])!=0))
{
for(i=1;a[i]!='\0';i++)
{
if((isalnum(a[i])==0)&&(a[i]!='_'))
{
flag=1;
}
}
}
else
{
flag=1;
}
}
if(flag==0)
{
printf("\n%s is an Identifier.",a);
}
else
{
printf("\n%s is Not an Identifier.",a);
}
getch();
}
Output:
Experiment 3
Title: Write a C program for dfa accepting string”abb”;
Program:
#include<stdio.h
>
#include<stdlib.h
>
void main()
int
initial_state=1,len,current_state=1;
MOHAMAD MAHMOUD 91800104060 Page |
4
MARWADI UNIVERSITY
DEPARTMENT OF INFORMATION
TECHNOLOGY CLASS: TT1 BATCH: B
char a,b,str[10];
scanf("%s",&str
);
len=strlen(str);
if(len==3){
current_state=2;
else {
printf("String is Rejected.");
exit(0);}
if(current_state==2 &&
str[1]=='b'){ current_state=3;
else {
printf("String is
Rejected."); exit(0);}
current_state=4;
else {
printf("String is
rejected."); exit(0);}
else{
); exit(0);
getch();
Output:
Experiment 4
Title: Write a C program to remove left recursion from the grammar.
Program:
#include<stdio.h
>
#include<conio.h
>
#include<string.h
10 void main () {
char
non_terminal;
char beta,alpha;
char
production[SIZE]; int
index=3;
scanf("%s",production);
non_terminal=production[0];
if(non_terminal==production[index]
) { alpha=production[index+1];
printf("Grammar is left
while(production[index]!=0 &&
production[index]!='|') index++;
if(production[index]!=0) {
beta=production[index+1];
printf("%c-
>%c%c\'",non_terminal,beta,non_terminal);
printf("\n%c\'->%c%c\'|E\n",non_terminal,alpha,non_terminal);
else
else
Output: