16 - Data Structure - Expression Parsing
16 - Data Structure - Expression Parsing
Parsing
Infix Notation
These notations are named as how they use operator in expression. We shall
learn the same here in this chapter.
Infix Notation
Prefix Notation
Postfix Notation
The following table briefly tries to show the difference in all three notations −
2 (a + b) ∗ c ∗+abc ab+c∗
3 a ∗ (b + c) ∗a+bc abc+∗
5 (a + b) ∗ (c + d) ∗+ab+cd ab+cd+∗
Parsing Expressions
Precedence
Associativity
Associativity describes the rule where operators with the same precedence
appear in an expression. For example, in expression a + b − c, both + and –
have the same precedence, then which part of the expression will be evaluated
first, is determined by associativity of those operators. Here, both + and − are
left associative, so the expression will be evaluated as (a + b) − c.
The above table shows the default behavior of operators. At any point of time
in expression evaluation, the order can be altered by using parenthesis. For
example −
In a + b*c, the expression part b*c will be evaluated first, with multiplication as
precedence over addition. We here use parenthesis for a + b to be evaluated
first, like (a + b)*c.