Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>How to write grammar for formal language?</h2> <p>Before read my this answer you should read first: <a href="https://stackoverflow.com/questions/15126824/tips-for-creating-context-free-grammar/15130451#15130451"><strong>Tips for creating Context free grammars</strong></a>. </p> <h2>Grammar for {a<sup>n</sup> b<sup>m</sup> | n,m = 0,1,2,..., n &lt;= 2m }</h2> <blockquote> <p>What is you language L = {a<sup>n</sup> b<sup>m</sup> | n,m = 0,1,2,..., n &lt;= 2m } description?</p> </blockquote> <p><strong>Language description</strong>: The language <strong>L</strong> is consist of set of all strings in which symbols <em><code>a</code></em> followed by symbols <em><code>b</code></em>, where number of symbol <em><code>b</code></em> are more than or equals to <strong>half</strong> of number of <em><code>a</code></em>'s. </p> <p>To understand more clearly: </p> <p>In pattern <strong>a<sup>n</sup> b<sup>m</sup></strong>, first symbols <em><code>a</code></em> come then symbol <em><code>b</code></em>. total number of <em><code>a</code></em> 's is <code>n</code> and number of <em><code>b</code></em>'s is <code>m</code>. The inequality equation says about relation between <code>n</code> and <code>m</code>. To understand the equation: </p> <pre><code>given: n &lt;= 2m =&gt; n/2 &lt;= m means `m` should be = or &gt; then n/2 =&gt; numberOf(b) &gt;= numberOf(a)/2 ...eq-1 </code></pre> <p>So inequality of <em>n</em> and <em>m</em> says: </p> <blockquote> <p>numberOf(<strong>b</strong>) must be <em>more then or equals</em> to <strong>half</strong> of numberOf(<strong>a</strong>)</p> </blockquote> <p>Some example strings in L: </p> <pre><code>b numberOf(a)=0 and numberOf(b)=1 this satisfy eq-1 bb numberOf(a)=0 and numberOf(b)=2 this satisfy eq-1 </code></pre> <p>So in language string any number of <em><code>b</code></em> are possible without <em><code>a</code></em>'s. (any string of b) because any number is greater then zero (0/2 = 0). </p> <p>Other examples: </p> <pre><code> m n -------------- ab numberOf(a)=1 and numberOf(b)=1 &gt; 1/2 abb numberOf(a)=1 and numberOf(b)=2 &gt; 1/2 abbb numberOf(a)=1 and numberOf(b)=3 &gt; 1/2 aabb numberOf(a)=2 and numberOf(b)=2 &gt; 2/2 = 1 aaabb numberOf(a)=3 and numberOf(b)=2 &gt; 3/2 = 1.5 aaaabb numberOf(a)=4 and numberOf(b)=2 = 4/2 = 2 </code></pre> <p>Points to be note: </p> <ul> <li><p>all above strings are possible because number of <em><code>b</code></em>'s are either equal(=) to half of the number of <em><code>a</code></em> <strong>or</strong> more (>). </p></li> <li><p>and interesting point to notice is that total <em><code>a</code></em>'s can also be more then number of <em><code>b</code></em>'s, <em>but not too much</em>. Whereas number of <em><code>b</code></em>'s can be more then number of <em><code>a</code></em>'s by any number of times.</p></li> </ul> <p>Two more important case are: </p> <ul> <li><p>only <code>a</code> as a string not possible. </p></li> <li><p><strong>note:</strong> null <code>^</code> string is also allowed because in <code>^</code> , <code>numberOf(a) = numberOf(b) = 0</code> that satisfy equation. </p></li> </ul> <p><sub>At once, it look that writing grammar is tough but really not...</sub></p> <p>According to language description, we need following kinds of rules: </p> <p><strong>rule 1</strong>: To generate <code>^</code> null string. </p> <pre><code> N --&gt; ^ </code></pre> <p><strong>rule 2</strong>: To generate any number of <em><code>b</code></em> </p> <pre><code> B --&gt; bB | b </code></pre> <p><strong>Rule 3</strong>: to generate <em><code>a</code></em>'s:<br> (1) Remember you can't generate too many <em><code>a</code></em>'s without generating <em><code>b</code></em>'s.<br> (2) Because <em><code>b</code></em>'s are more then = to half of <em><code>a</code></em>'s; you need to generate one <em><code>b</code></em> for every alternate <em><code>a</code></em><br> (3) Only <code>a</code> as a string not possible so for first (odd) alternative you need to add <em><code>b</code></em> with an <em><code>a</code></em><br> (4) Whereas for even alternative you <strong>can</strong> discard to add <em><code>b</code></em> (<em>but not compulsory</em>) </p> <p>So you overall grammar: </p> <pre><code> S --&gt; ^ | A | B B --&gt; bB | b A --&gt; aCB | aAB | ^ C --&gt; aA | ^ </code></pre> <p>here <code>S</code> is start Variable. </p> <p>In the above grammar rules you may have confusion in <code>A --&gt; aCB | aAB | ^</code>, so below is my explanation: </p> <pre><code>A --&gt; aCB | aAB | ^ ^_____^ for second alternative a C --&gt; aA &lt;== to discard `b` and aAB to keep b </code></pre> <p>let us we generate some strings in language using this grammar rules, I am writing Left most derivation to avoid explanation. </p> <pre><code> ab S --&gt; A --&gt; aCB --&gt; aB --&gt; ab abb S --&gt; A --&gt; aCB --&gt; aB --&gt; abB --&gt; abb abbb S --&gt; A --&gt; aCB --&gt; aB --&gt; abB --&gt; abB --&gt; abbB --&gt; abbb aabb S --&gt; A --&gt; aAB --&gt; aaABB --&gt; aaBB --&gt; aabB --&gt; aabb aaabb S --&gt; A --&gt; aCB --&gt; aaAB --&gt; aaaABB --&gt; aaaBB --&gt; aaabB --&gt; aaabb aaaabb S --&gt; A --&gt; aCB --&gt; aaAB --&gt; aaaCBB --&gt; aaaaABB --&gt; aaaaBB --&gt; aaaabB --&gt; aaaabb </code></pre> <p>One more for <em>non-member</em> string: </p> <p>according to language a<sup>5</sup> b<sup>2</sup> = <code>aaaaabb</code> is <strong>not</strong> possible. because 2 >= 5/2 = 2.5 ==> 2 >= 2.5 inequality fails. So we can't generate this string using grammar too. I try to show below: </p> <p>In our grammar to generate extra <em><code>a</code></em>'s we have to use C variable. </p> <pre><code>S --&gt; A --&gt; aCB --&gt; aaAB --&gt; aa aCB B --&gt; aaa aA BB --&gt; aaaa aCB BB --- ^ here with firth `a` I have to put a `b` too </code></pre> <p>While my answer is done but I think you can change <code>A</code>'s rules like: </p> <pre><code>A --&gt; aCB | A | ^ </code></pre> <p>Give it a Try!!</p> <p><strong>EDIT:</strong><br> <sub>as @us2012 commented: <em>It would seem to me that then, <code>S -&gt; ^ | ab | aaSb | Sb</code> would be a simpler description</em>. I feel this question would be good for OP and other also.</sub> </p> <p>OP's language: </p> <blockquote> <p>L = {a<sup>n</sup> b<sup>m</sup> | n,m = 0,1,2,..., n &lt;= 2m}. </p> </blockquote> <p>@us2012's Grammar: </p> <pre><code>S -&gt; ^ | ab | aaSb | Sb </code></pre> <p>@us2012's question:</p> <blockquote> <p>Whether this grammar also generates language L? </p> </blockquote> <p>Answer is <strong>Yes!</strong></p> <p>The inequality in language between number of <em><code>a</code></em>'s = <code>n</code> and number of <em><code>b</code></em> = m is <code>n =&lt; 2m</code></p> <p>We can also understand as: </p> <pre><code> n =&lt; 2m that is numberOf(a) = &lt; twice of numberOf(b) </code></pre> <p>And In grammar, when even we add <em>one</em> or <em>two</em> <em><code>a</code></em>'s we also add <em>one</em> <code>b</code> . So ultimately number of <em><code>a</code></em> can't be more then twice of number of <em><code>b</code></em>. </p> <p>Grammar also have rules to generate. any numbers of <em><code>b</code></em>'s and null <code>^</code> strings. </p> <p>So the simplified Grammar provided by @us2012 is CORRECT and also generates language L exactly. </p> <p><strong>Notice:</strong> The first solution came from derivation as I written in am linked answer, I started with language description then tried to write some basic rules and progressively I could write complete grammar. </p> <p>Whereas @us2012 answer came by aptitude, you can gain the aptitude to write grammar by reading others solution and writing your for same..like you learn programming. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload