copyright © 2006 addison-wesley. all rights reserved. ambiguity in grammars a grammar is ambiguous...

29
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

Upload: lorin-warren

Post on 14-Dec-2015

214 views

Category:

Documents


1 download

TRANSCRIPT

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

Copyright © 2006 Addison-Wesley. All rights reserved.

An Ambiguous Expression Grammar

<assign> -> <id> = <expr>

<id> -> A|B|C

<expr> -> <expr> + <expr>

|<expr> * <expr>

|(<expr>)

|<id>

a. <assign>

<id> = <expr>

A <expr> + <expr>

<id> <expr> * <expr>

B <id> <id>

C A

b. <assign>

<id> = <expr>

A <expr> * <expr>

<expr> + <expr> <id>

<id> <id> A

B C

A = B + C * A

3-19

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

Copyright © 2006 Addison-Wesley. All rights reserved.

An Unambiguous Expression Grammar

<assign> -> <id> = <expr>

<id> - > A | B | C

<expr> - > <expr> + <term>

|<term>

<term> - > <term> * <factor>

|<factor>

<factor> - >(<expr>)

|<id>

<assign>

<id> = <expr>

A <expr> + <term>

<term> <term> * <factor>

<factor> <factor> <id>

<id> <id> A

B C

3-21

Copyright © 2006 Addison-Wesley. All rights reserved.

Leftmost and leftmost derivations

leftmost:<assign> =><id> = <expr>=>A = <expr> A = <expr> + <term> A = <term> + <term> A = <factor> + <term> A = <id> + <term> A = B + <term> A = B + <term> * <factor> A = B + <factor> * <factor> A = B + <id> * <factor> A = B + C * <factor> A = B + C * <id> A = B + C * A

rightmost:<assign> =><id> = <expr>=><id> = <expr> + <term>=><id> = <expr> + <term>*<factor>

=><id> = <expr> + <term>*<id> =><id> = <expr> + <term>*A=><id> = <expr> + <factor> * A<id> =<expr> + <id> * A<id> = <expr> + C * A<id> = <term> + C * A<id> = <factor> + C * A<id> = <id> + C * A<id> = B + C * AA = 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

Copyright © 2006 Addison-Wesley. All rights reserved.

3.3.2 Extended BNF

• Optional parts are placed in brackets [ ]<selection> -> if (<expression>) <statement> [else <statement>]

• Repetitions (0 or more) are placed inside braces { }<ident_list> -> <identifier> {, <identifier>}

• When a single element must be chosen from a group, the options are placed in parentheses and separated by the OR operator, |. <term> -> <term> (* | / | %)<factor>

3-23

Copyright © 2006 Addison-Wesley. All rights reserved.

BNF and EBNF

• BNF <expr> <expr> + <term> | <expr> - <term> | <term> <term> <term> * <factor> | <term> / <factor> | <factor>

• EBNF <expr> <term> {(+ | -) <term>} <term> <factor> {(* | /) <factor>}

3-24

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(FormalParameterListopt)

• 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

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

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

Copyright © 2006 Addison-Wesley. All rights reserved.

Attribute Grammars

• Attribute Grammar – a powerful mechanism that describes both syntax and static semantics

3-28

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

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

Copyright © 2006 Addison-Wesley. All rights reserved.

Attribute Grammars : Definition

S(X): synthesized attributes of XA(X)

I(X): inherited attributes of X{ X0

X1 Xn……

Synthesized attributes of X0 - values depend on child nodes

Inherited attributes of Xn – values depend on parent node or sibling

3-31

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 X0 -> X1 … Xn:

a. The synthesized attributes of X0 are

computed with semantic functions of the form S(X0) = f(A(X1), …,A(Xn)). 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

Copyright © 2006 Addison-Wesley. All rights reserved.

Attribute Grammars : Definition

X0

X1 Xn……

S(X0)

||f(A(X1),…,A(Xn))

3-33

Copyright © 2006 Addison-Wesley. All rights reserved.

Attribute Grammars : Definition

b. The inherited attributes of Xj, 1≤j≤n are

computed with semantic functions of the form I(Xj) = f(A(X0), …,A(Xn)). 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

Copyright © 2006 Addison-Wesley. All rights reserved.

Attribute Grammars : Definition

X0

X1 Xn… I(Xj) = f(A(X0),…,A(Xn))Xj …

parent

sibling

X0

X1 Xn… Xj-1 Xj …

Often restricted to I(Xj) = f(A(X0),…,A(Xj-1))

Only depends on attributes of the parent and the left siblings

3-35

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(X0), …, A(Xn)} 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

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

Copyright © 2006 Addison-Wesley. All rights reserved.

Attribute Grammars: An Example

• Syntax rule: <proc_def> ->procedure <proc_name>[1]

<proc_body> end <proc_name>[2];

• Predicate:<proc_name>[1].string == <proc_name>[2]. string

3-38

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

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

Copyright © 2006 Addison-Wesley. All rights reserved.

Attribute Grammars: Another Example

• Syntax<assign> -> <var> = <expr><expr> -> <var> + <var> | <var><var> A | B | C

<assign>

<expr>

<var> <var>[2] <var>[3]

A = A + B

3-41

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 <var> and <expr>. If <var>, the actual type is the intrinsic. If <expr>, it is determined from the actual types of the child(ren) node(s).

– expected_type : An inherited attribute associated with the non-terminal <expr>. It is used to store the type that is expected for the <expr>. It is determined by the type of the variable on the left side of the assignment statement.

3-42

Copyright © 2006 Addison-Wesley. All rights reserved.

Attribute Grammars: Another Example

The complete attribute grammar:

1. Syntax rule: <assign> <var> = <expr>Semantic rule: <expr>.expected_type <- <var>.actual_type

2. Syntax rule: <expr> <var>[2] + <var>[3]Semantic rule: <expr>.actual_type <-

if <var>[2].actual_type = int) and(<var>[3].actual_type = int)

then intelse realend if

Predicate: <expr>.actual_type == <expr>.expected_type

3-43

Copyright © 2006 Addison-Wesley. All rights reserved.

Attribute Grammars: Another Example3. Syntax rule: <expr> <var> Semantic rule:<expr>.actual_type <var>.actual_type

Predicate: <expr>.actual_type == <expr>.expected_type4. Syntax rule: <var> A | B | C Semantic rule:<var>.actual_type look-up(<var>.string)

3-44

Copyright © 2006 Addison-Wesley. All rights reserved.

Attribute Grammars: Another Example

<assign>

<expr>

<var> <var>[2] <var>[3]

A = A + B

actual_type

expected_type

actual_type

actual_type

actual_type

Flow of Attributes

3-45

Copyright © 2006 Addison-Wesley. All rights reserved.

Attribute Grammars: Another Example• How are attribute values computed?

1. <var>.actual_type <- look-up(A) (rule 4)2. <expr>.expected_type <- <var>.actual_type (rule 1)3. <var>[2].actual_type <- look-up(A) (rule 4) <var>[3].actual_type <- look-up(B) (rule 4)4. <expr>.actual_type <- either int or real (rule 2)5. <expr>.expected_type == <expr>.actual_type is either TRUE or FALSe (rule 2)

3-46