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

#include <GrammarBuilder.h>

Public Member Functions

 GrammarBuilder (std::string fileName)
 
GrammarBasebuild () const
 Build grammarBase from fileName.
 
GrammarBasebuild (Map< std::string, SVF::GrammarBase::Symbol > &preMap) const
 Build grammarBase from fileName with preset str2KindMap.
 

Private Member Functions

const std::string parseProductionsString () const
 Parse start symbol and production from file string.
 
const std::vector< std::string > loadWordProductions () const
 Parse whole production string to production vector.
 
const std::string stripSpace (std::string s) const
 Strip front and tail space.
 

Private Attributes

std::string fileName
 
GrammarBasegrammar
 

Detailed Description

Build Grammar from a user specified grammar text

Symbol Format: <kind> [bar] [ _alpha | _number ] kind: any nonspace string start with alphabet, epsilon stand for empty string bar: stand for reverse edge alpha: any single alpha number: any number start with capital: nonterminal start with noncapital: terminal

Production Format: <symbol> -> <symbol> *; LHS and RHS, Separate by '->', symbol separate by ' ', end by ';' support '*', '?', '(', ')'

Input Format: Start: M // Specify Start Symbol in Second Line Terminal: Addr Copy Store Load Gep Vgep // Specify the order of terminal Addr->0, Copy->1 .. Productions: // Each Symbol separate by 'Space', production end with ';' M -> V d; // Terminal in NonCapital M -> dbar V d; // NonTerminal in Capital V -> M abar M a M; // LHS and RHS, Separate by '->' V -> ( M ? abar ) * M ? ( a M ? ) *; // Support '(' ')' '?' '*' four regular expression sign Gep_j -> Gep_i F vgep; // Support variable attribute with variable attribute Gep_1 -> Gep_2; // Support fix number attribute

Definition at line 70 of file GrammarBuilder.h.

Constructor & Destructor Documentation

◆ GrammarBuilder()

SVF::GrammarBuilder::GrammarBuilder ( std::string  fileName)
inline

Definition at line 86 of file GrammarBuilder.h.

86 : fileName(fileName), grammar(nullptr)
87 {
88 grammar = new GrammarBase();
89 };
GrammarBase * grammar

Member Function Documentation

◆ build() [1/2]

GrammarBase * SVF::GrammarBuilder::build ( ) const

Build grammarBase from fileName.

Build grammarbase from textfile.

Definition at line 140 of file GrammarBuilder.cpp.

141{
142 std::string delimiter = " ";
143 std::string delimiter1 = "->";
144 std::string word = "";
145 size_t pos;
147 std::vector<std::string> wordProdVec = loadWordProductions();
148
149 for (auto wordProd : wordProdVec)
150 {
151 // Find the position of the '->' delimiter
152 if ((pos = wordProd.find(delimiter1)) != std::string::npos)
153 {
154 // Extract and strip RHS (right-hand side) and LHS (left-hand side)
155 std::string RHS = stripSpace(wordProd.substr(0, pos));
156 std::string LHS = stripSpace(wordProd.substr(pos + delimiter1.size()));
157
158 // Insert RHS symbol into grammar
159 GrammarBase::Symbol RHSSymbol = grammar->insertSymbol(RHS);
160 prod.push_back(RHSSymbol);
161
162 // Ensure RHS symbol exists in raw productions
164 {
165 grammar->getRawProductions().insert({RHSSymbol, {}});
166 }
167
168 // Parse LHS string into symbols
169 while ((pos = LHS.find(delimiter)) != std::string::npos)
170 {
171 // Extract each word before the space
172 word = stripSpace(LHS.substr(0, pos));
173 LHS.erase(0, pos + delimiter.length());
174
175 // Insert symbol into production
176 prod.push_back(grammar->insertSymbol(word));
177 }
178
179 // Insert the remaining word (if any) into the production
180 if (!LHS.empty())
181 {
182 prod.push_back(grammar->insertSymbol(stripSpace(LHS)));
183 }
184
185 // Add the production to raw productions
187
188 // Clear the production for the next iteration
189 prod = {};
190 }
191 }
192
193 return grammar;
194}
Symbol insertSymbol(std::string strLit)
SymbolMap< Symbol, Productions > & getRawProductions()
Definition CFGrammar.h:190
std::vector< Symbol > Production
Definition CFGrammar.h:156
const std::string stripSpace(std::string s) const
Strip front and tail space.
const std::vector< std::string > loadWordProductions() const
Parse whole production string to production vector.
llvm::IRBuilder IRBuilder
Definition BasicTypes.h:74

◆ build() [2/2]

GrammarBase * SVF::GrammarBuilder::build ( Map< std::string, SVF::GrammarBase::Symbol > &  preMap) const

Build grammarBase from fileName with preset str2KindMap.

◆ loadWordProductions()

const std::vector< std::string > SVF::GrammarBuilder::loadWordProductions ( ) const
inlineprivate

Parse whole production string to production vector.

Definition at line 109 of file GrammarBuilder.cpp.

110{
111 size_t pos = 0;
112 std::string lines = parseProductionsString();
113 std::string word = "";
114 std::vector<std::string> wordProds;
115 std::string delimiter = ";";
116 while ((pos = lines.find(";")) != std::string::npos)
117 {
118 word = lines.substr(0, pos);
119 wordProds.push_back(word);
120 lines.erase(0, pos + delimiter.length());
121 }
122 return wordProds;
123}
const std::string parseProductionsString() const
Parse start symbol and production from file string.

◆ parseProductionsString()

const std::string SVF::GrammarBuilder::parseProductionsString ( ) const
inlineprivate

Parse start symbol and production from file string.

Definition at line 38 of file GrammarBuilder.cpp.

39{
40 std::ifstream textFile(fileName);
41 if (!textFile.is_open())
42 {
43 std::cerr << "Can't open CFL grammar file `" << fileName << "`" << std::endl;
44 abort();
45 }
46
47 std::string lineString;
48 std::string lines = "";
49 std::string startString;
50 std::string symbolString;
51 const std::string WHITESPACE = " \n\r\t\f\v";
52 int lineNum = 0;
53
55 {
56 if (lineNum == 1)
57 {
59 }
60 else if (lineNum == 3)
61 {
62 // Trim leading and trailing whitespace
63 size_t start = lineString.find_first_not_of(WHITESPACE);
64 size_t end = lineString.find_last_not_of(WHITESPACE);
65 if (start != std::string::npos && end != std::string::npos)
66 {
67 symbolString = lineString.substr(start, end - start + 1);
68 }
69 }
70
71 // Append line to `lines` with whitespace trimmed
72 size_t start = lineString.find_first_not_of(WHITESPACE);
73 size_t end = lineString.find_last_not_of(WHITESPACE);
74 if (start != std::string::npos && end != std::string::npos)
75 {
76 lines.append(lineString.substr(start, end - start + 1));
77 }
78
79 lineNum++;
80 }
81
82 // Extract "Productions:" part from `lines` manually
83 size_t productionsPos = lines.find("Productions:");
84 if (productionsPos != std::string::npos)
85 {
86 lines = lines.substr(productionsPos + std::string("Productions:").length());
87 }
88
89 // Parse `symbolString` to insert symbols
90 std::string sString;
91 size_t pos = 0;
92 while ((pos = symbolString.find(" ")) != std::string::npos)
93 {
94 sString = stripSpace(symbolString.substr(0, pos));
95 symbolString.erase(0, pos + 1); // Remove the processed part
97 }
98 // Insert the remaining symbol
100
101 // Set the start kind and add the epsilon terminal
103 grammar->insertTerminalKind("epsilon");
104
105 return lines;
106}
char const int length
Definition cJSON.h:163
void setStartKind(Kind startKind)
Definition CFGrammar.h:210
Kind insertTerminalKind(std::string strLit)

◆ stripSpace()

const std::string SVF::GrammarBuilder::stripSpace ( std::string  s) const
inlineprivate

Strip front and tail space.

Definition at line 125 of file GrammarBuilder.cpp.

126{
127 // Remove leading spaces
128 size_t start = s.find_first_not_of(" ");
129 if (start == std::string::npos)
130 {
131 return ""; // Return an empty string if no non-space character is found
132 }
133
134 // Remove trailing spaces
135 size_t end = s.find_last_not_of(" ");
136 return s.substr(start, end - start + 1);
137}

Member Data Documentation

◆ fileName

std::string SVF::GrammarBuilder::fileName
private

Definition at line 73 of file GrammarBuilder.h.

◆ grammar

GrammarBase* SVF::GrammarBuilder::grammar
private

Definition at line 74 of file GrammarBuilder.h.


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