Recursive Data Structures and Grammars - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

Recursive Data Structures and Grammars

Description:

Recursive Definitions of Properties of Data Structures ... from the syntactic category S using the grammar G is defined inductively. ... – PowerPoint PPT presentation

Number of Views:22
Avg rating:3.0/5.0
Slides: 21
Provided by: JeremyR91
Category:

less

Transcript and Presenter's Notes

Title: Recursive Data Structures and Grammars


1
Recursive Data Structures and Grammars
  • Themes
  • Recursive Description of Data Structures
  • Grammars and Parsing
  • Recursive Definitions of Properties of Data
    Structures
  • Recursive Algorithms for Manipulating and
    Traversing Data Structures
  • Examples
  • Lists
  • Trees
  • Expressions and Expression Trees

2
Grammars
  • Syntactic Categories (non-terminals)
  • ltnumbergt
  • ltdigitgt
  • ltexprgt
  • Production Rules (replace syntactic category on
    the rhs by the lhs, is or)
  • ltexprgt ? ltexprgt ltexprgt
  • ltexprgt ? ltnumbergt
  • ltnumbergt ? ltdigitgt ltnumbergt
  • ltdigitgt ? 0123456789

3
Derivation
  • Repeatedly replace syntactic categories by the
    lhs of rules whose rhs is equal to the syntactic
    category
  • ltexprgt ? ltexprgtltexprgt
  • ? ltexprgtltexprgtltexprgt
  • ? ltnumbergtltexprgtltexprgt
  • ? ltnumbergtltnumbergtltexprgt
  • ? ltnumbergtltnumbergtltnumbergt

4
Derivation (e.g. 2)
  • ltnumbergt ? ltdigitgtltnumbergt
  • ? ltdigitgtltdigitgtltnumbergt
  • ? ltdigitgtltdigitgtltdigitgt
  • ? ltdigitgtltdigitgt3
  • ? ltdigitgt23
  • ? 123
  • When there are no more syntactic categories, the
    process stops and the resulting string is said to
    be derived from the initial syntactic category.

5
Languages
  • The language, L(ltSgt), derivable from the
    syntactic category ltSgt using the grammar G is
    defined inductively.
  • Initially L(ltSgt) is empty
  • If ltSgt ? X1 ? ? ? Xn is a production in G and si
    Xi is a terminal or si ? L(Xi), then the
    concatenation s1s2 sn is in L(ltSgt)

6
Language
  • The number of strings of length n in the language
    L(ltnumbergt) is 10n.
  • Proof is by induction.

7
Language
  • ltBgt ? () (ltBgt)
  • L(ltBgt) strings of n left parens followed by n
    right parens, for n gt 0.

8
Systematic Generation
  • C statement example

9
Binary Trees
  • A binary tree is
  • empty
  • consists of a node with 3 elements
  • value
  • left, which is a tree
  • right, which is a tree

10
Height of Binary Trees
  • Height(T) -1 if T is empty
  • max(Height(T.left),Height(T.right)) 1
  • Alternative Max over all nodes of the level of
    the node.

11
Number of Nodes of a Binary Trees
  • Nnodes(T) 0 if T is empty
  • Nnodes(T.left) Nnodes(T.right) 1

12
Internal Path Length
  • IPL(T) 0 if T is empty
  • IPL(T) IPL(T.left) IPL(T.right) Nnodes(T)-1
  • Alternative Sum over all nodes of the level of
    the node.

13
External Format for Binary Trees
  • ltbintreegt ?
  • ? ltvaluegt,ltbintreegt,ltbintreegt
  • ,
  • 1,,,
  • 2,1,,,, 2,,1,,
  • 3, 2,1,,,, , 3,
    2,,1,,,
  • 3, 1,,, 1,,,
  • 3, ,2,1,,,, 3,
    ,2,,1,,

14
Recurrence for the Number of Binary Trees
  • Let Tn be the number of binary trees with n
    nodes.
  • T0 1, T1 1, T2 2, T3 5

15
Binary Search Trees
  • Binary Tree
  • All elements in T-gtleft are lt T-gtvalue
  • All elements in T-gtright are gt T-gtvalue

16
Inorder traversal
  • Recursively visit nodes in T.left
  • visit root
  • Recursively visit nodes in T.right
  • An in order traversal of a BST lists the elements
    in sorted order. Proof by induction.

17
Parse Tree
  • A derivation is conveniently stored in a tree,
    where internal nodes correspond to syntactic
    categories and the children of a node correspond
    to the element of the rhs in the rule that was
    applied

18
Example Parse Tree
  • ltnumbergt
  • / \
  • ltdigitgt ltnumbergt
  • / \
  • 1 ltdigitgt ltnumbergt
  • 2 ltdigitgt
  • 3

19
Recursive Decent Parser
  • Balanced parentheses

20
Ambiguous Grammars
  • ltexprgt ltexprgt
  • / \ /
    \
  • ltexprgtltexprgt ltexprgtltexprgt
  • / \
    / \
  • ltexprgtltexprgt ltexprgtltexprgt
Write a Comment
User Comments (0)
About PowerShow.com