Exercise Partial Solution - Computer Science Foundations | CS 520, Study notes of Computer Science

Material Type: Notes; Professor: Schmidt; Class: Computer Science Foundations; Subject: Computer Science; University: Drexel University; Term: Unknown 1989;

Typology: Study notes

Pre 2010

Uploaded on 08/19/2009

koofers-user-kc8
koofers-user-kc8 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Exercise 11.2.5 (Partial) Solution
This solution differs a bit from the one provided earlier; that solution inducted over the #
of productions applied, rather than the height of the expression tree. They are very
similar, however.
We are to show that the following grammar generates the balanced profile strings, as defined in
section 2.6:
<BalancedE> ε
<BalancedE> (<BalancedE>)
<BalancedE> <BalancedE><BalancedE>
Part I: Show that all strings in L(<BalancedE>) are profile-balanced
We will use induction, on the height of the expression tree that produces strings s Є
L(<BalancedE>).
Our base case is simple. We have only one possible tree of height one:
Trivially, ε (the empty, or null string) is balanced. So, by the inductive hypothesis,
we may assume that any such tree of height <= n produces a profile-balanced string.
Section 2.6 defines a profile-balanced string s so: Set ctr=0, scan s from left to
right. For each character ‘(‘ that is encountered, increment ctr. For each ‘)’ that is encountered,
decrement ctr. If, and only if, after the entire string s is scanned, ctr is 0, and it at no time was
negative, then s is profile-balanced.
We now need to show that any string, s’, represented by an expression tree of height n+1 is also
balanced. There are 2 productions in the grammar that allow us to build on existing expressions.
Lets consider:
<BalancedE> <BalancedE><BalancedE>
Represented:
If the tree rooted at the red node has height n+1, then the 2 (blue and green) have height <= n, so,
by our hypothesis, s1 and s2 are profile balanced. We need to show that the string s’ =s1 s2 is
profile balanced. Start on the left, ctr = 0. After we scan s1 ctr is 0, and was never negative.
<BalancedE>
<BalancedE> <BalancedE>
s1 s2
<BalancedE>
ε
pf2

Partial preview of the text

Download Exercise Partial Solution - Computer Science Foundations | CS 520 and more Study notes Computer Science in PDF only on Docsity!

Exercise 11.2.5 (Partial) Solution

This solution differs a bit from the one provided earlier; that solution inducted over the # of productions applied, rather than the height of the expression tree. They are very similar, however.

We are to show that the following grammar generates the balanced profile strings, as defined in section 2.6:

  • → ε
  • → ()

Part I: Show that all strings in L() are profile-balanced

We will use induction, on the height of the expression tree that produces strings s Є L().

Our base case is simple. We have only one possible tree of height one:

Trivially, ε (the empty , or null string) is balanced. So, by the inductive hypothesis, we may assume that any such tree of height <= n produces a profile-balanced string.

Section 2.6 defines a profile-balanced string s so: Set ctr=0 , scan s from left to right. For each character ‘(‘ that is encountered, increment ctr. For each ‘)’ that is encountered, decrement ctr. If, and only if, after the entire string s is scanned, ctr is 0, and it at no time was negative, then s is profile-balanced.

We now need to show that any string, s’ , represented by an expression tree of height n+1 is also balanced. There are 2 productions in the grammar that allow us to build on existing expressions. Lets consider:

Represented:

If the tree rooted at the red node has height n+1, then the 2 (blue and green) have height <= n, so, by our hypothesis, s 1 and s 2 are profile balanced. We need to show that the string s’ =s 1 s 2 is profile balanced. Start on the left, ctr = 0. After we scan s 1 ctr is 0, and was never negative.

s 1 s (^2)

ε

We then scan s 2 , and again, (by our hypothesis) ctr is 0, and was never negative. We scanned s’ , ctr is 0, and was never negative, so, by the definition from 2.6, s’ is profile balanced.

We have another possible tree of height n+1 that we need to consider, given to us by the production:

→ ( )

We need to show that s’ = ( s ) is profile balanced. Same game. ctr = 0, start at the left, ‘(‘. ctr = 1. By the hypothesis s is balanced, so after parsing it ctr is still 1, and, further, was never less than 1 (specifically, was never negative). Hit that last ‘)’, ctr is back to 0. By definition, s’ is profile balanced. Done.

We would now want to show that all possible profile-balanced strings

of parenthesis can by generated from

I can offer no improvement over the solution previously provided.

Show that any profile-balanced string, s , is an element of L( ).

We will induct over the length of s. We are using strong induction. We need to get away from the n, n+1 thing, it’ll bog us down (since, think about it, parenthesis need to be added in pairs).

We start with ε, a balanced string of length 0. We verify that it is in L( ). We may now assume that all strings of length < n are elements of the language. We then need to consider strings of length n. Are they balanced?

We use the definition from 2.6, our ctr , from above. Start it at 0. When we’re done we know ctr is 0. We have 2 cases to consider:

  1. ctr reached 0 sometime before we reached the end of s (so, we have a concatenation 2 strictly smaller strings, which, by the hypothesis, are both in the language).
  2. ctr didn’t reach 0 until the very last character (in which case have a strictly smaller string of length n-2 nested inside parenthesis).

You simply need to verify that, in each case, the grammar takes a valid string (or strings) and can produce s , of length n.

s