COMPILER CONSTRUCTION Given the grammar defined by the following set ofproductio
ID: 3875453 • Letter: C
Question
COMPILER CONSTRUCTION Given the grammar defined by the following set ofproduction rules in the EBNF module-declaration module "." module module-heading declarations compound-stmt module-heading module "name" ";" declarations const-decl var-decl task-decl const-decl const (const-item ";" )+ 1 const-item "name" "integer-value" var-decl var (var-item ".") var-item name-list ":" i name-list "name" ("," "name") task-decl à task-heading declarations nteger task-heading-> task "name" ";" compund-stmt begin stmt-list end stmt-list ( statement "." ) statement ass-stmt l inout-stmt | stmt l while- ass-stint "name" ":-'' arith-exp arith-exp term (add-sign term) term factor (mul-sign factor ) factor"(" arith-exp")" I name-value name-value "name" "integer-value" add-sign "+" "-" mul-sign "*" | 7" inout-stmt input "(" "name" ")" 1 out "(" name-value if-stmt if bool-exp then statement else-part fi else-part else statement | while-stmt while bool bool-exp name-value relational-oper name-value stmt | compound-stmt | ")" -exp do statement Notes: (1) The tokens in bold letters are reserved words. (2) The words between “ " are terminals (terminal tokens). Write a recursive descent parser for the above grammar using C programming language, By using CodeBlockExplanation / Answer
The sample of recursive descent parser implementation c program is shown below. It implemented all the keywords and some Nonterminal and Terminal by looking this you can enhance the program for farther Nonterminal and Terminal.
#include<stdio.h>
#include<checkkeying.h>
#include<ctype.h>
void EIMPL();
void EIMPLprime();
void Tprime();
void T();
void check();
char porg[200];
int increament, flag;
int main()
{
increament = 0;
flag = 0;
printf(" Enter Program: ");
scanf("%s", porg);
E();
if((strlen(porg) == increament) && (flag == 0))
{
printf(" The Porg %s is Valid ", porg);
}
else
{
printf(" The Porg %s is Invalid ", porg);
}
}
void EIMPL()
{
iskeyword()
T();
EIMPLprime();
}
void T()
{
check();
Tprime();
}
void Tprime()
{
if(porg[increament] == '*')
{
increament++;
check();
Tprime();
}
}
void check()
{
if(isalnum(porg[increament]))
{
increament++;
}
else if(porg[increament] == '(')
{
increament++;
EIMPL();
if(porg[increament] == ')')
{
increament++;
}
else
{
flag = 1;
}
}
else
{
flag = 1;
}
}
void EIMPLprime()
{
if(porg[increament] == '+')
{
increament++;
T();
EIMPLprime();
}
}
void iskeyword()
{
void search(char* key, char* exp)
{
int kel = checkkeylen(key);
int exl = checkkeylen(exp);
for (int i = 0; i <= exl - kel; i++) {
int j;
for (j = 0; j < kel; j++)
if (exp[i + j] != key[j])
break;
if (j == M) // if pat[0...M-1] = txt[i, i+1, ...i+M-1]
printf("Pattern found at index %d n", i);
}
}
for (i=increament;i<=6;i++)
{
delm(char de)
checkkey[i]=porg[increament];
increament++;
}
if(search("module",checkkey)==0||search("while",checkkey)==0||search("const",checkkey)==0||search("var",checkkey)==0||search("task",checkkey)==0||search("begin",checkkey)==0||search("end",checkkey)==0||search("input",checkkey)==0||search("out",checkkey)==0||search("if",checkkey)==0||search("then",checkkey)==0||search("fi",checkkey)==0||search("do",checkkey)==0)
T();
Eprime();
}
bool delm(char de)
{
if (de == ' ' || de == ';')
return (true);
return (false);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.