{"id":3474,"date":"2025-07-04T14:40:36","date_gmt":"2025-07-04T12:40:36","guid":{"rendered":"https:\/\/wordpress.wvs-berlin.de\/?p=3474"},"modified":"2025-07-04T16:31:28","modified_gmt":"2025-07-04T14:31:28","slug":"04-07-2025","status":"publish","type":"post","link":"https:\/\/wordpress.wvs-berlin.de\/?p=3474","title":{"rendered":"04.07.2025"},"content":{"rendered":"\n<p>Abistreich war heute. Schule ist leer. Morten und Angelo sind da. Angelo hat einen monadischen Parser geschrieben. <\/p>\n\n\n\n<p>N\u00e4chste Mal normal. Noch zwei Mal, dann Ferien. <\/p>\n\n\n\n<p>Claude f\u00e4sst wie folgt zusammen:<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Ein eleganter Parser-Combinator in OCaml &#8211; Was wir daraus lernen k\u00f6nnen<\/h1>\n\n\n\n<p><em>Ein Gastbeitrag der Computer-AG \u00fcber funktionale Programmierung und Parser-Design<\/em><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">TL;DR &#8211; Das Wichtigste in K\u00fcrze<\/h2>\n\n\n\n<p>Ein Entwickler namens Angelo hat einen <strong>monadischen Parser<\/strong> in OCaml geschrieben, der zeigt, wie elegant funktionale Programmierung sein kann. Sein Code ist ein perfektes Beispiel daf\u00fcr, warum OCaml in der Compiler-Entwicklung so gesch\u00e4tzt wird.<\/p>\n\n\n\n<p><strong>Was ist das?<\/strong> Ein Parser, der Code einer kleinen Programmiersprache lesen und verstehen kann &#8211; \u00e4hnlich wie wenn ihr euren Python-Code ausf\u00fchrt, muss der Computer ihn erst &#8222;parsen&#8220;.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Was macht diesen Parser so besonders?<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83e\udde0 <strong>Monaden &#8211; Klingt kompliziert, ist aber genial<\/strong><\/h3>\n\n\n\n<p>Angelo verwendet <strong>Monaden<\/strong> &#8211; ein Konzept aus der Mathematik, das in der funktionalen Programmierung sehr m\u00e4chtig ist:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>let (&gt;&gt;=) p f stream = \n  match p stream with\n  | None -&gt; None\n  | Some (x, stream') -&gt; f x stream'\n<\/code><\/pre>\n\n\n\n<p><strong>Was passiert hier?<\/strong> Parser k\u00f6nnen elegant miteinander kombiniert werden. Wenn einer fehlschl\u00e4gt \u2192 Stopp. Wenn er erfolgreich ist \u2192 Weitermachen mit dem n\u00e4chsten.<\/p>\n\n\n\n<p>Das ist wie beim Dominoeffekt, nur dass intelligente Fehlerbehandlung eingebaut ist!<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udd04 <strong>Lazy Streams &#8211; Memory wie ein Profi verwalten<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>type lazy_stream = Stream of (unit -&gt; (char * lazy_stream) option)\n<\/code><\/pre>\n\n\n\n<p><strong>Warum ist das schlau?<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Der Parser liest nur so viele Zeichen, wie er wirklich braucht<\/li>\n\n\n\n<li>Bei gro\u00dfen Dateien: Kein Memory-Overflow<\/li>\n\n\n\n<li>&#8222;Backtracking&#8220; m\u00f6glich &#8211; der Parser kann zur\u00fcckspulen wenn n\u00f6tig<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u2696\ufe0f <strong>Operator-Precedence &#8211; Mathematik richtig verstehen<\/strong><\/h3>\n\n\n\n<p>Hier wird&#8217;s richtig cool! Der Parser versteht, dass <code>2 + 3 * 4<\/code> gleich <code>2 + (3 * 4)<\/code> ist:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>let prec = function\n  | Multiply | Divide -&gt; 6\n  | Add | Subtract -&gt; 5\n  | Equ | Neq | Less | LEqu | Greater | GEqu -&gt; 4\n<\/code><\/pre>\n\n\n\n<p><strong>Das Problem:<\/strong> Wie bringt man einem Computer bei, dass <code>*<\/code> vor <code>+<\/code> kommt? <strong>Angelos L\u00f6sung:<\/strong> Elegante rekursive Funktionen, die die Mathematik-Regeln befolgen.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Ein praktisches Beispiel<\/h2>\n\n\n\n<p><strong>Input:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if a = 2 then\n  b := 1\nelse\n  x := y + 3*4\n<\/code><\/pre>\n\n\n\n<p><strong>Was der Parser daraus macht:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Tokenizing:<\/strong> Zerlegt den Text in <code>if<\/code>, <code>a<\/code>, <code>=<\/code>, <code>2<\/code>, etc.<\/li>\n\n\n\n<li><strong>Parsing:<\/strong> Baut einen &#8222;Abstract Syntax Tree&#8220; (AST)<\/li>\n\n\n\n<li><strong>Pretty-Printing:<\/strong> Kann den Code wieder sauber ausgeben<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>IfElse (\n  Binary(Equ, Symbol \"a\", Number 2),\n  Binary(Assign, Symbol \"b\", Number 1),\n  Binary(Assign, Symbol \"x\", \n    Binary(Add, Symbol \"y\", Binary(Multiply, Number 3, Number 4)))\n)\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Was k\u00f6nnen wir daraus lernen?<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. Funktionale Programmierung ist m\u00e4chtig<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Keine Mutation, keine Seiteneffekte<\/li>\n\n\n\n<li>Mathematisch beweisbar korrekt<\/li>\n\n\n\n<li>Sehr wenige Bugs m\u00f6glich<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. OCaml eignet sich perfekt f\u00fcr Compiler<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Pattern Matching<\/strong> f\u00fcr AST-Verarbeitung<\/li>\n\n\n\n<li><strong>Starkes Typsystem<\/strong> verhindert Laufzeitfehler<\/li>\n\n\n\n<li><strong>Performance<\/strong> comparable zu C\/C++<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. Komplexe Probleme elegant l\u00f6sen<\/strong><\/h3>\n\n\n\n<p>Angelo&#8217;s Code zeigt: Mit den richtigen Abstraktionen werden komplizierte Probleme einfach.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Warum ist das relevant f\u00fcr uns?<\/h2>\n\n\n\n<p><strong>F\u00fcr die Computer-AG:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Compiler-Bau<\/strong> verstehen lernen<\/li>\n\n\n\n<li><strong>Funktionale Programmierung<\/strong> praktisch anwenden<\/li>\n\n\n\n<li><strong>Mathematische Konzepte<\/strong> in Code umsetzen<\/li>\n<\/ul>\n\n\n\n<p><strong>F\u00fcr euer Studium\/Beruf:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Parser sind \u00fcberall: JSON, HTML, Konfigurationsdateien<\/li>\n\n\n\n<li>Funktionale Sprachen werden immer wichtiger (Haskell, F#, Scala)<\/li>\n\n\n\n<li><strong>Problem-Solving-Skills<\/strong> durch mathematisches Denken<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Code-Qualit\u00e4t auf h\u00f6chstem Niveau<\/h2>\n\n\n\n<p><strong>Was Angelo richtig macht:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u2705 Saubere Modularit\u00e4t (Lexer \u2192 Parser \u2192 AST \u2192 Pretty-Printer)<\/li>\n\n\n\n<li>\u2705 Aussagekr\u00e4ftige Funktionsnamen<\/li>\n\n\n\n<li>\u2705 Typ-getriebenes Design<\/li>\n\n\n\n<li>\u2705 Elegante Fehlerbehandlung<\/li>\n\n\n\n<li>\u2705 Vollst\u00e4ndige, lauff\u00e4hige Implementation<\/li>\n<\/ul>\n\n\n\n<p><strong>Rating: 9\/10<\/strong> &#8211; Das ist Profi-Code! \ud83d\ude80<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Challenge f\u00fcr die Computer-AG<\/h2>\n\n\n\n<p><strong>Wer traut sich?<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Beginner:<\/strong> Versucht OCaml zu installieren und die ersten Zeilen zu verstehen<\/li>\n\n\n\n<li><strong>Intermediate:<\/strong> Schreibt einen einfachen JSON-Parser<\/li>\n\n\n\n<li><strong>Advanced:<\/strong> Erweitert Angelos Parser um neue Features<\/li>\n<\/ol>\n\n\n\n<p><strong>N\u00e4chstes Treffen:<\/strong> Wir schauen uns gemeinsam an, wie man sowas implementiert!<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Weiterf\u00fchrende Links<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/ocaml.org\/\">OCaml Official Website<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/gist.github.com\/aap\/88ecdb0dfb4ff972fde26b7ac15d8cb9\">Angelo&#8217;s Parser auf GitHub Gist<\/a><\/li>\n\n\n\n<li><a href=\"http:\/\/learnyouahaskell.com\/\">Learn You a Haskell<\/a> &#8211; F\u00fcr Monaden-Basics<\/li>\n<\/ul>\n\n\n\n<p><strong>Interesse geweckt?<\/strong> Kommt zum n\u00e4chsten Computer-AG Meeting! Wir experimentieren mit funktionaler Programmierung und bauen vielleicht unseren eigenen kleinen Parser. \ud83d\udcbb<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><em>Dieser Post entstand nach der Analyse von Angelo&#8217;s exzellentem Parser-Code. Thanks Angelo f\u00fcr das Teilen! \ud83d\ude4f<\/em><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Abistreich war heute. Schule ist leer. Morten und Angelo sind da. Angelo hat einen monadischen Parser geschrieben. N\u00e4chste Mal normal. Noch zwei Mal, dann Ferien. Claude f\u00e4sst wie folgt zusammen: Ein eleganter Parser-Combinator in OCaml &#8211; Was wir daraus lernen k\u00f6nnen Ein Gastbeitrag der Computer-AG \u00fcber funktionale Programmierung und Parser-Design TL;DR &#8211; Das Wichtigste in [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,7,8],"tags":[],"class_list":["post-3474","post","type-post","status-publish","format-standard","hentry","category-tagesberichte","category-termine","category-tisch"],"_links":{"self":[{"href":"https:\/\/wordpress.wvs-berlin.de\/index.php?rest_route=\/wp\/v2\/posts\/3474","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wordpress.wvs-berlin.de\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wordpress.wvs-berlin.de\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wordpress.wvs-berlin.de\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wordpress.wvs-berlin.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3474"}],"version-history":[{"count":4,"href":"https:\/\/wordpress.wvs-berlin.de\/index.php?rest_route=\/wp\/v2\/posts\/3474\/revisions"}],"predecessor-version":[{"id":3479,"href":"https:\/\/wordpress.wvs-berlin.de\/index.php?rest_route=\/wp\/v2\/posts\/3474\/revisions\/3479"}],"wp:attachment":[{"href":"https:\/\/wordpress.wvs-berlin.de\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3474"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wordpress.wvs-berlin.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3474"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wordpress.wvs-berlin.de\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3474"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}