Compiler Design

Program for construction of LR Parsing table using C

C Program to Recognize Strings Under 'a*', 'a*b+', 'abb'

ALGORITHM:

  1. Get the input expression and store it in the input buffer.
  2. Read the data from the input buffer one at the time and convert in to corresponding Non Terminal using production rules available.
  3. Perform push & pop operation for LR parsing table construction.
  4. Display the result with conversion of corresponding input symbols to production and production reduction to start symbol. No operation performed on the operator.

Algorithm Explanation:

  1. Input Expression:

    • The algorithm begins by getting the input expression from the user and storing it in an input buffer. This is typically a string representing a mathematical or logical expression.
  2. Reading Input:

    • It reads the data from the input buffer character by character. For each character, it attempts to convert it into a corresponding non-terminal using production rules.
  3. Push & Pop Operations:

    • The algorithm performs push and pop operations on a stack to manage the current state of the parsing process. This stack helps in managing the order of operations and productions applied during parsing.
  4. Display Result:

    • Finally, the result is displayed, showing how the input symbols have been converted to productions and how reductions have occurred to derive the start symbol of the grammar. Note that operators do not perform any specific operations in this parsing process.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char stack[30];
int top = -1;

void push(char c) {
top++;
stack[top] = c;
}

char pop() {
char c;
if (top != -1) {
c = stack[top];
top--;
return c;
}
return 'x'; // Return 'x' if the stack is empty
}

void printstat() {
int i;
printf("\n\t\t\t $");
for (i = 0; i <= top; i++)
printf("%c", stack[i]);
}

void main() {
int i, j, l;
char s1[20], ch1, ch2, ch3;

printf("\n\n\t\t LR PARSING");
printf("\n\t\t ENTER THE EXPRESSION: ");
scanf("%s", s1);
l = strlen(s1);

printf("\n\t\t $");

for (i = 0; i < l; i++) {
if (s1[i] == 'i' && s1[i + 1] == 'd') { // Assume 'id' is a terminal
s1[i] = ' '; // Replace 'id' with a space
s1[i + 1] = 'E'; // Map 'id' to non-terminal 'E'
printstat();
printf(" id");
push('E'); // Push non-terminal onto the stack
printstat();
} else if (s1[i] == '+' || s1[i] == '-' || s1[i] == '*' || s1[i] == '/') {
push(s1[i]); // Push operator onto the stack
printstat();
}
}

printstat();

// Process the stack until it's empty
while (top >= 0) {
ch1 = pop();

if (ch1 == 'x') { // If the stack is empty
printf("\n\t\t\t $");
break;
}

if (ch1 == '+' || ch1 == '/' || ch1 == '*' || ch1 == '-') {
ch3 = pop();
if (ch3 != 'E') { // Check if the top of the stack is 'E'
printf("error");
exit(1);
} else {
push('E'); // If 'E' is found, push it back
printstat();
}
}

ch2 = ch1; // Assign for future operations
}

getch(); // Wait for user input before closing
}

Key Components:

  1. Stack Operations:

    • The stack is implemented using an array, where push and pop functions manage the stack operations. The top variable tracks the current top of the stack.
  2. Input Handling:

    • The program accepts an expression from the user. It specifically looks for the sequence “id” to represent an identifier and maps it to a non-terminal ‘E’.
  3. Production Rules:

    • The program checks for operators (‘+’, ‘-‘, ‘*’, ‘/’) and applies rules as needed. If a valid sequence is found (like an operator followed by ‘E’), it processes it accordingly.
  4. Error Handling:

    • Basic error handling is implemented; if the expected non-terminal isn’t found when processing an operator, it prints “error” and exits.
  5. Display Function:

    • The printstat function visually represents the contents of the stack, helping to understand the state of parsing at various stages.
OUTPUT:

LR PARSING

ENTER THE EXPRESSION

id+id*id-id

$

$id

$E

$E+

$E+id

$E+E

$E+E*

$E+E*id

$E+E*E

$E+E*E-

$E+E*E-id

$E+E*E-E

$E+E*E-E

$E+E*E

$E

$
 

Team Educate

About Author

Leave a comment

Your email address will not be published. Required fields are marked *

You may also like

C Program to Recognize Strings Under 'a*', 'a*b+', 'abb'
Compiler Design

C Program to Recognize Strings Under ‘a’, ‘ab+’, ‘abb’

This C program is designed to recognize and classify strings according to three specific rules or patterns: a*: A string
Convert from NFA to DFA using Thompson’s rule for (a+b)*abb
Compiler Design

Convert from NFA to DFA using Thompson’s rule for (a+b)*abb

To convert the regular expression (a + b)*abb from an NFA to a DFA using Thompson’s construction, we will follow