Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2006 Addison-Wesley. All rights reserved. Ambiguity in Grammars A grammar is ambiguous if and only if it generates a sentential form that has.

Similar presentations


Presentation on theme: "Copyright © 2006 Addison-Wesley. All rights reserved. Ambiguity in Grammars A grammar is ambiguous if and only if it generates a sentential form that has."— Presentation transcript:

1 Copyright © 2006 Addison-Wesley. All rights reserved. Ambiguity in Grammars A grammar is ambiguous if and only if it generates a sentential form that has two or more distinct parse trees 3-18

2 Copyright © 2006 Addison-Wesley. All rights reserved. An Ambiguous Expression Grammar -> = -> A|B|C -> + | * |( ) | a. = A + * B C A b. = A * + A B C A = B + C * A 3-19

3 Copyright © 2006 Addison-Wesley. All rights reserved. Operator Precedence An operator in an arithmetic expression is generated lower in the parse tree (and therefore must be evaluated first) can be used to indicate that it has precedence over an operator produced higher up in the tree As in previous slides –Tree a: A = B + (C * A) –Tree b: A = (B + C ) * A 3-20

4 Copyright © 2006 Addison-Wesley. All rights reserved. An Unambiguous Expression Grammar -> = - > A | B | C - > + | - > * | - >( ) | = A + * A B C 3-21

5 Copyright © 2006 Addison-Wesley. All rights reserved. Leftmost and leftmost derivations leftmost: => = =>A =  A = +  A = B +  A = B + *  A = B + C *  A = B + C * A rightmost: => = => = + => = + * => = + *A  = + * A  = + C * A  = B + C * A  A = B + C * A Every derivation with an unambiguous grammar has a unique parse tree, although that tree can be represented by different derivations. 3-22

6 Copyright © 2006 Addison-Wesley. All rights reserved. 3.3.2 Extended BNF Optional parts are placed in brackets [ ] -> if ( ) [else ] Repetitions (0 or more) are placed inside braces { } -> {, } When a single element must be chosen from a group, the options are placed in parentheses and separated by the OR operator, |. -> (* | / | %) 3-23

7 Copyright © 2006 Addison-Wesley. All rights reserved. BNF and EBNF BNF  + | - |  * | / | EBNF  {(+ | -) }  {(* | /) } 3-24

8 Copyright © 2006 Addison-Wesley. All rights reserved. EBNF variations In place of the arrow, a colon is used and the RHS is placed on the next line Instead of a vertical bar to separate alternative RHSs, they are simply placed on separate lines In place of squared brackets to indicate something being optional, the subscript opt is used. E.g. –ConstructorDeclarator -> SimpleName(FormalParameterList opt ) Rather than using the | symbol in a parenthesized list of elements to indicate a choice, the words “one of” are used. E.g. –AssignmentOperator -> one of = *= /= %= += -= >= &= |= 3-25

9 Copyright © 2006 Addison-Wesley. All rights reserved. Attribute Grammars Extension of Context-free grammars Context-free grammars cannot describe all of the syntax of programming languages –E.g. “All variables must be declared before they are referenced.” – This rule cannot be specified in BNF. Additions to CFGs to carry some semantic info along parse trees, e.g. type checking Primary value of attribute grammars (AGs) –Static semantics specification –Compiler design (static semantics checking) 3-26

10 Copyright © 2006 Addison-Wesley. All rights reserved. Attribute Grammars Leads to another category of language rules called static semantics rules Static semantics of a language is only indirectly related to the meaning of programs during execution. It has to do with the legal forms of programs (syntax rather than semantics) Many static semantics rules state a language’s type constraints. Type checking are done at compile time. 3-27

11 Copyright © 2006 Addison-Wesley. All rights reserved. Attribute Grammars Attribute Grammar – a powerful mechanism that describes both syntax and static semantics 3-28

12 Copyright © 2006 Addison-Wesley. All rights reserved. Attribute Grammars: Basic Concepts Attributes –Associated with grammar symbols –Similar to variables in the sense that they can have values assigned to them. Attribute Computation Functions –Also called semantics functions –Associated with grammar rules Predicate Functions –State the static semantics rules –Associated with grammar rules 3-29

13 Copyright © 2006 Addison-Wesley. All rights reserved. Attribute Grammars : Definition An attribute grammar is a context-free grammar with the following additional features: 1) - Associated with each grammar symbol X is a set of attribute values A(X). - The set A(X) consists of two disjoint sets S(X) and I(X) - S(X): Synthesized attributes, pass semantic information up a parse tree I(X): Inherited attributes, pass semantic information down and across a tree 3-30

14 Copyright © 2006 Addison-Wesley. All rights reserved. Attribute Grammars : Definition S(X): synthesized attributes of X A(X) I(X): inherited attributes of X { X0X0 X1X1 XnXn …… Synthesized attributes of X 0 - values depend on child nodes Inherited attributes of X n – values depend on parent node or sibling 3-31

15 Copyright © 2006 Addison-Wesley. All rights reserved. Attribute Grammars : Definition 2) Each rule has a set of functions that define certain attributes of the non-terminals in the rule. For a rule X 0 -> X 1 … X n : a. The synthesized attributes of X 0 are computed with semantic functions of the form S(X 0 ) = f(A(X 1 ), …,A(X n )). So the value of a synthesized attribute on a parse tree node depends only on the values of the attributes on that node’s children nodes. 3-32

16 Copyright © 2006 Addison-Wesley. All rights reserved. Attribute Grammars : Definition X0X0 X1X1 XnXn …… S(X 0 ) || f(A(X 1 ),…,A(X n )) 3-33

17 Copyright © 2006 Addison-Wesley. All rights reserved. Attribute Grammars : Definition b. The inherited attributes of X j, 1≤j≤n are computed with semantic functions of the form I(X j ) = f(A(X 0 ), …,A(X n )). So the value of an inherited attribute on a parse tree node depends only on the values of the attributes on that node’s parent node and those of its sibling nodes. 3-34

18 Copyright © 2006 Addison-Wesley. All rights reserved. Attribute Grammars : Definition X0X0 X1X1 XnXn … I(X j ) = f(A(X 0 ),…,A(X n ))XjXj … parent sibling X0X0 X1X1 XnXn … X j-1 XjXj … Often restricted to I(X j ) = f(A(X 0 ),…,A(X j-1 )) Only depends on attributes of the parent and the left siblings 3-35

19 Copyright © 2006 Addison-Wesley. All rights reserved. Attribute Grammars : Definition 3) Each rule has a (possibly empty) set of predicates to check for attribute consistency. A predicate function has the form of a Boolean expression on the union of the attribute set {A(X 0 ), …, A(X n )} and a set of literal attribute values. The only derivations allowed with an attribute grammar are those in which every predicate associate with every non-terminal is true. A false predicate function value indicates a violation of the syntax or static semantics rules of the language. 3-36

20 Copyright © 2006 Addison-Wesley. All rights reserved. Attribute Grammars: Intrinsic Attributes intrinsic attributes are synthesized attributes of leaf nodes whose values are determined outside the parse tree. E.g. The initial values of variables come from declaration statements. These values can be used to compute the remaining attribute values on a parse tree. 3-37

21 Copyright © 2006 Addison-Wesley. All rights reserved. Attribute Grammars: An Example Syntax rule: ->procedure [1] end [2]; Predicate: [1].string == [2]. string 3-38

22 Copyright © 2006 Addison-Wesley. All rights reserved. Attribute Grammars: Another Example Syntax and semantics descriptions: –The only variable names are A, B, and C –The right side of the assignments can either be a variable or an expression in the form of a variable added to another variable. –The variables can be one of two types: int or real –When there are two variables on the right side of an assignment, they need not be the same type. –The type of the expression when the operand types are not the same is always real. When they are the same, the expression type is that of the operands. –The type of the left side of the assignment must match the type of the right side. –The assignment is valid only if the LHS and the value resulting from the evaluating the RHS have the same type. 3-39

23 Copyright © 2006 Addison-Wesley. All rights reserved. Attribute Grammars: Another Example E.g. Are the following statements valid? –int A; int B; A = A + B; -int A; real B; A = A + B; -int A; real B; B = A + B; -real A; real D; D = A + D; 3-40

24 Copyright © 2006 Addison-Wesley. All rights reserved. Attribute Grammars: Another Example Syntax -> = -> + | A | B | C [2] [3] A = A + B 3-41

25 Copyright © 2006 Addison-Wesley. All rights reserved. Attribute Grammars: Another Example Attributes of the non-terminals: –actual_type : A synthesized attribute associated with the non-terminals and. If, the actual type is the intrinsic. If, it is determined from the actual types of the child(ren) node(s). –expected_type : An inherited attribute associated with the non-terminal. It is used to store the type that is expected for the. It is determined by the type of the variable on the left side of the assignment statement. 3-42

26 Copyright © 2006 Addison-Wesley. All rights reserved. Attribute Grammars: Another Example The complete attribute grammar: 1. Syntax rule:  = Semantic rule:.expected_type.actual_type 2. Syntax rule:  [2] + [3] Semantic rule:.actual_type <- if [2].actual_type = int) and ( [3].actual_type = int) then int else real end if Predicate:.actual_type ==.expected_type 3-43

27 Copyright © 2006 Addison-Wesley. All rights reserved. Attribute Grammars: Another Example 3. Syntax rule:  Semantic rule:.actual_type .actual_type Predicate:.actual_type ==.expected_type 4. Syntax rule:  A | B | C Semantic rule:.actual_type  look-up(.string) 3-44

28 Copyright © 2006 Addison-Wesley. All rights reserved. Attribute Grammars: Another Example [2] [3] A = A + B actual_type expected_type actual_type Flow of Attributes 3-45

29 Copyright © 2006 Addison-Wesley. All rights reserved. Attribute Grammars: Another Example How are attribute values computed? 1..actual_type <- look-up(A) (rule 4) 2..expected_type.actual_type (rule 1) 3. [2].actual_type <- look-up(A) (rule 4) [3].actual_type <- look-up(B) (rule 4) 4..actual_type <- either int or real (rule 2) 5..expected_type ==.actual_type is either TRUE or FALSe (rule 2) 3-46


Download ppt "Copyright © 2006 Addison-Wesley. All rights reserved. Ambiguity in Grammars A grammar is ambiguous if and only if it generates a sentential form that has."

Similar presentations


Ads by Google