HAL  v4.5.0-254-g8f9f6a423
The Hardware Analyzer - a comprehensive reverse engineering and manipulation framework for gate-level netlists.
hal::BooleanFunctionParser Namespace Reference

Classes

struct  Token
 Token refers to a token identifier and accompanied data. More...
 

Enumerations

enum class  ParserType { Liberty , Standard , LibertyNoSpace }
 ParserType refers to the parser identifier. More...
 
enum class  TokenType {
  And , Not , NotSuffix , Or ,
  Xor , Eq , Question , Colon ,
  Add , Sub , Mul , Variable ,
  Constant , BracketOpen , BracketClose
}
 TokenType refers to a token identifier for a Boolean function string. More...
 

Functions

Result< std::vector< Token > > parse_with_standard_grammar (const std::string &expression, const std::map< std::string, u16 > &var_sizes={})
 
Result< std::vector< Token > > parse_with_liberty_grammar (const std::string &expression, const std::map< std::string, u16 > &var_sizes={})
 
Result< std::vector< Token > > reverse_polish_notation (std::vector< Token > &&tokens, const std::string &expression, const ParserType &parser)
 
Result< BooleanFunction > translate (std::vector< Token > &&tokens, const std::string &expression)
 
std::ostream & operator<< (std::ostream &os, const Token &token)
 

Detailed Description

Contains the tokenizer and the parsers that translate a string into a Boolean function.

Enumeration Type Documentation

◆ ParserType

ParserType refers to the parser identifier.

Enumerator
Liberty 

refers to the 'Liberty' Boolean function parser

Standard 

refers to the 'Standard' Boolean function parser

LibertyNoSpace 

as 'Liberty' above, but removing space first

Definition at line 40 of file parser.h.

◆ TokenType

TokenType refers to a token identifier for a Boolean function string.

Enumerator
And 

Boolean And operation (e.g., "&").

Not 

Boolean Not operation (e.g., "!&").

NotSuffix 
Or 

<Boolean Not operation as a expresion suffix (e.g., "'" for liberty grammar). Boolean Or operation (e.g., "|").

Xor 

Boolean Xor operation (e.g., "^").

Eq 

Equality comparison operation (e.g., "==").

Question 

Ternary ? (start of conditional expression).

Colon 

Ternary : (separator between then and else branches).

Add 

Arithmetic addition operation (e.g., "+").

Sub 

Arithmetic subtraction operation (e.g., "-").

Mul 

Arithmetic multiplication operation (e.g., "*").

Variable 

Boolean variable (e.g., "i0").

Constant 

Boolean constant (e.g., "0" or "1").

BracketOpen 

An opening bracket (e.g., "(").

BracketClose 

A closing bracket (e.g., "(").

Definition at line 51 of file parser.h.

Function Documentation

◆ operator<<()

std::ostream& hal::BooleanFunctionParser::operator<< ( std::ostream &  os,
const Token &  token 
)

Outputs a token to the given output stream.

Parameters
[in]os- The output stream.
[in]token- The token to be output.
Returns
The output stream.

Definition at line 133 of file parser_types.cpp.

◆ parse_with_liberty_grammar()

Result< std::vector< Token > > hal::BooleanFunctionParser::parse_with_liberty_grammar ( const std::string &  expression,
const std::map< std::string, u16 > &  var_sizes = {} 
)

Parses a Boolean function from a string representation into its tokens based on the data format defined for Liberty, see Liberty user guide.

Parameters
[in]expression- Boolean function string.
[in]var_sizes- Optional map from variable name to bit-width; variables not in the map default to 1 bit.
Returns
Ok() and the list of tokens on success, Err() otherwise.

Definition at line 13 of file parser_liberty.cpp.

References hal::BooleanFunctionParser::Token::And(), hal::BooleanFunctionParser::Token::BracketClose(), hal::BooleanFunctionParser::Token::BracketOpen(), hal::BooleanFunctionParser::Token::Constant(), ERR, name, hal::BooleanFunctionParser::Token::Not(), hal::BooleanFunctionParser::Token::NotSuffix(), OK, hal::BooleanFunctionParser::Token::Or(), hal::BooleanFunctionParser::Token::Variable(), and hal::BooleanFunctionParser::Token::Xor().

Referenced by hal::BooleanFunction::from_string().

◆ parse_with_standard_grammar()

Result< std::vector< Token > > hal::BooleanFunctionParser::parse_with_standard_grammar ( const std::string &  expression,
const std::map< std::string, u16 > &  var_sizes = {} 
)

◆ reverse_polish_notation()

Result< std::vector< Token > > hal::BooleanFunctionParser::reverse_polish_notation ( std::vector< Token > &&  tokens,
const std::string &  expression,
const ParserType &  parser 
)

Transforms a list of tokens in infix notation (e.g., "A & B") into the reverse polish notation notation (e.g., "A B &") that allows for an easy translation into the AST data structure.

In order to translate between the different notations, we leverage the well-known Shunting Yard algorithm, see wikipedia for an example: https://en.wikipedia.org/wiki/Shunting-yard_algorithm

Parameters
[in]tokens- List of tokens in infix notation.
[in]expression- Expression string.
[in]parser- Parser identifier
Returns
Ok() and list of tokens in reverse-polish notation on success, Err() otherwise.

Definition at line 186 of file parser_types.cpp.

References Add, And, BracketClose, BracketOpen, Colon, Constant, Eq, ERR, Mul, Not, hal::BooleanFunctionParser::Token::Not(), NotSuffix, OK, Or, hal::output, Question, Sub, Variable, and Xor.

Referenced by hal::BooleanFunction::from_string().

◆ translate()

Result< BooleanFunction > hal::BooleanFunctionParser::translate ( std::vector< Token > &&  tokens,
const std::string &  expression 
)