Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Practical-6: Implement Predictive Parser For The Given Grammar. E - T + E / T T - F T / F F - (E) / I

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

SP(2150708)

Practical-6
Implement Predictive Parser for the given grammar.
E -> T + E / T
T -> F * T / F
F -> ( E ) / i

Program:-

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char ll1[5][7][20];
char head[7][15]={"NT","i","+","*","(",")","$"};
char temp[100];
void call(char ch1,char ch2)
{
int i=0,j=0;
char s1[2],s2[2];
s1[0]=ch1;
s1[1]='\0';
s2[0]=ch2;
s2[1]='\0';
while(strcmp(head[j],s2)!=0)
j++;
while(strcmp(ll1[i][0],s1)!=0)
i++;
strcpy(temp,ll1[i][j]);
}
int check(char ch1, char ch2)
{
char s1[2],s2[2];
s1[0]=ch1;
s1[1]='\0';
s2[0]=ch2;
s2[1]='\0';
if(strcmp(s1,s2)==0)
return 1;
else
return -1;
}
int main()
{
int i=0,j=0,k=0,c=0,d=0,e=0;
char ch,str[10],answer[100],temp1[100];
printf("Predictive Parsing for the Grammar\n");
printf("E -> T + E / T\n");
printf("T -> F * T / F\n");
printf("F -> ( E ) / i\n");
170410107077 12 TY-CE-2-Batch-A
SP(2150708)

printf("\nEnter the LL(1) table for Predictive Parsing \n\n");


printf("Rows-\n(1) E\n(2) e\n(3) T\n(4) t\n(5) F\n\n");
printf("Columns-\n(1) Non Terminal (NT)\n(2) id\n(3) +\n(4) *\n(5) (\n(6) )\n(7) $\n\n");
for(i=0;i<5;i++){
for(j=0;j<7;j++)
scanf("%s",ll1[i][j]);
}
printf("\nLL(1) table\n\n");
printf(" ---------------------------------- \n");
for(i=0;i<7;i++)
printf("%s\t",head[i]);
printf("\n");
printf(" ---------------------------------- \n");
for(i=0;i<5;i++){
for(j=0;j<7;j++)
printf("%s\t",ll1[i][j]);
printf("\n");
}
printf(" ---------------------------------- \n");
printf("**e=E'**\n");
printf("**t=T'**\n");
printf("\nEnter string to be parsed ");
scanf("%s",str);
for(i=0;i<strlen(str);i++);
str[i]='$';
str[i+1]='\0';
i=0;
j=0;
strcpy(answer,"E");
printf("\nCSF");
printf("\n -- ");
printf("\n%s\n",answer);
while(str[i]!='$'){
if(answer[j]==')'){
j++;
i++;
}
call(answer[j],str[i]);
if(strcmp(temp,"?")==0){
printf("\nError in parsing string\n");
printf("Terminating Parser");
return 0;
}
if(strcmp(temp,"'?")==0){
c=0;
ch=answer[j];
while(answer[c]!=ch)
c++;

170410107077 13 TY-CE-2-Batch-A
SP(2150708)

d=c;
c++;
while(c<strlen(answer)){
answer[d]=answer[c];
d++;
c++;
}
answer[d]='\0';
printf("%s\n",answer);
continue;
}
c=0;
ch=answer[j];
while(answer[c]!=ch)
c++;
e=c;
c++;
d=0;
while(c<strlen(answer)){
temp1[d]=answer[c];
d++;
c++;
}
temp1[d]='\0';
d=0;
while(d<strlen(temp)){
answer[e]=temp[d];
e++;
d++;
}
answer[e]='\0';
strcat(answer,temp1);
d=check(answer[j],str[i]);
if(d==1){
i++;
j++;
}
if(answer[j]==')'){
j++;
i++;
continue;
}
printf("%s\n",answer);
if(str[i]=='$'){
while(answer[j]!='\0'){
call(answer[j],str[i]);
if(strcmp(temp,"'?")==0){
c=0;

170410107077 14 TY-CE-2-Batch-A
SP(2150708)

ch=answer[j];
while(answer[c]!=ch)
c++;
d=c;
c++;
while(c<strlen(answer)){
answer[d]=answer[c];
d++;
c++;
}
answer[d]='\0';
if(answer[j]==')'){
j++;
i++;
continue;
}
printf("%s\n",answer);
}
else{
printf("String cannot be parsed ");
printf("Terminating parser\n");
return 0;
}
}
}
}
answer[j]='\0';
for(i=0;i<strlen(str);i++);
str[i-1]='\0';
if(strcmp(str,answer)==0){
printf("\nString successfully parsed- %s\b\n",answer);
return 0;
}
else
printf("String parsing failed ");
}

