Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The lookaheads used in an LR(1) parser are computed as follows. First, the start state has an item of the form</p> <pre><code>S -&gt; .w ($) </code></pre> <p>for every production S -> w, where S is the start symbol. Here, the $ marker denotes the end of the input.</p> <p>Next, for any state that contains an item of the form A -> x.By (t), where x is an arbitrary string of terminals and nonterminals and B is a nonterminal, you add an item of the form B -> .w (s) for every production B -> w and for every terminal in the set FIRST(yt). (Here, FIRST refers to <a href="http://www.cs.virginia.edu/~cs415/reading/FirstFollowLL.pdf">FIRST sets</a>, which are usually introduced when talking about LL parsers. If you haven't seen them before, I would take a few minutes to look over those lecture notes).</p> <p>Let's try this out on your grammar. We start off by creating an item set containing</p> <pre><code>S -&gt; .AB ($) </code></pre> <p>Next, using our second rule, for every production of A, we add in a new item corresponding to that production and with lookaheads of every terminal in FIRST(B$). Since B always produces the string d, FIRST(B$) = d, so all of the productions we introduce will have lookahead d. This gives</p> <pre><code>S -&gt; .AB ($) A -&gt; .aAb (d) A -&gt; .a (d) </code></pre> <p>Now, let's build the state corresponding to seeing an 'a' in this initial state. We start by moving the dot over one step for each production that starts with a:</p> <pre><code>A -&gt; a.Ab (d) A -&gt; a. (d) </code></pre> <p>Now, since the first item has a dot before a nonterminal, we use our rule to add one item for each production of A, giving those items lookahead FIRST(bd) = b. This gives</p> <pre><code>A -&gt; a.Ab (d) A -&gt; a. (d) A -&gt; .aAb (b) A -&gt; .a (b) </code></pre> <p>Continuing this process will ultimately construct all the LR(1) states for this LR(1) parser. This is shown here:</p> <pre><code>[0] S -&gt; .AB ($) A -&gt; .aAb (d) A -&gt; .a (d) [1] A -&gt; a.Ab (d) A -&gt; a. (d) A -&gt; .aAb (b) A -&gt; .a (b) [2] A -&gt; a.Ab (b) A -&gt; a. (b) A -&gt; .aAb (b) A -&gt; .a (b) [3] A -&gt; aA.b (d) [4] A -&gt; aAb. (d) [5] S -&gt; A.B ($) B -&gt; .d ($) [6] B -&gt; d. ($) [7] S -&gt; AB. ($) [8] A -&gt; aA.b (b) [9] A -&gt; aAb. (b) </code></pre> <p>In case it helps, I taught a compilers course last summer and have <strong><a href="http://www.stanford.edu/class/archive/cs/cs143/cs143.1128/">all the lecture slides available online</a></strong>. The slides on bottom-up parsing should cover all of the details of LR parsing and parse table construction, and I hope that you find them useful!</p> <p>Hope this helps!</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