HIPO  4.3.0
High Performance Output data format for experimental physics
parser.h
Go to the documentation of this file.
1 
4 #ifndef PARSER_H
5 #define PARSER_H
6 
7 #ifdef WIN32
8 
9 // disable warnings about long names
10  #pragma warning( disable : 4786)
11 
12 #endif
13 
14 
15 #include <string>
16 #include <map>
17 #include <stdexcept>
18 #include <sstream>
19 
20 #include <cmath>
21 #include <ctime>
22 #include <cstdlib>
23 
24 namespace hipo {
25 
26 
28 
42 class Parser
43  {
44 
45  public:
46 
53  enum TokenType
54  {
55  NONE,
56  NAME,
58  END,
59  PLUS='+',
60  MINUS='-',
61  MULTIPLY='*',
62  DIVIDE='/',
63  ASSIGN='=',
64  LHPAREN='(',
65  RHPAREN=')',
66  COMMA=',',
67  NOT='!',
68 
69  LT='<',
70  GT='>',
71  LE,
72  GE,
73  EQ,
74  NE,
75  AND,
76  OR,
77 
81  ASSIGN_DIV
82 
83  };
84 
85  private:
86 
87  std::string program_;
88 
89  const char * pWord_;
90  const char * pWordStart_;
91  // last token parsed
92  TokenType type_;
93  std::string word_;
94  double value_;
95 
96  public:
97 
100  Parser (const std::string & program)
101  : program_ (program)
102  {
103  // insert pre-defined names:
104  symbols_ ["pi"] = 3.1415926535897932385;
105  symbols_ ["e"] = 2.7182818284590452354;
106  }
107 
110  const double Evaluate ();
114  const double Evaluate (const std::string & program);
115 
119  double & operator[] (const std::string & key) { return symbols_ [key]; }
120 
122  std::map<std::string, double> symbols_;
123 
124  private:
125 
126  const TokenType GetToken (const bool ignoreSign = false);
127  const double CommaList (const bool get);
128  const double Expression (const bool get);
129  const double Comparison (const bool get);
130  const double AddSubtract (const bool get);
131  const double Term (const bool get); // multiply and divide
132  const double Primary (const bool get); // primary (base) tokens
133 
134  inline void CheckToken (const TokenType wanted)
135  {
136  if (type_ != wanted)
137  {
138  std::ostringstream s;
139  s << "'" << static_cast <char> (wanted) << "' expected.";
140  throw std::runtime_error (s.str ());
141  }
142  }
143  }; // end of Parser
144 }
145 
146 #endif // PARSER_H
147 
Mathematical expression parser and evaluator.
Definition: parser.h:43
double & operator[](const std::string &key)
Access or create a symbol (variable) by name.
Definition: parser.h:119
std::map< std::string, double > symbols_
Symbol table mapping variable names to values. Can be accessed directly.
Definition: parser.h:122
TokenType
Token types used by the expression lexer.
Definition: parser.h:54
@ LHPAREN
Left parenthesis.
Definition: parser.h:64
@ NE
Not equal comparison (!=)
Definition: parser.h:74
@ RHPAREN
Right parenthesis.
Definition: parser.h:65
@ NOT
Logical NOT operator.
Definition: parser.h:67
@ EQ
Equal comparison (==)
Definition: parser.h:73
@ ASSIGN_DIV
Compound assignment (/=)
Definition: parser.h:81
@ DIVIDE
Division operator.
Definition: parser.h:62
@ MULTIPLY
Multiplication operator.
Definition: parser.h:61
@ NUMBER
Numeric literal.
Definition: parser.h:57
@ END
End of expression.
Definition: parser.h:58
@ OR
Logical OR (||)
Definition: parser.h:76
@ GE
Greater than or equal (>=)
Definition: parser.h:72
@ AND
Logical AND (&&)
Definition: parser.h:75
@ NAME
Identifier or variable name.
Definition: parser.h:56
@ LT
Less than comparison.
Definition: parser.h:69
@ ASSIGN_MUL
Compound assignment (*=)
Definition: parser.h:80
@ COMMA
Comma delimiter.
Definition: parser.h:66
@ ASSIGN_SUB
Compound assignment (-=)
Definition: parser.h:79
@ LE
Less than or equal (<=)
Definition: parser.h:71
@ NONE
No token (error state)
Definition: parser.h:55
@ GT
Greater than comparison.
Definition: parser.h:70
@ MINUS
Subtraction operator.
Definition: parser.h:60
@ PLUS
Addition operator.
Definition: parser.h:59
@ ASSIGN_ADD
Compound assignment (+=)
Definition: parser.h:78
@ ASSIGN
Assignment operator.
Definition: parser.h:63
const double Evaluate()
Evaluate the stored expression using current symbol values.
Definition: parser.cpp:653
Parser(const std::string &program)
Construct a parser with the given expression.
Definition: parser.h:100
Definition: bank.cpp:47