SPCC Exp4
SPCC Exp4
Lab Outcome: - Parse the given input string by constructing Top down/Bottom-up
parser
__________________________________________
Practical Incharge
EXPERIMENT NO-04
Theory:
A top-down parser that uses a one-token look ahead is called an LL(1) parser.
The first L indicates that the input is read from left to right. The second L says that it
produces a left-to-right derivation. And the 1 says that it uses one look ahead token. (Some
parsers look ahead at the next 2 tokens, or even more than that.)
Steps:-
1. Take Inputs of Non-Terminals and Terminals for a grammar.
2. Enter the inputs of the parser table (# for null entry).
3. We accept as input the string that is to be parsed.
4. This string is stored in the input buffer and the stack initially contains $ and
the start variable.
5. Each string in the input buffer is compared to each top of stack.
6. If they match, then we perform pop operation in stack and move the
element from buffer.
7. If they do not match, i.e. the top of stack is a non- terminal then it is replaced
by the production which will possibly generate the input string with the help
of the parser table used above.
8. The string is accepted by the parser if and only if the top of stack and the
element in the input buffer is b”$”.
Program:
#include<stdio.h>
#include<ctype.h>
#include<string.h>
int count,n=0;
char calc_first[10][100];
char calc_follow[10][100];
int m=0;
char production[10][10], first[10];
char f[10];
int k;
char ck;
int e;
void follow(char c)
{
int i ,j;
if(production[0][0]==c){
f[m++]='$';
}
for(i=0;i<10;i++)
{
for(j=2;j<10;j++)
{
if(production[i][j]==c)
{
if(production[i][j+1]!='\0'){
followfirst(production[i][j+1],i,(j+2));
}
if(production[i][j+1]=='\0'&&c!=production[i][0]){
follow(production[i][0]);
}
}
}
}
}
Output: