Upload
pitchrks19841
View
219
Download
0
Embed Size (px)
Citation preview
8/3/2019 System Software Beyond Syllabuis
1/11
omsakthi
ADHIPARASAKTHI ENGINEERING COLLEGE
MELMARUVATHUR.
DEPARTMENT OF INFORMATION TECHNOLOGY
SYSTEM SOFTWARE LAB
1. WRITE C PROGRAM FOR TOKEN SEPARATION2. Write Source code For Recursive Descent Parsing3. Write Shift Reduce Parsing Source Code in C
8/3/2019 System Software Beyond Syllabuis
2/11
WRITE C PROGRAM FOR TOKEN SEPARATION
#include
#include
#include
#includevoid main()
{
char exp[50]="\0",con[50]="\0",kwd[50]="\0",id[50]="\0",sym[50]="\0",opr[50]="\0";
char key[6][10]={"if","for","do","while","int","float"};
char ch;
char ptr[10][10]={"\0"};
int i=0,j=0,k=-1,n=-1,p=-1,s=-1;
clrscr();
puts("Enter the expression for lexical analysis");
gets(exp);
puts("\nThe tokens are");
do
{
ch=exp[i];
if(isalpha(ch))
{
k=-1;
ptr[++n][++k]=ch;
i++;ch=exp[i];
if(isalpha(ch)||isdigit(ch))
{
while(isalpha(ch)||isdigit(ch))
{
ptr[n][++k]=ch;
i++;
ch=exp[i];
}
while(j
8/3/2019 System Software Beyond Syllabuis
3/11
}
if(j==5)
{
ptr[n][++k]=' ';
strcat(id,ptr[n]);
}
j++;
}
}
else
{
ptr[n][++k]=' ';
strcat(id,ptr[n]);
}
i--;ch=exp[i];
j=0;
}
else if(isdigit(ch))
{
k=-1;
ptr[++n][++k]=ch;
i++;
ch=exp[i];
if(isdigit(ch)){
while(isdigit(ch))
{
ptr[n][++k]=ch;
i++;
ch=exp[i];
}
}
i--;
ch=exp[i];
ptr[n][++k]=' ';
strcat(con,ptr[n]);
}
else if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='%'||ch=='>'||ch=='
8/3/2019 System Software Beyond Syllabuis
4/11
opr[++p]=ch;
i++;
ch=exp[i];
if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='%'||ch=='>'||ch=='
8/3/2019 System Software Beyond Syllabuis
5/11
a b
Constant:
10
Operator:
/ ==
Symbol:
( )
Example Output Result Token separation
Enter the expression for lexical analysis
int a=1;float b;
The tokens are
Keyword:
int float
Identifier:
a bConstant:
1
Operator:
=
Symbol:
8/3/2019 System Software Beyond Syllabuis
6/11
Write Source code For Recursive Descent Parsing
#include
#include
int c=0;
char p[20];void s();
void l();
void lprime();
void l()
{
s();
lprime();
}
void lprime()
{
if(p[c]==',')
{
c++;
s();
lprime();
}
}
void s()
{
if(p[c]=='a')
c++;
else if(p[c]=='(')
{
c++;
l();
if(p[c]==')')
c++;
else
c--;
}
else
printf("\nInvalid Expression");
}
void main()
{
clrscr();
8/3/2019 System Software Beyond Syllabuis
7/11
printf("\nImplementation of RECURSIVE DESCENT PARSER\n");
printf("\nEnter the Expression:\n");
scanf("%s",p);
s();
if(p[c]=='$')
printf("\nThe String is accepted");
else
printf("\nThe string is rejected");
getch();
}
Recursive Descent Parsing Output Result
Implementation of RECURSIVE DESCENT PARSER
Enter the Expression:
a
The string is rejected
Implementation of RECURSIVE DESCENT PARSER
Enter the Expression:
a$
The String is accepted
Recursive Descent Parsing OUTPUT
Implementation of RECURSIVE DESCENT PARSER
Enter the Expression:
(a)$
The String is accepted
Implementation of RECURSIVE DESCENT PARSER
Enter the Expression:
b*c
Invalid Expression
The string is rejected
8/3/2019 System Software Beyond Syllabuis
8/11
Write Shift Reduce Parsing Source Code in C
#include
#include
#include
#include#include
struct stru1
{
char non_ter[1],pro[25];
}cfg[25];
int n,st=-1,j,i,t=-1,m;
int v,c,p=1;
char str[20],stack[20],ch,tmp[10];
void match(int k);
void matchl(int k);
void main()
{
clrscr();
cprintf("Enter the number of productions:\n\r");
cscanf("%d",&n);
cprintf("\n\r");
cprintf("Enter the productions on LEFT and RIGHT sides:\n\r");
for(i=0;i
8/3/2019 System Software Beyond Syllabuis
9/11
i++;
}while(str[i]!='\0');
c=st;
v=st;
cputs(stack);
cprintf("\n\r");
while(st!=0)
{
v=--st;
t=-1;
p=0;
while(v
8/3/2019 System Software Beyond Syllabuis
10/11
y=k-1;
for(j=0;j
E+E
E
->
E*E
8/3/2019 System Software Beyond Syllabuis
11/11
Enter the input string:
a*a+a
E*E+E
E*E
E
String is present in Grammar G
Output Result Shift Reduce Parsing Project
Enter the number of productions:
2
Enter the productions on LEFT and RIGHT sides:
E
->
id
E
->
E+E
Enter the input string:
id+id
E+E
E
String is present in Grammar G