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.
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.
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.
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.
Download as DOCX, PDF, TXT or read online from Scribd
Download as docx, pdf, or txt
You are on page 1of 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.