HAL  v4.5.0-83-g30c8f0afc
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 namespace hal
31 {
35  namespace BooleanFunctionParser
36  {
38  enum class ParserType
39  {
41  Liberty,
43  Standard,
46  };
47 
49  enum class TokenType
50  {
51 
52  And,
53  Not,
54  NotSuffix,
55  Or,
56  Xor,
58  Variable,
59  Constant,
61  BracketOpen,
62  BracketClose,
63  };
64 
66  struct Token
67  {
69  // Member
71 
75  std::tuple<std::string, u16> variable{};
77  std::vector<BooleanFunction::Value> constant{};
78 
80  // Constructors, Destructors, Operators
82 
91  Token(TokenType _type, std::tuple<std::string, u16> variable, std::vector<BooleanFunction::Value> constant);
92 
98  static Token And();
99 
105  static Token Not();
106 
112  static Token NotSuffix();
113 
119  static Token Or();
120 
126  static Token Xor();
127 
135  static Token Variable(std::string name, u16 size);
136 
143  static Token Constant(std::vector<BooleanFunction::Value> value);
144 
150  static Token BracketOpen();
151 
157  static Token BracketClose();
158 
160  // Interface
162 
169  unsigned precedence(const ParserType& type) const;
170 
177  bool is(TokenType type) const;
178 
186  friend std::ostream& operator<<(std::ostream& os, const Token& token);
187 
193  std::string to_string() const;
194  };
195 
202  Result<std::vector<Token>> parse_with_standard_grammar(const std::string& expression);
203 
211  Result<std::vector<Token>> parse_with_liberty_grammar(const std::string& expression);
212 
227  Result<std::vector<Token>> reverse_polish_notation(std::vector<Token>&& tokens, const std::string& expression, const ParserType& parser);
228 
237  Result<BooleanFunction> translate(std::vector<Token>&& tokens, const std::string& expression);
238 
239  } // namespace BooleanFunctionParser
240 } // namespace hal
u32 size
uint16_t u16
Definition: defines.h:40
Result< std::vector< Token > > parse_with_standard_grammar(const std::string &expression)
Result< std::vector< Token > > parse_with_liberty_grammar(const std::string &expression)
ParserType
ParserType refers to the parser identifier.
Definition: parser.h:39
@ 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:50
Result< std::vector< Token > > reverse_polish_notation(std::vector< Token > &&tokens, const std::string &expression, const ParserType &parser)
Definition: defines.h:45
std::string name
Token refers to a token identifier and accompanied data.
Definition: parser.h:67
std::tuple< std::string, u16 > variable
optional value and bit-size in case token is a variable
Definition: parser.h:75
TokenType type
refers to the underlying token type identifier
Definition: parser.h:73
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:77
unsigned precedence(const ParserType &type) const