Predictive Parsing Table for a Small Language Implementation

Verified

Added on  2019/09/23

|5
|927
|158
Project
AI Summary
This assignment presents a C++ implementation of a predictive parser designed for a small language. The code constructs a predictive parsing table based on grammar rules and FIRST/FOLLOW sets. The program processes input to generate the parsing table, which is then used to determine the correct parsing path. The solution includes the generation of the parsing table, including handling cases with '#' (epsilon) productions and follows set calculations. The code also handles the display of the parsing table for analysis. The assignment demonstrates key concepts in compiler design, including lexical analysis, syntax analysis, and the construction of parsing tables, making it a valuable resource for students studying computer science and artificial intelligence.
Document Page
PREDICTIVE PARSER
#include<iostream>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
char pr1[7][10]={"A","B","B","C","C","D","D"};
char pr2[7][10]={"B","Cb","Dd","aC","#","Dc","#"};
char pr3[7][10]={"A->B","B->Cb","B->Dd","C->aC","C->#","D >Dc","D->#"};
char first[7][10]={"abcd","ab","cd","a#","#","c#","#"};
char follow[7][10]={"$","$","$","a$","b$","c$","d$"};
char table[5][6][10];
//char pro[i];
int numr(char c)
{
switch(c)
{
case 'A': return 0;
case 'B': return 1;
case 'C': return 2;
case 'D': return 3;
case 'a': return 0;
case 'b': return 1;
case 'c': return 2;
case 'd': return 3;
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
case '$': return 4;
}
return(2);
}
using namespace std;
int main()
{
int i,j,k,x;
//char pro[10];
clrscr();
for(i=0;i<5;i++)
for(j=0;j<6;j++)
strcpy(table[i][j]," ");
cout<<"\n PREDICTIVE PARSING TABLE FOR SMALL LANGUAGE\n";
for(i=0;i<7;i++)
cout<<pr3[i] << endl ;
cout<<"\n PREDICTIVE PARSING TABLE \n"<< endl;
for(i=0;i<7;i++)
{
k=strlen(first[i]);
for(j=0;j<10;j++)
if(first[i][j]!='#')
Document Page
strcpy(table[numr(pr1[i][0])+1][numr(first[i][j])+1],pr3[i]);
}
for(i=0;i<7;i++)
{
if(strlen(pr3[i])==1)
{
if(pr2[i][0]=='#')
{
k=strlen(follow[i]);
for(j=0;j<k;j++)
strcpy(table[numr(pr1[i][0])+1][numr(follow[i][j])+1],pr3[i]);
}
}
}
strcpy(table[0][0],"");
strcpy(table[0][1],"a");
strcpy(table[0][2],"b");
strcpy(table[0][3],"c");
strcpy(table[0][4],"d");
strcpy(table[0][5],"$");
strcpy(table[1][0],"A");
strcpy(table[2][0],"B");
strcpy(table[3][0],"C");
strcpy(table[4][0],"D");
Document Page
cout<<"\n--------------------------------------------------------\n";
for(i=0;i<5;i++)
for(j=0;j<6;j++)
{
cout<<" " << table[i][j]<<" ";
for( x=0; x<(4-strlen(table[i][j])); x++)
cout<<" ";
if(j==5)
cout<<"\n--------------------------------------------------------\n";
}
return 0;
}
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
chevron_up_icon
1 out of 5
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]