0% found this document useful (0 votes)
14 views14 pages

Week - 3 3) First and Follow 3.1) Simulate First and Follow of A Grammar. Program

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)
14 views14 pages

Week - 3 3) First and Follow 3.1) Simulate First and Follow of A Grammar. Program

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/ 14

Exp No: Page No:

Date:
Week – 3
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 number 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 to calculate First and Follow: ");
scanf("%c",&c);
first(c);
printf("First(%c) = {",c);
for(i=0;i<m;i++)
printf(" %c ",f[i]);
printf("}\n");
follow(c);x`
printf("Follow(%c) = {",c);
for(;i<m;i++)

ADITYA ENGINEERING COLLEGE(A) Roll No: 20A91A0558


Exp No: Page No:
Date:
printf(" %c ",f[i]);
printf("}\n");
printf("Do you wish 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]);
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++)

ADITYA ENGINEERING COLLEGE(A) Roll No: 20A91A0558


Exp No: Page No:
Date:
{
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: 20A91A0558


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);}
%%
Input File Sample.c :
#include<stdio.h>
int main()
{
printf("Hello world");
return 0;
}
Output:

ADITYA ENGINEERING COLLEGE(A) Roll No: 20A91A0558


Exp No: Page No:
Date:
Week 4: Top Down Parsing

4.1) Develop an operator precedence parser for a given language

#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)
{

ADITYA ENGINEERING COLLEGE(A) Roll No: 20A91A0558


Exp No: Page No:
Date:
case 'i':return 0;
case '+':return 1;
case '*':return 2;
case '$':return 3;
}
}
int main()
{
Char pt[4][4]={
{'-','>','>','>'},
{'<','>','<','>'},
{'<','>','>','>'},
{'<','<','<','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();

ADITYA ENGINEERING COLLEGE(A) Roll No: 20A91A0558


Exp No: Page No:
Date:
}
}
return 0;
}
Output:

ADITYA ENGINEERING COLLEGE(A) Roll No: 20A91A0558


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();
}
void T()
{
check();
Tp();
}
void Tp()

ADITYA ENGINEERING COLLEGE(A) Roll No: 20A91A0558


Exp No: Page No:
Date:
{
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();
}
}

ADITYA ENGINEERING COLLEGE(A) Roll No: 20A91A0558


Exp No: Page No:
Date:
Output:

ADITYA ENGINEERING COLLEGE(A) Roll No: 20A91A0558


Exp No: Page No:
Date:

ADITYA ENGINEERING COLLEGE(A) Roll No: 20A91A0558


Exp No: Page No:
Date:

ADITYA ENGINEERING COLLEGE(A) Roll No: 20A91A0558


Exp No: Page No:
Date:

ADITYA ENGINEERING COLLEGE(A) Roll No: 20A91A0558


Exp No: Page No:
Date:

ADITYA ENGINEERING COLLEGE(A) Roll No: 20A91A0558

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