Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <map>
- #include <string.h>
- using namespace std;
- char keywords[6][10] = {"if", "while", "int", "main", "for", "long"};
- int isKeyword(char buffer[]) {
- for (int i = 0; i < 6; i++) {
- if (strcmp(keywords[i], buffer) == 0) {
- return true;
- }
- }
- return false;
- }
- map<int, string> synatxtable;
- char operators[12] = "{}()+-*%=";
- int compare() {
- int counter = 0;
- char buffer[10];
- int next = 0;
- int lineerror = -1;
- bool flag = false;
- ifstream reads("testfile.txt");
- while (!reads.eof()) { // keep looping untill the end of the file is reached.
- char buff = reads.get();
- for (int i = 0; i < 12; ++i) { // tests the charecters for operators
- if (buff == operators[i]) {
- cout << buff << " Is an operator " << endl;
- continue;
- }
- }
- if (buff == '/') {
- buff = reads.get();
- if (buff == '/') {
- while (true) { // if a / is followed by another dash then keep looping
- // untill we get to the end of the line
- // (/n) is acharecter that indicates a new line)
- buff = reads.get();
- if (buff == '\n') {
- cout << "Single line is Comment removed" << endl;
- break;
- }
- }
- } else if (buff == '*') {
- while (true) {
- buff = reads.get();
- if (buff == '/') {
- cout << "Multiple line is Comment removed" << endl;
- break;
- }
- }
- } else {
- cout << '/' << " Is an operator " << endl;
- }
- }
- if (isalnum(buff)) { // if
- buffer[next++] = buff;
- } else if (next && (buff == ',' || buff == ' ' || buff == '\n')) {
- buffer[next] = '\0';
- next = 0;
- if (isKeyword(buffer) == 1)
- cout << buffer << " is a keyword" << endl;
- else {
- cout << buffer << " is an indentifier" << endl;
- synatxtable[counter++] = buffer;
- }
- }
- }
- }
- int main() {
- compare();
- cout << "____________" << endl;
- for (auto i : synatxtable) {
- cout << "Identifier :" << i.first << " ID:" << i.second << endl;
- }
- cout << "____________" << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement