Content deleted Content added
Jason Quinn (talk | contribs) +norefs tag |
m Replaced 1 bare URLs by {{Cite web}}; Replaced "Archived copy" by actual titles |
||
(34 intermediate revisions by 25 users not shown) | |||
Line 1:
{{Short description|Computer mechanic}}
{{more citations needed|date=November 2024}}
In [[computer science]], a '''Simple LR''' or '''SLR parser''' is a type of [[LR parser]] with small [[LR parser#Constructing LR(0) parsing tables|parse table]]s and a relatively simple parser generator algorithm. As with other types of LR(1) parser, an SLR parser is quite efficient at finding the single correct [[bottom-up parsing|bottom-up parse]] in a single left-to-right scan over the input stream, without guesswork or backtracking. The parser is mechanically generated from a formal grammar for the language.
SLR and the more
SLR and LALR were both developed by [[Frank DeRemer]] as the first practical uses of [[Donald Knuth]]'s LR parser theory.<ref>{{Cite web| title=Introduction to Computational Linguistics - LR Parsers | url=https://wiki.eecs.yorku.ca/course_archive/2013-14/W/6339/_media/lrk.pdf | archive-url=https://web.archive.org/web/20210415164259/https://wiki.eecs.yorku.ca/course_archive/2013-14/W/6339/_media/lrk.pdf | archive-date=2021-04-15}}</ref><ref>{{Cite web| title=Introduction to LR-Parsing | url=https://www.seas.upenn.edu/~cis5110/notes/cis511-sl9.pdf | archive-url=https://web.archive.org/web/20240629070724/https://www.seas.upenn.edu/~cis5110/notes/cis511-sl9.pdf | archive-date=2024-06-29}}</ref> The tables created for real grammars by full LR methods were impractically large, larger than most computer memories of that decade, with 100 times or more parser states than the SLR and LALR methods.<ref>{{
== Lookahead
To understand the differences between SLR and LALR, it is important to understand their many similarities and how they both make shift-reduce decisions. (See the article [[LR parser]] now for that background, up through the section on reductions' '''lookahead sets'''.)
The one difference between SLR and LALR is how their generators calculate the lookahead sets of input symbols that should appear next, whenever some completed [[Formal grammar#The syntax of grammars|production rule]] is found and reduced.
SLR generators calculate that lookahead by an easy approximation method based directly on the grammar, ignoring the details of individual parser states and transitions. This ignores the particular context of the current parser state. If some nonterminal symbol ''S'' is used in several places in the grammar, SLR treats those places in the same single way rather than handling them individually. The SLR generator works out <code>Follow(S)</code>, the set of all terminal symbols which can immediately follow some occurrence of ''S''. In the parse table, each reduction to ''S'' uses Follow(S) as its LR(1) lookahead set. Such follow sets are also used by generators for LL top-down parsers. A grammar that has no shift/reduce or reduce/reduce conflicts when using follow sets is called an [[SLR grammar]]. {{citation needed|date=November 2024}}
== Example ==
A grammar that can be parsed by an SLR parser but not by an LR(0) parser is the following:
Line 26 ⟶ 25:
Constructing the action and goto table as is done for LR(0) parsers would give the following item sets and tables:
: S → • E
: + E → • 1 E
: + E → • 1
: E → 1 • E
: E → 1 •
Line 37 ⟶ 36:
: + E → • 1
: S → E •
: E → 1 E •
The action and goto tables:
{| class="wikitable"
! || colspan="2" |''action''||''goto''
|- align="center"
!1||$||E
!0
|s1||||2
|- align="center"
!1
|s1/r2||r2||3
!2
|||acc||
|- align="center"
!3
|r1||r1||
|}
As can be observed there is a shift-reduce conflict for state 1 and terminal '1'. This occurs because, when the action table for an LR(0) parser is created, reduce actions are inserted on a per-row basis. However, by using a follow set, reduce actions can be added with finer granularity. The follow set for this grammar:
{| class="wikitable"
|S||E||1
|- align="center"
! following
|$||$||1,$
|}
A reduce only needs to be added to a particular action column if that action is in the follow set associated with that reduce. This algorithm describes whether a reduce action must be added to an action column:
function mustBeAdded(reduceAction, action) {
}
for example, {{code|mustBeAdded(r2, "1")}} is false, because the left hand side of rule 2 is "E", and 1 is not in E's follow set.
Contrariwise, {{code|mustBeAdded(r2, "$")}} is true, because "$" is in E's follow set.
By using mustBeAdded on each reduce action in the action table, the result is a conflict-free action table:
{| class="wikitable"
! || colspan="2" |''action''||''goto''
|- align="center"
! ''state''
!1||$||E
|- align="center"
!0
|s1||||2
|- align="center"
!1
|s1||r2||3
|- align="center"
!2
|||acc||
|- align="center"
!3
|||r1||
|}
==See also==
Line 157 ⟶ 113:
* [[SLR grammar]]
==References==
{{reflist}}
{{Parsers}}
[[Category:Parsing algorithms]]
|