Attribute  Grammars as a framework for semantic processingTopAnouncementsParse Trees and Ambiguity

Parse Trees and Ambiguity

  1. For our purposes, it is not enough that a grammar specify the set of strings that belong to a language, we also want a grammar to impose grammatical structure on strings since this structure influences the meaning associated with a string.
  2. Representing structural information as a tree provides a better way to eliminate irrelevant choices about the order of non-terminal expansion.
    Parse Tree
    A parse tree for a sentential form S of a grammar G is a tree in which each node is labeled with an element of ( Vt U Vn ) in such a way that:
    1. The root is labeled with the start symbol,
    2. the nodes of the frontier of the tree (i.e. the leaves of the tree) are labeled with the symbols that form S, and
    3. each interior node is labeled with some non-terminal, N, such that N P and is the string of labels found on the node N's children.
  3. Given a parse tree, we can extract a derivation by walking the tree. Depending on whether we visit the children of each node left-to-right or right-to-left, we will obtain a derivation with one of two special forms:
    Direct leftmost derivation
    Given a grammar, G = (Vt , Vn , S, P), and two strings x and y in (Vt U Vn)* such that x = A and y = where Vt*, , (Vt U Vn)* and ( A, ) P we say that y can be directly derived leftmost from x. In this case we write x y .
    Direct rightmost derivation
    Given a grammar, G = (Vt , Vn , S, P), and two strings x and y in (Vt U Vn)* such that x = A and y = where Vt*, , (Vt U Vn)* and ( A, ) P we say that y can be directly derived rightmost from x. In this case we write x y .
    Left-most Derivation
    Given a grammar, G = (Vt , Vn , S, P), and two strings x and y in (Vt U Vn)* we say that x derives y leftmost if there exists a sequence of string 0, 1, 2, ... ,m all in (Vt U Vn)* such that
    1. for all i < m, i i+1 ,
    2. x = 0 , and
    3. y = m .
    In this case we write x y .
    Rightmost Derivation
    Given a grammar, G = (Vt , Vn , S, P), and two strings x and y in (Vt U Vn)* we say that x derives y rightmost if there exists a sequence of string 0, 1, 2, ... ,m all in (Vt U Vn)* such that
    1. for all i < m, i i+1 ,
    2. x = 0 , and
    3. y = m .
    In this case we write x y .


    Intuitively, a derivation is rightmost (leftmost) if at each step it is the rightmost (leftmost) non-terminal that is replaced by the right-hand side of some production.

    The first derivation shown earlier happens to be a leftmost derivation of x a x z y y in G.


Computer Science 434
Department of Computer Science
Williams College

Attribute  Grammars as a framework for semantic processingTopAnouncementsParse Trees and Ambiguity