Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cctype>
- #include <cstdio>
- #include <fstream>
- using namespace std;
- typedef enum TSymbolType {
- intconst, text, semicolon, period, quotas, othersy
- } TSymbol;
- char Char;
- TSymbol Symbol;
- char Spelling[9];
- const int MAXLENGTH = 8;
- int Constant;
- const int MAXINTEGER = 1000000;
- ifstream inputFile("C:\\Users\\Aleksandar\\source\\repos\\Parser\\Parser\\input.txt");
- void GetNextChar() {
- if (!inputFile.is_open()) {
- fprintf(stderr, "Error: Unable to open input file\n");
- return;
- }
- Char = inputFile.get();
- }
- void error(const char* message) {
- fprintf(stderr, "Error: %s\n", message);
- }
- void GetNextSymbol() {
- int digit;
- int k = 0;
- while (!inputFile.eof() && !isdigit(Char) && Char != '\"' && Char != ';' && Char != '.' && !isalpha(Char)) {
- GetNextChar();
- }
- if (inputFile.eof()) {
- Char = '\0';
- }
- while (Char == ' ') {
- GetNextChar();
- }
- switch (toupper(Char)) {
- case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
- case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
- case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
- case 'V': case 'W': case 'X': case 'Y': case 'Z': {
- do {
- if (k < MAXLENGTH) {
- Spelling[k] = Char;
- k++;
- }
- GetNextChar();
- } while ((Char >= 'A' && Char <= 'Z') || (Char >= '0' && Char <= '9'));
- Spelling[k] = '\0';
- if (k > 8) {
- error("String is too long!");
- }
- Symbol = text;
- cout << "Text: " << Spelling << endl;
- } break;
- case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': {
- Constant = 0;
- do {
- digit = Char - '0';
- if ((Constant < (MAXINTEGER / 10)) || ((Constant == (MAXINTEGER / 10)) && (digit <= (MAXINTEGER % 10)))) {
- Constant = (10 * Constant) + digit;
- }
- else {
- error("Int constant too large!");
- Constant = 0;
- }
- GetNextChar();
- } while (Char >= '0' && Char <= '9');
- Symbol = intconst;
- cout << "Integer: " << Constant << endl;
- } break;
- case '.': {
- Symbol = period;
- GetNextChar();
- cout << "Period" << endl;
- return;
- }
- case ';': {
- Symbol = semicolon;
- GetNextChar();
- cout << "Semicolon" << endl;
- } break;
- case '\"': {
- Symbol = quotas;
- GetNextChar();
- int insideQuote = 0;
- while (insideQuote < 8 && Char != '\"') {
- Spelling[insideQuote] = Char;
- insideQuote++;
- GetNextChar();
- }
- if (Char == '\"') {
- Spelling[insideQuote] = '\0';
- cout << "String: " << Spelling << endl;
- GetNextChar();
- }
- else {
- error("String is too long or missing closing double quote(s)!");
- Spelling[insideQuote] = '\0';
- cout << "String: " << Spelling << endl;
- }
- } break;
- case '\0': {
- if (Symbol != othersy) {
- Symbol = othersy;
- cout << "End of Input" << endl;
- }
- break;
- }
- default: {
- Symbol = othersy;
- cout << "Unknown Symbol with ASCII value: " << static_cast<int>(Char) << endl;
- GetNextChar();
- break;
- }
- }
- }
- int accept(TSymbol symbol) {
- if (Symbol == symbol) {
- GetNextSymbol();
- return 1;
- }
- return 0;
- }
- int expect(TSymbol symbol) {
- if (accept(symbol)) {
- return 1;
- }
- error("Unexpected symbol!");
- return 0;
- }
- void Field() {
- if (accept(intconst) || accept(quotas) || accept(text)) {
- return;
- }
- else if (accept(period)) {
- accept(quotas);
- if (Symbol == text) {
- Field();
- }
- else {
- error("Expects text after period!");
- }
- }
- else if (Symbol != period && Symbol != othersy && Symbol != '\0') {
- error("Expects intconst, string, text, period, or end of input!");
- }
- }
- void Record() {
- Field();
- while (Symbol == period) {
- accept(period);
- Field();
- }
- if (!(Symbol == semicolon || Symbol == othersy || Symbol == '\0')) {
- error("Expects semicolon or end of input after the last field!");
- }
- if (Symbol == semicolon) {
- accept(semicolon);
- }
- }
- void DataFile() {
- Record();
- while (Symbol != othersy) {
- Record();
- }
- inputFile.close();
- }
- int main() {
- GetNextChar();
- GetNextSymbol();
- DataFile();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement