Skip to content
Snippets Groups Projects
Commit 31390aa9 authored by Michael Leuschel's avatar Michael Leuschel
Browse files

add Notebook for Grammars

parent 3c4c7517
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id: tags:
## Grammatiken
Dieses Notebook begleitet Teil 2 der Vorlesung 3.
%% Cell type:markdown id: tags:
Eine __Grammatik__ ist ein Quadrupel
$G = (\Sigma , N, S, P)$, wobei
* $\Sigma$ ein Alphabet (von so genannten __Terminalsymbolen__) ist,
* $N$ eine endliche Menge (von so genannten __Nichtterminalen__) mit
$\Sigma \cap N = \emptyset$,
* $S \in N$ das __Startsymbol__ und
* $P \subseteq (N \cup \Sigma)^+ \times (N \cup \Sigma)^{\ast}$ die
endliche Menge der __Produktionen__ (Regeln).
Diese mathematische Definition setzen wir im Notebook folgendermaßen um.
Wir verwenden eine B Maschine um eine neue Basismenge an Symbolen (ΣN) einführen zu können.
Hier steht ```seq1(T)``` für die nicht leeren Folgen über T, also $T^+$ in der Notation des Skripts.
%% Cell type:code id: tags:
``` prob
::load
MACHINE Grammatik
SETS ΣN = {a,b,c, S}
CONSTANTS Σ, N, P
PROPERTIES
Σ = {a,b,c} ∧
Σ ∩ N = {} ∧
Σ ∪ N = ΣN ∧
S ∈ N ∧
P ⊆ seq1(ΣN) × seq(ΣN) ∧
// Die Beispiel Grammatik G1 von den Folien
P = { [S] ↦ [],
[S] ↦ [a,S,b]
}
DEFINITIONS SET_PREF_PP_SEQUENCES == TRUE
END
```
%% Output
Loaded machine: Grammatik
%% Cell type:code id: tags:
``` prob
:constants
```
%% Output
Machine constants set up using operation 0: $setup_constants()
%% Cell type:code id: tags:
``` prob
[S] ↦ RHS ∈ P
```
%% Output
$\newcommand{\qdot}{\mathord{\mkern1mu\cdot\mkern1mu}}\newcommand{\expn}{\mathbin{\widehat{\mkern1em}}}\newcommand{\expn}{\mathbin{\widehat{\mkern1em}}}\newcommand{\expn}{\mathbin{\widehat{\mkern1em}}}\newcommand{\expn}{\mathbin{\widehat{\mkern1em}}}\mathit{TRUE}$
**Solution:**
* $\mathit{abl} = /*@symbolic*/ \{\mathit{u},\mathit{v}\mid\exists(\mathit{x},\mathit{z},\mathit{p},\mathit{q})\qdot(\mathit{p} \mapsto \mathit{q} \in \{([S]\mapsto []),([S]\mapsto [a,\mathit{S},b])\} \land \mathit{u} = \mathit{x} \expn \mathit{p} \expn \mathit{z} \land \mathit{v} = \mathit{x} \expn \mathit{q} \expn \mathit{z})\}$
* $\mathit{RHS} = []$
TRUE
Solution:
abl = /*@symbolic*/ {u,v∣∃(x,z,p,q)·(p ↦ q ∈ {([S]↦[]),([S]↦[a,S,b])} ∧ u = x ^ p ^ z ∧ v = x ^ q ^ z)}
RHS = []
%% Cell type:markdown id: tags:
Mit dem relationalen Bild können wir alle Regeln mit der Folge S auf der rechten Seite finden:
%% Cell type:code id: tags:
``` prob
P[{[S]}]
```
%% Output
$\{[],[a,\mathit{S},b]\}$
{[],[a,S,b]}
%% Cell type:markdown id: tags:
Definiere die __unmittelbare Ableitungsrelation__ bzgl. $G$ so
* $u \vdash_{G} v \Longleftrightarrow u = xpz, v = xqz$,
wobei $x,z \in (N \cup \Sigma)^{\ast}$ und $p \rightarrow q$ eine Regel in $P$
ist.
Im Notebook können wir dies so umsetzen, können aber leider nicht das Symbol $\vdash_G$ verwenden.
Aus $u \vdash_{G} v$ wird $u \mapsto v \in abl$.
%% Cell type:code id: tags:
``` prob
:let abl {u,v | ∃(x,z,p,q).( p↦q ∈ P ∧ u = x^p^z ∧ v = x^q^z )}
```
%% Output
$\newcommand{\qdot}{\mathord{\mkern1mu\cdot\mkern1mu}}/*@symbolic*/ \{\mathit{u},\mathit{v}\mid\exists(\mathit{x},\mathit{z},\mathit{p},\mathit{q})\qdot(\mathit{p} \mapsto \mathit{q} \in \{([S]\mapsto []),([S]\mapsto [a,\mathit{S},b])\} \land \mathit{u} = \mathit{x} ⌒ \mathit{p} ⌒ \mathit{z} \land \mathit{v} = \mathit{x} ⌒ \mathit{q} ⌒ \mathit{z})\}$
/*@symbolic*/ {u,v∣∃(x,z,p,q)·(p ↦ q ∈ {([S]↦[]),([S]↦[a,S,b])} ∧ u = x ⌒ p ⌒ z ∧ v = x ⌒ q ⌒ z)}
%% Cell type:markdown id: tags:
Da diese Relation unendlich ist wird diese im Notebook symbolisch gehalten. Wir können aber prüfen ob Paare an Folgen in der Relation sind:
%% Cell type:code id: tags:
``` prob
[S] ↦ [a,S,b] ∈ abl
```
%% Output
$\newcommand{\qdot}{\mathord{\mkern1mu\cdot\mkern1mu}}\newcommand{\expn}{\mathbin{\widehat{\mkern1em}}}\newcommand{\expn}{\mathbin{\widehat{\mkern1em}}}\newcommand{\expn}{\mathbin{\widehat{\mkern1em}}}\newcommand{\expn}{\mathbin{\widehat{\mkern1em}}}\mathit{TRUE}$
**Solution:**
* $\mathit{abl} = /*@symbolic*/ \{\mathit{u},\mathit{v}\mid\exists(\mathit{x},\mathit{z},\mathit{p},\mathit{q})\qdot(\mathit{p} \mapsto \mathit{q} \in \{([S]\mapsto []),([S]\mapsto [a,\mathit{S},b])\} \land \mathit{u} = \mathit{x} \expn \mathit{p} \expn \mathit{z} \land \mathit{v} = \mathit{x} \expn \mathit{q} \expn \mathit{z})\}$
TRUE
Solution:
abl = /*@symbolic*/ {u,v∣∃(x,z,p,q)·(p ↦ q ∈ {([S]↦[]),([S]↦[a,S,b])} ∧ u = x ^ p ^ z ∧ v = x ^ q ^ z)}
%% Cell type:code id: tags:
``` prob
[S] ↦ [a,b] ∈ abl
```
%% Output
$\mathit{FALSE}$
FALSE
%% Cell type:code id: tags:
``` prob
[S] ↦ x : abl
```
%% Output
$\newcommand{\qdot}{\mathord{\mkern1mu\cdot\mkern1mu}}\newcommand{\expn}{\mathbin{\widehat{\mkern1em}}}\newcommand{\expn}{\mathbin{\widehat{\mkern1em}}}\newcommand{\expn}{\mathbin{\widehat{\mkern1em}}}\newcommand{\expn}{\mathbin{\widehat{\mkern1em}}}\mathit{TRUE}$
**Solution:**
* $\mathit{x} = []$
* $\mathit{abl} = /*@symbolic*/ \{\mathit{u},\mathit{v}\mid\exists(\mathit{x},\mathit{z},\mathit{p},\mathit{q})\qdot(\mathit{p} \mapsto \mathit{q} \in \{([S]\mapsto []),([S]\mapsto [a,\mathit{S},b])\} \land \mathit{u} = \mathit{x} \expn \mathit{p} \expn \mathit{z} \land \mathit{v} = \mathit{x} \expn \mathit{q} \expn \mathit{z})\}$
TRUE
Solution:
x = []
abl = /*@symbolic*/ {u,v∣∃(x,z,p,q)·(p ↦ q ∈ {([S]↦[]),([S]↦[a,S,b])} ∧ u = x ^ p ^ z ∧ v = x ^ q ^ z)}
%% Cell type:markdown id: tags:
Mit dem relationalen Abbild können wir die Menge aller Umschreibungen berrechnen:
%% Cell type:code id: tags:
``` prob
abl[ {[S]} ]
```
%% Output
$\{[],[a,\mathit{S},b]\}$
{[],[a,S,b]}
%% Cell type:code id: tags:
``` prob
abl[ {[a,S,b]} ]
```
%% Output
$\{[a,b],[a,\mathit{a},\mathit{S},\mathit{b},b]\}$
{[a,b],[a,a,S,b,b]}
%% Cell type:markdown id: tags:
Durch mehrfache Anwendung bekommen wir die Ergebnisse von immer längeren Ableitungen:
%% Cell type:code id: tags:
``` prob
abl[ abl[ {[S]} ] ]
```
%% Output
$\{[a,b],[a,\mathit{a},\mathit{S},\mathit{b},b]\}$
{[a,b],[a,a,S,b,b]}
%% Cell type:code id: tags:
``` prob
abl[ abl[ abl[ {[S]} ] ] ]
```
%% Output
$\{[a,\mathit{a},\mathit{b},b],[a,\mathit{a},\mathit{a},\mathit{S},\mathit{b},\mathit{b},b]\}$
{[a,a,b,b],[a,a,a,S,b,b,b]}
%% Cell type:markdown id: tags:
Durch $n$-malige Anwendung von $\vdash_{G}$ erhalten wir
$\vdash_{G}^{n}$. Das heiß t:
* $u \vdash_{G}^{n} v \Longleftrightarrow u = x_0 \vdash_{G} x_1
\vdash_{G} \cdots \vdash_{G} x_n = v$
Im Notebook kann man für die n-malige Anwendung einer binären Relation den Operator ```iterate``` verwenden:
%% Cell type:code id: tags:
``` prob
[S] ↦ [a,b] ∈ iterate(abl,2)
```
%% Output
$\newcommand{\qdot}{\mathord{\mkern1mu\cdot\mkern1mu}}\newcommand{\expn}{\mathbin{\widehat{\mkern1em}}}\newcommand{\expn}{\mathbin{\widehat{\mkern1em}}}\newcommand{\expn}{\mathbin{\widehat{\mkern1em}}}\newcommand{\expn}{\mathbin{\widehat{\mkern1em}}}\mathit{TRUE}$
**Solution:**
* $\mathit{abl} = /*@symbolic*/ \{\mathit{u},\mathit{v}\mid\exists(\mathit{x},\mathit{z},\mathit{p},\mathit{q})\qdot(\mathit{p} \mapsto \mathit{q} \in \{([S]\mapsto []),([S]\mapsto [a,\mathit{S},b])\} \land \mathit{u} = \mathit{x} \expn \mathit{p} \expn \mathit{z} \land \mathit{v} = \mathit{x} \expn \mathit{q} \expn \mathit{z})\}$
TRUE
Solution:
abl = /*@symbolic*/ {u,v∣∃(x,z,p,q)·(p ↦ q ∈ {([S]↦[]),([S]↦[a,S,b])} ∧ u = x ^ p ^ z ∧ v = x ^ q ^ z)}
%% Cell type:markdown id: tags:
Die Menge $\{w \mid S \vdash_{G}^{n} w\}$ kann direkt mit dem relationalen Bild berechnet werden. Zum Beispiel für n = 0,1,2,3,4:
%% Cell type:code id: tags:
``` prob
iterate(abl,0)[ {[S]} ]
```
%% Output
$\{[S]\}$
{[S]}
%% Cell type:code id: tags:
``` prob
iterate(abl,1)[ {[S]} ]
```
%% Output
$\{[],[a,\mathit{S},b]\}$
{[],[a,S,b]}
%% Cell type:code id: tags:
``` prob
iterate(abl,2)[ {[S]} ]
```
%% Output
$\{[a,b],[a,\mathit{a},\mathit{S},\mathit{b},b]\}$
{[a,b],[a,a,S,b,b]}
%% Cell type:code id: tags:
``` prob
iterate(abl,3)[ {[S]} ]
```
%% Output
$\{[a,\mathit{a},\mathit{b},b],[a,\mathit{a},\mathit{a},\mathit{S},\mathit{b},\mathit{b},b]\}$
{[a,a,b,b],[a,a,a,S,b,b,b]}
%% Cell type:code id: tags:
``` prob
iterate(abl,4)[ {[S]} ]
```
%% Output
$\{[a,\mathit{a},\mathit{a},\mathit{b},\mathit{b},b],[a,\mathit{a},\mathit{a},\mathit{a},\mathit{S},\mathit{b},\mathit{b},\mathit{b},b]\}$
{[a,a,a,b,b,b],[a,a,a,a,S,b,b,b,b]}
%% Cell type:markdown id: tags:
Die von der Grammatik $G$ erzeugte Sprache ist
definiert als
$L(G) = \{w \in \Sigma^* \mid S \vdash_{G}^{\ast} w\}.$
Bis zur Länge 4 gibt es folgende abgeleitenen Wörter über $\Sigma \cup N$
(auch Satzformen genannt):
%% Cell type:code id: tags:
``` prob
UNION(i).(i:0..4|iterate(abl,i)[ {[S]} ])
```
%% Output
$\{[],[a,b],[S],[a,\mathit{a},\mathit{b},b],[a,\mathit{S},b],[a,\mathit{a},\mathit{a},\mathit{b},\mathit{b},b],[a,\mathit{a},\mathit{S},\mathit{b},b],[a,\mathit{a},\mathit{a},\mathit{S},\mathit{b},\mathit{b},b],[a,\mathit{a},\mathit{a},\mathit{a},\mathit{S},\mathit{b},\mathit{b},\mathit{b},b]\}$
{[],[a,b],[S],[a,a,b,b],[a,S,b],[a,a,a,b,b,b],[a,a,S,b,b],[a,a,a,S,b,b,b],[a,a,a,a,S,b,b,b,b]}
%% Cell type:markdown id: tags:
Wenn wir mit $\Sigma^*$ schneiden bekommen wir Wörter der von der Grammatik generierten Sprache:
%% Cell type:code id: tags:
``` prob
UNION(i).(i:0..4|iterate(abl,i)[ {[S]} ]) /\ seq(Σ)
```
%% Output
$\{[],[a,b],[a,\mathit{a},\mathit{b},b],[a,\mathit{a},\mathit{a},\mathit{b},\mathit{b},b]\}$
{[],[a,b],[a,a,b,b],[a,a,a,b,b,b]}
%% Cell type:code id: tags:
``` prob
::load
MACHINE Grammatik
SETS ΣN = {a,b,c, S}
CONSTANTS Σ, N, P
ABSTRACT_CONSTANTS abl
PROPERTIES
Σ = {a,b,c} ∧
Σ ∩ N = {} ∧
Σ ∪ N = ΣN ∧
S ∈ N ∧
P ⊆ seq1(ΣN) × seq(ΣN) ∧
// Die Beispiel Grammatik G1 von den Folien
P = { [S] ↦ [],
[S] ↦ [a,S,b]
}
abl = {u,v | ∃(x,z,p,q).( p↦q ∈ P ∧ u = x^p^z ∧ v = x^q^z )}
DEFINITIONS SET_PREF_PP_SEQUENCES == TRUE
END
```
%% Output
Loaded machine: Grammatik
%% Cell type:code id: tags:
``` prob
:constants
```
%% Output
Machine constants set up using operation 0: $setup_constants()
%% Cell type:markdown id: tags:
Wir können die Ableitungsrelationen für alle Satzformen bis zu einer gegebenen Tiefe auch grafisch darstellen.
%% Cell type:code id: tags:
``` prob
:let SF UNION(i).(i:0..4|iterate(abl,i)[ {[S]} ])
```
%% Output
$\{[],[a,b],[S],[a,\mathit{a},\mathit{b},b],[a,\mathit{S},b],[a,\mathit{a},\mathit{a},\mathit{b},\mathit{b},b],[a,\mathit{a},\mathit{S},\mathit{b},b],[a,\mathit{a},\mathit{a},\mathit{S},\mathit{b},\mathit{b},b],[a,\mathit{a},\mathit{a},\mathit{a},\mathit{S},\mathit{b},\mathit{b},\mathit{b},b]\}$
{[],[a,b],[S],[a,a,b,b],[a,S,b],[a,a,a,b,b,b],[a,a,S,b,b],[a,a,a,S,b,b,b],[a,a,a,a,S,b,b,b,b]}
%% Cell type:code id: tags:
``` prob
:pref DOT_DECOMPOSE_NODES=FALSE
```
%% Output
Preference changed: DOT_DECOMPOSE_NODES = FALSE
%% Cell type:code id: tags:
``` prob
:dot expr_as_graph ("abl",SF <| abl |> SF)
```
%% Output
<Dot visualization: expr_as_graph [SFablSF={[],[a,b],[S],[a,a,b,b],[a,S,b],[a,a,a,b,b,b],[a,a,S,b,b],[a,a,a,S,b,b,b],[a,a,a,a,S,b,b,b,b]} & abl=uv#x,z,p,q.((p,q):{([S],[]),([S],[a,S,b])} & u=x^p^z & v=x^q^z)("abl",SF<|abl|>SF)]>
%% Cell type:code id: tags:
``` prob
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment