Static Value-Flow Analysis
Loading...
Searching...
No Matches
Classes | Public Types | Public Member Functions | Static Public Member Functions | Protected Attributes | Static Protected Attributes | Private Attributes | List of all members
SVF::GrammarBase Class Reference

#include <CFGrammar.h>

Inheritance diagram for SVF::GrammarBase:
SVF::CFGrammar

Classes

struct  Symbol
 
class  SymbolHash
 
struct  SymbolVectorHash
 

Public Types

typedef u32_t Kind
 
typedef u32_t Attribute
 
typedef u32_t VariableAttribute
 
typedef struct SVF::GrammarBase::Symbol Symbol
 
template<typename Key , typename Value , typename Hash = SymbolHash, typename KeyEqual = std::equal_to<Key>, typename Allocator = std::allocator<std::pair<const Key, Value>>>
using SymbolMap = std::unordered_map< Key, Value, Hash, KeyEqual, Allocator >
 
template<typename Key , typename Hash = SymbolVectorHash, typename KeyEqual = std::equal_to<Key>, typename Allocator = std::allocator<Key>>
using SymbolSet = std::unordered_set< Key, Hash, KeyEqual, Allocator >
 
typedef std::vector< SymbolProduction
 
typedef SymbolSet< ProductionProductions
 

Public Member Functions

Map< std::string, Kind > & getNonterminals ()
 
void setNonterminals (Map< std::string, Kind > &nonterminals)
 
Map< std::string, Kind > & getTerminals ()
 
void setTerminals (Map< std::string, Kind > &terminals)
 
Map< std::string, Kind > & getEBNFSigns ()
 
void setEBNFSigns (Map< std::string, Kind > &EBNFSigns)
 
SymbolMap< Symbol, Productions > & getRawProductions ()
 
const Map< Kind, Set< Attribute > > & getKindToAttrsMap () const
 
Kind getTotalKind ()
 
Kind getStartKind ()
 
void setStartKind (Kind startKind)
 
void setTotalKind (Kind totalKind)
 
std::string extractKindStrFromSymbolStr (const std::string &symbolStr) const
 
std::string extractAttributeStrFromSymbolStr (const std::string &symbolStr) const
 
void setRawProductions (SymbolMap< Symbol, Productions > &rawProductions)
 
void setKindToAttrsMap (const Map< Kind, Set< Attribute > > &kindToAttrsMap)
 
void setAttributeKinds (const Set< Kind > &attributeKind)
 
Kind strToKind (std::string str) const
 
Symbol strToSymbol (const std::string str) const
 
std::string kindToStr (Kind kind) const
 
std::string symToStrDump (Symbol sym) const
 
Symbol getSymbol (const Production &prod, u32_t pos)
 
const Set< Kind > & getAttrSyms () const
 
Kind insertNonterminalKind (std::string const kindStr)
 Insert kind to nonterminal and return kind.
 
Kind insertTerminalKind (std::string strLit)
 
Symbol insertSymbol (std::string strLit)
 
Symbol insertNonTerminalSymbol (std::string strLit)
 
Symbol insertTerminalSymbol (std::string strLit)
 
Symbol insertEBNFSigns (std::string strLit)
 
void insertAttribute (Kind kind, Attribute a)
 

Static Public Member Functions

static Kind getAttributedKind (Attribute attribute, Kind kind)
 
static Kind getVariabledKind (VariableAttribute variableAttribute, Kind kind)
 

Protected Attributes

Kind startKind
 

Static Protected Attributes

static constexpr unsigned char EdgeKindMaskBits = 8
 We use the lower 8 bits to denote edge kind.
 
static constexpr unsigned char AttributedKindMaskBits = 24
 We use the lower 24 bits to denote attributed kind.
 
static constexpr u64_t EdgeKindMask = (~0ULL) >> (64 - EdgeKindMaskBits)
 

Private Attributes

Map< std::string, Kindnonterminals
 
Map< std::string, Kindterminals
 
Map< std::string, KindEBNFSigns
 
Set< KindattributeKinds
 Map contains Signs' String and associated Symbols.
 
Map< Kind, Set< Attribute > > kindToAttrsMap
 
SymbolMap< Symbol, ProductionsrawProductions
 
u32_t totalKind
 

Detailed Description

Definition at line 36 of file CFGrammar.h.

Member Typedef Documentation

◆ Attribute

Definition at line 40 of file CFGrammar.h.

◆ Kind

Definition at line 39 of file CFGrammar.h.

◆ Production

Definition at line 156 of file CFGrammar.h.

◆ Productions

Definition at line 157 of file CFGrammar.h.

◆ Symbol

◆ SymbolMap

template<typename Key , typename Value , typename Hash = SymbolHash, typename KeyEqual = std::equal_to<Key>, typename Allocator = std::allocator<std::pair<const Key, Value>>>
using SVF::GrammarBase::SymbolMap = std::unordered_map<Key, Value, Hash, KeyEqual, Allocator>

Definition at line 150 of file CFGrammar.h.

◆ SymbolSet

template<typename Key , typename Hash = SymbolVectorHash, typename KeyEqual = std::equal_to<Key>, typename Allocator = std::allocator<Key>>
using SVF::GrammarBase::SymbolSet = std::unordered_set<Key, Hash, KeyEqual, Allocator>

Definition at line 154 of file CFGrammar.h.

◆ VariableAttribute

Definition at line 41 of file CFGrammar.h.

Member Function Documentation

◆ extractAttributeStrFromSymbolStr()

std::string GrammarBase::extractAttributeStrFromSymbolStr ( const std::string &  symbolStr) const

Definition at line 219 of file CFGrammar.cpp.

220{
221 std::string attributeStr;
222 // symbolStr end with '_', the whole symbolStr treat as kind, not with attribute.
223 auto underscorePosition = symbolStr.find_last_of("_", symbolStr.size()-1);
224 if (underscorePosition == std::string::npos)
225 {
226 return "";
227 }
228 return symbolStr.substr(underscorePosition+1);
229}
llvm::IRBuilder IRBuilder
Definition BasicTypes.h:74

◆ extractKindStrFromSymbolStr()

std::string GrammarBase::extractKindStrFromSymbolStr ( const std::string &  symbolStr) const

Definition at line 207 of file CFGrammar.cpp.

208{
209 std::string kindStr;
210 // symbolStr end with '_', the whole symbolStr treat as kind, not with attribute.
211 auto underscorePosition = symbolStr.find_last_of("_", symbolStr.size()-1);
212 if (underscorePosition == std::string::npos)
213 {
214 return symbolStr;
215 }
216 return symbolStr.substr(0, underscorePosition);
217}

◆ getAttributedKind()

static Kind SVF::GrammarBase::getAttributedKind ( Attribute  attribute,
Kind  kind 
)
inlinestatic

Definition at line 265 of file CFGrammar.h.

266 {
267 return ((attribute << EdgeKindMaskBits)| kind );
268 }
static constexpr unsigned char EdgeKindMaskBits
We use the lower 8 bits to denote edge kind.
Definition CFGrammar.h:276

◆ getAttrSyms()

const Set< Kind > & SVF::GrammarBase::getAttrSyms ( ) const
inline

Definition at line 243 of file CFGrammar.h.

244 {
245 return this->attributeKinds;
246 }
Set< Kind > attributeKinds
Map contains Signs' String and associated Symbols.
Definition CFGrammar.h:284

◆ getEBNFSigns()

Map< std::string, Kind > & SVF::GrammarBase::getEBNFSigns ( )
inline

Definition at line 180 of file CFGrammar.h.

181 {
182 return this->EBNFSigns;
183 }
Map< std::string, Kind > EBNFSigns
Definition CFGrammar.h:283

◆ getKindToAttrsMap()

const Map< Kind, Set< Attribute > > & SVF::GrammarBase::getKindToAttrsMap ( ) const
inline

Definition at line 195 of file CFGrammar.h.

196 {
197 return this->kindToAttrsMap;
198 }
Map< Kind, Set< Attribute > > kindToAttrsMap
Definition CFGrammar.h:285

◆ getNonterminals()

Map< std::string, Kind > & SVF::GrammarBase::getNonterminals ( )
inline

Definition at line 160 of file CFGrammar.h.

161 {
162 return this->nonterminals;
163 }
Map< std::string, Kind > nonterminals
Definition CFGrammar.h:281

◆ getRawProductions()

SymbolMap< Symbol, Productions > & SVF::GrammarBase::getRawProductions ( )
inline

Definition at line 190 of file CFGrammar.h.

191 {
192 return this->rawProductions;
193 }
SymbolMap< Symbol, Productions > rawProductions
Definition CFGrammar.h:286

◆ getStartKind()

Kind SVF::GrammarBase::getStartKind ( )
inline

Definition at line 205 of file CFGrammar.h.

206 {
207 return this->startKind;
208 }

◆ getSymbol()

Symbol SVF::GrammarBase::getSymbol ( const Production prod,
u32_t  pos 
)
inline

Definition at line 238 of file CFGrammar.h.

239 {
240 return prod.at(pos);
241 }

◆ getTerminals()

Map< std::string, Kind > & SVF::GrammarBase::getTerminals ( )
inline

Definition at line 170 of file CFGrammar.h.

171 {
172 return this->terminals;
173 }
Map< std::string, Kind > terminals
Definition CFGrammar.h:282

◆ getTotalKind()

Kind SVF::GrammarBase::getTotalKind ( )
inline

Definition at line 200 of file CFGrammar.h.

201 {
202 return this->totalKind;
203 }

◆ getVariabledKind()

static Kind SVF::GrammarBase::getVariabledKind ( VariableAttribute  variableAttribute,
Kind  kind 
)
inlinestatic

Definition at line 270 of file CFGrammar.h.

271 {
272 return ((variableAttribute << AttributedKindMaskBits) | kind);
273 }
static constexpr unsigned char AttributedKindMaskBits
We use the lower 24 bits to denote attributed kind.
Definition CFGrammar.h:277

◆ insertAttribute()

void GrammarBase::insertAttribute ( Kind  kind,
Attribute  a 
)

Definition at line 327 of file CFGrammar.cpp.

328{
329 attributeKinds.insert(kind);
330 if (kindToAttrsMap.find(kind)!= kindToAttrsMap.end())
331 {
332 kindToAttrsMap[kind].insert(attribute);
333 }
334 else
335 {
337 kindToAttrsMap.insert(make_pair(kind, attrs));
338 }
339}

◆ insertEBNFSigns()

GrammarBase::Symbol GrammarBase::insertEBNFSigns ( std::string  strLit)

Definition at line 311 of file CFGrammar.cpp.

312{
313 Symbol sign;
314 if (EBNFSigns.find(symbolStr) == EBNFSigns.end())
315 {
316 sign = totalKind++;
317 EBNFSigns.insert({symbolStr, sign});
318 }
319 else
320 {
322 }
323 return sign;
324
325}
Kind strToKind(std::string str) const
Definition CFGrammar.cpp:55
struct SVF::GrammarBase::Symbol Symbol

◆ insertNonterminalKind()

GrammarBase::Kind GrammarBase::insertNonterminalKind ( std::string const  kindStr)
inline

Insert kind to nonterminal and return kind.

Definition at line 192 of file CFGrammar.cpp.

193{
194 Kind kind;
195 if (nonterminals.find(kindStr) == nonterminals.end())
196 {
197 kind = totalKind++;
198 nonterminals.insert({kindStr, kind});
199 }
200 else
201 {
202 kind = strToKind(kindStr);
203 }
204 return kind;
205}

◆ insertNonTerminalSymbol()

GrammarBase::Symbol GrammarBase::insertNonTerminalSymbol ( std::string  strLit)

Definition at line 249 of file CFGrammar.cpp.

250{
255
256 if ( attributeStr == "") return symbol;
257
258 if ( (attributeStr.size() == 1) && (std::isalpha(attributeStr[attributeStr.size()-1])) )
259 {
260 attributeKinds.insert(symbol.kind);
261 symbol.variableAttribute = (u32_t)attributeStr[attributeStr.size()-1];
262 }
263 else
264 {
265 for( char &c : attributeStr)
266 {
267 if ( std::isdigit(c) == false )
268 {
269 SVFUtil::errs() << SVFUtil::errMsg("\t Symbol Attribute Parse Failure :") << symbolStr
270 << " Attribute:" << attributeStr << " (only number or single alphabet.)";
271 assert(false && "grammar loading failed!");
272 }
273 }
274 attributeKinds.insert(symbol.kind);
275 symbol.attribute = std::stoi(attributeStr);
276 }
277 return symbol;
278}
std::string extractAttributeStrFromSymbolStr(const std::string &symbolStr) const
std::string extractKindStrFromSymbolStr(const std::string &symbolStr) const
Kind insertNonterminalKind(std::string const kindStr)
Insert kind to nonterminal and return kind.
std::string errMsg(const std::string &msg)
Print error message by converting a string into red string output.
Definition SVFUtil.cpp:77
std::ostream & errs()
Overwrite llvm::errs()
Definition SVFUtil.h:56
unsigned u32_t
Definition GeneralType.h:46

◆ insertSymbol()

GrammarBase::Symbol GrammarBase::insertSymbol ( std::string  strLit)

Definition at line 231 of file CFGrammar.cpp.

232{
234 if ((symbolStr.size() == 1) && (!isalpha(symbolStr[0])))
235 {
237 }
238 else if (isupper(symbolStr[0]))
239 {
241 }
242 else
243 {
245 }
246 return symbol;
247}
Symbol insertEBNFSigns(std::string strLit)
Symbol insertNonTerminalSymbol(std::string strLit)
Symbol insertTerminalSymbol(std::string strLit)
int isupper(int c)
Definition extapi.c:881
int isalpha(int character)
Definition extapi.c:836

◆ insertTerminalKind()

GrammarBase::Kind GrammarBase::insertTerminalKind ( std::string  strLit)

From SymbolStr extract kind to inserted in nonterminal symbolStr = <kindStr> [_] [ attributeStr | variableattributeStr ]

Definition at line 177 of file CFGrammar.cpp.

178{
179 Kind kind;
180 if (terminals.find(kindStr) == terminals.end())
181 {
182 kind = totalKind++;
183 terminals.insert({kindStr, kind});
184 }
185 else
186 {
187 kind = strToKind(kindStr);
188 }
189 return kind;
190}

◆ insertTerminalSymbol()

GrammarBase::Symbol GrammarBase::insertTerminalSymbol ( std::string  strLit)

Definition at line 280 of file CFGrammar.cpp.

281{
286
287 if ( attributeStr == "") return symbol;
288
289 if ( (attributeStr.size() == 1) && (std::isalpha(attributeStr[attributeStr.size()-1])) )
290 {
291 attributeKinds.insert(symbol.kind);
292 symbol.variableAttribute = (u32_t)attributeStr[attributeStr.size()-1];
293 }
294 else
295 {
296 for( char &c : attributeStr)
297 {
298 if ( std::isdigit(c) == false )
299 {
300 SVFUtil::errs() << SVFUtil::errMsg("\t Symbol Attribute Parse Failure :") << symbolStr
301 << " Attribute:" << attributeStr << " (only number or single alphabet.)";
302 assert(false && "grammar loading failed!");
303 }
304 }
305 attributeKinds.insert(symbol.kind);
306 symbol.attribute = std::stoi(attributeStr);
307 }
308 return symbol;
309}
Kind insertTerminalKind(std::string strLit)

◆ kindToStr()

std::string GrammarBase::kindToStr ( Kind  kind) const

Definition at line 103 of file CFGrammar.cpp.

104{
105
106 std::string key = "";
107 for (auto &i : terminals)
108 {
109 if (i.second == kind)
110 {
111 key = i.first;
112 return key;
113 }
114 }
115
116 std::string nkey = "";
117 for (auto &ni : nonterminals)
118 {
119 if (ni.second == kind)
120 {
121 nkey = ni.first;
122 return nkey;
123 }
124 }
125
126 std::string signs = "";
127 for (auto &i : EBNFSigns)
128 {
129 if (i.second == kind)
130 {
131 signs = i.first;
132 return signs;
133 }
134 }
135
136 return "";
137}

◆ setAttributeKinds()

void GrammarBase::setAttributeKinds ( const Set< Kind > &  attributeKind)

Definition at line 50 of file CFGrammar.cpp.

51{
53}

◆ setEBNFSigns()

void SVF::GrammarBase::setEBNFSigns ( Map< std::string, Kind > &  EBNFSigns)
inline

Definition at line 185 of file CFGrammar.h.

186 {
187 this->EBNFSigns = EBNFSigns;
188 }

◆ setKindToAttrsMap()

void GrammarBase::setKindToAttrsMap ( const Map< Kind, Set< Attribute > > &  kindToAttrsMap)

Definition at line 45 of file CFGrammar.cpp.

46{
48}

◆ setNonterminals()

void SVF::GrammarBase::setNonterminals ( Map< std::string, Kind > &  nonterminals)
inline

Definition at line 165 of file CFGrammar.h.

166 {
168 }

◆ setRawProductions()

void GrammarBase::setRawProductions ( SymbolMap< Symbol, Productions > &  rawProductions)

Definition at line 40 of file CFGrammar.cpp.

41{
43}

◆ setStartKind()

void SVF::GrammarBase::setStartKind ( Kind  startKind)
inline

Definition at line 210 of file CFGrammar.h.

211 {
212 this->startKind = startKind;
213 }

◆ setTerminals()

void SVF::GrammarBase::setTerminals ( Map< std::string, Kind > &  terminals)
inline

Definition at line 175 of file CFGrammar.h.

176 {
177 this->terminals = terminals;
178 }

◆ setTotalKind()

void SVF::GrammarBase::setTotalKind ( Kind  totalKind)
inline

Definition at line 215 of file CFGrammar.h.

216 {
217 this->totalKind = totalKind;
218 }

◆ strToKind()

GrammarBase::Kind GrammarBase::strToKind ( std::string  str) const

Definition at line 55 of file CFGrammar.cpp.

56{
57
58 auto tit = terminals.find(str);
59 if(tit!=terminals.end())
60 return tit->second;
61
62 auto nit = nonterminals.find(str);
63 if(nit!=nonterminals.end())
64 return nit->second;
65
66 auto sit = EBNFSigns.find(str);
67 if(sit!=EBNFSigns.end())
68 return sit->second;
69
70 assert(false && "kind not found!");
71 abort();
72}

◆ strToSymbol()

GrammarBase::Symbol GrammarBase::strToSymbol ( const std::string  str) const

Definition at line 74 of file CFGrammar.cpp.

75{
79 symbol.kind = strToKind(kindStr);
80
81 if ( attributeStr == "") return symbol;
82
83 if ( (attributeStr.size() == 1) && (std::isalpha(attributeStr[attributeStr.size()-1])) )
84 {
85 symbol.variableAttribute = (u32_t)attributeStr[attributeStr.size()-1];
86 }
87 else
88 {
89 for( char &c : attributeStr)
90 {
91 if ( std::isdigit(c) == false )
92 {
93 SVFUtil::errs() << SVFUtil::errMsg("\t Symbol Attribute Parse Failure :") << str
94 << " Attribute:" << attributeStr << " (only number or single alphabet.)";
95 assert(false && "grammar loading failed!");
96 }
97 }
98 symbol.attribute = std::stoi(attributeStr);
99 }
100 return symbol;
101}

◆ symToStrDump()

std::string GrammarBase::symToStrDump ( Symbol  sym) const

Definition at line 139 of file CFGrammar.cpp.

140{
141 Kind kind = sym.kind;
142 Attribute attribute = sym.attribute;
143
144 std::string key = "";
145 for (auto &i : terminals)
146 {
147 if (i.second == kind)
148 {
149 key = i.first;
150 if(attribute != 0)
151 {
152 key.append("_");
153 key.append(std::to_string(attribute));
154 }
155 return key;
156 }
157 }
158
159 std::string nkey = "";
160 for (auto &ni : nonterminals)
161 {
162 if (ni.second == kind)
163 {
164 nkey = ni.first;
165 if(attribute != 0)
166 {
167 nkey.append("_");
168 nkey.append(std::to_string(attribute));
169 }
170 return nkey;
171 }
172 }
173
174 return "";
175}

Member Data Documentation

◆ AttributedKindMaskBits

constexpr unsigned char SVF::GrammarBase::AttributedKindMaskBits = 24
staticconstexprprotected

We use the lower 24 bits to denote attributed kind.

Definition at line 277 of file CFGrammar.h.

◆ attributeKinds

Set<Kind> SVF::GrammarBase::attributeKinds
private

Map contains Signs' String and associated Symbols.

Definition at line 284 of file CFGrammar.h.

◆ EBNFSigns

Map<std::string, Kind> SVF::GrammarBase::EBNFSigns
private

Definition at line 283 of file CFGrammar.h.

◆ EdgeKindMask

constexpr u64_t SVF::GrammarBase::EdgeKindMask = (~0ULL) >> (64 - EdgeKindMaskBits)
staticconstexprprotected

Definition at line 278 of file CFGrammar.h.

◆ EdgeKindMaskBits

constexpr unsigned char SVF::GrammarBase::EdgeKindMaskBits = 8
staticconstexprprotected

We use the lower 8 bits to denote edge kind.

Definition at line 276 of file CFGrammar.h.

◆ kindToAttrsMap

Map<Kind, Set<Attribute> > SVF::GrammarBase::kindToAttrsMap
private

Definition at line 285 of file CFGrammar.h.

◆ nonterminals

Map<std::string, Kind> SVF::GrammarBase::nonterminals
private

Definition at line 281 of file CFGrammar.h.

◆ rawProductions

SymbolMap<Symbol, Productions> SVF::GrammarBase::rawProductions
private

Definition at line 286 of file CFGrammar.h.

◆ startKind

Kind SVF::GrammarBase::startKind
protected

Definition at line 279 of file CFGrammar.h.

◆ terminals

Map<std::string, Kind> SVF::GrammarBase::terminals
private

Definition at line 282 of file CFGrammar.h.

◆ totalKind

u32_t SVF::GrammarBase::totalKind
private

Definition at line 287 of file CFGrammar.h.


The documentation for this class was generated from the following files: