Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
prob-teaching-notebooks
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
general
stups
prob-teaching-notebooks
Commits
dfa653d7
Commit
dfa653d7
authored
5 years ago
by
Michael Leuschel
Browse files
Options
Downloads
Patches
Plain Diff
Anpassung der Namen
parent
111f7a77
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
info4/kapitel-3/PDA_nach_kfG.ipynb
+41
-36
41 additions, 36 deletions
info4/kapitel-3/PDA_nach_kfG.ipynb
with
41 additions
and
36 deletions
info4/kapitel-3/PDA_nach_kfG.ipynb
+
41
−
36
View file @
dfa653d7
...
...
@@ -9,7 +9,7 @@
},
{
"cell_type": "code",
"execution_count": 1
6
,
"execution_count":
4
1,
"metadata": {},
"outputs": [
{
...
...
@@ -18,7 +18,7 @@
"Loaded machine: PDA_to_CFG"
]
},
"execution_count": 1
6
,
"execution_count":
4
1,
"metadata": {},
"output_type": "execute_result"
}
...
...
@@ -28,56 +28,61 @@
"MACHINE PDA_to_CFG\n",
"/* Translating a PDA to a CFG */\n",
"SETS\n",
"
STATES
= {z0,z1, symbol}; \n",
"
Z
= {z0,z1, symbol}; \n",
" /* symbol: virtueller Zustand um S und andere Symbole in der Grammatik darzustellen */\n",
" SYMBOL
S
={a,b, A, BOT, lambda, S} /* BOT = # =
bottom of stack
*/\n",
" SYMBOL
E
={a,b, A, BOT, lambda, S} /* BOT = # =
Ende vom Keller
*/\n",
"DEFINITIONS\n",
"
CF
G_Alphabet == (
STATES
*(SYMBOL
S-
{lambda})*
STATES
);\n",
"
kf
G_Alphabet == (
Z
*(SYMBOL
E \\
{lambda})*
Z
);\n",
" Σ == {a,b};\n",
" Γ == {A,BOT};\n",
" ANIMATION_FUNCTION1 == {r,c,i| r=1 ∧ c∈dom(cur) ∧ i=prj1(STATES,SYMBOLS)(prj1(STATES*SYMBOLS,STATES)(cur(c)))};\n",
" ANIMATION_FUNCTION2 == {r,c,i| r=2 ∧ c∈dom(cur) ∧ i=prj2(STATES,SYMBOLS)(prj1(STATES*SYMBOLS,STATES)(cur(c)))};\n",
" ANIMATION_FUNCTION3 == {r,c,i| r=3 ∧ c∈dom(cur) ∧ i=prj2(STATES*SYMBOLS,STATES)(cur(c))};\n",
" ANIMATION_STR_JUSTIFY_LEFT == TRUE;\n",
" SET_PREF_PP_SEQUENCES == TRUE;\n",
" PDA_STATES == (STATES-{symbol});\n",
" PDA_Zustände == (Z-{symbol});\n",
"\n",
" SYMS(s) == IF (s=lambda) THEN [] ELSE [SYM(s)] END;\n",
" SYM(s) == (symbol,s,symbol);\n",
" TERMINALS == {x|∃t.(t∈Σ ∧ x=SYM(t))}\n",
"CONSTANTS delta, Productions\n",
" SYM(s) == (symbol,s,symbol); // Darstellung eines Symbols als Tripel für die Grammatik\n",
" kfG_TERMINALE == {x|∃t.(t∈Σ ∧ x=SYM(t))};\n",
" \n",
" ANIMATION_FUNCTION1 == {r,c,i| r=1 ∧ c∈dom(cur) ∧ i=prj1(Z,SYMBOLE)(prj1(Z*SYMBOLE,Z)(cur(c)))};\n",
" ANIMATION_FUNCTION2 == {r,c,i| r=2 ∧ c∈dom(cur) ∧ i=prj2(Z,SYMBOLE)(prj1(Z*SYMBOLE,Z)(cur(c)))};\n",
" ANIMATION_FUNCTION3 == {r,c,i| r=3 ∧ c∈dom(cur) ∧ i=prj2(Z*SYMBOLE,Z)(cur(c))};\n",
" ANIMATION_STR_JUSTIFY_LEFT == TRUE;\n",
" SET_PREF_PP_SEQUENCES == TRUE\n",
"CONSTANTS δ, Regeln\n",
"PROPERTIES\n",
" /*
A
PDA
accepting
{a^mb^m| m≥1} ; Beispiel von Info 4 (Folie 95ff) */\n",
"
delta = {
(z0,a,BOT) ↦ (z0,[A,BOT]),\n",
" /*
Ein
PDA
für
{a^m
b^m| m≥1} ; Beispiel von Info 4 (Folie 95ff) */\n",
"
δ = {
(z0,a,BOT) ↦ (z0,[A,BOT]),\n",
" (z0,a,A) ↦ (z0,[A,A]),\n",
" (z0,b,A) ↦ (z1,[]),\n",
" (z1,lambda,BOT) ↦ (z1,[]),\n",
" (z1,b,A) ↦ (z1,[]) } ∧\n",
"\n",
"\n",
"
Productions
= /* Punkt 1 Folie 109 */\n",
" { lhs,rhs | ∃z.(z∈PDA_
STATES
∧ lhs=SYM(S) ∧ rhs = [(z0,BOT,z)])}\n",
"
Regeln
= /* Punkt 1 Folie 109 */\n",
" { lhs,rhs | ∃z.(z∈PDA_
Zustände
∧ lhs=SYM(S) ∧ rhs = [(z0,BOT,z)])}\n",
" ∪\n",
" /* Punkt 2 Folie 109 */\n",
" { lhs,rhs | ∃(z,a,A,z2).((z,a,A)↦(z2,[])∈
delta
∧\n",
" { lhs,rhs | ∃(z,a,A,z2).((z,a,A)↦(z2,[])∈
δ
∧\n",
" lhs=(z,A,z2) ∧ rhs = SYMS(a)) }\n",
" ∪\n",
" /* Punkt 3 Folie 110 */\n",
" { lhs,rhs | ∃(z,a,A,B,z1,z2).((z,a,A)↦(z1,[B])∈
delta
∧ z2∈PDA_
STATES
∧\n",
" { lhs,rhs | ∃(z,a,A,B,z1,z2).((z,a,A)↦(z1,[B])∈
δ
∧ z2∈PDA_
Zustände
∧\n",
" lhs=(z,A,z2) ∧ rhs = SYMS(a)^[(z1,B,z2)]) }\n",
" ∪\n",
" /* Punkt 4 Folie 110 */\n",
" { lhs,rhs | ∃(z,a,A,B,C,z1,z2,z3).((z,a,A)↦(z1,[B,C])∈delta ∧ \n",
" z2∈PDA_STATES ∧ z3∈PDA_STATES ∧\n",
" lhs=(z,A,z3) ∧ rhs = SYMS(a)^[(z1,B,z2),(z2,C,z3)]) }\n",
" { lhs,rhs | ∃(z,a,A,B,C,z1,z2,z3).((z,a,A)↦(z1,[B,C])∈δ ∧ \n",
" z2∈PDA_Zustände ∧ \n",
" z3∈PDA_Zustände ∧\n",
" lhs=(z,A,z3) ∧ \n",
" rhs = SYMS(a)^[(z1,B,z2),(z2,C,z3)]) }\n",
"VARIABLES cur\n",
"INVARIANT\n",
" cur ∈ seq(CFG_Alphabet)\n",
"INITIALISATION cur:=SYMS(S)\n",
" cur ∈ seq(kfG_Alphabet)\n",
"INITIALISATION\n",
" cur:=SYMS(S)\n",
"OPERATIONS\n",
" ApplyRule(LHS,RHS,pre,post) = PRE LHS↦RHS ∈ Productions ∧\n",
" // Anwendung einer Grammatikregel\n",
" ApplyRule(LHS,RHS,pre,post) = PRE LHS↦RHS ∈ Regeln ∧\n",
" cur = pre^[LHS]^post ∧\n",
" ran(pre) ⊆ TERMINAL
S
/* Links Ableitung */\n",
" ran(pre) ⊆
kfG_
TERMINAL
E
/* Links Ableitung */\n",
" THEN\n",
" cur := pre^RHS^post\n",
" END\n",
...
...
@@ -86,7 +91,7 @@
},
{
"cell_type": "code",
"execution_count":
17
,
"execution_count":
42
,
"metadata": {},
"outputs": [
{
...
...
@@ -95,7 +100,7 @@
"Machine constants set up using operation 0: $setup_constants()"
]
},
"execution_count":
17
,
"execution_count":
42
,
"metadata": {},
"output_type": "execute_result"
}
...
...
@@ -106,7 +111,7 @@
},
{
"cell_type": "code",
"execution_count":
18
,
"execution_count":
43
,
"metadata": {},
"outputs": [
{
...
...
@@ -115,7 +120,7 @@
"Machine initialised using operation 1: $initialise_machine()"
]
},
"execution_count":
18
,
"execution_count":
43
,
"metadata": {},
"output_type": "execute_result"
}
...
...
@@ -126,7 +131,7 @@
},
{
"cell_type": "code",
"execution_count":
37
,
"execution_count":
45
,
"metadata": {},
"outputs": [
{
...
...
@@ -138,18 +143,18 @@
"{(z0↦a↦A↦(z0↦[A,A])),(z0↦a↦BOT↦(z0↦[A,BOT])),(z0↦b↦A↦(z1↦[])),(z1↦b↦A↦(z1↦[])),(z1↦lambda↦BOT↦(z1↦[]))}"
]
},
"execution_count":
37
,
"execution_count":
45
,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"
delta
"
"
δ
"
]
},
{
"cell_type": "code",
"execution_count":
19
,
"execution_count":
46
,
"metadata": {},
"outputs": [
{
...
...
@@ -171,7 +176,7 @@
"<Animation function visualisation>"
]
},
"execution_count":
19
,
"execution_count":
46
,
"metadata": {},
"output_type": "execute_result"
}
...
...
%% Cell type:markdown id: tags:
# PDA nach kfG
%% Cell type:code id: tags:
```
prob
::load
MACHINE PDA_to_CFG
/* Translating a PDA to a CFG */
SETS
STATES
= {z0,z1, symbol};
Z
= {z0,z1, symbol};
/* symbol: virtueller Zustand um S und andere Symbole in der Grammatik darzustellen */
SYMBOL
S
={a,b, A, BOT, lambda, S} /* BOT = # =
bottom of stack
*/
SYMBOL
E
={a,b, A, BOT, lambda, S} /* BOT = # =
Ende vom Keller
*/
DEFINITIONS
CF
G_Alphabet == (
STATES
*(SYMBOL
S-
{lambda})*
STATES
);
kf
G_Alphabet == (
Z
*(SYMBOL
E \
{lambda})*
Z
);
Σ == {a,b};
Γ == {A,BOT};
ANIMATION_FUNCTION1 == {r,c,i| r=1 ∧ c∈dom(cur) ∧ i=prj1(STATES,SYMBOLS)(prj1(STATES*SYMBOLS,STATES)(cur(c)))};
ANIMATION_FUNCTION2 == {r,c,i| r=2 ∧ c∈dom(cur) ∧ i=prj2(STATES,SYMBOLS)(prj1(STATES*SYMBOLS,STATES)(cur(c)))};
ANIMATION_FUNCTION3 == {r,c,i| r=3 ∧ c∈dom(cur) ∧ i=prj2(STATES*SYMBOLS,STATES)(cur(c))};
ANIMATION_STR_JUSTIFY_LEFT == TRUE;
SET_PREF_PP_SEQUENCES == TRUE;
PDA_STATES == (STATES-{symbol});
PDA_Zustände == (Z-{symbol});
SYMS(s) == IF (s=lambda) THEN [] ELSE [SYM(s)] END;
SYM(s) == (symbol,s,symbol);
TERMINALS == {x|∃t.(t∈Σ ∧ x=SYM(t))}
CONSTANTS delta, Productions
SYM(s) == (symbol,s,symbol); // Darstellung eines Symbols als Tripel für die Grammatik
kfG_TERMINALE == {x|∃t.(t∈Σ ∧ x=SYM(t))};
ANIMATION_FUNCTION1 == {r,c,i| r=1 ∧ c∈dom(cur) ∧ i=prj1(Z,SYMBOLE)(prj1(Z*SYMBOLE,Z)(cur(c)))};
ANIMATION_FUNCTION2 == {r,c,i| r=2 ∧ c∈dom(cur) ∧ i=prj2(Z,SYMBOLE)(prj1(Z*SYMBOLE,Z)(cur(c)))};
ANIMATION_FUNCTION3 == {r,c,i| r=3 ∧ c∈dom(cur) ∧ i=prj2(Z*SYMBOLE,Z)(cur(c))};
ANIMATION_STR_JUSTIFY_LEFT == TRUE;
SET_PREF_PP_SEQUENCES == TRUE
CONSTANTS δ, Regeln
PROPERTIES
/*
A
PDA
accepting
{a^mb^m| m≥1} ; Beispiel von Info 4 (Folie 95ff) */
delta = {
(z0,a,BOT) ↦ (z0,[A,BOT]),
/*
Ein
PDA
für
{a^m
b^m| m≥1} ; Beispiel von Info 4 (Folie 95ff) */
δ = {
(z0,a,BOT) ↦ (z0,[A,BOT]),
(z0,a,A) ↦ (z0,[A,A]),
(z0,b,A) ↦ (z1,[]),
(z1,lambda,BOT) ↦ (z1,[]),
(z1,b,A) ↦ (z1,[]) } ∧
Productions
= /* Punkt 1 Folie 109 */
{ lhs,rhs | ∃z.(z∈PDA_
STATES
∧ lhs=SYM(S) ∧ rhs = [(z0,BOT,z)])}
Regeln
= /* Punkt 1 Folie 109 */
{ lhs,rhs | ∃z.(z∈PDA_
Zustände
∧ lhs=SYM(S) ∧ rhs = [(z0,BOT,z)])}
∪
/* Punkt 2 Folie 109 */
{ lhs,rhs | ∃(z,a,A,z2).((z,a,A)↦(z2,[])∈
delta
∧
{ lhs,rhs | ∃(z,a,A,z2).((z,a,A)↦(z2,[])∈
δ
∧
lhs=(z,A,z2) ∧ rhs = SYMS(a)) }
∪
/* Punkt 3 Folie 110 */
{ lhs,rhs | ∃(z,a,A,B,z1,z2).((z,a,A)↦(z1,[B])∈
delta
∧ z2∈PDA_
STATES
∧
{ lhs,rhs | ∃(z,a,A,B,z1,z2).((z,a,A)↦(z1,[B])∈
δ
∧ z2∈PDA_
Zustände
∧
lhs=(z,A,z2) ∧ rhs = SYMS(a)^[(z1,B,z2)]) }
∪
/* Punkt 4 Folie 110 */
{ lhs,rhs | ∃(z,a,A,B,C,z1,z2,z3).((z,a,A)↦(z1,[B,C])∈delta ∧
z2∈PDA_STATES ∧ z3∈PDA_STATES ∧
lhs=(z,A,z3) ∧ rhs = SYMS(a)^[(z1,B,z2),(z2,C,z3)]) }
{ lhs,rhs | ∃(z,a,A,B,C,z1,z2,z3).((z,a,A)↦(z1,[B,C])∈δ ∧
z2∈PDA_Zustände ∧
z3∈PDA_Zustände ∧
lhs=(z,A,z3) ∧
rhs = SYMS(a)^[(z1,B,z2),(z2,C,z3)]) }
VARIABLES cur
INVARIANT
cur ∈ seq(CFG_Alphabet)
INITIALISATION cur:=SYMS(S)
cur ∈ seq(kfG_Alphabet)
INITIALISATION
cur:=SYMS(S)
OPERATIONS
ApplyRule(LHS,RHS,pre,post) = PRE LHS↦RHS ∈ Productions ∧
// Anwendung einer Grammatikregel
ApplyRule(LHS,RHS,pre,post) = PRE LHS↦RHS ∈ Regeln ∧
cur = pre^[LHS]^post ∧
ran(pre) ⊆ TERMINAL
S
/* Links Ableitung */
ran(pre) ⊆
kfG_
TERMINAL
E
/* Links Ableitung */
THEN
cur := pre^RHS^post
END
END
```
%% Output
Loaded machine: PDA_to_CFG
%% Cell type:code id: tags:
```
prob
:constants
```
%% Output
Machine constants set up using operation 0: $setup_constants()
%% Cell type:code id: tags:
```
prob
:init
```
%% Output
Machine initialised using operation 1: $initialise_machine()
%% Cell type:code id: tags:
```
prob
delta
δ
```
%% Output
$\{(\mathit{z0}\mapsto \mathit{a}\mapsto \mathit{A}\mapsto(\mathit{z0}\mapsto [A,A])),(\mathit{z0}\mapsto \mathit{a}\mapsto \mathit{BOT}\mapsto(\mathit{z0}\mapsto [A,BOT])),(\mathit{z0}\mapsto \mathit{b}\mapsto \mathit{A}\mapsto(\mathit{z1}\mapsto [])),(\mathit{z1}\mapsto \mathit{b}\mapsto \mathit{A}\mapsto(\mathit{z1}\mapsto [])),(\mathit{z1}\mapsto \mathit{lambda}\mapsto \mathit{BOT}\mapsto(\mathit{z1}\mapsto []))\}$
{(z0↦a↦A↦(z0↦[A,A])),(z0↦a↦BOT↦(z0↦[A,BOT])),(z0↦b↦A↦(z1↦[])),(z1↦b↦A↦(z1↦[])),(z1↦lambda↦BOT↦(z1↦[]))}
%% Cell type:code id: tags:
```
prob
:show
```
%% Output
<table style="font-family:monospace"><tbody>
<tr>
<td style="padding:10px">symbol</td>
</tr>
<tr>
<td style="padding:10px">S</td>
</tr>
<tr>
<td style="padding:10px">symbol</td>
</tr>
</tbody></table>
<Animation function visualisation>
%% Cell type:code id: tags:
```
prob
:browse
```
%% Output
Machine: PDA_to_CFG
Sets: STATES, SYMBOLS
Constants: delta, Productions
Variables: cur
Operations:
ApplyRule((symbol|->S|->symbol),[(z0|->BOT|->z0)],[],[])
ApplyRule((symbol|->S|->symbol),[(z0|->BOT|->z1)],[],[])
%% Cell type:code id: tags:
```
prob
:exec ApplyRule RHS = [(z0|->BOT|->z1)]
```
%% Output
Executed operation: ApplyRule((symbol|->S|->symbol),[(z0|->BOT|->z1)],[],[])
%% Cell type:code id: tags:
```
prob
:show
```
%% Output
<table style="font-family:monospace"><tbody>
<tr>
<td style="padding:10px">z0</td>
</tr>
<tr>
<td style="padding:10px">BOT</td>
</tr>
<tr>
<td style="padding:10px">z1</td>
</tr>
</tbody></table>
<Animation function visualisation>
%% Cell type:code id: tags:
```
prob
:browse
```
%% Output
Machine: PDA_to_CFG
Sets: STATES, SYMBOLS
Constants: delta, Productions
Variables: cur
Operations:
ApplyRule((z0|->BOT|->z1),[(symbol|->a|->symbol),(z0|->A|->z0),(z0|->BOT|->z1)],[],[])
ApplyRule((z0|->BOT|->z1),[(symbol|->a|->symbol),(z0|->A|->z1),(z1|->BOT|->z1)],[],[])
%% Cell type:code id: tags:
```
prob
:exec ApplyRule RHS = [(symbol|->a|->symbol),(z0|->A|->z1),(z1|->BOT|->z1)]
```
%% Output
Executed operation: ApplyRule((z0|->BOT|->z1),[(symbol|->a|->symbol),(z0|->A|->z1),(z1|->BOT|->z1)],[],[])
%% Cell type:code id: tags:
```
prob
:show
```
%% Output
<table style="font-family:monospace"><tbody>
<tr>
<td style="padding:10px">symbol</td>
<td style="padding:10px">z0</td>
<td style="padding:10px">z1</td>
</tr>
<tr>
<td style="padding:10px">a</td>
<td style="padding:10px">A</td>
<td style="padding:10px">BOT</td>
</tr>
<tr>
<td style="padding:10px">symbol</td>
<td style="padding:10px">z1</td>
<td style="padding:10px">z1</td>
</tr>
</tbody></table>
<Animation function visualisation>
%% Cell type:code id: tags:
```
prob
:browse
```
%% Output
Machine: PDA_to_CFG
Sets: STATES, SYMBOLS
Constants: delta, Productions
Variables: cur
Operations:
ApplyRule((z0|->A|->z1),[(symbol|->b|->symbol)],[(symbol|->a|->symbol)],[(z1|->BOT|->z1)])
ApplyRule((z0|->A|->z1),[(symbol|->a|->symbol),(z0|->A|->z0),(z0|->A|->z1)],[(symbol|->a|->symbol)],[(z1|->BOT|->z1)])
ApplyRule((z0|->A|->z1),[(symbol|->a|->symbol),(z0|->A|->z1),(z1|->A|->z1)],[(symbol|->a|->symbol)],[(z1|->BOT|->z1)])
%% Cell type:code id: tags:
```
prob
:exec ApplyRule RHS=[(symbol|->b|->symbol)]
```
%% Output
Executed operation: ApplyRule((z0|->A|->z1),[(symbol|->b|->symbol)],[(symbol|->a|->symbol)],[(z1|->BOT|->z1)])
%% Cell type:code id: tags:
```
prob
:show
```
%% Output
<table style="font-family:monospace"><tbody>
<tr>
<td style="padding:10px">symbol</td>
<td style="padding:10px">symbol</td>
<td style="padding:10px">z1</td>
</tr>
<tr>
<td style="padding:10px">a</td>
<td style="padding:10px">b</td>
<td style="padding:10px">BOT</td>
</tr>
<tr>
<td style="padding:10px">symbol</td>
<td style="padding:10px">symbol</td>
<td style="padding:10px">z1</td>
</tr>
</tbody></table>
<Animation function visualisation>
%% Cell type:code id: tags:
```
prob
:browse
```
%% Output
Machine: PDA_to_CFG
Sets: STATES, SYMBOLS
Constants: delta, Productions
Variables: cur
Operations:
ApplyRule((z1|->BOT|->z1),[],[(symbol|->a|->symbol),(symbol|->b|->symbol)],[])
%% Cell type:code id: tags:
```
prob
:exec ApplyRule
```
%% Output
Executed operation: ApplyRule((z1|->BOT|->z1),[],[(symbol|->a|->symbol),(symbol|->b|->symbol)],[])
%% Cell type:code id: tags:
```
prob
:show
```
%% Output
<table style="font-family:monospace"><tbody>
<tr>
<td style="padding:10px">symbol</td>
<td style="padding:10px">symbol</td>
</tr>
<tr>
<td style="padding:10px">a</td>
<td style="padding:10px">b</td>
</tr>
<tr>
<td style="padding:10px">symbol</td>
<td style="padding:10px">symbol</td>
</tr>
</tbody></table>
<Animation function visualisation>
%% Cell type:code id: tags:
```
prob
:browse
```
%% Output
Machine: PDA_to_CFG
Sets: STATES, SYMBOLS
Constants: delta, Productions
Variables: cur
Operations:
%% Cell type:code id: tags:
```
prob
```
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment