LifeV
Parser Class Reference

Parser - A string parser for algebraic expressions. More...

#include <Parser.hpp>

+ Collaboration diagram for Parser:

Private Attributes

stringsVector_Type M_strings
 
results_Type M_results
 
bool M_evaluate
 
calculator_Type M_calculator
 

Public Types

typedef std::vector< std::string > stringsVector_Type
 Type definition for the vector containing the string segments. More...
 
typedef std::string::const_iterator stringIterator_Type
 Type definition for the iterator over the strings. More...
 
typedef ParserSpiritGrammar< stringIterator_Typecalculator_Type
 Type definition for the parser interpreter. More...
 
typedef calculator_Type::results_Type results_Type
 Type definition for the results. More...
 

Constructors & Destructor

 Parser ()
 Empty constructor (it needs a manual call to setString) More...
 
 Parser (const std::string &string)
 Constructor. More...
 
 Parser (const Parser &parser)
 Copy constructor. More...
 
virtual ~Parser ()
 Destructor. More...
 

Operators

Parseroperator= (const Parser &parser)
 Operator =. More...
 

Methods

const Realevaluate (const ID &id=0)
 Evaluate the expression. More...
 
UInt countSubstring (const std::string &substring) const
 Count how many substrings are present in the string (utility for BCInterfaceFunctionParser) More...
 
void clearVariables ()
 Clear all the variables. More...
 

Set Methods

void setString (const std::string &string, const std::string &stringSeparator=";")
 Set string function. More...
 
void setVariable (const std::string &name, const Real &value)
 Set/replace a variable. More...
 

Get Methods

const Realvariable (const std::string &name)
 Get variable. More...
 

Detailed Description

Parser - A string parser for algebraic expressions.

Author
Cristiano Malossi

Parser is a general interface class for LifeV algebraic parsers. At the present time it works only with boost::spirit::qi.

EXAMPLE - HOW TO USE

The syntax is very simple:

Parser parser;
parser.setString( "-sqrt(4)+1*2" );
Real result = parser.evaluate();

You can change the string at any time:

parser.setString( "c=2; [0., c, c*c, c*c*c]" );
Real result0 = parser.evaluate(0); // 0
Real result1 = parser.evaluate(1); // c
Real result2 = parser.evaluate(2); // c*c
Real result3 = parser.evaluate(3); // c*c*c

See ParserSpiritGrammar class for more details on the expression syntax.

Definition at line 77 of file Parser.hpp.

Member Typedef Documentation

◆ stringsVector_Type

typedef std::vector< std::string > stringsVector_Type

Type definition for the vector containing the string segments.

Definition at line 86 of file Parser.hpp.

◆ stringIterator_Type

typedef std::string::const_iterator stringIterator_Type

Type definition for the iterator over the strings.

Definition at line 90 of file Parser.hpp.

◆ calculator_Type

typedef ParserSpiritGrammar< stringIterator_Type > calculator_Type

Type definition for the parser interpreter.

Definition at line 94 of file Parser.hpp.

◆ results_Type

typedef calculator_Type::results_Type results_Type

Type definition for the results.

Definition at line 98 of file Parser.hpp.

Constructor & Destructor Documentation

◆ Parser() [1/3]

Parser ( )
explicit

Empty constructor (it needs a manual call to setString)

Definition at line 46 of file Parser.cpp.

◆ Parser() [2/3]

Parser ( const std::string &  string)
explicit

Constructor.

Parameters
stringexpression to parse

Definition at line 60 of file Parser.cpp.

◆ Parser() [3/3]

Parser ( const Parser parser)
explicit

Copy constructor.

Parameters
parserParser

Definition at line 75 of file Parser.cpp.

◆ ~Parser()

virtual ~Parser ( )
inlinevirtual

Destructor.

Definition at line 122 of file Parser.hpp.

Member Function Documentation

◆ operator=()

Parser & operator= ( const Parser parser)

Operator =.

Important note: this method is not working right now.

Parameters
parserParser
Returns
reference to a copy of the class

Definition at line 87 of file Parser.cpp.

◆ evaluate()

const Real & evaluate ( const ID id = 0)

Evaluate the expression.

Parameters
idexpression index (starting from 0)
Returns
computed value

Definition at line 107 of file Parser.cpp.

+ Here is the caller graph for this function:

◆ countSubstring()

UInt countSubstring ( const std::string &  substring) const

Count how many substrings are present in the string (utility for BCInterfaceFunctionParser)

Parameters
substringstring to find
Returns
number of substring

Definition at line 141 of file Parser.cpp.

◆ clearVariables()

void clearVariables ( )

Clear all the variables.

Definition at line 163 of file Parser.cpp.

◆ setString()

void setString ( const std::string &  string,
const std::string &  stringSeparator = ";" 
)

Set string function.

Parameters
stringExpression to evaluate
stringSeparatorSeparator identifier (default -> ";")

Definition at line 173 of file Parser.cpp.

◆ setVariable()

void setVariable ( const std::string &  name,
const Real value 
)

Set/replace a variable.

Parameters
namename of the parameter
valuevalue of the parameter

Definition at line 197 of file Parser.cpp.

◆ variable()

const Real & variable ( const std::string &  name)

Get variable.

Parameters
namename of the parameter
Returns
value of the variable

Definition at line 213 of file Parser.cpp.

Field Documentation

◆ M_strings

stringsVector_Type M_strings
private

Definition at line 199 of file Parser.hpp.

◆ M_results

results_Type M_results
private

Definition at line 201 of file Parser.hpp.

◆ M_evaluate

bool M_evaluate
private

Definition at line 203 of file Parser.hpp.

◆ M_calculator

calculator_Type M_calculator
private

Definition at line 205 of file Parser.hpp.


The documentation for this class was generated from the following files: