0% found this document useful (0 votes)
21 views9 pages

Compiler Lab Assignment

This document contains code for 4 C++ programs that: 1) Check if a character entered is a vowel or consonant. 2) Check if a string is accepted by a given finite state machine. 3) Display a symbol table for variables and operators in an expression. 4) Check if a value entered is a number, keyword, identifier, or operator.

Uploaded by

Imam Hussain
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)
21 views9 pages

Compiler Lab Assignment

This document contains code for 4 C++ programs that: 1) Check if a character entered is a vowel or consonant. 2) Check if a string is accepted by a given finite state machine. 3) Display a symbol table for variables and operators in an expression. 4) Check if a value entered is a number, keyword, identifier, or operator.

Uploaded by

Imam Hussain
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/ 9

Id:2193081057

\\lab_1

#include<iostream>
#include<conio.h>
using namespace std;

int main()
{
char c;
cout<<"ENTER A LETTER:";
cin>>c;

if(c=='a'|| c=='e'|| c=='i'|| c=='o'|| c=='u')


{
cout<<"IT IS VOWEL";
}
else if (c=='A'|| c=='E'|| c=='I'|| c=='O'|| c=='U')
{
cout<<"IT IS VOWEL";
}
else
{
cout<<"IT IS CONSONANT";
}
getch();

lab_2

#include <stdio.h>
#include<string.h>
//#include <stdlib.h>

int main()
{
char str[100];
int ts[3][2]={{1,0},{1,2},{2,2}};
int j=0, state=0,check=0;
int i=0;
printf("\n enter ny string :");
gets(str);

for(i=0;i<strlen(str);i++)
{
if(str[i]=='0')
{
state=ts[state][0];
}
else if(str[i]=='1')
{
state=ts[state][1];
}
else
check=0;
}
if(state==2&&check==1)
printf("the string accepted ");
else
printf("not accepted");
return 0

lab_3

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
void main()
{
//int ptr= 566879;
int x=0, n, i=0,j=0;
void *p,*address[5];
char ch,Search,b[15],d[15],c;
printf("Input the expression ending with $ sign :");
while((c=getchar())!='$')
{
b[i]=c;
i++;
}
n=i-1;
printf("Given Expression:");
i=0;
while(i<=n)
{
printf("%c",b[i]);
i++;
}
printf(" \nSymbol Table display \n");
printf("Symbol \t address \t type");
while(j<=n)
{
c=b[j];
if(isalpha(toascii(c)))
{
p=malloc(c);
address[x]=p;
d[x]=c;
printf("\n%c \t %d \t identifier\n",c,p);
x++;
j++;
}
else
{
ch=c;
if(ch=='+'||ch=='-'||ch=='*'||ch=='=')
{
p=malloc(ch);
address[x]=p;
d[x]=ch;
printf("\n %c \t %d \t operator\n",ch,p);
x++;
j++;
}
}
}
}
lab_4

#include<iostream>
#include<stdlib.h>
using namespace std;

int is_number(int a)
{
cout<<"enter a number:";
cin>>a;
if(a=='0'||a=='1'||a=='2'||a=='3'a=='4'||a=='5'||a=='6'||a=='7'||a=='8'||a=='9')
cout<<a<<"this is number"<<endl;
else{
cout <<a<<"it is not number"<<endl;
}
}
string iskeyword(string key)
{
cout>>"inter keyword :"endl;
cin<<key;
if(key=='asm'|| key=='void'|| key=='new'|| key'switch'|| key=='auto'
||key=='else'|| key=='operator'|| key=='template'|| key=='break'
|| key=='enum'|| key=='private'|| key=='this'|| key=='case'|| key=='extern'
||key=='protected'|| key=='throw'|| key=='catch'|| key=='public '
||key=='try'|| key=='for'|| key=='char'|| key=='typedef'|| key=='register'
||key=='union'|| key=='class'|| key=='friend'|| key=='return'|| key=='delete'
|| key=='unsigned'|| key=='const'|| key=='goto'|| key=='short'
|| key=='virtual'|| key=='continue'|| key=='if'|| key=='signed'||key=='default'
||key=='inline'|| key=='int'|| key=='sizedof' || key=='static'
|| key=='volatile'||key=='do' ||key=='long'|| key=='whil'|| key=='struct')
{
cout<<key<<"it is keyword"<<endl;
}

else
{
cout<<key<<"it is not keyword "<<endl;
}
}

string isidentifier(string id)


{
cout>>"inter identifier :";
cin<<id;

if(id=='asm'|| id=='void'|| id=='new'|| id=='switch'|| id=='auto'


||id=='else'|| id=='operator'|| id=='template'|| id=='break'
|| id=='enum'id=='if'|| id=='private'|| id=='this'|| id=='case'|| id=='extern'
||id=='protected'|| id=='throw'|| id=='catch'|| id=='public '
||id=='try'|| id=='for'|| id=='char'|| id=='typedef'|| id=='register'
||id=='union'|| id=='class'|| id=='friend'|| id=='return'|| id=='delete'
|| id=='unsigned'|| id=='const'|| id=='goto'|| id=='short'
|| id=='virtual'|| id=='continue'|| id=='if'|| id=='signed'||id=='default'
||id=='inline'|| id=='int'|| id=='sizedof' || id=='static'
|| id=='volatile'|| id=='do' ||id=='long'|| id=='whil'|| id=='struct')
{
cout<<id<<"it is identifier.it is not keyword"<<endl;
}

else
{
cout<<id<<"it is not an identifier "endl;
}
}
string operator(op)
{
cout<<"enter an operator :"<<endl;
cin>>op;
if(op=='+'||op=='-'||op=='/'||op=='*'||)
{
cout<<op<<" is operator."endl;
}
else
{
cout<<op<<" is not a operator"endl;
}

int main()
{

int a;
string keyword, id;
is_number(a);
iskeyword(keyword);
operator(op);
isidentifier(id);

You might also like