You are on page 1of 13

SLR PARSER

Kevin Thomas Koshy (RA2011003010018)


Bhavansh Gali (RA2011003010019)
Objective
To implement a code for generating an SLR parsing table for the given production
rules and to check if there exists any shift-reduce or reduce-reduce conflict for the
given production rules.
PARSER OVERVIEW
What is a Parser?
In the context of programming languages, a parser is often used to analyze source
code and convert it into an internal representation that can be further processed or
executed by a compiler or interpreter. The parser's job is to check the syntax of the
code and identify any errors or inconsistencies, such as missing or mismatched
parentheses of semicolons.

Parsers can be implemented using a variety of techniques, including regular


expressions, context-free grammars, and top-down or bottom-upparsing algorithms.
Bottom-Up Parser
A bottom-up parser is a type of parsing algorithm used in Computer Science to analyze and
parse input data based on a formal grammar. Unlike top-down parsers, which start with the
start symbol of the grammar and build the parse tree from the root to the leaves, bottom-up
parsers build the parse tree from the leaves to the root.

Bottom-up parsers work by identifying the smallest substrings of the input data that can be
matched to a production rule in the grammar,called tokens or terminals. These tokens are
then combined into larger and larger substrings until the entire input data is parsed and the
root of the parse tree is constructed.
LR Parser
LR parsing is a bottom-up parsing technique, meaning that the parser begins with the input
tokens and attempts to build up the parse tree from the leaves to the root. The parser uses a
stack to keep track of its current state, and the parsing table to determine which actions to
take based on the current state and input symbol.

There are several different types of LR parsers, which differ in the amount of lookahead they
use to make parsing decisions. LR(0) parsers have no lookahead, meaning they make
parsing decisions based solely on the current state. SLR(1) parsers use a one-token
lookahead to make decisions, while LALR(1) parsers use a more powerful lookahead
mechanism to make more informed parsing decisions.
SLR Parser
SLR parser stands for Simple LR parser. It is a type of bottom-up parser used in computer
science to parse and analyze input data based on a formal grammar. SLR parsing is a
simpler and less powerful version of LR parsing, but it is still capable of handling a wide
range of context free grammars.

SLR parsing is similar to LR parsing, but it uses a simplified parsing table that is generated
from a reduced LR(0) item set. This makes the parsing table smaller and faster to construct,
but it also means that the parser may be less powerful than other types of LR parsers.
SLR parsers use a deterministic finite automaton (DFA) to recognize the valid prefixes of the
input string, and a parsing table that contains entries for each state and input symbol. The
parsing table contains information about the expected transitions and actions that the parser
should take based on the current state and input symbol.

One of the advantages of SLR parsing is its simplicity and efficiency, as it is often faster and
easier to implement than more complex types of LR parsing.
Algorithm for SLR Parser Implementation
1.) Compute the FIRST and FOLLOW sets: You need to compute the FIRST and
FOLLOW sets for all the non-terminals in the grammar. The FIRST set of a non-terminal
is the set of all terminals that can be derived from that non-terminal. The FOLLOW set of
a non-terminal is the set of all terminals that can appear immediately after that
non-terminal.
2.) Construct the LR(0) items: The LR(0) items are the sets of production rules with a
"dot" (the position marker) that indicates how much of the right-hand side of the rule has
been recognized. To construct the LR(0) items, you start with the augmented grammar
(i.e., the grammar with a new start symbol and a production rule S' → S), and add the
initial item S' → ·S to the set of LR(0) items. Then, for each item A → α·Bβ in the set, you
add the items B → ·γ to the set for all the production rules B → γ in the grammar.
3.) Compute the Closure and Goto sets: The Closure set of an LR(0) item is the set of all
LR(0) items that can be reached by applying production rules to the items in the set. The
Goto set of a Closure set and a symbol a is the set of all LR(0) items that can be reached by
shifting a onto the position marker of a production rule in the Closure set. You can use these
sets to construct the LR(0) automaton.

4.) Construct the SLR(1) parsing table: The SLR(1) parsing table is constructed by applying
the SLR(1) parsing algorithm to the LR(0) automaton. Each cell in the table contains an
action (shift, reduce, or accept) or a state number. If the table contains a shift reduce conflict
or a reduce-reduce conflict, then the grammar is not SLR(1).
Requirements to Run Script

A programming language: SLR parsers can be implemented in various


programming languages, so you will need to choose a language that you are
familiar with. Some popular choices include Python, Java, and C++.

A text editor or Integrated Development Environment (IDE): You will need a text
editor or IDE to write your parser code. There are many options available, such as
Sublime Text, Visual Studio Code, or Eclipse
Parser generator: You will need a parser generator tool to generate the parser
code from your grammar rules.

Grammar rules: You will need to write the grammar rules for the language you
want to parse in the format recognized by the parser generator
Thank You

You might also like