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

LRparser

This document contains code for an LR parser that evaluates simple arithmetic expressions. It uses a stack to parse the infix notation into postfix notation. It prompts the user for an expression like "id+id*id", then iterates through the characters, pushing operands and operators onto a stack. It pops operators and operands off the stack to evaluate the expression in postfix order, pushing the result back onto the stack.

Uploaded by

dramyadorai
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

LRparser

This document contains code for an LR parser that evaluates simple arithmetic expressions. It uses a stack to parse the infix notation into postfix notation. It prompts the user for an expression like "id+id*id", then iterates through the characters, pushing operands and operators onto a stack. It pops operators and operands off the stack to evaluate the expression in postfix order, pushing the result back onto the stack.

Uploaded by

dramyadorai
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

LR parser:

#include<stdio.h>
#include<conio.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';
}
void printstat()
{
int i;
printf("\n $");
for(i=0;i<=top;i++)
printf("%c",stack[i]);
}
void main()
{
int i,k,l;
char s1[20],s2[20],ch1,ch2;
clrscr();
printf("\n ENTER THE EXPRESSIOPN:");
scanf("%s",s1);
l=strlen(s1);
printf("\n$");
for(i=0;i<l;i++)
{
if(s1[i]=='i'&&s1[i+1]=='d')
{
s1[i]=' ';
s1[i+1]='E';
printstat();
printf("id");

push('E');
printstat();
}
else if(s1[i]=='+'||s1[i]=='-'||s1[i]=='*'||s1[i]=='/'||s1[i]=='d')
{
push(s1[i]);
printstat();
}
}
printstat();
l=strlen(s2);
while(1)
{
ch1=pop();
if(ch1=='x')
{
printf("\n$");
break;
}
if(ch1=='+'|| ch1=='-'||ch1=='*'||ch1=='/')
{
ch2=pop();
if(ch2!='E')
{
printf("ERROR");
exit();
}
else
{
push('E');
printstat();
}
}
}
getch();
}
OUTPUT:
ENTER THE EXPRESSIOPN:id+id*id
$
$id
$E
$E+
$E+id

$E+E
$E+E*
$E+E*id
$E+E*E
$E+E*E
$E+E
$E
$

You might also like