0% found this document useful (0 votes)
30 views

Shift Reduce Parsers Using Stack Allocation

This document describes a C program to implement shift-reduce parsers using a stack. The program takes the number of productions, non-terminals, right-hand sides, and an input string. It creates a stack with '$' at the top and shifts input symbols onto the stack. If the stack matches a production's right-hand side, it replaces it with the left-hand side non-terminal. It repeats shifting and reducing until the input is consumed and only '$' remains on the stack, then displays the result.

Uploaded by

vasanthi2014
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Shift Reduce Parsers Using Stack Allocation

This document describes a C program to implement shift-reduce parsers using a stack. The program takes the number of productions, non-terminals, right-hand sides, and an input string. It creates a stack with '$' at the top and shifts input symbols onto the stack. If the stack matches a production's right-hand side, it replaces it with the left-hand side non-terminal. It repeats shifting and reducing until the input is consumed and only '$' remains on the stack, then displays the result.

Uploaded by

vasanthi2014
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

6.

Shift Reduce Parsers using Stack allocation


Aim:
To write a C program to implement Shift Reduce Parsers
Algorithm:
Get the no.of productions and the input string
Create a stack with $ symbol at the top of the stack
Push the current input symbol to the stack
If the stack that matches with the right side of the production then replace
the RHS with the corresponding non terminal
Repeat Step 3 & 4 until the input string and stack that contains only $
Display the result
Input:
Enter the no of productions -> 3
Enter the non terminal -> E
Enter the RHS -> E+E
Enter the non terminal -> E
Enter the RHS -> E-E
Enter the non terminal -> E
Enter the RHS -> a
Enter the input symbol ->a+a-a
Output:
STACK INPUT
$ a+a-a$
$a +a-a$
$E +a-a$
$E+ a-a$
$E+a -a$
$E+E -a$
$E -a$
$E- a$
$E-a $
$E-E $
$E $
$ $
Success
Result:
Thus the program for shift reduce parsers is successfully executed and the
result is verified.

You might also like