diff --git a/info4/kapitel-8/LOOP-Programme.ipynb b/info4/kapitel-8/LOOP-Programme.ipynb index 3e401844f4b6a3e2068720b95023bacfb4ddf01e..b91361a0b5cfac0456f01e388952bdb9b9c2f991 100644 --- a/info4/kapitel-8/LOOP-Programme.ipynb +++ b/info4/kapitel-8/LOOP-Programme.ipynb @@ -1,57 +1,121 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# LOOP-Programme\n", + "\n", + "Im Folgenden werden wir Konzepte von prozeduralen Programmiersprachen kennenlernen. Als erste einfache Programmiersprache betrachten wir LOOP.\n", + "Die Syntax solcher Programme ist definiert durch:\n", + "* $x_i := c$, $x_i := x_j + c$ und $x_i := x_j - c$ mit $i,j,c \\in \\mathbb{N}$ sind LOOP-Programme\n", + "* Falls $P_1$ und $P_2$ LOOP-Programme sind ist $P_1; P_2$ ein LOOP-Programm\n", + "* 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", + "metadata": {}, + "source": [ + "Die Semantik von LOOP-Programmen lässt sich wie folgt verstehen:\n", + "* 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$.\n", + "* Die restlichen Variablen starten mit dem Wert 0.\n", + "* 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.\n", + "* Bei $P_1; P_2$ wird erst $P_1$ und dann $P_2$ ausgeführt.\n", + "* 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.\n", + "* Das Programm stoppt mit dem Wert $f(n_1,n_2,...,n_k)$ in der Variablen $x_0$." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "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.\n", + "Ein Beispiel dafür ist die Addition $f(n_1, n_2) = n_1 + n_2$:" + ] + }, { "cell_type": "code", "execution_count": 1, + "metadata": { + "scrolled": false + }, + "outputs": [], + "source": [ + "%run Interpreter/interpreter.py" + ] + }, + { + "cell_type": "code", + "execution_count": 5, "metadata": {}, "outputs": [ { - "name": "stdout", - "output_type": "stream", - "text": [ - "HI\n" - ] - }, - { - "ename": "SyntaxError", - "evalue": "Syntax Error in line: 4 (<string>)", - "output_type": "error", - "traceback": [ - "\u001b[0;36m File \u001b[0;32m\"<string>\"\u001b[0;36m, line \u001b[0;32munknown\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m Syntax Error in line: 4\n" - ] + "data": { + "text/plain": [ + "9" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "%run Interpreter/lexer.py" + "interpret('''\n", + "x1:=3;\n", + "x2:=6;\n", + "LOOP x1 DO\n", + " x0≔ x0 + 1\n", + "END''')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Für die Visualisierung und das Verständnis haben wir die Syntax um das Keyword BREAK ereitert.\n", + "Wenn dieses erreicht wird, wird der Zustand ausgegeben und man wird gefragt, ob fortgefahren oder abgebrochen werden soll.\n", + "Dabei werden Variablen, denen noch kein Wert zugewiesen wurde ignoriert.\n", + "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", - "execution_count": 2, + "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "HI\n" + "BREAK in Zeile 5\n", + "Aktueller Zustand:\n", + "Variable x1: 5\n", + "Variable x0: 2\n", + "Drücke ENTER zum Fotfahren oder schreibe EXIT zum Beenden:1\n" ] }, { - "ename": "SyntaxError", - "evalue": "Syntax Error in line: 1 (<string>)", - "output_type": "error", - "traceback": [ - "Traceback \u001b[0;36m(most recent call last)\u001b[0m:\n", - " File \u001b[1;32m\"/usr/lib/python3.8/site-packages/IPython/core/interactiveshell.py\"\u001b[0m, line \u001b[1;32m3418\u001b[0m, in \u001b[1;35mrun_code\u001b[0m\n exec(code_obj, self.user_global_ns, self.user_ns)\n", - " File \u001b[1;32m\"<ipython-input-2-93cfbd622072>\"\u001b[0m, line \u001b[1;32m1\u001b[0m, in \u001b[1;35m<module>\u001b[0m\n tokenize(\"LOOP x1 DO P END\")\n", - "\u001b[0;36m File \u001b[0;32m\"/home/christopher/uni/Projektarbeit/prob-teaching-notebooks/info4/kapitel-8/Interpreter/lexer.py\"\u001b[0;36m, line \u001b[0;32m43\u001b[0;36m, in \u001b[0;35mtokenize\u001b[0;36m\u001b[0m\n\u001b[0;31m raise SyntaxError('Syntax Error in line: ' + str(program.count(\"\\n\", 0, current_position) + 1))\u001b[0m\n", - "\u001b[0;36m File \u001b[0;32m\"<string>\"\u001b[0;36m, line \u001b[0;32munknown\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m Syntax Error in line: 1\n" - ] + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" } ], "source": [ - "tokenize(\"LOOP x1 DO P END\")" + "interpret('''\n", + "x1:=5;\n", + "x0:=2;\n", + "LOOP x2 DO\n", + " x0:=1\n", + " BREAK\n", + "END\n", + "''')" ] } ],