C++ Program to parse a string using Operator Precedence parsing
Program to parse a string using Operator Precedence parsing in 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 |
#include<conio.h> #include<stdio.h> #include<stdlib.h> int Operator_Position_Finder(char ); #define node struct Basic_tree int T4Tutorials[5][5]={ {1,0,0,1,1}, {1,1,0,1,1}, {0,0,0,2,3}, {1,1,3,1,1}, {0,0,0,3,2}}; int tos=-1; void T4Tutorials_value(void); //node create_node(char,*node);void DisplayTree( node *); int isOperator(char ); struct Basic_tree { char data; node *lptr; node *rptr; }*first; struct opr { char op_name; node *t; }oprate[50]; char current_operators[5]={'+','*','(',')','['}; char OperatorsInStack[5]={'+','*','(',')',']'}; void main() { char exp[10]; int myVariable=0,row=0,col=0; node *temp; // clrscr(); printf("Enter Exp : "); scanf("%s",exp); T4Tutorials_value(); while(exp[myVariable] != '\0') { if(myVariable==0) { tos++; oprate[tos].op_name = exp[tos]; } else { if(isOperator(exp[myVariable]) == -1) { oprate[tos].t = (node*) malloc (sizeof(node)); oprate[tos].t->data = exp[myVariable]; oprate[tos].t->lptr = '\0'; oprate[tos].t->rptr = '\0'; } else { row = Operator_Position_Finder(oprate[tos].op_name); col = Operator_Position_Finder(exp[myVariable]); if(T4Tutorials[row][col] == 0) { tos++; oprate[tos].op_name = exp[myVariable]; } elseif(T4Tutorials[row][col] == 1) { temp = (node*) malloc (sizeof(node)); temp->data = oprate[tos].op_name; temp->lptr = (oprate[tos-1].t); temp->rptr = (oprate[tos].t); tos--; oprate[tos].t = temp
; myVariable--; } elseif(T4Tutorials[row][col] == 2) { //temp = (node*) malloc (sizeof(node)); temp = oprate[tos].t; tos--; oprate[tos].t = temp; } elseif(T4Tutorials[row][col] == 3) { printf("\nExpression is Invalid...\n"); printf("%c %c can not occur simultaneously\n",oprate[tos].op_name,exp[myVariable]); break; } } } myVariable++; } printf("show tree \n\n\n"); DisplayTree(oprate[tos].t); printf("Over"); getch(); getch(); } int isOperator(char c) { int i=0; for(i=0;i<5;i++) { if (c==current_operators[i] || c==OperatorsInStack[i]) break; } if(i==5) return (-1); elsereturn i; } int Operator_Position_Finder(char c) { int i; for(i=0;i<5;i++) { if (c==current_operators[i] || c==OperatorsInStack[i]) break; } return i; } void DisplayTree(node *start) { if(start->lptr != NULL) DisplayTree(start->lptr); if(start->rptr != NULL) DisplayTree(start->rptr); printf("%c \n",start->data); } void T4Tutorials_value(void) { int i,j; printf("OPERATOR PRECEDENCE Matrix\n"); printf("===========================\n "); for(i=0; i<5; i++) { printf("%c ",OperatorsInStack[i]); } printf("\n"); for(i=0;i<5;i++) { printf("%c ",current_operators[i]); for(j=0;j<5;j++) { if(T4Tutorials[i][j] == 0) printf("< "); elseif(T4Tutorials[i][j] == 1) printf("> "); elseif(T4Tutorials[i][j] == 2) printf("= "); elseif(T4Tutorials[i][j] == 3) printf(" "); } printf("\n"); } } |
Output