170410107077 15 TY-CE-2-Batch-A
SP(2150708)

Output:-

170410107077 16 TY-CE-2-Batch-A
SP(2150708)

Practical-7
Write a SAL program in text file and generate SYMTAB and LITTAB.

Program:-

#include<stdio.h>
#include<string.h>
struct intermediate{
int address;
char label[10],mnem[10],op[10];
}res;
struct symbol{
char symbol[10]; int address;
}sy;
struct literal{
char value[20];
}lit;
int main()
{
int i=0;
FILE *s1,*p1,*p2,*p3,*p4;
s1=fopen("code.txt","r+");
p1=fopen("symbol.txt","w");
p2=fopen("literal.txt","w");
char ch;
while(!feof(s1)){
fscanf(s1,"%d %s %s %s",&res.address,res.label,res.mnem,res.op);
if(strcmp(res.label,"NULL")!=0 ){
strcpy(sy.symbol,res.label);
sy.address=res.address;
fprintf(p1,"%s \t%d\n",sy.symbol,sy.address);
}
if(strcmp(res.mnem,"LTORG")==0){
strcpy(lit.value,res.op);
fprintf(p2,"%d \t%s\n",++i,lit.value);
}
}
printf("symbol table created...\n");
fclose(s1);
fclose(p1);
fclose(p2);
p3=fopen("symbol.txt","r+");
ch=fgetc(p3);
while(ch!=EOF){
printf("%c",ch);
ch=fgetc(p3);
}
printf("Literal table created...\n");
170410107077 17 TY-CE-2-Batch-A
SP(2150708)

p4=fopen("literal.txt","r+");
ch=fgetc(p4);
while(ch!=EOF){
printf("%c",ch);
ch=fgetc(p4);
}
fclose(p3);
fclose(p4);
return 0;
}

Code.text file:-

Output:-

literal.txt :-

symbol.txt :-

Terminal :-

170410107077 18 TY-CE-2-Batch-A
SP(2150708)

Practical-8
Use macro features of C language.

Program:-

#include<stdio.h>
#include<stdlib.h>
#define SQUARE(x) (x*x)
#define CUBE(y) (y*y*y)
int main()
{
int a,sq,cube;
printf("Finding square and cube of a number using Macro\n\n");
printf("Enter a number ");
scanf("%d",&a);
sq=SQUARE(a);
cube=CUBE(a);
printf("\nSquare of the number is ");
printf("%d",sq);
printf("\nCube of the number is ");
printf("%d\n",cube);
return 0;
}

Output:-

170410107077 19 TY-CE-2-Batch-A
SP(2150708)

Practical-9
Write a program which generates Quadruple Table for the given postfix String.

Program:-

