70 likes | 171 Views
مولد تحلیلگر نحوی. مولد تحلیلگر نحوی. void main() { int NoRules, len; char **Rules, *statement; NoRules = ReadGrammar(&rules, statement); len = TopDownParse(rules, statement, 0, NoRules, 0); if(len != strlen(statement)) { clrscr(); puts("ERROR"); } }.
E N D
void main() { int NoRules, len; char **Rules, *statement; NoRules = ReadGrammar(&rules, statement); len = TopDownParse(rules, statement, 0, NoRules, 0); if(len != strlen(statement)) { clrscr(); puts("ERROR"); } }
int TopDownParse(char **rules, char *statement, int start, int NoRules, int ix) { int i, j, k, m, NewK; char leftterm, input; input = statement[ix]; i= StartWhichRule(rules, input, start, NoRules); if(i < 0)return ix; for(j=1,k=ix; rules[i][j] && statement[k] && i< NoRules; j++) if(isupper(rules[i][j])) { for(m = 0; rules[m][0] != rules[i][j] && m < NoRules; m++); Newk = TopDownParse(rules, statement, m, NoRules, k); if(k==NewK) return 0; else k = NewK; } else if(rules[i][j] == statement[k]) k++; return k }
int StartWhichRule(char **rules, char input, int start, int NoRules) { int success, i, k; char LeftNonTerm; LeftNonTerm = rules[start][0]; i= start; while((rules[i][1] !=input) && rules[i][0] == LeftNonTerm)) i++; if(rules[i][1] == input) return i; for(i = start, success = -1; rules[i][0] == LeftNonTerm && success < 0; i++) if(isupper(rules[i][1])) { for(k = 0; rules[k][0] != rules[i][1] && k < NoRules; k++); success = StartWhicnRule(rules, input, k, NoRules); } if (success <0) return success; retum i; }
int StartWhichRule(char **rules, char input, int start, int NoRules) { int success, i, k; char LeftNonTerm; LeftNonTerm = rules[start][0]; i= start; while((rules[i][1] !=input) && rules[i][0] == LeftNonTerm)) i++; if((rules[i][1] == input) && rules[i][0] == LeftNonTerm)) return i; for(i = start, success = -1; rules[i][0] == LeftNonTerm && success < 0; i++) if(isupper(rules[i][1])) { for(k = 0; rules[k][0] != rules[i][1] && k < NoRules; k++); success = StartWhicnRule(rules, input, k, NoRules); } if (success <0) return success; retum i; }