Ask a question to Desklib · AI bot


Predictive Parser

Added on -2019-09-23

This program generates a predictive parsing table for a small language using C++. It takes the grammar rules, first and follow sets as input and outputs the parsing table. The program uses a switch case statement to map the input symbols to their corresponding row and column numbers in the table. The table is then populated with the grammar rules based on the first and follow sets. The output is a table that can be used to parse strings in the given language.
| 5 pages
| 927 words

Trusted by 2+ million users,
1000+ happy students everyday

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;
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]!='#')

Found this document preview useful?

You are reading a preview
Upload your documents to download
Become a Desklib member to get accesss

Students who viewed this