Exercises for "Advanced Dynamic Programming"

Transcription

Exercises for "Advanced Dynamic Programming"
Universität Bielefeld
Technische Fakultät
AG Praktische Informatik
Exercises for
Advanced Dynamic Programming
Sommersemester 2013
(sheet tree-grammar)
Exercise 3.1: Draw the derivation trees of the two optimal solutions (from the last
exercise) under the tree grammar for the edit distance problem (from the lecture slides).
Exercise 3.2: Write the context free grammar of the yield language of the tree grammar
for edit distance problem down.
Exercise 3.3:
A context free string grammar G is defined by the 4-tuple G =
(T, N, A, P ), where
T is the finite set of terminal symbols,
N is the finite set of non-terminal symbols, disjoint from T ,
A ∈ N is the axiom and
P is the finite set of productions of the form N → (N ∪ T )∗
1. Design a context free string grammar to parse the record of a Skat card game deal.
A deal is the two cards from the Skat, followed by a series of ten tricks. A trick
consists of three played cards. An example record of a deal is:
♦10 ♠7 ♦J ♠J ♥8 ♦A ♦Q ♦7 ♣7 ♣A ♥A ♥J ♦8 ♥Q ♥7 ♠8
♣J ♥10 ♣8 ♠K ♥K ♠9 ♣9 ♦K ♠Q ♠10 ♦9 ♣Q ♣K ♥9 ♣10 ♠A
(Second player was declarer, playing hearts as trump suit.)
2. Design a second context free string grammar, able to parse a simple XML like
document of the form:
< floor >
< office >
<id > M3 -114 </ id >
< person > Stefan </ person >
</ office >
< office >
<id > M3 -107 </ id >
< person > Maddis </ person >
< person > Jan </ person >
</ office >
</ floor >
Ignore all white spaces and take the order as fixed. Number of offices and persons
must be variable.
1
Universität Bielefeld
Technische Fakultät
AG Praktische Informatik
Exercise 3.4:
There are many styles to write down a regular tree grammar. You should know at least
three from the lectures: trees, ADP-Haskell syntax and Bellman’s GAP syntax. In the
following are three different tree grammar given in one style, transform them into the
other two: (assume first non-terminal is axiom)
1.
A = skipl | app ( skipl , S );
skipl = skipr | sl ( CHAR , skipl );
skipr = sr ( skipr , CHAR ) | S ;
S = match ( CHAR , S , CHAR ) |
match ( CHAR , turn ( SEQ ) , CHAR ) |
match ( CHAR , sl ( CHAR , A ) , CHAR ) |
match ( CHAR , sr (A , CHAR ) , CHAR );
2.
S =
|||
|||
|||
|||
S
→
lbase
rbase
pair
branch
nil
nil
<<<
<<<
<<<
<<<
<<<
base ~~~ S
S ~~~ base
base ~~~ S ~~~ base
S ~~~ S
empty
|
|
pair
open
3.
base
S
base
S
base
S
Exercise 3.5: Show: For every context free string grammar, generating a string language L, there is a regular tree grammar that has L as its yield language. And vice
versa!
The webpage for the lecture and the exercises is available at
http://www.techfak.uni-bielefeld.de/ags/pi/lehre/ADP .
If there are any questions regarding the exercises please contact me:
[email protected] or meet me in M3-114.
2
Universität Bielefeld
Technische Fakultät
AG Praktische Informatik
Bellman’s GAP syntax example:
import " helper . hh "
input <raw , raw >
type Rope = extern
type ali = ( Rope first , Rope second )
signature sig_alignment ( alphabet , answer ) {
answer del ( < alphabet , void > , answer );
choice [ answer ] h ([ answer ]);
}
algebra alg_count auto count ;
algebra alg_enum auto enum ;
algebra alg_unit implements sig_alignment ( alphabet = char , answer = int ) {
int del ( < char a , void > , int x ) {
return 1 + x ;
}
choice [ int ] h ([ int ] candList ) {
return list ( minimum ( candList ));
}
}
algebra alg_prettyprint implements sig_alignment ( alphabet = char , answer = ali ) {
ali del ( < char a , void > , alignment x ) {
ali res ;
append ( res . first , a );
append ( res . second , ’ - ’);
append ( res . first , x . first );
append ( res . second , x . second );
return res ;
}
choice [ ali ] h ([ ali ] candList ) {
return candList ;
}
}
grammar gra_globsim uses sig_alignment ( axiom = A ) {
A = nil ( < EMPTY , EMPTY >)
| del ( < CHAR , EMPTY > , A )
# h;
}
instance unitenum = gra_globsim ( alg_unit * alg_enum * alg_prettyprint );
instance unittikz = gra_globsim ( alg_unit * alg_tikz );
3