Skip to content
Snippets Groups Projects
Commit d5d1fc95 authored by Chris's avatar Chris
Browse files

Notebook zu LOOP-Programmen

parent 401ed8f9
Branches
No related tags found
1 merge request!1Master
%% Cell type:markdown id: tags:
# LOOP-Programme
Im Folgenden werden wir Konzepte von prozeduralen Programmiersprachen kennenlernen. Als erste einfache Programmiersprache betrachten wir LOOP.
Die Syntax solcher Programme ist definiert durch:
* $x_i := c$, $x_i := x_j + c$ und $x_i := x_j - c$ mit $i,j,c \in \mathbb{N}$ sind LOOP-Programme
* Falls $P_1$ und $P_2$ LOOP-Programme sind ist $P_1; P_2$ ein LOOP-Programm
* Falls $P$ ein LOOP-Programm ist und $x_i$ nicht in $P$ vorkommt, so ist auch $LOOP\ x_i\ DO\ P\ END$ ein LOOP-Programm.
%% Cell type:markdown id: tags:
Die Semantik von LOOP-Programmen lässt sich wie folgt verstehen:
* Wenn eine k-stellige Funktion $f(n_1,n_2,...,n_k)$ berechnet wird sind die Startwerte $n_1,...,n_k\in\mathbb{N}$ in den Variablen $x_1,...,x_k$.
* Die restlichen Variablen starten mit dem Wert 0.
* Zuweisungen $x_i := c$, $x_i := x_j + c$ werden wie üblich interpretiert. Bei $x_i := x_j - c$ wird $x_i$ auf 0 gesetzt, falls $c\geq x_j$ ist.
* Bei $P_1; P_2$ wird erst $P_1$ und dann $P_2$ ausgeführt.
* Das Programm $P$ in $LOOP\ x_i\ DO\ P\ END$ wird so oft ausgeführt, wie der Wert von $x_i$ zu Beginn des LOOPs ist.
* Das Programm stoppt mit dem Wert $f(n_1,n_2,...,n_k)$ in der Variablen $x_0$.
%% Cell type:markdown id: tags:
Eine Funktion heißt LOOP-berechenbar, falls es ein LOOP-Programm gibt, das mit $n_1,...,n_k$ in den Variablen $x_1,...,x_k$ gestartet wird und mit $f(n_1,n_2,...,n_k)$ in der Variablen $x_0$ endet.
Ein Beispiel dafür ist die Addition $f(n_1, n_2) = n_1 + n_2$:
%% Cell type:code id: tags:
``` python
%run Interpreter/lexer.py
%run Interpreter/interpreter.py
```
%% Cell type:code id: tags:
``` python
interpret('''
x1:=3;
x2:=6;
LOOP x1 DO
x0≔ x0 + 1
END''')
```
%% Output
HI
9
%% Cell type:markdown id: tags:
File "<string>", line unknown
SyntaxError: Syntax Error in line: 4
Für die Visualisierung und das Verständnis haben wir die Syntax um das Keyword BREAK ereitert.
Wenn dieses erreicht wird, wird der Zustand ausgegeben und man wird gefragt, ob fortgefahren oder abgebrochen werden soll.
Dabei werden Variablen, denen noch kein Wert zugewiesen wurde ignoriert.
Im folgenden Fall wird $x_0$ nicht verändet, da $x_2$ nie zugewiesen wurde und die Schleife somit nicht ausgeführt wird.
%% Cell type:code id: tags:
``` python
tokenize("LOOP x1 DO P END")
interpret('''
x1:=5;
x0:=2;
LOOP x2 DO
x0:=1
BREAK
END
''')
```
%% Output
HI
BREAK in Zeile 5
Aktueller Zustand:
Variable x1: 5
Variable x0: 2
Drücke ENTER zum Fotfahren oder schreibe EXIT zum Beenden:1
Traceback (most recent call last):
File "/usr/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 3418, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-2-93cfbd622072>", line 1, in <module>
tokenize("LOOP x1 DO P END")
File "/home/christopher/uni/Projektarbeit/prob-teaching-notebooks/info4/kapitel-8/Interpreter/lexer.py", line 43, in tokenize
raise SyntaxError('Syntax Error in line: ' + str(program.count("\n", 0, current_position) + 1))
File "<string>", line unknown
SyntaxError: Syntax Error in line: 1
2
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment