Slr Parsing Table Program In C

SLR Parser Examples. Example 1: Parsing of the input string id. id + id using the SLR parser table for the grammar. To parse a string using First and Follow algorithm and LL-1 parser: Dec 26: To parse a string using First and Follow algorithm and LL-1 parser: Dec 25: Program that prompts the user to enter a character, and on subsequent lines prin. Sep 28: Program to show the implementation of Bottom-Up Parsing: Sep 13: Stacks code in C: Dec 06.

SLR(1) Parsing

It is a simple LR parsing. Most of the function of this parsing is the same as LR(0) parsing. The parsing table for both the parser vary. The SLR(1) parsing use canonical LR(0) item. The reduce move is placed only in the FOLLOW of those variables whose production is reduced.

Slr Parsing Table Program In C Online

The step involves in SLR(1) parsing is given below:

  • Write a CFG for the given input string
  • Check if the grammar is ambiguous or not.
  • Add an augmented grammar.
  • Create a canonical LR(0) item.
  • Draw DFA
  • Construct an SLR(1) parsing table.

Example

E => BB

B => cB / d

Add augment production and insert ‘.’ symbol in the first position for every production in G.

E’ => .E

E => .BB

B => .cB

B => .d

I0 state

Add starting production to the I0 State and Compute the Closure.

I0 = closure (E’ => .E)

Since “.” is on the left side of the symbol. It means we have not seen anything on the right-hand side. Therefore, all productions beginning with E will be added in the I0 State because “.” is followed by the non-terminal. So, the modified I0 State will be:

I0 = E’ => .E

E => .BB

Here we can see that “.” is on the left of the variable B. Therefore, all productions beginning with B will be added in the I0 State because “•” is followed by the non-terminal. So, the modified I0 State becomes:

E’ => .E

Slr Parsing Table Program In C Compiler

E => .BB

B => .cB

B => .d

I1 = Here we have applied Goto on (I0 E) = closure (E’ => E.)

Here, the “.” is on the right side of the symbol, so production is reduced so close the state.

I1 becomes E’ => E.

I2 = we have applied Goto on (I0 B) = Closure (E => B.B)

We apply closure and will get all the A production with “.” in the beginning. So I2 becomes:

Table

E => B.B

B => .cB

B => .d

Goto (I2 c) = Closure (B => c.B) (Same as I3)

Goto on (I2 d) = Closure (B => .d) (Same as I4)

I3 = Goto on (I0 c) = Closure (B => c.B)

Add productions starting with B in I3.

B => c.B

B => .cB

B => .d

Go to on (I3 c) = Closure (B => c. B) (Same as I3)

Go to on (I3 d) = Closure (B => d.) (Same as I4)

I4 = Go to on (I0 d) = Closure (B => d.) = B => d.

I5 = Go to on (I2 B) = Closure (E => BB.)

I6 = Go to on (I3 B) = Closure (B =>cB.)

Productions are numbered as follows:

Program

E =>BB (1)

B => cB (2)

B => d (3)

Language

Drawing DFA

SLR(1) Table

Explanations:

Follow of B = First of B = c and d and Follow of E = $

  • I1 state have the final item which drives E => E and follow(E) = ($), so action (I1 $) = Accept
  • I4 state have the final item which drives B => d and follow(B) = c, d and follow(E) = $ , So action c, d, and $ will be r3
  • I5 state have the final item which drives E => BB and follow(E) = $, So action $ in I5 will be r1
  • I6 state have the final item which drives B => cB and follow(B) = c, d and follow(E) =$, So action c, d, and $ in I6 will be r2.

NOTE:

When a state moves to some other state on terminals, it will correspond to a shift move in the action part.

When a state moves to another state on a variable, it will correspond to the goto move in the go-to part.

When a state has a final item with no transition to the next state, the production is known as reduce production.

A compiler is a program that translates the code that is written in one language to a machine code without changing the logic of the program. The compiler also tries to make the program more efficient.

Compiler design principles give a detailed view of the translation and optimization process of a program. Compiler design covers everything from basic translation mechanism to recovery and error detection. It includes various methods like lexical, syntax, and semantic analysis as front end, and code generation and optimization as back-end.

In this post, we will write the program to generate an SLR parse table from CFG grammar.

Slr Parsing Table Program In C

We will use C++ to write this program due to the standard template library support. Although, it’s very similar to C.

Slr Parsing Table Program In C Programming

Slr Parsing Table Program In C

C# Parsing Text

Input

C Programming String Parsing

Output

Program

Let us know in the comments if you are having any questions regarding this compiler design program.

Slr Parsing Table Program In Ct

And if you found this post helpful, then please help us by sharing this post with your friends. Thank You