180 likes | 312 Views
4.3 Arithmetic Expressions. In this section, we shall introduce context-free grammars to generate a class of arithmetic expressions with operators , , , and ^ . Especially, we have to handle the associativities and precedences of the operators. For the associativities,.
E N D
4.3 Arithmetic Expressions In this section, we shall introduce context-free grammars to generate a class of arithmetic expressions with operators , , , and ^. Especially, we have to handle the associativities and precedences of the operators. For the associativities, the operators and are associative, the operators andare left associative, and The exponential operator ^is right associative.
Example 12 : 12 + 4 + 3 = ( ( 12 + 4 ) + 3 ) = ( 12 + ( 4 + 3 ) ) 12 4 3 = ( ( 12 4 ) 3 ) = ( 12 ( 4 3 ) ) 12 4 3 = ( ( 12 4 ) 3 ) ( 12 ( 4 3 ) ) 12 4 3 = ( ( 12 4 ) 3 ) ( 12 ( 4 3 ) ) 12 ^ 4 ^ 3 = ( 12 ^ ( 4 ^ 3 ) ) = ( 12 ^ 64 ) (12 ^ 4) ^ 3 = 12 ^ 12
The following operators takes precedence in decreasing order. ( unary minus ) ^ ( exponent ) , , . Example 13 : 12 2 + 15 3 4 ^ 3 = ( 12 2 ) + ( (15 3) (4 ^ 3) )
Left associativity Example 14: The following grammar as in the example 10 is ambiguous. E E + E | E E | (E) | <integer> In order to eliminate ambiguity, we impose left associativities for the operators + and . After introduce a new variable, say <term>, we have a new grammar as follows. E E + <term> | E <term> | <term> <term> (E) | <integer> Note : Sometimes we need to apply the theorem 4 to revise the above left recursive productions to right recursive productions.
E E + <term> E <term> <integer> * E + <term> <integer> 5 * <term> <integer> 6 * <integer> 4 * 7 The unique parse tree for the sentence 7 + 4 6 + 5 is as follows. The parse tree interprets the expression 7+46+5 as (((7+4) 6)+5).
Right associativity Example 15: The following grammar generating expressions with operator ^ is ambiguous. E E ^ E | (E) | <integer> The leftmost derivation for 2^3^2 is as follows. E E ^ E * 2 ^ E 2 ^ E ^ E * 2 ^ 3 ^ E * 2 ^ 3 ^ 2 The rightmost derivation for 2^3^2 is as follows. E E ^ E * E ^ 2 E ^ E ^ 2 * E ^ 3 ^ 2 * 2 ^ 3 ^ 2
E E ^ E E E E ^ E ^ E E E <integer> ^ <integer> * <integer> <integer> * <integer> <integer> 2 * * 2 * * 2 3 3 2 Hence, the sentence 2^3^2 has 2 different parse trees as follows. The left tree interprets 2^3^2 as 2^(3^2)=2^9 = 512. The right tree interprets 2^3^2 as (2^3)^2 = 8^2 = 64, a wrong result.
In order to eliminate ambiguity, we impose right associativity for the operator ^. After introduce a new variable, say <expon>, we have a new grammar as follows. Example 16: The following grammar generates expressions for operator ^ to possess right associativity. E <expon> ^ E | <expon> <expon> (E) | <integer> Consider the leftmost derivation and the rightmost derivation for the sentence 2^3^2.
E ^ <expon> E <expon> E ^ <integer> <expon> * <integer> 2 * <integer> 3 * 2 E <expon> ^ E * 2 ^ E 2 ^ <expon> ^ E * 2 ^ 3 ^ E * 2 ^ 3 ^ <expon> * 2^3^2 E <expon> ^ E <expon> ^ <expon>^E <expon> ^ <expon> ^ <expon> * <expon> ^<expon> ^ 2 * 2 ^ 3 ^ 2 Hence, the sentence 2^3^2 has a unique parse tree as follows.
Precedence Both the operators + and are (left) associative. The operator has higher precedence than the operator +. Consider the grammar in the following example 17. Although it generates expressions for operators to posses left associativities, it is not correct because of ignoring the precedence rule. Example 17: Consider the grammar E E + <term> | E <term> | <term> <term> (E) | <integer> Consider the leftmost derivation and the rightmost derivation for the sentence 2 + 3 2.
The leftmost derivation for 2 + 3 2 is as follows. E E <term> E+ <term> <term> <term>+ <term> <term> * 2 + 3 2 The rightmost derivation for 2 + 3 2 is as follows. E E <term> * E 2 E + <term> 2 <term> + <term> 2 * 2 + 3 2 There is a unique parse tree for the derivation of 2 + 3 2 as follows.
E E <term> E <term> + <integer> * <term> <integer> 2 * <integer> 3 * 2 But, the sentence 2 + 3 2 is interpreted as (2+3) 2 = 10, which is not correct.
In order to impose a precedence rule for the operators,we need to introduce a variable for each precedence level of operators. the parentheses and a single number have the highest precedence, let it be the variable <basic>. We have productions: <basic> (E) | <integer> Next, should be the operator , let it be the variable <prod>. We have productions: <prod> <prod> <basic> | <basic> Then consider the operator +, let it be the variable E. We have productions: E E + <prod> | <prod>
Example 18: The following grammar generates expressions for operators + and to obey the precedence rule. E E + <prod> | <prod> <prod> <prod> <basic> | <basic> <basic> (E) | <integer> The rightmost derivation for the sentence 7 + 4 6 + 5 is as follows. E E + <prod> E + <prod> + <basic> * E + <prod> + 5 E + <prod> <basic> + 5 * E + <prod> 6 + 5 E + <basic> 6 + 5 * E + 4 6 + 5 <prod> + 4 6 + 5 <basic> + 4 6 + 5 * 7 + 4 6 + 5
E E + <prod> E + <prod> <basic> <prod> <prod> <basic> <integer> * * <basic> <integer> 5 <basic> * <integer> <integer> 6 * * 4 7 The unique parse tree for the sentence 7 + 4 6 + 5 is as follows. The parse tree interprets 7 + 4 6 + 5 as ((7 + (4 6)) + 5).
Example 19: The following grammar generates expressions for operators +, ,, and ^ to obey the precedence rules and associativities. E E + <prod> | E <prod> | <prod> <prod> <prod> <expon> | <prod> <expon> | <expon> <expon> <item> ^ <expon> | <item> <item> <item> | <basic> <basic> (E) | <integer> [[ right recursive productions : <expon> <item> ^ B | B B B ^ <item> | <item> ]]