forked from stupidly-logical/lex-yacc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths0e0.cpp
executable file
·137 lines (102 loc) · 2.14 KB
/
s0e0.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Yash Krishan Verma
// A rudimentary C++ code for lexical analysis.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
map<string,ll> mp;
int checkConstant(string s ,int st, int end)
{
int res=-1,i;
for( i=st;i<end;i++)
{
if(!(s[i]==46 ||(s[i]>=48 && s[i]<=57)))
break;
}
if(i==st)
return -1;
else
return i;
}
bool isoperand(char x)
{
return ((x=='=')||(x=='-')||(x=='+')||(x=='*')||(x=='/')||(x=='%')|| (x=='(')||(x==')'));
}
string checkOperand(char x)
{
string s="";
if(x=='=')
s+="Assignment";
else if(x=='-')
s+="Subtraction";
else if(x=='+')
s+="Addition";
else if(x=='*')
s+="Multiplication";
else if(x=='/')
s+="Division";
else if(x=='%')
s+="Modulus";
else if(x=='(')
s+="Bracket Open";
else if(x==')')
s+="Bracket Close";
s+="Operator";
return s;
}
int main(int argc, char* argv[])
{
string exp;
cout<<"Input expression"<<endl;
cin>>exp;
int f=exp.size();
int identi=1;
for(int i=0;i<f;i++)
{
if(exp[i]==' ')
continue;
else if(exp[i]==46 ||(exp[i]>=48 && exp[i]<=57))
{
string stri="";
stri+=exp[i];
bool flag=(exp[i]==46);
int j;
for(j=i+1;j<f;j++)
{
if(!(exp[j]==46 ||(exp[j]>=48 && exp[j]<=57)))
break;
flag= flag|(exp[j]==46);
stri+=exp[j];
}
if(flag)
cout<<"<Floating Const,"<<stri<<">"<<" "<<endl;
else
cout<<"<Const,"<<stri<<">"<<" ";
i=j-1;
}
else if(isoperand(exp[i]))
{
cout<<"<"<<checkOperand(exp[i])<<">"<<" "<<endl;
}
else
{
string stri="";
stri+=exp[i];
int j;
for( j=i+1;j<f;j++)
{
if(isoperand(exp[j]))
break;
if(exp[j]==' ')
break;
stri+=exp[j];
}
if(mp[stri]==0)
{mp[stri]=identi;
identi++;
}
cout<<"<identifier,"<<mp[stri]<<">"<<" "<<endl;
i=j-1;
}
}
}