0% found this document useful (0 votes)
4 views5 pages

23BKT0059 RDP

The document contains a C++ program that implements a recursive descent parser for a specified grammar involving expressions, terms, and factors. It includes functions for parsing different components of the grammar and error handling for syntax errors. The program prompts the user for an expression and indicates whether parsing was successful or if there was invalid input.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

23BKT0059 RDP

The document contains a C++ program that implements a recursive descent parser for a specified grammar involving expressions, terms, and factors. It includes functions for parsing different components of the grammar and error handling for syntax errors. The program prompts the user for an expression and indicates whether parsing was successful or if there was invalid input.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Compiler Design

02/08/2025

Name : Shubhangam Singh


Reg No. : 23BKT0059

Que) Write a C++ Program to implement recursive descent parser for the following grammar
E -> TE'
E' -> +TE' | ε
T -> FT'
T' -> *FT' | ε
F -> (E) | id

Code)

// 23BKT0059 Shubhangam Singh


#include <bits/stdc++.h>

char lookahead;

void E();
void E_prime();
void T();
void T_prime();
void F();

void match(char terminal)


{
if(lookahead == terminal)
{
lookahead = getchar();
}
else
{
printf("Syntax Error");
exit(1);
}
}

void E() // E -> TE'


{
T();
E_prime();
}

void E_prime() // E' -> +TE' | ε


{
if(lookahead == '+')
{
match('+');
T();
E_prime();
}
}

void T() // T -> FT'


{
F();
T_prime();
}

void T_prime() // T' -> *FT' | ε


{
if(lookahead == '*')
{
match('*');
F();
T_prime();
}
}

void F() // F -> (E) | id


{

if(lookahead == '(')
{
match('(');
E();
match(')');
}
else if(lookahead == 'i')
{
match('i');
}
else
{
printf("Syntax error");
exit(1);
}
}

int main()
{
printf("Enter an expression ending with $:\n");
lookahead=getchar();

E();

if(lookahead == '$')
{
printf("\nParsing Successful\n");
}
else
{
printf("\nInvalid Input\n");
}

return 0;
}

Screen Shots ->

You might also like