Guest User

Untitled

a guest
May 24th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6.  
  7. using namespace std;
  8. // check for keywords
  9. int isKeyword(char buffer[]) {
  10. char keywords[32][10] = {
  11. "auto", "break", "case", "char", "const", "continue",
  12. "default", "do", "double", "else", "enum", "extern",
  13. "float", "for", "goto", "if", "int", "long",
  14. "register", "return", "short", "signed", "sizeof", "static",
  15. "struct", "switch", "typedef", "union", "unsigned", "void",
  16. "volatile", "while"};
  17. int i, flag = 0;
  18.  
  19. for (i = 0; i < 32; ++i) {
  20. if (strcmp(keywords[i], buffer) == 0) {
  21. flag = 1;
  22. break;
  23. }
  24. }
  25.  
  26. return flag;
  27. }
  28. // Main function
  29. int main() {
  30. char ch, buffer[15], operators[] = "+-*/%=";
  31. ifstream fin("program.txt"); // File containing the program
  32. int i, j = 0;
  33.  
  34. if (!fin.is_open()) {
  35. cout << "error while opening the file\n";
  36. exit(0);
  37. }
  38. // iterate on the file containing the program to be analyzed
  39. while (!fin.eof()) {
  40. ch = fin.get();
  41. // get all the operators
  42. for (i = 0; i < 6; ++i) {
  43. if (ch == operators[i]) cout << ch << " is operator\n";
  44. }
  45. // get the numbers to be operated on
  46. if (isalnum(ch)) {
  47. buffer[j++] = ch;
  48. }
  49. // get the statements
  50. else if ((ch == ' ' || ch == '\n') && (j != 0)) {
  51. buffer[j] = '\0';
  52. j = 0;
  53.  
  54. if (isKeyword(buffer) == 1)
  55. cout << buffer << " is keyword\n";
  56. else
  57. cout << buffer << " is indentifier\n";
  58. }
  59. }
  60.  
  61. fin.close();
  62.  
  63. return 0;
  64. }
Add Comment
Please, Sign In to add comment