|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
Arithmetic expression evaluator using operator precedence. More...
#include <cctype>#include <cstring>#include <cstdlib>#include <iostream>#include <tclap/CmdLine.h>#include <tpl_arrayStack.H>Go to the source code of this file.
Enumerations | |
| enum | Token_Type { Value , Operator , Lpar , Rpar , End , Error } |
Functions | |
| Token_Type | lexer (char *&str, size_t &len) |
| Lexer - extracts next token from input string. | |
| char * | str_to_token (const char *token_str, const size_t &len) |
| Convert token string to null-terminated string. | |
| unsigned | precedence (const char &op) |
| Get operator precedence. | |
| void | apply (ArrayStack< int > &val_stack, ArrayStack< char > &op_stack) |
| Apply operator to top two values on stack. | |
| int | eval (char *input) |
| Evaluate arithmetic expression. | |
| int | main (int argc, char **argv) |
Arithmetic expression evaluator using operator precedence.
This example demonstrates a classic expression evaluator that correctly handles operator precedence and associativity. It uses two stacks: one for operands (values) and one for operators, implementing a variant of the shunting-yard algorithm. This is fundamental to understanding how compilers and interpreters evaluate expressions.
Evaluate arithmetic expressions like 3 + 4 * 2 correctly:
Problem: Operators have different precedence levels
Use two stacks to manage operator precedence:
The evaluator processes the expression left-to-right:
Expression: 3 + 4 * 2
| Operator | Precedence | Associativity | Notes |
|---|---|---|---|
| +, - | 1 | Left-to-right | Addition, subtraction |
| *, / | 2 | Left-to-right | Multiplication, division |
/ (slash): Division (integer division)This implementation is similar to the shunting-yard algorithm:
Alternative approach:
After studying this example, you understand:
| Aspect | Complexity | Notes |
|---|---|---|
| Time | O(n) | Single pass through expression |
| Space | O(n) | Stacks store tokens |
| Operators | O(1) | Constant number of operators |
Definition in file evalExp.C.
| enum Token_Type |
| void apply | ( | ArrayStack< int > & | val_stack, |
| ArrayStack< char > & | op_stack | ||
| ) |
Apply operator to top two values on stack.
Definition at line 345 of file evalExp.C.
References Aleph::exit(), Aleph::maps(), Aleph::DynList< T >::pop(), Aleph::DynList< T >::push(), and Aleph::HTList::size().
Referenced by eval().
| int eval | ( | char * | input | ) |
Evaluate arithmetic expression.
| input | The expression string to evaluate |
Definition at line 390 of file evalExp.C.
References apply(), End, Error, Aleph::exit(), lexer(), Lpar, Aleph::maps(), Operator, Aleph::DynList< T >::pop(), precedence(), Aleph::DynList< T >::push(), Rpar, Aleph::HTList::size(), str_to_token(), Aleph::DynList< T >::top(), and Value.
Referenced by main().
| Token_Type lexer | ( | char *& | str, |
| size_t & | len | ||
| ) |
Lexer - extracts next token from input string.
| str | Pointer to current position in string (updated on return) |
| len | Length of extracted token (updated on return) |
Definition at line 281 of file evalExp.C.
References End, Error, Lpar, Aleph::maps(), Operator, Rpar, and Value.
Referenced by eval().
| int main | ( | int | argc, |
| char ** | argv | ||
| ) |
Definition at line 452 of file evalExp.C.
References eval(), and Aleph::maps().
| unsigned precedence | ( | const char & | op | ) |
Get operator precedence.
Precedence levels: $ < ( < +- < /
Definition at line 326 of file evalExp.C.
References Aleph::exit(), and Aleph::maps().
Referenced by eval().
| char * str_to_token | ( | const char * | token_str, |
| const size_t & | len | ||
| ) |
Convert token string to null-terminated string.
Definition at line 313 of file evalExp.C.
References Aleph::maps().
Referenced by eval().