@@ -8,11 +8,6 @@ ReversePolishNotation::~ReversePolishNotation()
88{
99}
1010
11- void ReversePolishNotation::reversePolishNotationAnalyze (std::string filePathOrName_C, std::string fileName_Path_SaveAnalis)
12- {
13-
14- }
15-
1611int ReversePolishNotation::getPriority (std::string word)
1712{
1813 // priority
@@ -23,4 +18,218 @@ int ReversePolishNotation::getPriority(std::string word)
2318 }
2419 return -1 ;
2520}
21+
22+ bool ReversePolishNotation::isExistsSymbolToTable (std::string word)
23+ {
24+ for (auto & item : priority)
25+ if (item.first == word)
26+ return true ;
27+ return false ;
28+ }
29+
30+
31+ void ReversePolishNotation::reversePolishNotationAnalyze (std::string fileName_lexical, std::string fileName_RPN)
32+ {
33+ std::ifstream lexical;
34+ std::ofstream fileAnalysis (fileName_RPN);
35+ lexical.exceptions (std::ifstream::badbit);
36+ try
37+ {
38+ lexical.open (fileName_lexical);
39+
40+ if (lexical.is_open ())
41+ {
42+ bool readComment = false ;
43+ std::string temp = " " ;
44+ while (!lexical.eof ())
45+ {
46+ std::string lineLexical = " " ;
47+ getline (lexical, lineLexical);
48+ if (lineLexical.length () > 2 && (isComment ((int )lineLexical[0 ],(int )lineLexical[1 ]) || isOneStringComment ((int )lineLexical[0 ], (int )lineLexical[1 ])))
49+ continue ;
50+
51+ size_t pos = 0 ;
52+ std::string token=" " , tempReadFunction=" " ;
53+ bool readFunction = false ;
54+ while ((pos = lineLexical.find (' ' )) != std::string::npos || lineLexical.length () !=0 )
55+ {
56+ if (pos == std::string::npos)
57+ {
58+ token = lineLexical.substr (0 , lineLexical.length ());
59+ lineLexical = " " ;
60+ }
61+ else
62+ {
63+ token = lineLexical.substr (0 , pos);
64+ lineLexical.erase (0 , pos + 1 );
65+ }
66+ if (tempReadFunction != " " )
67+ {
68+ if (tempReadFunction[0 ] == ' I' && token == " R3" )
69+ readFunction = true ;
70+ else
71+ tempReadFunction = token;
72+ }else
73+ tempReadFunction = token;
74+
75+
76+ int priorityToken = getPriority (token);
77+ if (priorityToken > -1 )
78+ {
79+ if (stack.size () == 0 )
80+ {
81+ std::map<std::string, int > tempMap;
82+ tempMap.insert (std::pair<std::string,int >(token, 0 ));
83+ stack.push_back (tempMap);
84+ continue ;
85+ }
86+ else
87+ {
88+ if (token == " R3" )
89+ {
90+ if (readFunction == false )
91+ {
92+ std::map<std::string, int > tempMap;
93+ tempMap.insert (std::pair<std::string, int >(token, 0 ));
94+ stack.push_back (tempMap);
95+ continue ;
96+ }
97+ else
98+ {
99+ std::map<std::string, int > tempMap;
100+ tempMap.insert (std::pair<std::string, int >(" Ô" , 1 ));
101+ stack.push_back (tempMap);
102+ continue ;
103+ }
104+
105+ }
106+ if (token == " R4" )
107+ {
108+ std::string end = " R3" ;
109+ if (readFunction == true )
110+ end = " Ô" ;
111+ while (stack.size () != 0 && stack.back ().rbegin ()->first != end)
112+ {
113+ auto upElemStack = stack.back ().rbegin ();
114+ fileAnalysis << upElemStack->first << " " ;
115+ stack.pop_back ();
116+ }
117+ if (readFunction == true && stack.size () !=0 )
118+ {
119+ auto upElemStack = stack.back ().rbegin ();
120+ upElemStack->second ++;
121+ fileAnalysis << upElemStack->second << upElemStack->first << " " ;
122+ readFunction = false ;
123+ }
124+ stack.pop_back ();
125+ continue ;
126+ }
127+ if (token == " R1" )
128+ {
129+ std::map<std::string, int > tempMap;
130+ tempMap.insert (std::pair<std::string, int >(" ÀÝÌ" , 2 ));
131+ stack.push_back (tempMap);
132+ continue ;
133+ }
134+ if (token == " R8" )
135+ {
136+ if (readFunction == false )
137+ {
138+ auto upElemStack = stack.back ().rbegin ();
139+ while (stack.size () != 0 && upElemStack->first != " ÀÝÌ" )
140+ {
141+ fileAnalysis << upElemStack->first << " " ;
142+ stack.pop_back ();
143+ if (stack.size () != 0 )
144+ upElemStack = stack.back ().rbegin ();
145+ }
146+ upElemStack->second ++;
147+ continue ;
148+ }
149+ else
150+ {
151+ auto upElemStack = stack.back ().rbegin ();
152+ while (stack.size () != 0 && upElemStack->first != " Ô" )
153+ {
154+ fileAnalysis << upElemStack->first << " " ;
155+ stack.pop_back ();
156+ if (stack.size () != 0 )
157+ upElemStack = stack.back ().rbegin ();
158+ }
159+ upElemStack->second ++;
160+ continue ;
161+ }
162+
163+ }
164+ if (token == " R2" )
165+ {
166+ auto upElemStack = stack.back ().rbegin ();
167+ while (stack.size () != 0 && upElemStack->first != " ÀÝÌ" )
168+ {
169+ fileAnalysis << upElemStack->first << " " ;
170+ stack.pop_back ();
171+ if (stack.size () != 0 )
172+ upElemStack = stack.back ().rbegin ();
173+ }
174+ fileAnalysis << upElemStack->second << upElemStack->first << " " ;
175+ stack.pop_back ();
176+ continue ;
177+ }
178+ auto upElemStack = stack.back ().rbegin ();
179+ bool upElemDownPriop = false ;
180+ while (stack.size () != 0 && getPriority (upElemStack->first )>=getPriority (token))
181+ {
182+ upElemDownPriop = true ;
183+ fileAnalysis << upElemStack->first << " " ;
184+ stack.pop_back ();
185+ if (stack.size () != 0 )
186+ upElemStack = stack.back ().rbegin ();
187+ }
188+ if (upElemDownPriop == true )
189+ {
190+ upElemDownPriop = false ;
191+ std::map<std::string, int > tempMap;
192+ tempMap.insert (std::pair<std::string, int >(token, 0 ));
193+ stack.push_back (tempMap);
194+ continue ;
195+ }
196+ std::map<std::string, int > tempMap;
197+ tempMap.insert (std::pair<std::string, int >(token, 0 ));
198+ stack.push_back (tempMap);
199+
200+ }
201+ }
202+ else
203+ {
204+ fileAnalysis << token << " " ;
205+ }
206+ }
207+
208+ if (stack.size () != 0 )
209+ {
210+ auto upElemStack = stack.back ().rbegin ();
211+ while (stack.size () != 0 )
212+ {
213+ fileAnalysis << upElemStack->first << " " ;
214+ stack.pop_back ();
215+ if (stack.size () != 0 )
216+ upElemStack = stack.back ().rbegin ();
217+
218+
219+ }
220+ }
221+ fileAnalysis << std::endl;
222+ }
223+ }
224+
225+ }
226+ catch (const std::ifstream::failure & exep)
227+ {
228+ std::cout << " Exception opening/reading file" ;
229+ std::cout << exep.what ();
230+ }
231+
232+ lexical.close ();
233+ fileAnalysis.close ();
26234}
235+
0 commit comments