#include<stdio.h>
#include<string.h>
int main()
{
char line[20];
int s[20];
int t=1;
int i=0;
printf("Enter string.. :");
scanf("%s",line);
for(i=0;i<20;i++)
s[i]=0;
printf("op\ta1\ta2\tres\n");
for(i=2;line[i]!='\0';i++)
{
if(line[i]=='/' || line[i]=='*'){
printf("\n");
if(s[i]==0){
if(s[i+1]==0){
printf(":=\t%c\t\t t%d\n",line[i+1],t);
s[i+1]=t++;
}
printf("%c\t",line[i]);
(s[i-1]==0)?printf("%c\t",line[i-1]):printf("t%d\t",s[i-1]);
printf("t%d \t t%d",s[i+1],t);
s[i-1]=s[i+1]=t++;
s[i]=1;
}
}
}
for(i=2;line[i]!='\0';i++)
{
if(line[i]=='+' || line[i]=='-'){
printf("\n");
if(s[i]==0){
if(s[i+1]==0){
printf(":=\t%c\t\t t%d\n",line[i+1],t);
s[i+1]=t++;
}
printf("%c\t",line[i]);
(s[i-1]==0)?printf("%c\t",line[i-1]):printf("t%d\t",s[i-1]);
printf("t%d \t t%d",s[i+1],t);
s[i-1]=s[i+1]=t++;
170410107077 20 TY-CE-2-Batch-A
SP(2150708)

s[i]=1;
}
}
}
printf(“\n:=\tt%d\t\t%c",t-1,line[0]);
printf("\n");
}

Output:-

170410107077 21 TY-CE-2-Batch-A
SP(2150708)

Practical-10
Write a C program to parse a given string using Predictive parsing for given
grammar.
type  simple | id | array [ simple ] of type
simple  integer | char | num dotdot num.

Program:-

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char ll1[2][7][20];
char ll2[2][7][20]={{"type","simple","simple","simple","^id","ast","?"},
{"simple","integer","char","ndn", "?","?","?"}};
char head1[7][15]={"NT","integer","char","num","^id","array","$"};
char head[7][15]={"NT","7","8","9","3","4","$"};
char temp[100];
char* if1(){
return "type ";
}
char* if2(){
return "simple ";
}
char* if3(){
return "^id ";
}
char* if4(){
return "array ";
}
char* if7(){
return "integer ";
}
char* if8(){
return "char ";
}
char* if9(){
return "num dotdot num";
}
void call(char ch1,char ch2)
{
int i=0,j=0;
char s1[2],s2[2];
s1[0]=ch1;
s1[1]='\0';
s2[0]=ch2;
s2[1]='\0';
while(strcmp(head[j],s2)!=0)
170410107077 22 TY-CE-2-Batch-A
SP(2150708)

j++;
while(strcmp(ll1[i][0],s1)!=0)
i++;
strcpy(temp,ll1[i][j]);
}
int check(char ch1, char ch2)
{
char s1[2],s2[2];
s1[0]=ch1;
s1[1]='\0';
s2[0]=ch2;
s2[1]='\0';
if(strcmp(s1,s2)==0)
return 1;
else
return -1;
}
int main()
{
int i=0,j=0,k=0,c=0,d=0,e=0,m=0;
char ch,str[40],answer[100],temp1[100];
printf("Predictive Parsing for the Grammar\n");
printf("type -> simple | ^id | array [ simple ] of type \n");
printf("simple -> integer | char | num dotdot num\n");
printf("\n\nEnter the LL(1) table for Predictive Parsing \n\n");
printf("Rows-\n(1) integer(1)\n(2) simple(2)\n\n");
printf("Columns-\n(1) Non Terminal (NT)\n(2) integer(7)\n(3) char(8)\n(4) num(9)\n(5)
^id(3)\n(6) array(4)\n(7) $\n\n");
for(i=0;i<2;i++){
for(j=0;j<7;j++)
scanf("%s",ll1[i][j]);
}
printf("\nLL(1) table\n\n");
printf(" ------------------------------------------------- \n");
for(i=0;i<7;i++)
printf("%s\t",head1[i]);
printf("\n");
printf(" ------------------------------------------------- \n");
for(i=0;i<2;i++){
for(j=0;j<7;j++)
printf("%s\t",ll2[i][j]);
printf("\n");
}
printf(" ------------------------------------------------- \n");
printf("**ast=array simple type**\n");
printf("**ndn=num dotdot num**\n");
printf("\nEnter string to be parsed ");
scanf("%s",str);

170410107077 23 TY-CE-2-Batch-A
SP(2150708)

for(i=0;i<strlen(str);i++);
str[i]='$';
str[i+1]='\0';
int z=0,in[10];
char str2[40];
j=0;
for(i=0;i<strlen(str);i++){
str2[j]=str[i];
j++;
str2[j]='\0';
if(strcmp(str2,"integer")==0){
in[z]=7;
j=0;
str2[0]='\0';
z++;
}
else if(strcmp(str2,"char")==0){
in[z]=8;
j=0;
str2[0]='\0';
z++;
}
else if(strcmp(str2,"numdotdotnum")==0){
in[z]=9;
j=0;
str2[0]='\0';
z++;
}
else if(strcmp(str2,"^id")==0){
in[z]=3;
j=0;
str2[0]='\0';
z++;
}
else if(strcmp(str2,"array")==0){
in[z]=4;
j=0;
str2[0]='\0';
z++;
}
else{}
}
char str1[10];
for(i=0;i<z;i++)
str1[i]=in[i]+'0';
str1[i]='\0';
strcpy(str2,str);
strcpy(str,str1);

170410107077 24 TY-CE-2-Batch-A
SP(2150708)

i=0;
j=0;
strcpy(answer,"1");
printf("\nCSF");
printf("\n---");
printf("\n%s\n",if1());
while(str[i]!='$'){
call(answer[j],str[i]);
if(strcmp(temp,"?")==0){
printf("\nError in parsing string\n");
printf("Terminating Parser");
return 0;
}
if(strcmp(temp,"'?")==0){
c=0;
ch=answer[j];
while(answer[c]!=ch)
c++;
d=c;
c++;
while(c<strlen(answer)){
answer[d]=answer[c];
d++;
c++;
}
answer[d]='\0';
printf("%s\n",answer);
continue;
}
c=0;
ch=answer[j];
while(answer[c]!=ch)
c++;
e=c;
c++;
d=0;
while(c<strlen(answer)){
temp1[d]=answer[c];
d++;
c++;
}
temp1[d]='\0';
d=0;
while(d<strlen(temp)){
answer[e]=temp[d];
e++;
d++;
}

170410107077 25 TY-CE-2-Batch-A
SP(2150708)

answer[e]='\0';
strcat(answer,temp1);
for(m=0;m<strlen(answer);m++){
if(answer[m]=='1')
printf("%s ",if1());
else if(answer[m]=='2')
printf("%s ",if2());
else if(answer[m]=='3')
printf("%s ",if3());
else if(answer[m]=='4')
printf("%s ",if4());
else if(answer[m]=='7')
printf("%s ",if7());
else if(answer[m]=='8')
printf("%s ",if8());
else if(answer[m]=='9')
printf("%s ",if9());
else{}
}
printf("\n");
d=check(answer[j],str[i]);
if(d==1){
i++;
j++;
if(answer[j]=='\0')
break;
}
if(str[i]=='$'){
while(answer[j]!='\0'){
call(answer[j],str[i]);
if(strcmp(temp,"'?")==0){
c=0;
ch=answer[j];
while(answer[c]!=ch)
c++;
d=c;
c++;
while(c<strlen(answer)){
answer[d]=answer[c];
d++;
c++;
}
answer[d]='\0';
printf("%s\n",answer);
}
else{
printf("\nString cannot be parsed\n");
printf("Terminating parser\n");

170410107077 26 TY-CE-2-Batch-A
SP(2150708)

return 0;
}
}
}
}
answer[j]='\0';
for(i=0;i<strlen(str);i++);
str[i-1]='\0';
if(strcmp(str1,answer)==0){
printf("\nString successfully parsed- ");
for(m=0;m<strlen(answer);m++){
if(answer[m]=='1')
printf("%s ",if1());
else if(answer[m]=='2')
printf("%s ",if2());
else if(answer[m]=='3')
printf("%s ",if3());
else if(answer[m]=='4')
printf("%s ",if4());
else if(answer[m]=='7')
printf("%s ",if7());
else if(answer[m]=='8')
printf("%s ",if8());
else if(answer[m]=='9')
printf("%s ",if9());
else{}
}
printf("\n");
return 0;
}
else
printf("\nString parsing failed ");
}

170410107077 27 TY-CE-2-Batch-A
SP(2150708)

Output:-

170410107077 28 TY-CE-2-Batch-A

You might also like