Constructing SLR(1) Parsing Table | Compiler Design - Computer Science Engineering (CSE) PDF Download

CONSTRUCTING SLR(1) PARSING TABLE

To perform SLR parsing, take grammar as input and do the following:

1.   Find LR(0) items.

2.  Completing the closure.

3.  Compute goto(I,X), where, I is set of items and X is grammar symbol.

 

LR(0) items:

An LR(0) item of a grammar G is a production of G with a dot at some position of the right side. For example, production A → XYZ yields the four items :

A→.XYZ

A → X . YZ

A → XY . Z

A → XYZ .

 

Closure operation:

If I is a set of items for a grammar G, then closure(I) is the set of items constructed from I by the two rules:

1.   Initially, every item in I is added to closure(I).

2.   If A → α . Bβ is in closure(I) and B → γ is a production, then add the item B → . γ to I , if it

is not already there. We apply this rule until no more new items can be added to closure(I).

Goto operation:

Goto(I, X) is defined to be the closure of the set of all items [A→ αX . β] such that [A→ α . Xβ] is in I.

Steps to construct SLR parsing table for grammar G are:

1.   Augment G and produce G’

2.  Construct the canonical collection of set of items C for G’

3.  Construct the parsing action function action and goto using the following algorithm that requires FOLLOW(A) for each non-terminal of grammar.

 

Algorithm for construction of SLR parsing table:

Input : An augmented grammar G’

Output : The SLR parsing table functions action and goto for G’

Method :

1. Construct C = {I0, I1, …. In}, the collection of sets of LR(0) items for G’.

2. State i is constructed from Ii.. The parsing functions for state i are determined as follows:

(a) If [A→α∙aβ] is in Ii and goto(Ii,a) = Ij, then set action[i,a] to “shift j”. Here a must be terminal.

(b) If [A→α∙] is in Ii , then set action[i,a] to “reduce A→α” for all a in FOLLOW(A).

(c)   If [S’→S.] is in Ii, then set action[i,$] to “accept”.

If any conflicting actions are generated by the above rules, we say grammar is not SLR(1). 3. The goto transitions for state i are constructed for all non-term

If goto(Ii,A) = Ij, then goto[i,A] = j.

4.   All entries not defined by rules (2) and (3) are made “error”

5.   The initial state of the parser is the one constructed from the [S’→.S].

The document Constructing SLR(1) Parsing Table | Compiler Design - Computer Science Engineering (CSE) is a part of the Computer Science Engineering (CSE) Course Compiler Design.
All you need of Computer Science Engineering (CSE) at this link: Computer Science Engineering (CSE)
26 videos|66 docs|30 tests

Top Courses for Computer Science Engineering (CSE)

FAQs on Constructing SLR(1) Parsing Table - Compiler Design - Computer Science Engineering (CSE)

1. What is SLR(1) parsing?
Ans. SLR(1) parsing is a bottom-up parsing technique used in compiler construction to analyze and process programming languages. It stands for Simple LR(1) parsing and is based on a deterministic finite automaton called the LR(1) automaton. SLR(1) parsing uses a parsing table to determine the next action based on the current state and input symbol, allowing it to efficiently recognize and parse the input language.
2. How is the SLR(1) parsing table constructed?
Ans. The construction of an SLR(1) parsing table involves several steps. First, the grammar is augmented by adding a new start symbol and a new production rule for it. Then, the LR(0) items are computed for each production rule. The closure operation is applied to these items to obtain the complete set of items. Next, the transition function is used to compute the set of items for each state in the LR(0) automaton. Finally, the parsing table is constructed by filling in the appropriate actions (shift, reduce, or accept) for each state and input symbol combination.
3. What is the significance of the SLR(1) parsing table?
Ans. The SLR(1) parsing table is a crucial component in the SLR(1) parsing process. It serves as a guide for the parser to determine the appropriate action for each input symbol and current state. The table contains entries for each state and input symbol, specifying whether to shift to a new state, reduce using a particular production rule, or accept the input if it matches the grammar. By following the entries in the parsing table, the parser can efficiently process the input language and detect any syntax errors.
4. What is the difference between SLR(1) and LR(1) parsing?
Ans. The main difference between SLR(1) and LR(1) parsing lies in the construction of the parsing table. SLR(1) parsing tables are simpler to construct as they use the LR(0) automaton and a subset of LR(1) items. This simplification makes SLR(1) parsing more efficient in terms of table size and construction time. However, LR(1) parsing tables are more powerful and can handle a larger class of grammars, including those with more complex productions and conflicts. LR(1) parsing is more precise but comes at the cost of increased table size and construction complexity.
5. Can SLR(1) parsing handle all types of grammars?
Ans. No, SLR(1) parsing cannot handle all types of grammars. SLR(1) parsing is limited to a subset of grammars known as SLR(1) grammars. These grammars must satisfy certain properties to ensure that the parsing process is unambiguous and efficient. For example, the grammar must be unambiguous, contain no left recursion, and have a limited number of conflicts. If a grammar does not meet these restrictions, SLR(1) parsing may produce incorrect results or fail altogether. In such cases, more powerful parsing techniques like LR(1) or LALR(1) should be used.
26 videos|66 docs|30 tests
Download as PDF
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Constructing SLR(1) Parsing Table | Compiler Design - Computer Science Engineering (CSE)

,

past year papers

,

Constructing SLR(1) Parsing Table | Compiler Design - Computer Science Engineering (CSE)

,

Extra Questions

,

mock tests for examination

,

video lectures

,

Previous Year Questions with Solutions

,

Free

,

Viva Questions

,

Sample Paper

,

pdf

,

practice quizzes

,

Exam

,

Objective type Questions

,

MCQs

,

study material

,

Semester Notes

,

shortcuts and tricks

,

Summary

,

ppt

,

Important questions

,

Constructing SLR(1) Parsing Table | Compiler Design - Computer Science Engineering (CSE)

;