lab manual - crectirupati.com lab manual-r13.pdf · compiler design lab tasks 1. write a program to...

49
LAB MANUAL On COMPILER DESIGN III-B.Tech CSE I Semester (R13) Ms. V. Bhargavi, M.Tech. Asst.Professor CHADALAWADA RAMANAMMA ENGINEERING COLLEGE Chadalawada Nagar, Renigunta Road, Tirupati-517502 Department of Computer Science & Engineering

Upload: doantuyen

Post on 13-Aug-2018

270 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: LAB MANUAL - crectirupati.com Lab Manual-R13.pdf · COMPILER DESIGN Lab Tasks 1. Write a program to search for a given pattern in a set of files. It should support regular expressions

LAB MANUAL

On

COMPILER DESIGN

III-B.Tech CSE I Semester

(R13)

Ms. V. Bhargavi, M.Tech.

Asst.Professor

CHADALAWADA RAMANAMMA ENGINEERING COLLEGE Chadalawada Nagar, Renigunta Road, Tirupati-517502

Department of Computer Science & Engineering

Page 2: LAB MANUAL - crectirupati.com Lab Manual-R13.pdf · COMPILER DESIGN Lab Tasks 1. Write a program to search for a given pattern in a set of files. It should support regular expressions

COMPILER DESIGN

Lab Tasks

1. Write a program to search for a given pattern in a set of files. It should support

regular expressions. It should work similar to grep and fgrep of Linux environment.

2. Write programs for DFA, NFA.

3. Consider the following regular expressions:

(a) (0 + 1)* 1(0+1)(0+1)

(b) (ab*c + (def)++ a*d+e)+

(c) ((a + b)*(c + d)*)++ ab*c*d

Write separate programs for recognizing the strings generated by each of the regular

expressions mentioned above (Using FA).

4. Given a text-file which contains some regular expressions, with only one RE in each

line of the file. Write a program which accepts a string from the user and reports

which regular expression accepts that string. If no RE from the file accepts the string,

then report that no RE is matched.

5. Design a PDA for any given CNF. Simulate the processing of a string using the PDA

and show the parse tree.

6. Design a Lexical analyzer for identifying different types of tokens used in C language

.Note: The reserved keywords such as if, else, class, struct etc must be reported as

invalid identifiers. C allows identifier names to begin with underscore character too.

7. Simulate a simple desktop calculator using any lexical analyzer generator tool (LEX

or FLEX).

8. Program to recognize the identifiers, if and switch statements of C using a lexical

analyzer generator tool.

9. Consider the following grammar:

S --> ABC

A--> abA | ab

B--> b | BC

C--> c | cC

Design any shift reduced parser which accepts a string and tells whether the

string is accepted by above grammar or not.

10. Design a YACC program that reads a C program from input file and identify all valid

C identifiers and for loop statements.

11. Program to eliminate left recursion and left factoring from a given CFG.

12. YACC program that reads the input expression and convert it to post fix expression.

13. YACC program that finds C variable declarations in C source file and save them into

the symbol table, which is organized using binary search tree.

14. YACC program that reads the C statements from an input file and converts them into

quadruple three address intermediate code

Page 3: LAB MANUAL - crectirupati.com Lab Manual-R13.pdf · COMPILER DESIGN Lab Tasks 1. Write a program to search for a given pattern in a set of files. It should support regular expressions

Experiment No: 1

Aim:

To Write a C program to search for a pattern for a given regular expressions. Program:

#include <stdio.h>

#include <string.h>

#define MATCH printf("\nThe Text Matches The Regular Expression");

#define NOTMATCH printf("\nThe Text Doesn't match the Regular Expression");

char reg[20], text[20];

int main()

{

int i, rlen, tlen, f = 0;

char ans;

clrscr();

do {

printf("\nEnter the Regular Expression\n");

scanf(" %[^\n]s", reg);

for (rlen = 0; reg[rlen] != '\0';rlen++);

printf("\nEnter the text\n");

scanf(" %[^\n]s", text);

for (tlen = 0;text[tlen] != '\0' ; tlen++);

if (reg[0] == '*')

{

printf("\nInvalid regular expression");

}

/* If the regular expression starts with Alphabet */

if ((reg[0] >= 65 && reg[0] <= 90) || (reg[0] >= 97 && reg[0] <=122))

{

if (reg[0] == text [0])

{

switch (reg[1])

{

case '.' :

switch (reg[2])

{

case '*':

if (tlen != 1)

{

if (reg[3] == text[tlen-1])

{

MATCH;

}

Page 4: LAB MANUAL - crectirupati.com Lab Manual-R13.pdf · COMPILER DESIGN Lab Tasks 1. Write a program to search for a given pattern in a set of files. It should support regular expressions

else

{

NOTMATCH;

}

}

else

{

NOTMATCH;

}

break;

case '+':

if (text[1] != reg[3])

{

if (reg[3] == text[tlen - 1])

{

MATCH;

}

else

{

NOTMATCH;

}

}

break;

case '?':

if (text[1] == reg[3] || text[2] == reg[3])

{

if (text[1] == reg[3] || text[2] == reg[3])

{

MATCH;

}

else

{

NOTMATCH;

}

}

else

{

NOTMATCH;

}

break;

}

break;

case '*':

if (reg[rlen-1] == text[tlen-1])

Page 5: LAB MANUAL - crectirupati.com Lab Manual-R13.pdf · COMPILER DESIGN Lab Tasks 1. Write a program to search for a given pattern in a set of files. It should support regular expressions

{

for (i = 0;i <= tlen-2;i++)

{

if(text[i] == reg[0])

{

f = 1;

}

else

{

f = 0;

}

}

if ( f == 1)

{

MATCH;

}

else

{

NOTMATCH;

}

}

else

{

NOTMATCH;

}

break;

case '+' :

if (tlen <= 2)

{

NOTMATCH;

}

else if (reg[rlen-1] == text[tlen-1])

{

for (i = 0;i < tlen-2;i++)

{

if (text[i] == reg[0])

{

f = 1;

}

else

{

f = 0;

}

}

Page 6: LAB MANUAL - crectirupati.com Lab Manual-R13.pdf · COMPILER DESIGN Lab Tasks 1. Write a program to search for a given pattern in a set of files. It should support regular expressions

if (f == 1)

{

MATCH;

}

else

{

NOTMATCH;

}

}

break;

case '?':

if (reg[rlen -1] == text[tlen-1])

{

MATCH;

}

else

{

NOTMATCH;

}

break;

}

}

else

printf("Does not match");

}

/* If Regular Expression starts with '^' */

else if (reg[0] == '^')

{

if (reg[1] == text[0])

{

MATCH;

}

else

{

NOTMATCH;

}

}

/* If Regular Expression Ends with '$' */

else if (reg[rlen-1] == '$')

{

if (reg[rlen-2] == text[rlen-1])

{

Page 7: LAB MANUAL - crectirupati.com Lab Manual-R13.pdf · COMPILER DESIGN Lab Tasks 1. Write a program to search for a given pattern in a set of files. It should support regular expressions

MATCH;

}

else

{

NOTMATCH;

}

}

else

printf("Not Implemented");

printf("\nDo you want to continue?(Y/N)");

scanf(" %c", &ans);

} while (ans == 'Y' || ans == 'y');

return 0;

}

Output:

Page 8: LAB MANUAL - crectirupati.com Lab Manual-R13.pdf · COMPILER DESIGN Lab Tasks 1. Write a program to search for a given pattern in a set of files. It should support regular expressions

Experiment No: 2 Aim:

To Write a C program for DFA(Deterministic Finite Automata).

Program:

#include<stdio.h>

#include<string.h>

#define fl(i,a,b) for(i=a; i<b; i++)

#define scan(a) scanf("%d", &a)

#define nline printf("\n")

#define MAX 1000

int states, symbols, symdir[20], final_states, mark[20], mat[20][20];

int cases,curr,limit;

char str1[MAX];

int main()

{

int i, j, k;

printf("Enter the number of states : ");

scan(states);

printf("Enter the number of symbols : ");

scan(symbols);

printf("Enter the symbols : ");

nline;

fl(i,0,symbols)

{

printf("Enter the symbol number %d : ", i);

scan(symdir[i]);

}

printf("Enter the number of final states : ");

scan(final_states);

printf("Enter the number of the states which are final : ");

nline;

fl(i,0,final_states)

{

int temp;

scan(temp);

mark[temp]=1;

}

printf("Define the relations for the states and symbols : ");

nline;

fl(i,0,states)

{

fl(j,0,symbols)

{

Page 9: LAB MANUAL - crectirupati.com Lab Manual-R13.pdf · COMPILER DESIGN Lab Tasks 1. Write a program to search for a given pattern in a set of files. It should support regular expressions

printf("Enter the relation for Q(%d) -> %d : ", i, symdir[j]);

scan(mat[i][symdir[j]]);

}

}

printf("Enter the number of strings to be tested : ");

scan(cases);

fl(k,0,cases)

{

printf("Enter the string to be tested : ");

scanf("%s", &str1);

curr=0;

limit=strlen(str1);

fl(i,0,limit)

{

int ele=(int)(str1[i]-'0');

curr=mat[curr][ele];

}

printf("The entered string is ");

if(mark[curr]==1)

printf("Accepted");

else

printf("Rejected");

nline;

}

return 0;

}

OUTPUT:

Page 10: LAB MANUAL - crectirupati.com Lab Manual-R13.pdf · COMPILER DESIGN Lab Tasks 1. Write a program to search for a given pattern in a set of files. It should support regular expressions

Aim:

To Write a C program for NFA (NonDeterministic Finite Automata).

Program

#include<stdio.h>

#include<string.h>

#define fl(i,a,b) for(i=a; i<b; i++)

#define scan(a) scanf("%d", &a)

#define nline printf("\n")

#define MAX 1000

int cases,states, symbols, symdir[20], final_states, mark[20], mat[20][20][20];

char str1[MAX];

int curr[20], t[20];

int flag=0,ind, ind1,limit;

int l1;

int main()

{

int i, j, k;

fl(i,0,20)

Page 11: LAB MANUAL - crectirupati.com Lab Manual-R13.pdf · COMPILER DESIGN Lab Tasks 1. Write a program to search for a given pattern in a set of files. It should support regular expressions

fl(j,0,20)

fl(k,0,20)

mat[i][j][k]=-1;

printf("Enter the number of states : ");

scan(states);

printf("Enter the number of symbols : ");

scan(symbols);

printf("Enter the symbols : ");

nline;

fl(i,0,symbols)

{

printf("Enter the symbol number %d : ", i);

scan(symdir[i]);

}

printf("Enter the number of final states : ");

scan(final_states);

printf("Enter the number of the states which are final : ");

fl(i,0,final_states)

{

int temp;

scan(temp);

mark[temp]=1;

}

printf("Define the relations for the states and symbols : ");

nline;

fl(i,0,states)

{

fl(j,0,symbols)

{

int ntemp;

printf("Enter the number of relations for Q(%d) -> %d : ", i, symdir[j]);

scan(ntemp);

fl(k,0,ntemp)

{

printf("Enter the relation number %d for Q(%d) -> %d : ", k, i, symdir[j]);

scan(mat[i][symdir[j]][k]);

}

}

}

printf("Enter the number of strings to be tested : ");

scan(cases);

Page 12: LAB MANUAL - crectirupati.com Lab Manual-R13.pdf · COMPILER DESIGN Lab Tasks 1. Write a program to search for a given pattern in a set of files. It should support regular expressions

fl(k,0,cases)

{

printf("Enter the string to be tested : ");

scanf("%s", str1);

curr[0]=0;

ind=1;

limit=strlen(str1);

fl(i,0,limit)

{

int ele=(int)(str1[i]-'0');

ind1=0;

fl(l1,0,ind)

{

j=0;

while(mat[curr[l1]][ele][j]!=-1)

{

t[ind1++]=mat[curr[l1]][ele][j];

j++;

}

}

fl(l1,0,ind1)

{

curr[l1]=t[l1];

}

ind=ind1;

}

fl(i,0,ind)

{

if(mark[curr[i]]==1)

{

flag=1;

break;

}

}

printf("The entered string is ");

if(flag==1)

printf("Accepted");

else

printf("Rejected");

nline;

}

return 0;

}

Page 13: LAB MANUAL - crectirupati.com Lab Manual-R13.pdf · COMPILER DESIGN Lab Tasks 1. Write a program to search for a given pattern in a set of files. It should support regular expressions

OUTPUT:

Page 14: LAB MANUAL - crectirupati.com Lab Manual-R13.pdf · COMPILER DESIGN Lab Tasks 1. Write a program to search for a given pattern in a set of files. It should support regular expressions

Experiement No: 03

Aim:

Write a C program to recognize strings under ‘(ab*c + (def)+ +a*d+e)+’

Program:

#include<stdio.h>

#include<conio.h>

int ninputs;

int check(char,int); //function declaration

int dfa[10][10];

char c[10], string[10];

int main()

{

int nstates, nfinals; int f[10];

int i,j,s,final; clrscr();

printf("enter the number of states that your dfa consist of \n");

scanf("%d",&nstates);

printf("enter the number of input symbol that dfa have \n");

scanf("%d",&ninputs);

printf("\nenter input symbols\t");

for(i=0; i<ninputs; i++)

{

printf("\n\n %d input\t", i+1);

printf("%c",c[i]=getch());

}

printf("\n\nenter number of final states\t");

scanf("%d",&nfinals);

for(i=0; i<nfinals; i++)

{

printf("\n\nFinal state %d : q",i+1);

scanf("%d",&f[i]);

}

printf("-------------------------");

printf("\n\ndefine transition rule as (initial state, input symbol ) = final state\n");

for(i=0; i<ninputs; i++)

{

for(j=0; j<nstates; j++)

{

printf("\n(q%d , %c ) = q",j,c[i]);

scanf("%d",&dfa[i][j]);

}

}

do

{

i=0, final=0, s=0;

printf("\n\nEnter Input String.. ");

Page 15: LAB MANUAL - crectirupati.com Lab Manual-R13.pdf · COMPILER DESIGN Lab Tasks 1. Write a program to search for a given pattern in a set of files. It should support regular expressions

scanf("%s",string);

while(string[i]!='\0')

if((s=check(string[i++],s))<0)

break;

for(i=0 ; i<nfinals ; i++)

if(f[i] ==s )

final=1;

if(final==1)

printf("\n valid string");

else

printf("invalid string");

printf("\nDo you want to end? \n(t) ");

}

while(getch()!='t');

getch();

}

int check(char b,int d)

{

int j;

for(j=0; j<ninputs; j++)

if(b==c[j]){

printf("dfa[%d][%d] = %d\n", d,j,dfa[d][j]);

return(dfa[j][d]);}

return -1;

}

Input & Output:

Enter a String: aaaabbbbb

aaaabbbbb is accepted

Enter a string: cdgs

cdgs is not recognized

Page 16: LAB MANUAL - crectirupati.com Lab Manual-R13.pdf · COMPILER DESIGN Lab Tasks 1. Write a program to search for a given pattern in a set of files. It should support regular expressions

Experiement No: 04

Aim:

To Design a Lexical analyzer for identifying different types of tokens using C

language.

Program:

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

void removeduplicate();

void final();

int Isiden(char ch);

int Isop(char ch);

int Isdel(char ch);

int Iskey(char * str);

void removeduplicate();

char op[8]={'+','-','*','/','=','<','>','%'};

char del[8]={'}','{',';','(',')','[',']',','};

char *key[]={"int","void","main","char","float"};

//char *operato[]={"+","-","/","*","<",">","=","%","<=",">=","++"};

int idi=0,idj=0,k,opi=0,opj=0,deli=0,uqdi=0,uqidi=0,uqoperi=0,kdi=0,liti=0,ci=0;

int uqdeli[20],uqopi[20],uqideni[20],l=0,j;

char uqdel[20],uqiden[20][20],uqop[20][20],keyword[20][20];

char iden[20][20],oper[20][20],delem[20],litral[20][20],lit[20],constant[20][20];

void lexanalysis(char *str)

{

int i=0;

while(str[i]!='\0')

{

if(Isiden(str[i])) //for identifiers

{

while(Isiden(str[i]))

{

iden[idi][idj++]=str[i++];

}

iden[idi][idj]='\0';

idi++;idj=0;

}

else

if(str[i]=='"') //for literals

{

lit[l++]=str[i];

for(j=i+1;str[j]!='"';j++)

Page 17: LAB MANUAL - crectirupati.com Lab Manual-R13.pdf · COMPILER DESIGN Lab Tasks 1. Write a program to search for a given pattern in a set of files. It should support regular expressions

{

lit[l++]=str[j];

}

lit[l++]=str[j];lit[l]='\0';

strcpy(litral[liti++],lit);

i=j+1;

}

else

if(Isop(str[i])) // for operators

{

while(Isop(str[i]))

{

oper[opi][opj++]=str[i++];

}

oper[opi][opj]='\0';

opi++;opj=0;

}

else

if(Isdel(str[i])) //for delemeters

{

while(Isdel(str[i]))

{

delem[deli++]=str[i++];

}

}

else

{

i++;

}

}

removeduplicate();

final();

}

int Isiden(char ch)

{

if(isalpha(ch)||ch=='_'||isdigit(ch)||ch=='.')

return 1;

else

return 0;

}

int Isop(char ch)

{

int f=0,i;

for(i=0;i<8&&!f;i++)

Page 18: LAB MANUAL - crectirupati.com Lab Manual-R13.pdf · COMPILER DESIGN Lab Tasks 1. Write a program to search for a given pattern in a set of files. It should support regular expressions

{

if(ch==op[i])

f=1;

}

return f;

}

int Isdel(char ch)

{

int f=0,i;

for(i=0;i<8&&!f;i++)

{

if(ch==del[i])

f=1;

}

return f;

}

int Iskey(char * str)

{

int i,f=0;

for(i=0;i<5;i++)

{

if(!strcmp(key[i],str))

f=1;

}

return f;

}

void removeduplicate()

{

int i,j;

for(i=0;i<20;i++)

{

uqdeli[i]=0;

uqopi[i]=0;

uqideni[i]=0;

}

for(i=1;i<deli+1;i++) //removing duplicate delemeters

{

if(uqdeli[i-1]==0)

{

uqdel[uqdi++]=delem[i-1];

for(j=i;j<deli;j++)

{

if(delem[i-1]==delem[j])

uqdeli[j]=1;

Page 19: LAB MANUAL - crectirupati.com Lab Manual-R13.pdf · COMPILER DESIGN Lab Tasks 1. Write a program to search for a given pattern in a set of files. It should support regular expressions

}

}

}

for(i=1;i<idi+1;i++) //removing duplicate identifiers

{

if(uqideni[i-1]==0)

{

strcpy(uqiden[uqidi++],iden[i-1]);

for(j=i;j<idi;j++)

{

if(!strcmp(iden[i-1],iden[j]))

uqideni[j]=1;

}

}

}

for(i=1;i<opi+1;i++) //removing duplicate operators

{

if(uqopi[i-1]==0)

{

strcpy(uqop[uqoperi++],oper[i-1]);

for(j=i;j<opi;j++)

{

if(!strcmp(oper[i-1],oper[j]))

uqopi[j]=1;

}

}

}

}

void final()

{

int i=0;

idi=0;

for(i=0;i<uqidi;i++)

{

if(Iskey(uqiden[i])) //identifying keywords

strcpy(keyword[kdi++],uqiden[i]);

else

if(isdigit(uqiden[i][0])) //identifying constants

strcpy(constant[ci++],uqiden[i]);

else

strcpy(iden[idi++],uqiden[i]);

}

// printing the outputs

Page 20: LAB MANUAL - crectirupati.com Lab Manual-R13.pdf · COMPILER DESIGN Lab Tasks 1. Write a program to search for a given pattern in a set of files. It should support regular expressions

printf("\n\tDelemeter are : \n");

for(i=0;i<uqdi;i++)

printf("\t%c\n",uqdel[i]);

printf("\n\tOperators are : \n");

for(i=0;i<uqoperi;i++)

{

printf("\t");

puts(uqop[i]);

}

printf("\n\tIdentifiers are : \n");

for(i=0;i<idi;i++)

{

printf("\t");

puts(iden[i]);

}

printf("\n\tKeywords are : \n");

for(i=0;i<kdi;i++)

{

printf("\t");

puts(keyword[i]);

}

printf("\n\tConstants are :\n");

for(i=0;i<ci;i++)

{

printf("\t");

puts(constant[i]);

}

printf("\n\tLiterals are :\n");

for(i=0;i<liti;i++)

{

printf("\t");

puts(litral[i]);

}

}

void main()

{

char str[50];

//clrscr();

printf("\nEnter the string : ");

scanf("%[^\n]c",str);

lexanalysis(str);

getch();

}

Page 21: LAB MANUAL - crectirupati.com Lab Manual-R13.pdf · COMPILER DESIGN Lab Tasks 1. Write a program to search for a given pattern in a set of files. It should support regular expressions

Input & Output:

Enter the string :

Int a,b,c; a=2; b=3; c=a+b;

Delimiters are: ;;;; , , ,

Identifiers are: a b c

Operators are: = +

Keywords are: int

Constants are: 2 3

Page 22: LAB MANUAL - crectirupati.com Lab Manual-R13.pdf · COMPILER DESIGN Lab Tasks 1. Write a program to search for a given pattern in a set of files. It should support regular expressions

Experiment No: 05

Aim:

Write a C program to test whether a given identifier is valid or not

Program Logic:

1. Read the given input string.

2. Check the initial character of the string is numerical or any special character except ‘_’

then print it is not a valid identifier.

3. Otherwise print it as valid identifier if remaining characters of string doesn’t contains any

special characters except ‘_’.

Program:

#include<stdio.h>

#include<conio.h>

#include<ctype.h>

void main()

{

char a[10]; int flag, i=1;

clrscr();

printf("\n Enter an identifier:");

gets(a);

if(isalpha(a[0]))

flag=1;

else

printf("\n Not a valid identifier");

while(a[i]!='\0')

{

if(!isdigit(a[i])&&!isalpha(a[i]))

{

flag=0;

break;

}

i++;

}

if(flag==1)

printf("\n Valid identifier");

getch();

}

Input & Output:

Enter an identifier: first Valid identifier Enter an identifier: 1aqw Not a valid identifier

Page 23: LAB MANUAL - crectirupati.com Lab Manual-R13.pdf · COMPILER DESIGN Lab Tasks 1. Write a program to search for a given pattern in a set of files. It should support regular expressions

Experiment No: 06

Aim:

Design a PDA for any given CNF. Simulate the processing of a string using the PDA and show the parse tree. Program: #include<iostream.h>

#include<dos.h>

#include<stdio.h>

#include<stdio.h>

#include<conio.h>

struct pdastate

{

int type; char sym; int trno;

int trans[20]; char tsym[20];

};

class pda

{

public:

int n;

pdastate s[30];

pda(void)

{

n=0;

}

void show(void);

};

void pda::show(void)

{

pdastate p;

clrscr();

for(int i=0;i<n;i++)

{

p=s[i];

cout<<""State No " := "<<i;

cout<<"" Tag "";

if(p.type==0)

cout<<"" Start State "";

else

if(p.type==1)

cout<<" " Push "<<p.sym<<""";

else

if(p.type==2)

Page 24: LAB MANUAL - crectirupati.com Lab Manual-R13.pdf · COMPILER DESIGN Lab Tasks 1. Write a program to search for a given pattern in a set of files. It should support regular expressions

cout<<" " Pop State "";

else

if(p.type==3)

cout<<" " Read State "";

else

if(p.type==4)

cout<<" " Stop State ""; for(int j=0;j<p.trno;j++)

{

cout<<" " Transition To State "<<p.trans[j]<<""";

if(p.tsym[j]!=0)

cout<<" " On Symbol "<<p.tsym[j]<<"""<<endl;

else

cout<<endl;

}

getch();

if(i==5)

clrscr();

}

}

union pr

{

char a;

char b[2];

};

struct prod

{

int type; union pr p;

};

class cnf

{

private:

char v[10];

int vno;

char t[10];

int tno;

struct prod p[30];

int n;

int vpr[10];

public:

cnf(void);

pda mkpda(void);

};

cnf:: cnf(void)

{

Page 25: LAB MANUAL - crectirupati.com Lab Manual-R13.pdf · COMPILER DESIGN Lab Tasks 1. Write a program to search for a given pattern in a set of files. It should support regular expressions

clrscr();

char *mess[]={"-","=","["," ","C","F","A"," ","T","O"," ","P","D","A"," ","]","=","-",};

int xx=31,xxx=48,i,j;

_setcursortype(_NOCURSOR);

for(i=0,j=17;i<10,j>=8;i++,j--)

{

gotoxy(xx,1);

cout<<mess[i];

xx++;

gotoxy(xxx,1);

cout<<mess[j];

xxx--;

delay(50);

}

xx=30;xxx=49;

_setcursortype(_NORMALCURSOR);

cout<<" " Enter No Of Non Terminal Symbols ":= ";

cin>>vno;

for(i=0;i<vno;i++)

{

cout<<" " Enter A Non-Terminal Symbol ":=";

cin>>v[i];

}

cout<<" " Enter The No Of Terminal Symbols ":=";

cin>>tno;

for(i=0;i<tno;i++)

{

cout<<" " Enter A Terminal Symbol ":=";

cin>>t[i];

}

cout<<" " Enter The No Of Productions ":=";

cin>>n;

int count=0;

for(i=0;i<vno;i++)

{

cout<<" " Enter No Of Productions Corrosponding To The Non-Terminal "<<v[i]<<" ":= ";

cin>>vpr[i];

for(int j=0;j<vpr[i];j++)

{

cout<<" " Enter The Type Of Production <1> A-->b , <2> a-->BC ":=";

cin>>p[count].type;

if(p[count].type==1)

{

Page 26: LAB MANUAL - crectirupati.com Lab Manual-R13.pdf · COMPILER DESIGN Lab Tasks 1. Write a program to search for a given pattern in a set of files. It should support regular expressions

cout<<" "<<v[i]<<" --> ";

cin>>p[count].p.a;

}

else

{

cout<<" "<<v[i] <<" --> "; cin>>p[count].p.b[0]; cin>>p[count].p.b[1];

}

count++;

}

}

}

pda cnf:: mkpda(void)

{

pda p1; p1.s[p1.n].type=0; p1.s[p1.n].trno=1;

p1.s[p1.n].trans[0]=1;

p1.s[p1.n].tsym[0]=0;

p1.n++;

p1.s[p1.n].type=1;

p1.s[p1.n].sym=v[0];

p1.s[p1.n].trno=1;

p1.s[p1.n].trans[0]=2;

p1.s[p1.n].tsym[0]=0;

p1.n++;

p1.s[p1.n].type=2;

p1.s[p1.n].trno=1;

p1.s[p1.n].trans[0]=3;

p1.s[p1.n].tsym[0]=238;

p1.n++;

p1.s[p1.n].type=3;

p1.s[p1.n].trno=1;

p1.s[p1.n].trans[0]=4;

p1.s[p1.n].tsym[0]=238;

p1.n++;

p1.s[p1.n].type=4;

p1.s[p1.n].trno=0;

p1.n++;

int cnt=p1.s[2].trno;

int c1=0;

prod temp;

for(int i=0;i<vno;i++)

{

for(int j=0;j<vpr[i];j++)

{

temp=p[c1++];

Page 27: LAB MANUAL - crectirupati.com Lab Manual-R13.pdf · COMPILER DESIGN Lab Tasks 1. Write a program to search for a given pattern in a set of files. It should support regular expressions

if(temp.type==1)

{

p1.s[2].trans[cnt]=p1.n;

p1.s[2].tsym[cnt]=v[i];

p1.s[p1.n].type=3;

p1.s[p1.n].trno=1;

p1.s[p1.n].trans[0]=2;

p1.s[p1.n].tsym[0]=temp.p.a;

p1.n++;

cnt++;

}

else

{

p1.s[2].trans[cnt]=p1.n;

p1.s[2].tsym[cnt]=v[i];

p1.s[p1.n].type=1;

p1.s[p1.n].sym=temp.p.b[1];

p1.s[p1.n].trno=1;

p1.s[p1.n].trans[0]=(p1.n)+1;

p1.s[p1.n].tsym[0]=0;

p1.n++;

cnt++;

p1.s[p1.n].type=1;

p1.s[p1.n].sym=temp.p.b[0];

p1.s[p1.n].trno=1;

p1.s[p1.n].trans[0]=2;

p1.s[p1.n].tsym[0]=0;

p1.n++;

}

}

p1.s[2].trno=cnt;

}

return(p1);

}

void main()

{

cnf c;

pda p1;

p1=c.mkpda();

getch();

p1.show();

getch();

}

Page 28: LAB MANUAL - crectirupati.com Lab Manual-R13.pdf · COMPILER DESIGN Lab Tasks 1. Write a program to search for a given pattern in a set of files. It should support regular expressions

Experiment No: 07

Aim:

Simulate a simple desktop calculator using any lexical analyzer generator tool (LEX or FLEX) Program:

%{

int op=0,i;

float a,b;

%}

dig [0-9]+|([0-9]*)"."([0-9]+)

add "+"

sub "-"

mul "*"

div "/"

pow "^"

ln \n

%%

{dig} {digi();} /*** digi() is a user defined function ***/

{add} {op=1;}

{sub} {op=2;}

{mul} {op=3;}

{div} {op=4;}

{pow} {op=5;}

{ln} {printf("\n the result :%f\n\n",a);}

%%

digi()

{

if(op==0)

a=atof(yytext); /*** atof() is used to convert the ASCII input to float***/

else

{

b=atof(yytext);

switch(op)

{

case 1:a=a+b;

break;

case 2:a=a-b;

break;

case 3:a=a*b;

break;

case 4:a=a/b;

break;

Page 29: LAB MANUAL - crectirupati.com Lab Manual-R13.pdf · COMPILER DESIGN Lab Tasks 1. Write a program to search for a given pattern in a set of files. It should support regular expressions

case 5:for(i=a;b>1;b--)

a=a*i;

break;

}

op=0;

}

}

main(int argv,char *argc[])

{

yylex();

}

yywrap()

{

return 1;

}

Page 30: LAB MANUAL - crectirupati.com Lab Manual-R13.pdf · COMPILER DESIGN Lab Tasks 1. Write a program to search for a given pattern in a set of files. It should support regular expressions

Experiment No: 08

AIM:

Design any shift reduced parser which accepts a string and tells whether the string is

accepted by above grammar or not.

Program Logic:

1. Read the input string.

2. Perform shift reduce parsing actions to the given string to check it is valid or not

3. Parsing is completed when we reach $ symbol.

Program:

#include<stdio.h>

#include<conio.h>

#include<string.h>

char exp[30],stack[30],arr[30],temp[30];

int i,k=0,j,l,r,s;

void push(char exp[])

{

arr[i]=exp[k];

i++;

}

void dispinp()

{

printf("\t\t\t");

for(k=0;k<strlen(exp);k++)

printf("%c",exp[k]); printf("$");

}

void dispstk()

{

printf("\n");

for(k=0;k<strlen(stack);k++)

printf("%c",stack[k]);

}

void assign()

{

stack[++j]=arr[i];

exp[i]=' ';

dispstk();

dispinp();

}

void main()

{

clrscr();

Page 31: LAB MANUAL - crectirupati.com Lab Manual-R13.pdf · COMPILER DESIGN Lab Tasks 1. Write a program to search for a given pattern in a set of files. It should support regular expressions

printf("\t\t\tSHIFT REDUCE PARSER\n");

printf("\nThe Production is: E->E+E/E*E/E-E/i\n");

printf("\nEnter the string to be parsed:\n");

gets(exp);

printf("\nSTACK\t\t\tINPUT\t\t\tACTION\n");

printf("\n$");

dispinp();

printf("\t\t\tShift");

for(k=0;k<strlen(exp);k++)

push(exp);

l=strlen(exp);

stack[0]='$';

for(i=0;i<l;i++)

{

switch(arr[i])

{

case 'i': assign();

printf("\t\t\tReduce by E->i");

stack[j]='E';

dispstk();

dispinp();

if(arr[i+1]!='\0')

printf("\t\t\tShift");

break;

case '+': assign();

printf("\t\t\tShift");

break;

case '*': assign();

printf("\t\t\tShift");

break;

case '-': assign();

printf("\t\t\tShift");

break;

default: printf("\nError:String not accepted");

goto label;

}

}

l=strlen(stack);

while(l>2)

{

r=0;

for(i=l-1;i>=l-3;i--)

{

temp[r]=stack[i];

Page 32: LAB MANUAL - crectirupati.com Lab Manual-R13.pdf · COMPILER DESIGN Lab Tasks 1. Write a program to search for a given pattern in a set of files. It should support regular expressions

r++;

}

temp[r]=NULL;

if((strcmp(temp,"E+E")==0)||(strcmp(temp,"E*E")==0)||(strcmp(temp,"E-E")==0))

{

for(i=l;i>l-3;i--)

stack[i]=' ';

stack[l-3]='E';

printf("\t\t\tReduce by E->");

for(i=0;i<strlen(temp);i++)

printf("%c",temp[i]);

dispstk();

dispinp(); l=l-2;

}

else

{

printf("\nError:String not accepted"); goto label;

}

}

printf("\t\t\tAccept"); printf("\n\nString accepted");

label:

getch();

Input & Output: SHIFT REDUCE PARSER The Production is: E->E+E/E*E/E-E/i Enter the string to be parsed: i+i*i STACK INPUT ACTION

$ i+i*i$ Shift

$i +i*i$ Reduce by E->i

$E +i*i$ Shift

$E+ i*i$ Shift

$E+i *i$ Reduce by E->i

$E+E *i$ Shift

$E+E* i$ Shift

$E+E*i $ Reduce by E->i

$E+E*E $ Reduce by E->E*E

$E+E $ Reduce by E->E+E

$E $ Accept String accepted

Page 33: LAB MANUAL - crectirupati.com Lab Manual-R13.pdf · COMPILER DESIGN Lab Tasks 1. Write a program to search for a given pattern in a set of files. It should support regular expressions

Experiement No: 09 AIM: To Write a Program to eliminate left recursion from a given CFG. PROGRAM: #include<stdio.h> #include<string.h> #define SIZE 10 int main () { char non_terminal;

char beta,alpha; int num; char production[10][SIZE]; int index=3; /* starting of the string following "->" */ printf("Enter Number of Production : "); scanf("%d",&num); printf("Enter the grammar as E->E-A :\n"); for(int i=0;i<num;i++) { scanf("%s",production[i]); } for(int i=0;i<num;i++){ printf("\nGRAMMAR : : : %s",production[i]); non_terminal=production[i][0]; if(non_terminal==production[i][index]) { alpha=production[i][index+1]; printf(" is left recursive.\n"); while(production[i][index]!=0 && production[i][index]!='|') index++; if(production[i][index]!=0) {

beta=production[i][index+1]; printf("Grammar without left recursion:\n"); printf("%c->%c%c\'",non_terminal,beta,non_terminal); printf("\n%c\'->%c%c\'|E\n",non_terminal,alpha,non_terminal);

} else printf(" can't be reduced\n"); } else

printf(" is not left recursive.\n"); index=3; } }

Page 34: LAB MANUAL - crectirupati.com Lab Manual-R13.pdf · COMPILER DESIGN Lab Tasks 1. Write a program to search for a given pattern in a set of files. It should support regular expressions

OUTPUT:

Page 35: LAB MANUAL - crectirupati.com Lab Manual-R13.pdf · COMPILER DESIGN Lab Tasks 1. Write a program to search for a given pattern in a set of files. It should support regular expressions

Experiement No:10

Aim:

To write a Program to generate left factoring from a given CFG.

Program:

#include<stdio.h> #include<string.h> int main() { char gram[20],part1[20],part2[20],modifiedGram[20],newGram[20],tempGram[20]; int i,j=0,k=0,l=0,pos; printf("Enter Production : A->"); gets(gram); for(i=0;gram[i]!='|';i++,j++) part1[j]=gram[i]; part1[j]='\0'; for(j=++i,i=0;gram[j]!='\0';j++,i++) part2[i]=gram[j]; part2[i]='\0'; for(i=0;i<strlen(part1)||i<strlen(part2);i++) { if(part1[i]==part2[i]) { modifiedGram[k]=part1[i]; k++; pos=i+1; } } for(i=pos,j=0;part1[i]!='\0';i++,j++) { newGram[j]=part1[i]; } newGram[j++]='|'; for(i=pos;part2[i]!='\0';i++,j++){ newGram[j]=part2[i]; } modifiedGram[k]='X'; modifiedGram[++k]='\0'; newGram[j]='\0'; printf("\n A->%s",modifiedGram); printf("\n X->%s\n",newGram); } OUTPUT: Enter the productions: A-> aE+bcD|aE+eIT A -> aE+X X -> bcD|eIT

Page 36: LAB MANUAL - crectirupati.com Lab Manual-R13.pdf · COMPILER DESIGN Lab Tasks 1. Write a program to search for a given pattern in a set of files. It should support regular expressions

Experiment No: 11 Aim: Design a YACC program that reads a C program from input file and identify all valid C identifiers and for loop statements. Program:

%{

#include<ctype.h>

char ch,arr[20];

int id,i,j;

int test(char *);

%}

%%

^[ \t]*("int "|"float "|"double "|"char ")[ \t]* {

ch=input();

while(1)

{

if(ch=='\n'||ch==0)

break;

if(ch==','||ch==';')

{

arr[i]=0;

i=0;

j=test(arr);

if(j!=-1)

{

id+=j;

printf("\n%s is a %s",arr,j?"identifier":"nonidentifier");

}

if(ch==';')

break;

ch=input();

continue;

}

arr[i++]=ch;

ch=input();

}

}

.|[\n] ;

%%

yywrap()

{

return 1;

Page 37: LAB MANUAL - crectirupati.com Lab Manual-R13.pdf · COMPILER DESIGN Lab Tasks 1. Write a program to search for a given pattern in a set of files. It should support regular expressions

}

int rno(char *a,int state)

{

if(*a=='='&&state==0&&!(*a=0))

return 1;

if(isdigit(*a)&&state==1)

state=1;

if(*a==']'&&state==1)

state=0;

if(*a==0&&state==0)

return 1;

if(*a=='['&&state==0)

state=1;

a++;

return rno(a,state);

}

int test(char *a)

{

char *b=a;

int i;

while(*b==' ')

b++;

if(!isalpha(*b))

return 0;

while(*b!=0)

{

if(*b=='='&&!(*b=0))

return 1;

if(*b=='[')

{

i=rno(b++,1);

b--;

if(i==1)

*b=0;

return i;

}

if(!isalnum(*b))

return 0;

b++;

}

return 1;

}

int main(int argc,char **argv)

{

Page 38: LAB MANUAL - crectirupati.com Lab Manual-R13.pdf · COMPILER DESIGN Lab Tasks 1. Write a program to search for a given pattern in a set of files. It should support regular expressions

if(argc!=2)

{

printf("\nImproper usage!\n");

return 1;

}

yyin=fopen(*(argv+1),"r");

if(!yyin)

{

printf("\nSpecified file cannot be opened!\n");

return 1;

}

yylex();

printf("\nTotal identifiers is %d\n",id);

}

Page 39: LAB MANUAL - crectirupati.com Lab Manual-R13.pdf · COMPILER DESIGN Lab Tasks 1. Write a program to search for a given pattern in a set of files. It should support regular expressions

Experiment No: 12

Aim:

YACC program that reads the C statements from an input file and converts them into

quadruple three address intermediate code

Program:

/* LEX FILE */

%{

#include “y.tab.h” extern char yyval;

%}

NUMBER [0-9]+

LETTER [a-zA-Z]+

%%

{NUMBER} {yylval.sym=(char)yytext[0]; return NUMBER;} {LETTER}

{yylval.sym=(char)yytext[0];return LETTER;}

\n {return 0;}

. {return yytext[0];}

%%

/* yacc file */

%{

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

void ThreeAddressCode();

void triple();

void qudraple();

char AddToTable(char ,char, char);

int ind=0;

char temp=’A';

struct incod

{

char opd1;

char opd2;

char opr;

};

Page 40: LAB MANUAL - crectirupati.com Lab Manual-R13.pdf · COMPILER DESIGN Lab Tasks 1. Write a program to search for a given pattern in a set of files. It should support regular expressions

%}

%union

{

char sym;

}

%token <sym> LETTER NUMBER

%type <sym> expr

%left ‘-”+’

%right ‘*”/’

%%

statement: LETTER ‘=’ expr ‘;’ {AddToTable((char)$1,(char)$3,’=');}

| expr ‘;’

;

expr: expr ‘+’ expr {$$ = AddToTable((char)$1,(char)$3,’+');}

| expr ‘-’ expr {$$ = AddToTable((char)$1,(char)$3,’-');}

| expr ‘*’ expr {$$ = AddToTable((char)$1,(char)$3,’*');}

| expr ‘/’ expr {$$ = AddToTable((char)$1,(char)$3,’/');}

| ‘(‘ expr ‘)’ {$$ = (char)$2;}

| NUMBER {$$ = (char)$1;}

| LETTER {$$ = (char)$1;}

;

%%

yyerror(char *s)

{

printf(“%s”,s);

exit(0);

}

struct incod code[20];

int id=0;

char AddToTable(char opd1,char opd2,char opr)

{

code[ind].opd1=opd1;

code[ind].opd2=opd2;

code[ind].opr=opr;

Page 41: LAB MANUAL - crectirupati.com Lab Manual-R13.pdf · COMPILER DESIGN Lab Tasks 1. Write a program to search for a given pattern in a set of files. It should support regular expressions

ind++;

temp++;

return temp;

}

void ThreeAddressCode()

{

int cnt=0;

temp++;

printf(“\n\n\t THREE ADDRESS CODE\n\n”);

while(cnt<ind)

{

printf(“%c : = \t”,temp);

if(isalpha(code[cnt].opd1))

printf(“%c\t”,code[cnt].opd1);

else

{printf(“%c\t”,temp);}

printf(“%c\t”,code[cnt].opr);

if(isalpha(code[cnt].opd2))

printf(“%c\t”,code[cnt].opd2);

else

{printf(“%c\t”,temp);}

printf(“\n”);

cnt++;

temp++;

}

}

void quadraple()

{

int cnt=0;

temp++;

printf(“\n\n\t QUADRAPLE CODE\n\n”);

while(cnt<ind)

{

//printf(“%c : = \t”,temp);

Page 42: LAB MANUAL - crectirupati.com Lab Manual-R13.pdf · COMPILER DESIGN Lab Tasks 1. Write a program to search for a given pattern in a set of files. It should support regular expressions

printf(“%d”,id);

printf(“\t”);

printf(“%c”,code[cnt].opr);

printf(“\t”);

if(isalpha(code[cnt].opd1))

printf(“%c\t”,code[cnt].opd1);

else

{printf(“%c\t”,temp);}

//printf(“%c\t”,code[cnt].opr);

if(isalpha(code[cnt].opd2))

printf(“%c\t”,code[cnt].opd2);

else

{printf(“%c\t”,temp);}

printf(“%c”,temp);

printf(“\n”);

cnt++;

temp++;

id++;

}

}

void triple()

{

int cnt=0,cnt1,id1=0;

temp++;

printf(“\n\n\t TRIPLE CODE\n\n”);

while(cnt<ind)

{

//printf(“%c : = \t”,temp);

if(id1==0)

{

printf(“%d”,id1);

printf(“\t”);

printf(“%c”,code[cnt].opr);

printf(“\t”);

Page 43: LAB MANUAL - crectirupati.com Lab Manual-R13.pdf · COMPILER DESIGN Lab Tasks 1. Write a program to search for a given pattern in a set of files. It should support regular expressions

if(isalpha(code[cnt].opd1))

printf(“%c\t”,code[cnt].opd1);

else

{printf(“%c\t”,temp);}

//printf(“%c\t”,code[cnt].opr);

cnt1=cnt-1;

if(isalpha(code[cnt].opd2))

printf(“%c”,code[cnt].opd2);

else

{printf(“%c\t”,temp);}

}

else

{

printf(“%d”,id1);

printf(“\t”);

printf(“%c”,code[cnt].opr);

printf(“\t”);

if(isalpha(code[cnt].opd1))

printf(“%c\t”,code[cnt].opd1);

else

{printf(“%c\t”,temp);}

//printf(“%c\t”,code[cnt].opr);

cnt1=cnt-1;

if(isalpha(code[cnt].opd2))

printf(“%d”,id1-1);

else

{printf(“%c\t”,temp);}

}

printf(“\n”);

cnt++;

temp++;

id1++;

}

}

Page 44: LAB MANUAL - crectirupati.com Lab Manual-R13.pdf · COMPILER DESIGN Lab Tasks 1. Write a program to search for a given pattern in a set of files. It should support regular expressions

main()

{

printf(“\nEnter the Expression: “);

yyparse();

temp=’A';

ThreeAddressCode();

quadraple();

triple();

}

yywrap()

{

return 1;

}

INPUT & OUTPUT:

administrator@ubuntu:~/Desktop$ flex th.l

administrator@ubuntu:~/Desktop$ yacc -d th.y

administrator@ubuntu:~/Desktop$ gcc lex.yy.c y.tab.c -ll -lm

administrator@ubuntu:~/Desktop$ ./a.out

administrator@ubuntu:~/Desktop$ ./a.out

Enter the Expression: a=((b+c)*(d/e));

THREE ADDRESS CODE

B : = b + c

C : = d / e

D : = B * C

E : = a = D

QUADRAPLE CODE

0 + b c G

1 / d e H

2 * B C I

3 = a D J

TRIPLE CODE

0 + b c

1 / d 0

2 * B 1

3 = a 2

Page 45: LAB MANUAL - crectirupati.com Lab Manual-R13.pdf · COMPILER DESIGN Lab Tasks 1. Write a program to search for a given pattern in a set of files. It should support regular expressions

Experiment No: 13

Aim:

Design a YACC program that reads the input expression and convert it to post fix expression.

Program:

gram.l

%{

#include"y.tab.h" extern int yylval;

%}

%%

[0-9]+ {yylval=atoi(yytext); return NUM;}

\n return 0;

. return *yytext;

%%

int yywrap()

{

return 1;

}

gram.y

%{

#include<stdio.h>

%}

%token NUM %left '+' '-' %left '*' '/'

%right NEGATIVE

%%

S: E {printf("\n");} ;

E: E '+' E {printf("+");}

| E '*' E {printf("*");}

| E '-' E {printf("-");}

| E '/' E {printf("/");}

| '(' E ')'

| '-' E %prec NEGATIVE {printf("-");}

| NUM {printf("%d", yylval);}

;

%%

int main()

{

yyparse();

}

int yyerror (char *msg)

Page 46: LAB MANUAL - crectirupati.com Lab Manual-R13.pdf · COMPILER DESIGN Lab Tasks 1. Write a program to search for a given pattern in a set of files. It should support regular expressions

{

return printf ("error YACC: %s\n", msg);

}

INPUT & OUTPUT:

yacc -d gram.y flex gram.l

cc lex.yy.c y.tab.c

./a.out

input: 2+6*2-5/3

output: 262*+53/-

Page 47: LAB MANUAL - crectirupati.com Lab Manual-R13.pdf · COMPILER DESIGN Lab Tasks 1. Write a program to search for a given pattern in a set of files. It should support regular expressions

Experiment No: 14

Aim:

Write a C program for implementing the functionalities of predictive parser

Program:

#include<stdio.h>

#include<conio.h>

#include<string.h>

char prol[7][10]={"S","A","A","B","B","C","C"};

char pror[7][10]={"A","Bb","Cd","aB","@","Cc","@"};

char prod[7][10]={"S->A","A->Bb","A->Cd","B->aB","B->@","C->Cc","C->@"};

char first[7][10]={"abcd","ab","cd","a@","@","c@","@"};

char follow[7][10]={"$","$","$","a$","b$","c$","d$"};

char table[5][6][10];

numr(char c)

{

switch(c)

{

case 'S': return 0;

case 'A': return 1;

case 'B': return 2;

case 'C': return 3;

case 'a': return 0;

case 'b': return 1;

case 'c': return 2;

case 'd': return 3;

case '$': return 4;

}

return(2);

}

void main()

{

int i,j,k;

Page 48: LAB MANUAL - crectirupati.com Lab Manual-R13.pdf · COMPILER DESIGN Lab Tasks 1. Write a program to search for a given pattern in a set of files. It should support regular expressions

clrscr();

for(i=0;i<5;i++)

for(j=0;j<6;j++)

strcpy(table[i][j]," ");

printf("\nThe following is the predictive parsing table for the following grammar:\n");

for(i=0;i<7;i++)

printf("%s\n",prod[i]);

printf("\nPredictive parsing table is\n");

fflush(stdin);

for(i=0;i<7;i++)

{

k=strlen(first[i]);

for(j=0;j<10;j++)

if(first[i][j]!='@')

strcpy(table[numr(prol[i][0])+1][numr(first[i][j])+1],prod[i]);

}

for(i=0;i<7;i++)

{

if(strlen(pror[i])==1)

{

if(pror[i][0]=='@')

{

k=strlen(follow[i]);

for(j=0;j<k;j++)

strcpy(table[numr(prol[i][0])+1][numr(follow[i][j])+1],prod[i]);

}

}

}

strcpy(table[0][0]," ");

strcpy(table[0][1],"a");

strcpy(table[0][2],"b");

strcpy(table[0][3],"c");

strcpy(table[0][4],"d");

strcpy(table[0][5],"$");

Page 49: LAB MANUAL - crectirupati.com Lab Manual-R13.pdf · COMPILER DESIGN Lab Tasks 1. Write a program to search for a given pattern in a set of files. It should support regular expressions

strcpy(table[1][0],"S");

strcpy(table[2][0],"A");

strcpy(table[3][0],"B");

strcpy(table[4][0],"C");

printf("\n-------------------------------------------------------- \n");

for(i=0;i<5;i++)

for(j=0;j<6;j++)

{

printf("%-10s",table[i][j]);

if(j==5)

printf("\n-------------------------------------------------------- \n");

}

getch();

}

Input & Output:

The following is the predictive parsing table for the following grammar:

S->A

A->Bb

A->Cd

B->aB

B->@

C->Cc

C->@

Predictive parsing table is

------------------------------------------------------------------

a b c d $

------------------------------------------------------------------

SS->AS->A S->A S->A

------------------------------------------------------------------

A A->Bb A->Bb A->Cd A->Cd

------------------------------------------------------------------

B B->aB B->@ B->@ B->@

------------------------------------------------------------------

C C->@ C->@ C->@

------------------------------------------------------------------