HAL
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 {
32  namespace BooleanFunctionParser
33  {
35  enum class ParserType
36  {
38  Liberty,
40  Standard,
41  };
42 
44  enum class TokenType
45  {
46 
47  And,
48  Not,
49  NotSuffix,
50  Or,
51  Xor,
53  Variable,
54  Constant,
56  BracketOpen,
57  BracketClose,
58  };
59 
61  struct Token
62  {
64  // Member
66 
70  std::tuple<std::string, u16> variable{};
72  std::vector<BooleanFunction::Value> constant{};
73 
75  // Constructors, Destructors, Operators
77 
86  Token(TokenType _type, std::tuple<std::string, u16> variable, std::vector<BooleanFunction::Value> constant);
87 
93  static Token And();
94 
100  static Token Not();
101 
107  static Token NotSuffix();
108 
114  static Token Or();
115 
121  static Token Xor();
122 
130  static Token Variable(std::string name, u16 size);
131 
138  static Token Constant(std::vector<BooleanFunction::Value> value);
139 
145  static Token BracketOpen();
146 
152  static Token BracketClose();
153 
155  // Interface
157 
164  unsigned precedence(const ParserType& type) const;
165 
172  bool is(TokenType type) const;
173 
181  friend std::ostream& operator<<(std::ostream& os, const Token& token);
182 
188  std::string to_string() const;
189  };
190 
197  Result<std::vector<Token>> parse_with_standard_grammar(const std::string& expression);
198 
206  Result<std::vector<Token>> parse_with_liberty_grammar(const std::string& expression);
207 
222  Result<std::vector<Token>> reverse_polish_notation(std::vector<Token>&& tokens, const std::string& expression, const ParserType& parser);
223 
232  Result<BooleanFunction> translate(std::vector<Token>&& tokens, const std::string& expression);
233 
234  } // namespace BooleanFunctionParser
235 } // namespace hal
uint16_t u16
Definition: defines.h:40
parser
Definition: control.py:13
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:36
@ Liberty
refers to the 'Liberty' Boolean function parser
@ 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:45
Result< std::vector< Token > > reverse_polish_notation(std::vector< Token > &&tokens, const std::string &expression, const ParserType &parser)
std::string name
Token refers to a token identifier and accompanied data.
Definition: parser.h:62
std::tuple< std::string, u16 > variable
optional value and bit-size in case token is a variable
Definition: parser.h:70
TokenType type
refers to the underlying token type identifier
Definition: parser.h:68
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:72
unsigned precedence(const ParserType &type) const