0% found this document useful (0 votes)
4 views8 pages

CD Record Exp 8

The document outlines the design of an LALR bottom-up parser for a given language, detailing the algorithms for creating item sets and constructing parsing tables. It includes a C program that implements the parsing logic, demonstrating how to handle input strings and manage stack operations. The output section provides a sample run of the parser, showing the stack and input at various stages, ultimately indicating whether the input is accepted.

Uploaded by

yuvaraj250921
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views8 pages

CD Record Exp 8

The document outlines the design of an LALR bottom-up parser for a given language, detailing the algorithms for creating item sets and constructing parsing tables. It includes a C program that implements the parsing logic, demonstrating how to handle input strings and manage stack operations. The output section provides a sample run of the parser, showing the stack and input at various stages, ultimately indicating whether the input is accepted.

Uploaded by

yuvaraj250921
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Roll No:

EXERCISE – 8

Aim: Design LALR bottom-up parser for the given language.


Description:
The LALR (Look Ahead-LR) parsing technique is between SLR and Canonical LR, both in terms of power
of parsing grammars and ease of implementation. This method is often used in practice because the tables
obtained by it are considerably smaller than the Canonical LR tables, yet most common syntactic constructs
of programming languages can be expressed conveniently by an LALR grammar. The same is almost true
for SLR grammars, but there are a few constructs that cannot be handled by SLR techniques. A core is a set
of LR (0) (SLR) items for the grammar, and an LR (1) (Canonical LR) grammar may produce more than
two sets of items with the same core. The core does not contain any look ahead information.
Algorithm:
Creating new item sets:
1. For each terminal and non-terminal symbol A appearing after a '•' in each already existing item set
k, create a new item set m by adding to m all the rules of k where '•' is followed by A, but only if m
will not be the same as an already existing item set after step 3.
2. Shift all the '•'s for each rule in the new item set one symbol to the right.
3. Create the closure of the new item set .
4. Repeat from step 1 for all newly created item sets, until no more new sets appear.

Algorithm to design CLR parsing table:


1. Construct C= {I0,I1………..,In}, the collection of sets of LR(1) items for G‟.
2. State I of the parser is constructed from Ii. The parsing actions for state I are determined as follows:
a. If [A → α. a β, b] is in Ii , and goto (Ii, a) = Ij, then set action [ i,a] to “shift j.” Here, a is
required to be a terminal.
b. If [ A → α., a] is in Ii , A ≠ S‟, then set action[ i,a] to “reduce A → α.”
c. If [S‟→S.,$] is in Ii, then set action[ i ,$] to “accept.”
3. The goto transition for state i are determined as follows: If goto(Ii , A)= Ij ,then goto[i,A]=j.
4. All entries not defined by rules (2) and (3) are made “error.”
5. The initial state of the parser is the one constructed from the set containing item [S‟→.S, $].

Algorithm to design LALR parsing table:


1. Construct C= {I0, I1, I2… In}, the collection of sets of LR(1) items.
2. For each core present in among the set of LR (1) items , find all sets having the core, and replace
these sets by their union.
3. Parsing action table is constructed as for Canonical LR.
4. The goto table is constructed by taking the union of all sets of items having the same core. If J is
the union of one or more sets of LR (1) items, that is, J=I1 U I2 U … U Ik, then the cores of
goto(I1,X), goto(I2,X),…, goto(Ik, X) are the same as all of them have same core. Let K be the
union of all sets of items having same core as goto(I1, X). Then goto(J,X)=K.

33 | P a g e
Roll No:

Program:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
void push(char *,int *,char);
char stacktop(char *);
void isproduct(char,char);
int ister(char);
int isnter(char);
int isstate(char);
void error();
void isreduce(char,char);
char pop(char *,int *);
void printt(char *,int *,char [],int);
void rep(char [],int);
struct action{
char row[6][5];
};
const struct action A[12]={
{"sf","emp","emp","se","emp","emp"},{"emp","sg","emp","emp","emp","acc"},
{"emp","rc","sh","emp","rc","rc"},{"emp","re","re","emp","re","re"},
{"sf","emp","emp","se","emp","emp"},{"emp","rg","rg","emp","rg","rg"},
{"sf","emp","emp","se","emp","emp"},{"sf","emp","emp","se","emp","emp"},
{"emp","sg","emp","emp","sl","emp"},{"emp","rb","sh","emp","rb","rb"},
{"emp","rb","rd","emp","rd","rd"},{"emp","rf","rf","emp","rf","rf"}
};
struct gotol{
char r[3][4];
};
const struct gotol G[12]={
{"b","c","d"},{"emp","emp","emp"},{"emp","emp","emp"},{"emp","emp","emp"},{"i","c","d"},
{"emp","emp","emp"},{"emp","j","d"},{"emp","emp","k"},{"emp","emp","emp"},{"emp","emp","emp"},
};
char ter[6]={'i','+','*',')','(','$'};
char nter[3]={'E','T','F'};

34 | P a g e
Roll No:

char states[12]={'a','b','c','d','e','f','g','h','m','j','k','l'};
char stack[100];
int top=-1;
char temp[10];
struct grammar{
char left;
char right[5];
};
const struct grammar rl[6]={
{'E',"e+T"},{'E',"T"},{'T',"T*F"},{'T',"F"},{'F',"(E)"},{'F',"i"},
};
void main(){
char inp[80],x,p,dl[80],y,bl='a';
int i=0,j,k,l,n,m,c,len;
printf(" Enter the input : ");
scanf("%s",inp);
len=strlen(inp);
inp[len]='$';
inp[len+1]='\0';
push(stack,&top,bl);
printf("\n stack \t\t\t input");
printt(stack,&top,inp,i);
do{
x=inp[i];
p=stacktop(stack);
isproduct(x,p);
if(strcmp(temp,"emp")==0)error();
if(strcmp(temp,"acc")==0)
break;
else{
if(temp[0]=='s') {
push(stack,&top,inp[i]);
push(stack,&top,temp[1]);
i++;
}
else {

35 | P a g e
Roll No:

if(temp[0]=='r') {
j=isstate(temp[1]);
strcpy(temp,rl[j-2].right);
dl[0]=rl[j-2].left;
dl[1]='\0';
n=strlen(temp);
for(k=0;k<2*n;k++)
pop(stack,&top);
for(m=0;dl[m]!='\0';m++)
push(stack,&top,dl[m]);
l=top;
y=stack[l-1];
isreduce(y,dl[0]);
for(m=0;temp[m]!='\0';m++)
push(stack,&top,temp[m]);
}
}
}
printt(stack,&top,inp,i);
}
while(inp[i]!='\0');
if(strcmp(temp,"acc")==0)
printf(" \n accept the input ");
else
printf(" \n do not accept the input ");
getch();
}
void push(char *s,int *sp,char item){
if(*sp==100)
printf(" stack is full ");
else
{
*sp=*sp+1;
s[*sp]=item;
}
}

36 | P a g e
Roll No:

char stacktop(char *s) {


char i;
i=s[top];
return i;
}
void isproduct(char x,char p) {
int k,l;
k=ister(x);
l=isstate(p);
strcpy(temp,A[l-1].row[k-1]);
}
int ister(char x) {
int i;
for(i=0;i<6;i++)
if(x==ter[i])
return i+1;
return 0;
}
int isnter(char x) {
int i;
for(i=0;i<3;i++)
if(x==nter[i])
return i+1;
return 0;
}
int isstate(char p) {
int i;
for(i=0;i<12;i++)
if(p==states[i])
return i+1;
return 0;
}
void error() {
printf(" error in the input ");
exit(0);
}

37 | P a g e
Roll No:

void isreduce(char x,char p) {


int k,l;
k=isstate(x);
l=isnter(p);
strcpy(temp,G[k-1].r[l-1]);
}
char pop(char *s,int *sp){
char item;
if(*sp==-1)
printf(" stack is empty ");
else {
item=s[*sp];
*sp=*sp-1;
}
return item;
}
void printt(char *t,int *p, char inp[],int i) {
int r;
printf("\n");
for(r=0;r<=*p;r++)
rep(t,r);
printf("\t\t\t");
for(r=i;inp[r]!='\0';r++)
printf("%c",inp[r]);
}
void rep(char t[],int r) {
char c;
c=t[r];
switch(c) {
case 'a': printf("0");
break;
case 'b': printf("1");
break;
case 'c': printf("2");
break;
case 'd': printf("3");

38 | P a g e
Roll No:

break;
case 'e': printf("4");
break;
case 'f': printf("5");
break;
case 'g': printf("6");
break;
case 'h': printf("7");
break;
case 'm': printf("8");
break;
case 'j': printf("9");
break;
case 'k': printf("10");
break;
case 'l': printf("11");
break;
default :printf("%c",t[r]);
break;
}
}

39 | P a g e
Roll No:

Output:

Stack Input
0 i*i+i*i$
0i5 *i+i*i$
0F3 *i+i*i$
0T2 *i+i*i$
0T2*7 i+i*i$
0T2*7i5 +i*i$
0T2*7F10 +i*i$
0E1 +i*i$
0E1+6 i*i$
0E1+6i5 *i$
0E1+6F3 *i$
0E1+6T9 *i$
0E1+6T9*7 *i$
0E1+6T9*7i5 $
0E1+6*7F10 $
0E1+6T9 $
0E1 $
Accept the Input

40 | P a g e

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy