Input:
The user will input strings like :
------------------
int a = 12;
------------------
float b = 23.32;
------------------
a + b;
------------------
a+12
------------------
Output:
***
---------------------
Keyword: int
Identifier: a
Operator: =
Constant: 12
---------------------
Keyword: float
Identifier b
Operator: =
Constant: 23.23
---------------------
Identifier: a
Operator: +
Identifier: b
---------------------
[Error] : Statements must be terminated by a semicolon!
---------------------
***
This program will use the space to separate the various tokens and then identifies them. So for example, this won't work:
int a=12;
We were given the old Turbo C++ compiler for use in our college. So writing this was a lot of mess. -.-
Anyways, here is the code:
The user will input strings like :
------------------
int a = 12;
------------------
float b = 23.32;
------------------
a + b;
------------------
a+12
------------------
Output:
***
---------------------
Keyword: int
Identifier: a
Operator: =
Constant: 12
---------------------
Keyword: float
Identifier b
Operator: =
Constant: 23.23
---------------------
Identifier: a
Operator: +
Identifier: b
---------------------
[Error] : Statements must be terminated by a semicolon!
---------------------
***
This program will use the space to separate the various tokens and then identifies them. So for example, this won't work:
int a=12;
We were given the old Turbo C++ compiler for use in our college. So writing this was a lot of mess. -.-
Anyways, here is the code:
#include <iostream.h> #include <stdio.h> #include <ctype.h> #include <string.h> #include <conio.h> char input[50]; char keyword[50][50] = { "int", "float", "double", "byte", "long" }; char op[50][50] = { "+", "-", "/", "*", "%", "=", "-=", "+=" }; void check(char *buff){ //check for keywords: for (int i = 0; i < 5; ++i){ if (strcmp(keyword[i], buff) == 0){ cout << "\nKeyword: " << buff; return; } } //check for operators: for (int j = 0; j < 8; ++j){ if (strcmp(op[j], buff) == 0){ cout << "\nOperator: " << buff; return; } } if (buff[0] == '!'){ cout << "\nOperator: " << '!'; cout << "\nIdentifier: "; int index = 1; while (buff[index] != '\0'){ putchar(buff[index]); index++; } return; } //check for constants and identifiers: int index = 0; int isNotConstant = 0; while (buff[index] != '\0'){ if (buff[index] == '.') ; else if (!isdigit(buff[index])){ isNotConstant = 1; break; } index++; } if (isNotConstant){ cout << "\nIdentifier: " << buff; } else cout << "\nConstant: " << buff; } int main(){ clrscr(); cout << "Enter the expression: "; gets(input); int last = -1; char buff[50]; int error = 1; int buffIndex = 0; for (int i = 0; i < 50; ++i){ if (input[i] == ';'){ error = 0; for (int j = i; j >= 0; --j){ if (last == -1)last = i; if (j == 0){ for (int k = 0; k < last; ++k){ buff[buffIndex++] = input[k]; } buff[buffIndex++] = '\0'; check(buff); buffIndex = 0; } if (input[j] == ' '){ for (int k = j + 1; k < last; ++k){ buff[buffIndex++] = input[k]; } buff[buffIndex++] = '\0'; check(buff); buffIndex = 0; last = j; } } } } if (error) cout << "\n[Error] : Statements must be terminated by a semicolon!"; cout << endl; getch(); return 0; }
No comments:
Post a Comment