#include #include typedef char lexunit[12]; enum lexeme {REC, PROG, PROC, FUNC, FOR, WHILE, REPEAT, THEN, ELSE, DO, BEGIN, UNTIL, END1, END2, END3, JUNK}; void Search (char [], lexunit [], lexeme &); int main () { lexunit keys[] = {"record", "program", "procedure", "function", "for", "while", "repeat", "then", "else", "do", "begin", "until", "end", "end;", "end."}; lexeme stack[50]; int top = -1; lexeme where; int ahead; char word[60]; char filename[30]; ifstream input; cout << "File to analyze: "; cin >> filename; input.open(filename); input >> word; while (input) { ahead = 0; Search (word, keys, where); if (where < JUNK) { if (where < DO) { top++; stack[top] = where; if (where != REC) cout << ' ' << (int)where; if (where == REPEAT) cout << 'B'; } if (where == THEN || where == ELSE || where == DO) { input >> word; ahead = 1; if (strcmp(word,keys[(int)BEGIN]) != 0) top--; } if (where == BEGIN) cout << 'B'; if (where >= UNTIL) { if (stack[top] != REC) cout << ' ' << (int)stack[top] << 'E'; top--; } } if (!ahead) input >> word; } input.close(); cout << endl; return 0; } void Search (char word[], lexunit keys[], lexeme &where) { int j = 0; int count = (int)JUNK; while (j < count && strcmp(word, keys[j]) != 0) j++; where = (lexeme) j; }