{"id":1282,"date":"2023-09-23T16:44:50","date_gmt":"2023-09-23T14:44:50","guid":{"rendered":"http:\/\/oopm.org\/?page_id=1282"},"modified":"2025-01-14T12:44:16","modified_gmt":"2025-01-14T11:44:16","slug":"6-v-grammar-example","status":"publish","type":"page","link":"https:\/\/oopm.org\/?page_id=1282","title":{"rendered":"10.1 Grammar example"},"content":{"rendered":"<div class=\"pdfprnt-buttons pdfprnt-buttons-page pdfprnt-top-right\"><a href=\"https:\/\/oopm.org\/index.php?rest_route=wpv2pages1282&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=wpv2pages1282&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 will show an example of nested classes for representing the concept of a context free grammar. We are thus in the domain of grammars from programming  languages.<\/p>\n\n\n\n<style>\n#grammar10 {\n  width: 30%;\n  padding-left: 15px;\n  margin-left: 15px;\n  float: right;\n  background-color: lightgray;\n  font-size: 0.85em;\n}\n<\/style>\n<div id=\"grammar10\">If you are not familiar with the notion of a context free grammar, please seek information elsewhere if the brief explanation here is insufficient<\/div>\n\n\n\n<p class=\"wp-block-paragraph\">In <a href=\"https:\/\/qbeta.dev\">qBeta \u2013 a new object-oriented language for programming and modeling<\/a>, a context free grammar is used to define the syntax of qBeta, which we use in this book.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A context free grammar consists of:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A set of <em>Nonterminal<\/em> symbols.<\/li>\n\n\n\n<li>A set of <em>Terminal<\/em> symbols.<\/li>\n\n\n\n<li>A set of <em>Rules<\/em>. <\/li>\n\n\n\n<li>A distinguished Nonterminal called the <em>Start symbol.<\/em> <\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">The sets of nonterminal and terminal symbols are disjoint, i.e. no symbol can be both a nonterminal and a terminal. A <em>symbol<\/em> is<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-foreground-color\"> <\/mark>either a nonterminal or a terminal symbol.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We define a class <code>Grammar<\/code> to represent the idea of a context-free grammar. Instances of this class represent grammars of specific languages like Java and Simula.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Class <code>Grammar<\/code> defines the symbols (nonterminals and terminals) of a given grammar. Different concrete grammars have different symbols. Java symbols differ from Simula symbols. We reflect this property in the definition of the <code>Grammar<\/code> class. The same applies to the rules of a given grammar. Different grammars have different rules and this is also be reflected in the definition of class <code>Grammar<\/code>. We use nested classes for this purpose as shown in the first sketch of class <code>Grammar<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>class<\/strong> Grammar: \n   nonterminals: <strong>obj<\/strong> Set(Nonterminal)\n   terminals: <strong>obj<\/strong> Set(Terminal)\n   rules: <strong>obj<\/strong> OrderedList(Rule)\n   start: <strong>ref<\/strong> Nonterminal\n   <strong>class<\/strong> Symbol(name: <strong>var<\/strong> String):\n      ...\n   <strong>class<\/strong> Nonterminal: Symbol\n      ...\n   <strong>class<\/strong> Terminal: Symbol \n      ...\n   <strong>class<\/strong> Rule: \n      ...<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The nonterminals are represented by a set of <code>Nonterminal<\/code> symbols and the terminals are represented by a set of <code>Terminal<\/code> symbols. The rules are represented by an <code>OrderedList<\/code> of <code>Rule<\/code>-objects. The Start symbol is represented by a reference, <code>start<\/code> to a <code>Nonterminal<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The reason we use <code>OrderedList<\/code> to represent the rules and not <code>Set<\/code> as for <code>Nonterminals<\/code> and <code>Terminals<\/code>, is that we want to control the order of the rules when e.g. printing as shown below,<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-7387b849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:70%\">\n<p class=\"wp-block-paragraph\">The fact that terminals and nonterminals are special symbols is represented by the classes <code>Terminal<\/code> and <code>Nonterminal<\/code> are subclasses of the class <code>Symbol<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">All these classes are local classes of <code>Grammar<\/code>, as they are parts of the definition Grammar. The <code>Symbol<\/code> class has a parameter, representing name of the <code>Symbol<\/code>.<\/p>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-vertically-aligned-center is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:33.33%\">\n<figure class=\"wp-block-image size-full is-style-default\"><img decoding=\"async\" src=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/11\/tempImageOgKKI8.heic\" alt=\"\" class=\"wp-image-9624\"\/><\/figure>\n<\/div>\n<\/div>\n\n\n\n<p class=\"wp-block-paragraph\">The figures below show two different diagrams for illustrating nested classes. In the first diagram, the circle shows that class&nbsp;<code>Grammar<\/code> has local classes&nbsp;<code>Symbol<\/code>,&nbsp;<code>Terminal<\/code>&nbsp;and&nbsp;<code>NonTerminal<\/code>. In the second diagram,&nbsp;<code>Symbol<\/code>,&nbsp;<code>Terminal<\/code>&nbsp;and&nbsp;<code>Nonterminal<\/code>&nbsp;are shown inside the&nbsp;<code>Grammar <\/code>class \u2013 in addition, the class\/subclass relations between&nbsp;<code>Symbol<\/code>,&nbsp;<code>Terminal<\/code>&nbsp;and&nbsp;<code>NonTerminal<\/code> are shown, which is not the case in the first diagram. We use the two types of diagrams depending on what is to be illustrated.<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-7387b849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<figure class=\"wp-block-image size-full is-resized\"><img fetchpriority=\"high\" decoding=\"async\" width=\"364\" height=\"327\" src=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/10\/classGrammarWithLocalClasses-alt-1-1.jpg\" alt=\"\" class=\"wp-image-8437\" style=\"width:304px;height:auto\" srcset=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/10\/classGrammarWithLocalClasses-alt-1-1.jpg 364w, https:\/\/oopm.org\/wp-content\/uploads\/2024\/10\/classGrammarWithLocalClasses-alt-1-1-300x270.jpg 300w\" sizes=\"(max-width: 364px) 100vw, 364px\" \/><\/figure>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-vertically-aligned-top is-layout-flow wp-block-column-is-layout-flow\">\n<figure class=\"wp-block-image size-full is-resized\"><img decoding=\"async\" src=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/11\/tempImagewCZ1yS.heic\" alt=\"\" class=\"wp-image-9612\" style=\"width:298px;height:auto\"\/><\/figure>\n<\/div>\n<\/div>\n\n\n\n<p class=\"wp-block-paragraph\">We may declare an object representing a (tiny part) of a Java grammar as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Java: <strong>obj<\/strong> Grammar\n   jClass: <strong>obj<\/strong> Terminal(\"class\")\n   jStatic: <strong>obj<\/strong> Terminal(\"static\")\n   ...  <\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The object <code>Java<\/code> is a sub of <code>Grammar<\/code> and it contains the declaration of two terminal symbols representing the Java symbols <code>class<\/code> and <code>static<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We may in a similar way declare an object representing grammar for SIMULA:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Simula: <strong>obj<\/strong> Grammar \n   sClass: <strong>obj<\/strong> Terminal(\"class\")\n   sBegin: <strong>obj<\/strong> Terminal(\"begin\")<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The symbols <code>jClass<\/code> and <code>jStatic<\/code> represent the Java symbols class and static and  <code>sClass<\/code> and <code>sBegin<\/code> represent the Simula symbols class and begin. Note, even if <code>jClass<\/code> and <code>sClass<\/code> have the same name they represent the class-symbol of different grammars.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The next diagrams are similar to the diagrams above showing the nesting of <code>Symbol<\/code>, <code>Terminal<\/code> and <code>NonTerminal<\/code> in class <code>Grammar<\/code>. The difference is that the diagrams below show the nesting within instances of class <code>Grammar<\/code> &#8211; the <code>Java<\/code> <code>Grammar<\/code>-object and the <code>Simula<\/code> <code>Grammar<\/code>-object. As before, the circle  is used to illustrate nesting in the leftmost diagram. It shows that the <code>Java<\/code> <code>Grammar<\/code>-object and the <code>Simula<\/code> <code>Grammar<\/code>-object both have local classes <code>Symbol<\/code>, <code>Terminal<\/code> and <code>NonTerminal<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the second diagram, <code>Symbol<\/code>, <code>Terminal<\/code> and <code>Nonterminal<\/code> are shown inside the <code>Java<\/code>&#8211; and <code>Simula<\/code>-objects respectively &#8211; in addition, the class\/subclass relations between is shown, which is not the case in the first diagram. As said above, we use the two types of diagrams depending on what is to be illustrated.<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-7387b849 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<figure class=\"wp-block-image size-full is-resized\"><img decoding=\"async\" width=\"611\" height=\"329\" src=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/10\/grammarWithLocalClasses-alt-1-2.jpg\" alt=\"\" class=\"wp-image-8440\" style=\"width:459px;height:auto\" srcset=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/10\/grammarWithLocalClasses-alt-1-2.jpg 611w, https:\/\/oopm.org\/wp-content\/uploads\/2024\/10\/grammarWithLocalClasses-alt-1-2-300x162.jpg 300w\" sizes=\"(max-width: 611px) 100vw, 611px\" \/><\/figure>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<figure class=\"wp-block-image size-full is-resized\"><img decoding=\"async\" src=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/11\/tempImageMLV50L.heic\" alt=\"\" class=\"wp-image-9610\" style=\"width:496px;height:auto\"\/><\/figure>\n<\/div>\n<\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><code>Java<\/code> and <code>Simula<\/code> are both instances of the same class <code>Grammar<\/code>. The objects <code>jClass<\/code>, <code>jStatic<\/code> and <code>sClass<\/code>, <code>sBegin<\/code> are, however, not instances of the same class; <code>jClass<\/code>, and <code>jStatic<\/code> are instances of class <code>Java.Terminal<\/code>, whereas <code>sClass<\/code>, and <code>sBegin<\/code> are instances of class <code>Simula.Terminal<\/code>. The following figure illustrates this for <code>jClass<\/code> and <code>sClass<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">From a modeling point of view, this is what we want, since <code>jClass<\/code>, <code>jStatic<\/code> are Java symbols and <code>sClass<\/code>, <code>sBegin<\/code> are Simula symbols. The two classes of symbols are clearly different.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The following figure illustrates that objects of classes <code>Java.Terminal<\/code> and <code>Simula.Terminal<\/code> represents two different elements of two grammars. The <code>Grammar<\/code> objects, Java and SImula represents each their grammar.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"584\" src=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/12\/JavaSimularGrammar-3-1024x584.jpg\" alt=\"\" class=\"wp-image-10021\" srcset=\"https:\/\/oopm.org\/wp-content\/uploads\/2024\/12\/JavaSimularGrammar-3-1024x584.jpg 1024w, https:\/\/oopm.org\/wp-content\/uploads\/2024\/12\/JavaSimularGrammar-3-300x171.jpg 300w, https:\/\/oopm.org\/wp-content\/uploads\/2024\/12\/JavaSimularGrammar-3-768x438.jpg 768w, https:\/\/oopm.org\/wp-content\/uploads\/2024\/12\/JavaSimularGrammar-3.jpg 1218w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">JavaSimularGrammar\/<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">At this point we have not added the necessary content to the <code>Java.nonterminals<\/code>, <code>Java.terminals<\/code>, <code>Java.rules<\/code> and <code>Java.start<\/code> in order to represent a complete grammar for Java and similarly for Simula. These grammars are large and will thus take up a lot of space in this book. Instead, we show a complete example of a grammar for a subset of arithmetic expressions in section <script>mkRef(\"A simple expression grammar\")<\/script> below.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the next version of <code>Grammar<\/code>,  we add a <code>print<\/code> method:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>class<\/strong> Grammar:\n   nonterminals: <strong>obj<\/strong> Set(Nonterminal)\n   terminals: <strong>obj<\/strong> Set(Terminal)\n   rules: <strong>obj<\/strong> OrderedList(Rule)\n   start: <strong>ref<\/strong> Nonterminal\n   ...\n   print:\n      console.print(\"Grammar:\\n\")\n      rules.scan\n         current.print<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>print<\/code> method prints the text <code>\"Grammar\"<\/code> and then it scans through the <code>rules<\/code> and invoke <code>print<\/code> on each rule in the grammar. And as mentioned above, we want to print the <code>rules<\/code> in the order we have added them to the <code>OrderedList<\/code>. See the expression grammar example below for details.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The difference between <code>Java.Symbol<\/code> and <code>Simula.Symbol<\/code> is the same as the difference between <code>Java.print<\/code> and <code>Simula.print<\/code>. The two statements<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>Java.print<code>Simula<span style=\"font-family: inherit; font-size: 0.9rem;\">.print<\/span><\/code><\/code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">invoke different <code>print<\/code> methods, since they have different contexts.<mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-custom-color-3-color\"> <\/mark><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">By declaring <code>Symbol<\/code> local to the <code>Grammar<\/code> class, we have the possibility of distinguishing between symbols of different grammars. Also, since the class <code>Symbol<\/code> is local to <code>Grammar<\/code>, a <code>Symbol<\/code> has no existence without a <code>Grammar<\/code> object.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the example above, the data-items <code>jClass<\/code>, <code>jStatic<\/code> and <code>sClass<\/code>, <code>sBegin<\/code> are constant references in the sense that they each of them refer to the same object during the whole of the program execution. It is, of course, also possible to declare variable references using <strong><code>ref<\/code><\/strong>. In the example below we declare data-items where the type is a remote name:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>aJavaSymbol: <strong>ref<\/strong> Java.Symbol \naSimulaSymbol: <strong>ref<\/strong> Simula.Symbol<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The reference <code>aJavaSymbol<\/code> may denote any instance of <code>Java.Symbol<\/code> and <code>aSimulaSymbol<\/code> may denote any instance of <code>Simula.Symbol<\/code>. It is important to emphasise that these two data-items have different types. Class <code>Symbol<\/code> in Java is different from class <code>Symbol<\/code> in Simula.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Suppose that we want to declare a reference that can denote arbitrary symbols of any <code>Grammar<\/code>. This is done by declaring a variable reference qualified by <code>Grammar.Symbol<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>anySymbol: <strong>ref<\/strong> Grammar.Symbol<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Note the difference from the declaration of <code>aJavaSymbol<\/code> using <code>Java.Symbol<\/code>, where <code>Java<\/code> is a reference to a <code>Grammar<\/code> object. In the declaration of <code>anySymbol<\/code>, <code>Grammar<\/code> is a class name. <code>anySymbol<\/code> can refer to instances of either <code>Java.Symbol<\/code> or <code>Simula.Symbol<\/code>. In fact, the class <code>Grammar.Symbol<\/code> may be viewed as a generalization of the classes <code>Java.Symbol<\/code> and <code>Simula.Symbol<\/code>. The <code>Grammar.Symbol<\/code> class is then a representation of the general concept of a symbol of a context free grammar.<\/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=1282\" 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 will show an example of nested classes for representing the concept of a context free grammar. We are thus in the domain of grammars from programming languages. If you are not familiar with the notion of a context free grammar, please seek information elsewhere if the brief explanation here is insufficient [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1232,"menu_order":1,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1282","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\/1282","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=1282"}],"version-history":[{"count":117,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/1282\/revisions"}],"predecessor-version":[{"id":11110,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/1282\/revisions\/11110"}],"up":[{"embeddable":true,"href":"https:\/\/oopm.org\/index.php?rest_route=\/wp\/v2\/pages\/1232"}],"wp:attachment":[{"href":"https:\/\/oopm.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1282"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}