Next: About this document ...
Up: Parsing (II)
Previous: Bottom-up parsing
KEY IDEAS.
- The LR parsing method is the most general non-backtrackingshift-reduce parsing method known.
- LR parsers can parse a strictly larger class of grammars than (top-down) predictive parsers.
- LR parsers can usually recognize all programming language construct that can be specified by context-free grammars.
- LR parsers detect errors fast.
- Drawback: it is too much work to construct an LR parser by hand.
- Fortunately, we can use an LR parser generator such as YACC.
THE LR PARSING PRINCIPLE
Figure 9:
Model of an LR parser.
![\begin{figure}\htmlimage
\centering\includegraphics[scale=.5]{LR-Parser-Model.eps}
\end{figure}](img151.png) |
An LR-Parser uses
- states to memorize information during the parsing process,
- an action table to make decision (such as shift or reduce)
and to compute states
- a goto table to compute states
These states are embedded with grammar symbols in a stack.
More, precisely, after eahc iteration, the stack stores a word
of the form
s0X1s1X2 ... sm-1Xmsm where
s0 is the end-of-stack symbol and sm is the state on top of the stack.
For a state s and a terminal a, the entry
action[s, a] has one of
the four following forms
-
shift s' where s' is a state,
-
reduce A
,
-
accept,
-
error.
Algorithm 10
![\fbox{
\begin{minipage}{13 cm}
\begin{description}
\item[{\bf Input:}] A LR-Pars...
...} \\
\> \> {\bf else} \\
\> \> \> {\bf error} \\
\end{tabbing}\end{minipage}}](img152.png)
Next: About this document ...
Up: Parsing (II)
Previous: Bottom-up parsing
Marc Moreno Maza
2004-12-02