-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathLexicalAnalyzer.cpp
89 lines (76 loc) · 1.63 KB
/
LexicalAnalyzer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include<iostream>
#include<cstring>
#include<stdlib.h>
#include<ctype.h>
#include<fstream>
using namespace std;
string arr[] = { "void", "using", "namespace", "int", "include", "iostream", "std", "main",
"cin", "cout", "return", "float", "double", "string" };
bool
isKeyword (string a)
{
for (int i = 0; i < 14; i++)
{
if (arr[i] == a)
{
return true;
}
}
return false;
}
int main()
{
string s;
cout << "Enter the input: \n";
cout << "\n";
while (cin >> s)
{
if (s == "+" || s == "-" || s == "" || s == "/" || s == "^" || s == "&&" || s == "||" || s == "=" || s == "==" || s == "&" || s == "|" || s == "%" || s == "++" || s == "--" || s == "+=" || s == "-=" || s == "/=" || s == "=" || s == "%=")
{
cout << s << " is an operator\n";
s = "";
}
else if (isKeyword (s))
{
cout << s << " is a keyword\n";
s = "";
}
else if (s == "(" || s == "{" || s == "[" || s == ")" || s == "}" || s == "]" || s == "<" || s == ">" || s == "()" || s == ";" || s == "<<" || s == ">>" || s == "," || s == "#")
{
cout << s << " is a symbol\n";
s = "";
}
else if (s == "\n" || s == " " || s == "")
{
s = "";
}
else if (isdigit (s[0]))
{
int x = 0;
if (!isdigit (s[x++]))
{
continue;
}
else
{
cout << s << " is a constant\n";
s = "";
}
}
else
{
cout << s << " is an identifier\n";
s = "";
}
}
return 0;
}
// Input for the code
// #include <stdio.h>
// void main ( )
// {
// int x = 6 ;
// int y = 4 ;
// x = x + y ;
// printf("%d", x);
// }