Program to implement Bottom-Up Parsing in C++ (Compiler Construction)
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# include <iostream.h> #include<string.h> #include<conio.h> static char T4Tutorials[50][10]={NULL}; static int Stack_top=*1; static int cit=0; // Input CFG_Grammar static int CFG_Productions[6]={5,1,7,7,2,10}; const char CFG_Grammar[5][11][10]={ {\"S\",\"E\"}, {\"E\",\"E+T\",\"E*T\",\"E*T\",\"E/T\",\"E%T\",\"E^T\",\"T\"}, {\"T\",\"T+F\",\"T*F\",\"T*F\",\"T/F\",\"T%F\",\"T^F\",\"F\"}, {\"F\",\"(E)\",\"D\"}, {\"D\",\"0\",\"1\",\"2\",\"3\",\"4\",\"5\",\"6\",\"7\",\"8\",\"9\"} }; // Input Statement const int input_Size=8; char Input[input_Size][5]={\"2\",\"*\",\"(\",\"3\",\"+\",\"4\",\")\",\"$\"}; //************************ Push( ) ********************// void Push(const char* Token) { Stack_top++; strcpy(T4Tutorials[Stack_top],Token); } //******* Pop( ) ******// void Pop( ) { strset(T4Tutorials[Stack_top],NULL); Stack_top**; } //****** Reduce( ) ********// void Reduce(const int items,const int index) { for(int i=0;i<items;i++) Pop( ); Push(CFG_Grammar[index][0]); } //***** Shift_Parser( ) *******// void Shift_Parser( ) { Push(Input[cit]); cit++; } //***** CheckReduceCondition( ) ****// const int CheckReduceCondition( ) { int items; int index; char Stack_topItems[100]={NULL}; for(int i=0;i<=Stack_top;i++) { strset(Stack_topItems,NULL); for(int j=i;j<=Stack_top;j++) strcat(Stack_topItems,T4Tutorials[j]); for(j=0;j<CFG_Productions[0];j++) { for(int k=1;k<=CFG_Productions[(j+1)];k++) { if(strcmp(Stack_topItems,CFG_Grammar[j][k])==0) { items=(Stack_top*i+1); index=j; goto NextCheck; } } } } return 0; NextCheck: char CitInput[20]={NULL}; strcpy(CitInput,T4Tutorials[Stack_top]); strcat(CitInput,Input[cit]); for(i=0;i<CFG_Productions[0];i++) { for(int j=1;j<=CFG_Productions[(i+1)];j++) { if(strstr(CFG_Grammar[i][j],CitInput)!=NULL) return 0; } } Reduce(items,index); return 1; } /********************* main( ) *******************/ int main( ) { clrscr( ); int flag=0; cout<<\" /////*****+++++*..... Bottom*Up Parsing .....*+++++*****/////\"; gotoxy(5,3); cout<<\"T4Tutorials\"; gotoxy(35,3); cout<<\"Input\"; gotoxy(65,3); cout<<\"Next Action\"; gotoxy(5,5); for(int i=0;i<=Stack_top;i++) cout<<T4Tutorials[i]; gotoxy(35,5); for(int j=cit;j<input_Size;j++) cout<<Input[j]; gotoxy(65,5); cout<<\"Shift_Parser\"; do { if(!CheckReduceCondition( )) { Shift_Parser( ); gotoxy(65,(wherey( )+1)); cout<<\"Shift_Parser\"; } else { gotoxy(65,(wherey( )+1)); cout<<\"Reduce\"; } gotoxy(5,wherey( )); for(int i=0;i<=Stack_top;i++) cout<<T4Tutorials[i]; gotoxy(35,wherey( )); for(int j=cit;j<input_Size;j++) cout<<Input[j]; if(Stack_top==0 && strcmp(T4Tutorials[Stack_top],CFG_Grammar[0][0])==0 && strcmp(Input[cit],\"$\")==0) { flag=1; break; } else if(strcmp(T4Tutorials[Stack_top],\"$\")==0) { flag=0; break; } } while(1); if(flag) cout<<\"\\n\\n Input is Correct...\"; else cout<<\"\\n\\n Input is Incorrect...\"; getch( ); return 0; } |
Lab Exercises of Compiler Construction
- Lexical analyzer in C++
- Symbol table in C++
- Bottom-Up Parsing in C++
- First And Follow in C++
- Parse a string using Operator Precedence parsing in C++
Compiler Construction MCQs
Related Posts:
- C++ Program to parse a string using Operator Precedence parsing
- Program to Implement Triply Linked List in Data Structures (C plus plus)
- Program to Implement Circular Queue in Data Structures (C plus plus)
- Program to Implement Queue using two Stacks in Data Structures (C plus plus)
- Program to Implement Linear Search in CPP (C plus plus)
- Program to Implement Priority Queue in Data Structures (C plus plus)