Just revising on the previous part we have the following relation. Here $Lang$ denote the set of languages defined by the given set of grammars.
Coming to LR parsing we have $3$ main types:
Now we will see more about these:
LR(0) Parser
LR(0) parser is quite uncommon because it is quite restrictive (not in terms of the language but interms of the grammars it can parse). By adding an end delimiter we can remove the prefix-free restriction of $LR(0)$ set of languages (when an end delimiter is added, no string becomes a prefix of any other string) but even then it does not allow $\epsilon$ production for a symbol if that symbol is dervining anything else. Still $LR(0)$ parser is the basic LR parser and will help us understand how LR parsers work in general. SLR(1), CLR(1) and LALR(1) are all extensions to $LR(0)$ parser.
Lets consider a grammar
Now, when we parse a string say “id + id”, at each step we will either Shift or Reduce. For example:
$id + id$ (Shift “id”)
$\quad => T + id$ Reduce $(T \to id)$
$\quad => E + id$ Reduce $(T \to E)$ Shift “+”
$\quad=> E + T$ Reduce $(T \to id)$
$\quad=> E$ Reduce $(E \to E + T)$
$\quad=> E’$ Reduce $(E’ \to E)$
Now, to get these steps programmatically lets construct a DFA. The states of the DFA are the LR(0) items and they are made from the Grammar productions. The formal procedure is given here but the as you can see below, the construction is pretty intuitive. For the start production add a “$\bullet$” to the leftmost point on RHS. Then, if a non-terminal follows, add its production too to the item. Rest of the procedure can even be inferred from the below diagram.
As you can see in the diagram
Now, how we will use this DFA to parse a given string? Say “id+id”. It is not the case that a given string will take the DFA to accepting state – because the given grammar is context-free and not necessarily regular. So, we will need something EXTRA which is a stack. Lets see how this STACK helps.
If this part is thorough, the next $3$ parsers $SLR(1), CLR(1)$ and $LALR(1)$ will be quite straightforward.
This page shows all details regarding GATE CSE 2022 Admissions including results, admission offers and…
Source Add your Response Rank Predictor for GATE CSE 2021 Result Responses: GATE CSE 2021…
Best Books for GATE CSE with Relevant Chapters to Read Heads Up! These GATE CSE…
Indian Statistical Institute(ISI) offers M Tech in Computer Science with the Admission Test Codes MMA/PCA…
Indian Statistical Institute(ISI) offers Junior Research Fellowships (JRF) in Computer Science, Statistics, Mathematics, Quantitative Economics,…
Indian Statistical Institute(ISI) conduct the admission test for Postgraduate Diploma in Computer Applications(PGDCA) with the…
View Comments