Program to Construct a recursive descent parser for an expression.
Agenda
1.Algorithm
2.Code(program)
3.Input/output
1.Algorithm
Read the input string.
Write procedures for the non terminals
Verify the next token equals to non terminals if it satisfies match the non terminal.
If the input string does not match print error.
2.Code
#include<stdio.h>
#include<conio.h>
#include<string.h>
char input[100];
int i, l;
void main()
{
clrscr();
printf("\nRecursive descent parsing for the following grammar\n");
printf("\nEnter the string to be checked:");
gets(input);
if (E())
{
if (input[i + 1] == '\0')
printf("\nString is accepted");
else
printf("\nString is not accepted");
} else
printf("\nString not accepted");
getch();
}
E()
{
if (T())
{
if (EP())
return (1);
else
return (0);
} else
return (0);
}
EP()
{
if (input[i] == '+')
{
i++;
if (T())
{
if (EP())
return (1);
else
return (0);
} else
return (0);
} else
return (1);
}
T()
{
if (F())
{
if (TP())
return (1);
else
return (0);
} else
return (0);
}
TP()
{
if (input[i] == '*')
{
i++;
if (F())
{
if (TP())
return (1);
else
return (0);
} else
return (0);
} else
return (1);
}
F()
{
if (input[i] == '(')
{
i++;
if (E())
{
if (input[i] == ')')
{
i++;
return (1);
} else
return (0);
} else
return (0);
} else if (input[i] >= 'a' && input[i] <= 'z' || input[i] >= 'A' && input[i] <= 'Z')
{
i++;
return (1);
} else
return (0);
}
3.Input/Output
Recursive descent parsing for the following grammar
E->TE'
E'->+TE'/@
T->FT'
T'->*FT'/@
F->(E)/ID
Enter the string to be checked:(a+b)*c
String is accepted
Recursive descent parsing for the following grammar
E->TE'
E'->+TE'/@
T->FT'
T'->*FT'/@
F->(E)/ID
Enter the string to be checked:a/c+d
String is not accepted
Comments
Post a Comment