1+ #include " SyntaxAnalisator.h"
2+ #include " function.h"
3+
4+
5+ SyntaxAnalisator::SyntaxAnalisator ()
6+ {
7+ }
8+
9+
10+ SyntaxAnalisator::~SyntaxAnalisator ()
11+ {
12+ }
13+ string SyntaxAnalisator::getServiceWordCode (string str)
14+ {
15+ for (int i = 0 ; i < SIZE_serviceWord; i++)
16+ if (serviceWord[i][0 ] == str)
17+ return serviceWord[i][1 ];
18+ return " \0 " ;
19+ }
20+
21+ string SyntaxAnalisator::getOperationsCode (string str)
22+ {
23+ for (int i = 0 ; i < SIZE_operation; i++)
24+ if (operations[i][0 ] == str)
25+ return operations[i][1 ];
26+ return " \0 " ;
27+ }
28+
29+ string SyntaxAnalisator::getSeparatorsCode (string str)
30+ {
31+ for (int i = 0 ; i < SIZE_separators; i++)
32+ if (separators[i][0 ] == str)
33+ return separators[i][1 ];
34+ return " \0 " ;
35+ }
36+
37+ string SyntaxAnalisator::getIdentifierCode (string str)
38+ {
39+ for (const auto & word : identifier)
40+ if (word.first == str)
41+ return word.second ;
42+ return " \0 " ;
43+ }
44+
45+ string SyntaxAnalisator::getNumberConstCode (string str)
46+ {
47+ for (const auto & word : numberConst)
48+ if (word.first == str)
49+ return word.second ;
50+ return " \0 " ;
51+ }
52+
53+ string SyntaxAnalisator::getSymbolsConstCode (string str)
54+ {
55+ for (const auto & word : symbolsConst)
56+ if (word.first == str)
57+ return word.second ;
58+ return " \0 " ;
59+ }
60+
61+ void SyntaxAnalisator::addCode (string str, map<string, string> & table, int numTable)
62+ {
63+ int indexCode = 0 ;
64+ for (const auto & word : table)
65+ {
66+ indexCode++;
67+ }
68+ indexCode++;
69+ if (numTable == 1 )
70+ table.insert (pair<string, string>(str, " I" + to_string (indexCode)));
71+ if (numTable == 2 )
72+ table.insert (pair<string, string>(str, " N" + to_string (indexCode)));
73+ if (numTable == 3 )
74+ table.insert (pair<string, string>(str, " C" + to_string (indexCode)));
75+ }
76+
77+ int SyntaxAnalisator::checkStringSingleElem (string const & word)
78+ {
79+ if (isDigit ((int )word[0 ]) == true )
80+ return 1 ;
81+ if (isOperation ((int )word[0 ]) == true || isLogicalSingleOperation ((int )word[0 ]) == true )
82+ return 2 ;
83+ if (isSeparators ((int )word[0 ]) == true )
84+ return 3 ;
85+ if (isLetter ((int )word[0 ]) == true )
86+ return 4 ;
87+ return 0 ;
88+ }
89+
90+ string SyntaxAnalisator::getCodeWordLength_1 (string word)
91+ {
92+ switch (checkStringSingleElem (word))
93+ {
94+ case 1 :
95+ if (getNumberConstCode (word) == " \0 " )
96+ addCode (word, numberConst, 2 );
97+ return getNumberConstCode (word);
98+ case 2 :
99+ return getOperationsCode (word);
100+ case 3 :
101+ return getSeparatorsCode (word);
102+ case 4 :
103+ if (getIdentifierCode (word) == " \0 " )
104+ addCode (word, identifier, 1 );
105+ return getIdentifierCode (word);
106+ default :
107+ return " " ;
108+ }
109+ /*
110+ string code = getOperationsCode(word);
111+ if (code == "\0")
112+ code = getSeparatorsCode(word);
113+ if (code=="\0")
114+ {
115+ if (isDigit((int)word[0]) == true)
116+ {
117+ if (getNumberConstCode(word) == "\0")
118+ addCode(word, numberConst, 2);
119+ return getNumberConstCode(word);
120+ }
121+ if (isLetter((int)word[0]) == true)
122+ {
123+ if(getIdentifierCode(word) =="\0")
124+ addCode(word,identifier,1);
125+ return getIdentifierCode(word);
126+ }
127+ }
128+ else
129+ return code;*/
130+ }
131+
132+
133+ string SyntaxAnalisator::getCodeWordLengthGreaterOne (string word)
134+ {
135+ string code = getServiceWordCode (word);
136+ if (code == " \0 " )
137+ code = getOperationsCode (word);
138+ if (code == " \0 " )
139+ {
140+ if (isNumber (word) == true )
141+ {
142+ if (getNumberConstCode (word) == " \0 " )
143+ addCode (word, numberConst, 2 );
144+ return getNumberConstCode (word);
145+ }
146+ else
147+ {
148+ if ((int )word[0 ] == 34 )// \"
149+ {
150+ if (isLibrary_header (word) == false )
151+ {
152+ if (getSymbolsConstCode (word) == " \0 " )
153+ addCode (word, symbolsConst, 3 );
154+ return getSymbolsConstCode (word);
155+ }
156+ }
157+ if (getIdentifierCode (word) == " \0 " )
158+ addCode (word, identifier, 1 );
159+ return getIdentifierCode (word);
160+ }
161+ }
162+ else
163+ return code;
164+ }
165+
166+ string SyntaxAnalisator::getCodeWord (string word)
167+ {
168+ if (word.length () == 1 )
169+ return getCodeWordLength_1 (word);
170+ else
171+ return getCodeWordLengthGreaterOne (word);
172+ }
0 commit comments