Advertisement
Nayeemzaman

Lexical Analyzer Syntax Tokenization

Apr 24th, 2021
979
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.46 KB | None | 0 0
  1.  
  2. #include<iostream>
  3. #include<fstream>
  4. #include<stdlib.h>
  5. #include<string.h>
  6. #include<ctype.h>
  7.  
  8. using namespace std;
  9.  
  10. int isKeyword(char buffer[]){
  11.     char keywords[32][10] = {"auto","break","case","char","const","continue","default",
  12.                             "do","double","else","enum","extern","float","for","goto",
  13.                             "if","int","long","register","return","short","signed",
  14.                             "sizeof","static","struct","switch","typedef","union",
  15.                             "unsigned","void","volatile","while"};
  16.     int i, flag = 0;
  17.  
  18.     for(i = 0; i < 32; ++i){
  19.         if(strcmp(keywords[i], buffer) == 0){
  20.             flag = 1;
  21.             break;
  22.         }
  23.     }
  24.  
  25.     return flag;
  26. }
  27. bool isInteger(char* str)
  28. {
  29.     int i, length = strlen(str);
  30.  
  31.     if (length == 0)
  32.         return false;
  33.     for (i = 0; i < length; i++) {
  34.         if (str[i] != '0' && str[i] != '1' && str[i] != '2'
  35.             && str[i] != '3' && str[i] != '4' && str[i] != '5'
  36.             && str[i] != '6' && str[i] != '7' && str[i] != '8'
  37.             && str[i] != '9' || (str[i] == '-' && i > 0))
  38.  
  39.         return false;
  40.     }
  41.     return true;
  42. }
  43. bool isRealNumber(char* str)
  44. {
  45.     int i, length = strlen(str);
  46.     bool hasDecimal = false;
  47.  
  48.  
  49.     if (length == 0)
  50.         return false;
  51.     for (i = 0; i < length; i++) {
  52.         if (str[i] != '0' && str[i] != '1' && str[i] != '2'
  53.             && str[i] != '3' && str[i] != '4' && str[i] != '5'
  54.             && str[i] != '6' && str[i] != '7' && str[i] != '8'
  55.             && str[i] != '9' && str[i] != '.' ||
  56.             (str[i] == '-' && i > 0))
  57.             return false;
  58.         if (str[i] == '.')
  59.             hasDecimal = true;
  60.     }
  61.     return hasDecimal;
  62. }
  63.  
  64. int main(){
  65.     char character, buffer[15], operators[] = "+-*/%=";
  66.     ifstream file("compiler.txt");
  67.     int i,j=0;
  68.  
  69.     if(!file.is_open()){
  70.         cout<<"Can not open the file\n";
  71.         exit(0);
  72.     }
  73.  
  74.     while(!file.eof()){ // .eof return true when there is no data in the file and return false otherwise
  75.         character = file.get();
  76.  
  77.         for(i = 0; i < 6; ++i){
  78.             if(character == operators[i])
  79.                 cout<<character<<" is operator\n";
  80.         }
  81.  
  82.         if(isalnum(character)){
  83.             buffer[j++] = character;
  84.         }
  85.         else if((character == ' ' || character == '\n') && (j != 0)){
  86.                 buffer[j] = '\0';
  87.                 j = 0;
  88.  
  89.                 if(isKeyword(buffer) == 1)
  90.                     cout<<buffer<<" is keyword\n";
  91.                 else if(isInteger(buffer))
  92.                     cout<<buffer<<" is an Integer\n";
  93.                 else if(isRealNumber(buffer))
  94.                     cout<<buffer<<" is a Real Number\n";
  95.                 else
  96.                     cout<<buffer<<" is Identifier\n";
  97.         }
  98.  
  99.     }
  100.  
  101.     file.close();
  102.  
  103.     return 0;
  104. }
  105.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement