{"id":3691,"date":"2024-02-20T12:22:24","date_gmt":"2024-02-20T11:22:24","guid":{"rendered":"https:\/\/oopm.org\/?page_id=3691"},"modified":"2025-01-14T12:36:09","modified_gmt":"2025-01-14T11:36:09","slug":"10-1-1-complete-grammar-example","status":"publish","type":"page","link":"https:\/\/oopm.org\/?page_id=3691","title":{"rendered":"10.1.1 A simple expression grammar"},"content":{"rendered":"<div class=\"pdfprnt-buttons pdfprnt-buttons-page pdfprnt-top-right\"><a href=\"https:\/\/oopm.org\/index.php?rest_route=wpv2pages3691&print=pdf\" class=\"pdfprnt-button pdfprnt-button-pdf\" target=\"_blank\"><img decoding=\"async\" src=\"https:\/\/oopm.org\/wp-content\/plugins\/pdf-print\/images\/pdf.png\" alt=\"image_pdf\" title=\"View PDF\" \/><\/a><a href=\"https:\/\/oopm.org\/index.php?rest_route=wpv2pages3691&print=print\" class=\"pdfprnt-button pdfprnt-button-print\" target=\"_blank\"><img decoding=\"async\" src=\"https:\/\/oopm.org\/wp-content\/plugins\/pdf-print\/images\/print.png\" alt=\"image_print\" title=\"Print Content\" \/><\/a><\/div>\n<p class=\"wp-block-paragraph\">In this section, we extend the grammar example with code to represent a complete grammar, a parser, and an evaluator. The reader may skip this section and the subsequent ones on the expression parser and the abstract syntax tree during the first reading of this book.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We use a grammar for describing aritemhetic expressions of digits using <code>'+',<\/code> <code>'*'<\/code> and parentheses:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Start: &lt;Exp&gt;\n&lt;Exp&gt; ::= &lt;Exp&gt; \"+\" &lt;Term&gt; | &lt;Term&gt;\n&lt;Term&gt; ::= &lt;Term&gt; \"*\" &lt;Primary&gt; | &lt;Primary&gt;\n&lt;Primary&gt; ::= &lt;Number&gt; | \"(\" &lt;Exp&gt; \")\"\n&lt;Number&gt; ::= &lt;Number&gt; &lt;Digit&gt; | &lt;Digit&gt;\n&lt;Digit&gt; ::= \"0\" | \"1\" | \"2\" | \"3\" | \"4\" | \"5\" | \"6\" | \"7\" | \"8\" | \"9\" <\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The start symbol of the grammar is the nonterminal <code>&lt;Exp&gt;<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The nonterminal symbols are: <code>&lt;Exp&gt;<\/code>, <code>&lt;Term&gt;<\/code>, <code>&lt;Primary&gt;<\/code>, <code>&lt;Number&gt;<\/code>, and <code>&lt;Digit&gt;<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The terminal symbols are: <code>\"+\"<\/code>, <code>\"*\"<\/code>, <code>\"(\"<\/code>, <code>\")\"<\/code> and the digits <code>\"0\"<\/code> &#8211; <code>\"9\"<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We declare an <code>ExpressionGrammar<\/code> as a sub of <code>Grammar<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ExpressionGrammar: <strong>obj<\/strong> Grammar\n   -- declaration of symbols\n   -- declaration of rules\n   -- specification of start symbol<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The symbols are declared as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>   expSy: <strong>obj<\/strong> Nonterminal(\"Exp\")\n   termSy: <strong>obj<\/strong> Nonterminal(\"Term\")\n   primarySy: <strong>obj<\/strong> Nonterminal(\"Primary\")\n   numberSy: <strong>obj<\/strong> Nonterminal(\"Number\")\n   digitSy: <strong>obj<\/strong> Nonterminal(\"Digit\")\n   add: <strong>obj<\/strong> Terminal(\"+\")\n   mult: <strong>obj<\/strong> Terminal(\"*\")\n   leftB: <strong>obj<\/strong> Terminal(\"(\")\n   rightB: <strong>obj<\/strong> Terminal(\")\")<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">To generate the rules, we introduce a method for each nonterminal. The one for <code>&lt;Exp&gt;<\/code> looks as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>   mkExpRule: addRule(expSy)\n      S: <strong>ref<\/strong> SymbolList\n      S := SymbolList.insertList((ExpSy,add,termSy))\n      R.alternatives.insert(Alternative(S))\n      S := SymbolList.insertList(termSy)\n      alt := Alternative(S)\n      R.alternatives.insert(alt)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">As can bee seen, <code>mkExpRule<\/code> is a submethod of the method <code>addRule<\/code>, which we have added to class <code>Grammar<\/code>. In addition, we have added class <code>SymbolList<\/code> with a <code>print<\/code> method:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>class<\/strong> Grammar:\n   ...\n   <strong>class<\/strong> SymbolList: OrderedList(#Symbol)\n       print:\n          scan\n             current.print\n   addRule(L: <strong>ref<\/strong> Nonterminal): \n      R: <strong>ref<\/strong> Rule\n      R := Rule\n      R.leftSide := L\n      inner(addRule)\n      rules.insert(R) <\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The method <code>addRule<\/code> has the leftside of the rule to be added as a parameter <code>L<\/code>. It generates a <code>Rule<\/code>-object and assign its reference to <code>R<\/code>, and then assigns <code>L<\/code> to the <code>leftSide<\/code> of <code>R<\/code>. Then it executes <code>inner(addRule)<\/code> implying that the mainpart of <code>mkExpRule<\/code> is executed. When returning from inner, the <code>Rule<\/code> <code>R<\/code> is inserted into the list of rules.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The expression <code>SymbolList.insertList((ExpSy,add,termSy))<\/code> may need an explanation:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>First <code>SymboList<\/code> is evaluated creating a <code>SymbolList<\/code>-object returning a reference to this newly created object.<\/li>\n\n\n\n<li>Then <code>insertList((ExpSy,add,termSy))<\/code> is invoked on the reference to the new <code>SymbolList<\/code>-object.<\/li>\n\n\n\n<li>The parameter of <code>insertList<\/code> is an array and the argument of the invocation is the array-literal<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-color-3-color\"> <\/mark><code>(ExpSy,add,termSy)<\/code> &#8211; array-literal is introduced in section <script>mkRef(\"Array and for-loop\")<\/script>. <code>InsertList<\/code> inserts each element of the array in the newly generated <code>SymbolList<\/code>-object.<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">To complete the generation of <code>ExpressionGrammer<\/code>, we may add methods similar to <code>mkExpRule<\/code> for the other nonterminals, but leaves this an exercise for the reader. The <code>ExpressionGrammer<\/code> then looks as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ExpressionGrammar: <strong>obj<\/strong> Grammar\n   -- declaration of symbols\n   ExpSy: <strong>obj<\/strong> Nontermial(\"Exp\")\n   ...\n   -- declaration of rules\n   mkExpRule: addRule(ExpSy)\n      ...\n   mkTermRule: addRule(TermSy)\n      ...\n   mkPrimaryRule: addRule(PrimarySy)\n      ...\n   mkNumberRule: addRule(NumberSy)\n      ...\n   mkDigitRule: addRule(DigitSy)\n      ...\n   -- specification of start symbol\n   start := ExpSy\n   mkExpRule\n   ...<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Parser and abstract syntax tree<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Next we show how to write a parser for the expressions of our grammar and how to represent an expression by means of an abstract syntax tree. The reader may skip theses sections during a first reading.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n<div style=\"display:flex; gap:10px;justify-content:center\" class=\"wps-pgfw-pdf-generate-icon__wrapper-frontend\">\n\t\t<a  href=\"https:\/\/oopm.org?action=genpdf&amp;id=3691\" class=\"pgfw-single-pdf-download-button\" ><img src=\"https:\/\/oopm.org\/wp-content\/plugins\/pdf-generator-for-wp\/admin\/src\/images\/PDF_Tray.svg\" title=\"Generate PDF\" style=\"width:auto; height:45px;\"><\/a>\n\t\t<\/div>","protected":false},"excerpt":{"rendered":"<p>In this section, we extend the grammar example with code to represent a complete grammar, a parser, and an evaluator. The reader may skip this section and the subsequent ones on the expression parser and the abstract syntax tree during the first reading of this book. We use a grammar for describing aritemhetic expressions of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1282,"menu_order":1,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-3691","page","type-page","status-publish","hentry"],"mb":[],"mfb_rest_fields":["title","gutenberg_elementor_mode"],"_links":{"self":[{"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/3691","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3691"}],"version-history":[{"count":35,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/3691\/revisions"}],"predecessor-version":[{"id":11112,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/3691\/revisions\/11112"}],"up":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/1282"}],"wp:attachment":[{"href":"https:\/\/oopm.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3691"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}