HAL  v4.5.0-254-g8f9f6a423
The Hardware Analyzer - a comprehensive reverse engineering and manipulation framework for gate-level netlists.
parser.h
Go to the documentation of this file.
1 // MIT License
2 //
3 // Copyright (c) 2019 Ruhr University Bochum, Chair for Embedded Security. All Rights reserved.
4 // Copyright (c) 2019 Marc Fyrbiak, Sebastian Wallat, Max Hoffmann ("ORIGINAL AUTHORS"). All rights reserved.
5 // Copyright (c) 2021 Max Planck Institute for Security and Privacy. All Rights reserved.
6 // Copyright (c) 2021 Jörn Langheinrich, Julian Speith, Nils Albartus, René Walendy, Simon Klix ("ORIGINAL AUTHORS"). All Rights reserved.
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in all
16 // copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 // SOFTWARE.
25 
26 #pragma once
27 
29 
30 #include <map>
31 
32 namespace hal
33 {
37  namespace BooleanFunctionParser
38  {
40  enum class ParserType
41  {
43  Liberty,
45  Standard,
48  };
49 
51  enum class TokenType
52  {
53 
54  And,
55  Not,
56  NotSuffix,
57  Or,
58  Xor,
59  Eq,
60  Question,
61  Colon,
62  Add,
63  Sub,
64  Mul,
66  Variable,
67  Constant,
69  BracketOpen,
70  BracketClose,
71  };
72 
74  struct Token
75  {
77  // Member
79 
83  std::tuple<std::string, u16> variable{};
85  std::vector<BooleanFunction::Value> constant{};
86 
88  // Constructors, Destructors, Operators
90 
99  Token(TokenType _type, std::tuple<std::string, u16> variable, std::vector<BooleanFunction::Value> constant);
100 
106  static Token And();
107 
113  static Token Not();
114 
120  static Token NotSuffix();
121 
127  static Token Or();
128 
134  static Token Xor();
135 
141  static Token Eq();
142 
148  static Token Question();
149 
155  static Token Colon();
156 
162  static Token Add();
163 
169  static Token Sub();
170 
176  static Token Mul();
177 
185  static Token Variable(std::string name, u16 size);
186 
193  static Token Constant(std::vector<BooleanFunction::Value> value);
194 
200  static Token BracketOpen();
201 
207  static Token BracketClose();
208 
210  // Interface
212 
219  unsigned precedence(const ParserType& type) const;
220 
228  bool is_left_associative() const;
229 
236  bool is(TokenType type) const;
237 
245  friend std::ostream& operator<<(std::ostream& os, const Token& token);
246 
252  std::string to_string() const;
253  };
254 
263  Result<std::vector<Token>> parse_with_standard_grammar(const std::string& expression, const std::map<std::string, u16>& var_sizes = {});
264 
274  Result<std::vector<Token>> parse_with_liberty_grammar(const std::string& expression, const std::map<std::string, u16>& var_sizes = {});
275 
290  Result<std::vector<Token>> reverse_polish_notation(std::vector<Token>&& tokens, const std::string& expression, const ParserType& parser);
291 
300  Result<BooleanFunction> translate(std::vector<Token>&& tokens, const std::string& expression);
301 
302  } // namespace BooleanFunctionParser
303 } // namespace hal
u32 size
uint16_t u16
Definition: defines.h:40
Result< std::vector< Token > > parse_with_standard_grammar(const std::string &expression, const std::map< std::string, u16 > &var_sizes={})
ParserType
ParserType refers to the parser identifier.
Definition: parser.h:41
@ Liberty
refers to the 'Liberty' Boolean function parser
@ LibertyNoSpace
as 'Liberty' above, but removing space first
@ Standard
refers to the 'Standard' Boolean function parser
Result< BooleanFunction > translate(std::vector< Token > &&tokens, const std::string &expression)
TokenType
TokenType refers to a token identifier for a Boolean function string.
Definition: parser.h:52
Result< std::vector< Token > > reverse_polish_notation(std::vector< Token > &&tokens, const std::string &expression, const ParserType &parser)
Result< std::vector< Token > > parse_with_liberty_grammar(const std::string &expression, const std::map< std::string, u16 > &var_sizes={})
Definition: defines.h:45
std::string name
Token refers to a token identifier and accompanied data.
Definition: parser.h:75
std::tuple< std::string, u16 > variable
optional value and bit-size in case token is a variable
Definition: parser.h:83
TokenType type
refers to the underlying token type identifier
Definition: parser.h:81
static Token Constant(std::vector< BooleanFunction::Value > value)
Token(TokenType _type, std::tuple< std::string, u16 > variable, std::vector< BooleanFunction::Value > constant)
Construct a new initialized Token.
bool is(TokenType type) const
static Token Variable(std::string name, u16 size)
friend std::ostream & operator<<(std::ostream &os, const Token &token)
std::vector< BooleanFunction::Value > constant
optional value in case token is a constant
Definition: parser.h:85
unsigned precedence(const ParserType &type) const