Advertisement
hamzarmehyar

Untitled

May 26th, 2020
1,283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.21 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <map>
  5. #include <string.h>
  6. using namespace std;
  7. char keywords[6][10] = {"if", "while", "int", "main", "for", "long"};
  8. int isKeyword(char buffer[]) {
  9.   for (int i = 0; i < 6; i++) {
  10.     if (strcmp(keywords[i], buffer) == 0) {
  11.       return true;
  12.     }
  13.   }
  14.   return false;
  15. }
  16. map<int, string> synatxtable;
  17. char operators[12] = "{}()+-*%=";
  18. int compare() {
  19.   int counter = 0;
  20.   char buffer[10];
  21.   int next = 0;
  22.   int lineerror = -1;
  23.   bool flag = false;
  24.   ifstream reads("testfile.txt");
  25.   while (!reads.eof()) { // keep looping untill the end of the file is reached.
  26.     char buff = reads.get();
  27.     for (int i = 0; i < 12; ++i) { // tests the charecters for operators
  28.       if (buff == operators[i]) {
  29.         cout << buff << " Is an operator " << endl;
  30.         continue;
  31.       }
  32.     }
  33.     if (buff == '/') {
  34.       buff = reads.get();
  35.       if (buff == '/') {
  36.         while (true) { // if a / is followed by another dash then keep looping
  37.                        // untill we get to the end of the line
  38.                        // (/n) is acharecter that indicates a new line)
  39.           buff = reads.get();
  40.           if (buff == '\n') {
  41.             cout << "Single line is Comment removed" << endl;
  42.             break;
  43.           }
  44.         }
  45.       } else if (buff == '*') {
  46.         while (true) {
  47.           buff = reads.get();
  48.           if (buff == '/') {
  49.             cout << "Multiple line is Comment removed" << endl;
  50.             break;
  51.           }
  52.         }
  53.       } else {
  54.         cout << '/' << " Is an operator " << endl;
  55.       }
  56.     }
  57.     if (isalnum(buff)) { // if
  58.       buffer[next++] = buff;
  59.     } else if (next && (buff == ',' || buff == ' ' || buff == '\n')) {
  60.       buffer[next] = '\0';
  61.       next = 0;
  62.       if (isKeyword(buffer) == 1)
  63.         cout << buffer << " is a keyword" << endl;
  64.       else {
  65.         cout << buffer << " is an indentifier" << endl;
  66.         synatxtable[counter++] = buffer;
  67.       }
  68.     }
  69.   }
  70. }
  71.  
  72. int main() {
  73.   compare();
  74.   cout << "____________" << endl;
  75.   for (auto i : synatxtable) {
  76.     cout << "Identifier :" << i.first << " ID:" << i.second << endl;
  77.   }
  78.   cout << "____________" << endl;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement