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

530 CD

Uploaded by

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

530 CD

Uploaded by

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

Exp No: Page No:

Date:
3)First and Follow
3.1) Simulate First and Follow of a Grammar.
Program:
#include<stdio.h>
#include<string.h>
int n, m = 0, p, i = 0, j = 0;
char a[10][10], f[10];
void follow(char c);
void first(char c);
int main()
{
int i, z;
char c, ch;
printf("enter the no. of productions:");
scanf("%d", &n);
printf("enter the productions(epsilon = $):\n");
for(i = 0; i < n; i++)
scanf("%s%c", a[i], &ch);
do
{
m = 0;
printf("enter the element whose FIRST & FOLLOW is to be found:");
scanf("%c", &c);
first(c);
printf("FIRST(%c) = {", c);
for(i = 0;i<m;i++)
printf("%c", f[i]);
printf("}\n");
follow(c);
printf("FOLLOW(%c) = {", c);
for(;i<m;i++)
printf("%c", f[i]);
printf("}\n");
printf("do you want to continue(0/1)?");
scanf("%d%c", &z, &ch);
}
while(z = 1);
}
void follow(char c)
{
if(a[0][0] = c)
f[m++] = '$';
for(i = 0;i<n;i++)
{
for(j = 2;j<strlen(a[i]);j++)
{
if(a[i][j] = c)
{
if(a[i][j+1] != '\0')first(a[i][j+1]);

ADITYA ENGINEERING COLLEGE(A) Roll No: 20A91A0530


Exp No: Page No:
Date:
if(a[i][j+1] = '\0'&&c != a[i][0])
follow(a[i][0]);
}
}
}
}
void first(char c)
{
int k;
if(!(isupper(c)))
f[m++] = c;
for(k = 0;k<n;k++)
{
if(a[k][0] = c)
{
if(a[k][2] = '$')
follow(a[i][0]);
else if(islower(a[k][2]))
f[m++] = a[k][2];
else
first(a[k][2]);
}
}
}
Output:

ADITYA ENGINEERING COLLEGE(A) Roll No: 20A91A0530


Exp No: Page No:
Date:

3.2) Implement the lexical analyzer using JLex, flex or lex or other lexical analyzer
generating tools
Program:
%{
#include<stdio.h>
int i=0,id=0;
%}
%%
[#].*[<].*[>]\n {}
[ \t\n]+ {}
\/\/.*\n {}
\/\*(.*\n)*.*\*\/ {}
auto|break|case|char|const|continue|default|do|double|else|enum|extern|float|for|goto|if|int|long|
register|return|short|signed|sizeof|static|struct|switch|typedef|union|unsigned|void|volatile|
while {printf("token : %d < keyword , %s >\n",++i,yytext);}
[+\-\*\/%<>] {printf("token : %d < operator , %s >\n",++i,yytext);}
[();{}] {printf("token : %d < special char , %s >\n",++i,yytext);}
[0-9]+ {printf("token : %d < constant , %s >\n",++i,yytext);}
[a-zA-Z_][a-zA-Z0-9_]* {printf("token : %d < Id%d ,%s >\n",++i,++id,yytext);}
^[^a-zA-Z_] {printf("ERROR Invaild token %s \n",yytext);}
%%
Output:

ADITYA ENGINEERING COLLEGE(A) Roll No: 20A91A0530


Exp No: Page No:
Date:

Week - 4
4)Top Down Parsing
4.1) Develop an operator precedence parser for a given language.
Program:
#include<stdio.h>
#include<string.h>
char stack[20],temp;
int top=-1;
void push(char item)
{
if(top>=20)
{
printf("STACK OVERFLOW");
return;
}
stack[++top]=item;
}

char pop()
{
if(top<=-1)
{
printf("STACK UNDERFLOW");
return;
}
char c;
c=stack[top--];
printf("Popped element:%c\n",c);
return c;
}
char TOS()
{
return stack[top];
}
int convert(char item)
{
switch(item)
{
case 'i':return 0;
case '+':return 1;
case '*':return 2;
case '$':return 3;
}
}
int main()
{
char pt[4][4]={

ADITYA ENGINEERING COLLEGE(A) Roll No: 20A91A0530


Exp No: Page No:
Date:
{'-','>','>','>'},
{'<','>','<','>'},
{'<','>','>','>'},
{'<','<','<','1'}};
char input[20];
int lkh=0;
printf("Enter input with $ at the end\n");
scanf("%s",input);
push('$');
while(lkh<=strlen(input))
{
if(TOS()=='$'&&input[lkh]=='$')
{
printf("SUCCESS\n");
return 1;
}

else if(pt[convert(TOS())][convert(input[lkh])]=='<')
{
push(input[lkh]);
printf("Push---%c\n",input[lkh]);
lkh++;
}
else
{
pop();
}
}
return 0;
}
Output:

ADITYA ENGINEERING COLLEGE(A) Roll No: 20A91A0530


Exp No: Page No:
Date:

4.2) Construct a recursive descent parser for an expression


Program:
#include<stdio.h>
#include<ctype.h>
#include<string.h>
void Tp();
void Ep();
void E();
void T();
void check();
int count,flag;
char expr[10];
int main()
{
count=0;
flag=0;
printf("\nEnter an Algebraic Expression:\t");
scanf("%s",expr);
E();
if((strlen(expr)==count)&&(flag==0))
printf("\nThe expression %s is valid\n",expr);
else
printf("\nThe expression %s is invalid\n",expr);
return 0;
}
void E()
{
T();
Ep();

ADITYA ENGINEERING COLLEGE(A) Roll No: 20A91A0530


Exp No: Page No:
Date:
}
void T()
{
check();
Tp();
}
void Tp()
{
if(expr[count]=='*')
{
count++;
check();
Tp();
}
}
void check()
{
if(isalnum(expr[count]))
count++;
else if(expr[count]=='(')
{
count++;
E();
if(expr[count]==')')
count++;
else
flag=1;
}
else
flag=1;
}
void Ep()
{
if(expr[count]=='+')
{
count++;
T();
Ep();
}
}

Output:

ADITYA ENGINEERING COLLEGE(A) Roll No: 20A91A0530


Exp No: Page No:
Date:

ADITYA ENGINEERING COLLEGE(A) Roll No: 20A91A0530

You might also like