Static Value-Flow Analysis
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. More...
 
GrammarBasebuild (Map< std::string, SVF::GrammarBase::Symbol > &preMap) const
 Build grammarBase from fileName with preset str2KindMap. More...
 

Private Member Functions

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

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
std::string fileName

Member Function Documentation

◆ build() [1/2]

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

Build grammarBase from fileName.

build grammarbase from textfile

Definition at line 115 of file GrammarBuilder.cpp.

116 {
117  std::smatch matches;
118  std::string delimiter = " ";
119  std::string delimiter1 = "->";
120  std::string word = "";
121  size_t pos;
123  std::vector<std::string> wordProdVec = loadWordProductions();
124 
125  for (auto wordProd : wordProdVec)
126  {
127  if ((pos = wordProd.find(delimiter1)) != std::string::npos)
128  {
129  std::string RHS = stripSpace(wordProd.substr(0, pos));
130  std::string LHS = wordProd.substr(pos + delimiter1.size(), wordProd.size() - 1);
131  GrammarBase::Symbol RHSSymbol = grammar->insertSymbol(RHS);
132  prod.push_back(RHSSymbol);
133  if (grammar->getRawProductions().find(RHSSymbol) == grammar->getRawProductions().end()) grammar->getRawProductions().insert({RHSSymbol, {}});
134  std::regex LHSRegEx("\\s*(.*)");
135  std::regex_search(LHS, matches, LHSRegEx);
136  LHS = matches.str(1);
137  while ((pos = LHS.find(delimiter)) != std::string::npos)
138  {
139  word = LHS.substr(0, pos);
140  LHS.erase(0, pos + delimiter.length()); //Capital is Nonterminal, Otherwise is terminal
141  prod.push_back(grammar->insertSymbol(word));
142  }
143  prod.push_back(grammar->insertSymbol(LHS));
144  grammar->getRawProductions().at(RHSSymbol).insert(prod);
145  prod = {};
146  }
147  }
148 
149  return grammar;
150 };
const char *const string
Definition: cJSON.h:172
Symbol insertSymbol(std::string strLit)
Definition: CFGrammar.cpp:231
SymbolMap< Symbol, Productions > & getRawProductions()
Definition: CFGrammar.h:190
struct SVF::GrammarBase::Symbol Symbol
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.

◆ 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 89 of file GrammarBuilder.cpp.

90 {
91  size_t pos = 0;
93  std::string word = "";
94  std::vector<std::string> wordProds;
95  std::string delimiter = ";";
96  while ((pos = lines.find(";")) != std::string::npos)
97  {
98  word = lines.substr(0, pos);
99  wordProds.push_back(word);
100  lines.erase(0, pos + delimiter.length());
101  }
102  return wordProds;
103 }
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 39 of file GrammarBuilder.cpp.

40 {
41  std::ifstream textFile(fileName);
42  if (!textFile.is_open())
43  {
44  std::cerr << "Can't open CFL grammar file `" << fileName << "`" << std::endl;
45  abort();
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  while (getline(textFile, lineString))
54  {
55  if(lineNum == 1)
56  {
57  startString = stripSpace(lineString);
58  }
59  if(lineNum == 3)
60  {
61  symbolString = lineString.substr(lineString.find_first_not_of(WHITESPACE), lineString.find_last_not_of(WHITESPACE)+1);
62  }
63 
64  lines.append(lineString.substr(lineString.find_first_not_of(WHITESPACE), lineString.find_last_not_of(WHITESPACE)+1));
65  lineNum++;
66  }
67 
68  std::regex reg("Start:([\\s\\S]*)Terminal:(.*)Productions:([\\s\\S]*)");
69  std::smatch matches;
70  if (std::regex_search(lines, matches, reg))
71  {
72  lines = matches.str(3);
73  }
74  std::string sString;
75  size_t pos = 0;
76  while ((pos = symbolString.find(" ")) != std::string::npos)
77  {
78  sString = stripSpace(symbolString.substr(0, pos));
79  symbolString.erase(0, pos + 1); //Capital is Nonterminal, Otherwise is terminal
80  grammar->insertSymbol(sString);
81  }
82  grammar->insertSymbol(symbolString);
83  grammar->setStartKind(grammar->insertSymbol(startString));
84  grammar->insertTerminalKind("epsilon");
85 
86  return lines;
87 }
void setStartKind(Kind startKind)
Definition: CFGrammar.h:210
Kind insertTerminalKind(std::string strLit)
Definition: CFGrammar.cpp:177

◆ stripSpace()

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

Strip front and tail space.

Definition at line 105 of file GrammarBuilder.cpp.

106 {
107  std::smatch matches;
108  std::regex stripReg("\\s*(\\S*)\\s*");
109  std::regex_search(s, matches, stripReg);
110  return matches.str(1);
111 }

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: