Created
May 18, 2018 23:19
-
-
Save axsddlr/6adaad82919f90ae6376171c90bc0a95 to your computer and use it in GitHub Desktop.
Project 1 created by Ayysir - https://repl.it/@Ayysir/Project-1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <fstream> | |
| #include <stdlib.h> | |
| #include <string.h> | |
| #include <ctype.h> | |
| using namespace std; | |
| // check for keywords | |
| int isKeyword(char buffer[]) { | |
| char keywords[32][10] = { | |
| "auto", "break", "case", "char", "const", "continue", | |
| "default", "do", "double", "else", "enum", "extern", | |
| "float", "for", "goto", "if", "int", "long", | |
| "register", "return", "short", "signed", "sizeof", "static", | |
| "struct", "switch", "typedef", "union", "unsigned", "void", | |
| "volatile", "while"}; | |
| int i, flag = 0; | |
| for (i = 0; i < 32; ++i) { | |
| if (strcmp(keywords[i], buffer) == 0) { | |
| flag = 1; | |
| break; | |
| } | |
| } | |
| return flag; | |
| } | |
| // Main function | |
| int main() { | |
| char ch, buffer[15], operators[] = "+-*/%="; | |
| ifstream fin("program.txt"); // File containing the program | |
| int i, j = 0; | |
| if (!fin.is_open()) { | |
| cout << "error while opening the file\n"; | |
| exit(0); | |
| } | |
| // iterate on the file containing the program to be analyzed | |
| while (!fin.eof()) { | |
| ch = fin.get(); | |
| // get all the operators | |
| for (i = 0; i < 6; ++i) { | |
| if (ch == operators[i]) cout << ch << " is operator\n"; | |
| } | |
| // get the numbers to be operated on | |
| if (isalnum(ch)) { | |
| buffer[j++] = ch; | |
| } | |
| // get the statements | |
| else if ((ch == ' ' || ch == '\n') && (j != 0)) { | |
| buffer[j] = '\0'; | |
| j = 0; | |
| if (isKeyword(buffer) == 1) | |
| cout << buffer << " is keyword\n"; | |
| else | |
| cout << buffer << " is indentifier\n"; | |
| } | |
| } | |
| fin.close(); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment