Advertisement
AleksandarH

III Parser

Dec 19th, 2023
526
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.79 KB | None | 0 0
  1. #include <iostream>
  2. #include <cctype>
  3. #include <cstdio>
  4. #include <fstream>
  5.  
  6. using namespace std;
  7.  
  8. typedef enum TSymbolType {
  9.     intconst, text, semicolon, period, quotas, othersy
  10. } TSymbol;
  11.  
  12. char Char;
  13. TSymbol Symbol;
  14. char Spelling[9];
  15. const int MAXLENGTH = 8;
  16. int Constant;
  17. const int MAXINTEGER = 1000000;
  18. ifstream inputFile("C:\\Users\\Aleksandar\\source\\repos\\Parser\\Parser\\input.txt");
  19.  
  20. void GetNextChar() {
  21.     if (!inputFile.is_open()) {
  22.         fprintf(stderr, "Error: Unable to open input file\n");
  23.         return;
  24.     }
  25.     Char = inputFile.get();
  26. }
  27.  
  28. void error(const char* message) {
  29.     fprintf(stderr, "Error: %s\n", message);
  30. }
  31.  
  32. void GetNextSymbol() {
  33.     int digit;
  34.     int k = 0;
  35.     while (!inputFile.eof() && !isdigit(Char) && Char != '\"' && Char != ';' && Char != '.' && !isalpha(Char)) {
  36.         GetNextChar();
  37.     }
  38.     if (inputFile.eof()) {
  39.         Char = '\0';
  40.     }
  41.     while (Char == ' ') {
  42.         GetNextChar();
  43.     }
  44.     switch (toupper(Char)) {
  45.     case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
  46.     case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
  47.     case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
  48.     case 'V': case 'W': case 'X': case 'Y': case 'Z': {
  49.         do {
  50.             if (k < MAXLENGTH) {
  51.                 Spelling[k] = Char;
  52.                 k++;
  53.             }
  54.             GetNextChar();
  55.         } while ((Char >= 'A' && Char <= 'Z') || (Char >= '0' && Char <= '9'));
  56.         Spelling[k] = '\0';
  57.         if (k > 8) {
  58.             error("String is too long!");
  59.         }
  60.         Symbol = text;
  61.         cout << "Text: " << Spelling << endl;
  62.     } break;
  63.     case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': {
  64.         Constant = 0;
  65.         do {
  66.             digit = Char - '0';
  67.             if ((Constant < (MAXINTEGER / 10)) || ((Constant == (MAXINTEGER / 10)) && (digit <= (MAXINTEGER % 10)))) {
  68.                 Constant = (10 * Constant) + digit;
  69.             }
  70.             else {
  71.                 error("Int constant too large!");
  72.                 Constant = 0;
  73.             }
  74.             GetNextChar();
  75.         } while (Char >= '0' && Char <= '9');
  76.         Symbol = intconst;
  77.         cout << "Integer: " << Constant << endl;
  78.     } break;
  79.     case '.': {
  80.         Symbol = period;
  81.         GetNextChar();
  82.         cout << "Period" << endl;
  83.         return;
  84.     }
  85.     case ';': {
  86.         Symbol = semicolon;
  87.         GetNextChar();
  88.         cout << "Semicolon" << endl;
  89.     } break;
  90.     case '\"': {
  91.         Symbol = quotas;
  92.         GetNextChar();
  93.         int insideQuote = 0;
  94.         while (insideQuote < 8 && Char != '\"') {
  95.             Spelling[insideQuote] = Char;
  96.             insideQuote++;
  97.             GetNextChar();
  98.         }
  99.         if (Char == '\"') {
  100.             Spelling[insideQuote] = '\0';
  101.             cout << "String: " << Spelling << endl;
  102.             GetNextChar();
  103.         }
  104.         else {
  105.             error("String is too long or missing closing double quote(s)!");
  106.             Spelling[insideQuote] = '\0';
  107.             cout << "String: " << Spelling << endl;
  108.         }
  109.     } break;
  110.     case '\0': {
  111.         if (Symbol != othersy) {
  112.             Symbol = othersy;
  113.             cout << "End of Input" << endl;
  114.         }
  115.         break;
  116.     }
  117.     default: {
  118.         Symbol = othersy;
  119.         cout << "Unknown Symbol with ASCII value: " << static_cast<int>(Char) << endl;
  120.         GetNextChar();
  121.         break;
  122.     }
  123.     }
  124. }
  125.  
  126. int accept(TSymbol symbol) {
  127.     if (Symbol == symbol) {
  128.         GetNextSymbol();
  129.         return 1;
  130.     }
  131.     return 0;
  132. }
  133.  
  134. int expect(TSymbol symbol) {
  135.     if (accept(symbol)) {
  136.         return 1;
  137.     }
  138.     error("Unexpected symbol!");
  139.     return 0;
  140. }
  141.  
  142. void Field() {
  143.     if (accept(intconst) || accept(quotas) || accept(text)) {
  144.         return;
  145.     }
  146.     else if (accept(period)) {
  147.         accept(quotas);
  148.         if (Symbol == text) {
  149.             Field();
  150.         }
  151.         else {
  152.             error("Expects text after period!");
  153.         }
  154.     }
  155.     else if (Symbol != period && Symbol != othersy && Symbol != '\0') {
  156.         error("Expects intconst, string, text, period, or end of input!");
  157.     }
  158.  
  159. }
  160.  
  161. void Record() {
  162.     Field();
  163.     while (Symbol == period) {
  164.         accept(period);
  165.         Field();
  166.     }
  167.     if (!(Symbol == semicolon || Symbol == othersy || Symbol == '\0')) {
  168.         error("Expects semicolon or end of input after the last field!");
  169.     }
  170.     if (Symbol == semicolon) {
  171.         accept(semicolon);
  172.     }
  173. }
  174.  
  175. void DataFile() {
  176.     Record();
  177.     while (Symbol != othersy) {
  178.         Record();
  179.     }
  180.     inputFile.close();
  181. }
  182.  
  183. int main() {
  184.     GetNextChar();
  185.     GetNextSymbol();
  186.     DataFile();
  187.     return 0;
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement