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

Week-10 CD LAB

The document describes a recursive descent parser for a specific grammar that accepts arithmetic expressions involving addition, multiplication, and parentheses. It outlines the grammar rules, the process of eliminating left recursion, and provides a C code implementation for the parser. The parser checks the syntax of input expressions and determines if they are accepted or rejected based on the defined grammar.
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)
2 views5 pages

Week-10 CD LAB

The document describes a recursive descent parser for a specific grammar that accepts arithmetic expressions involving addition, multiplication, and parentheses. It outlines the grammar rules, the process of eliminating left recursion, and provides a C code implementation for the parser. The parser checks the syntax of input expressions and determines if they are accepted or rejected based on the defined grammar.
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/ 5

Week-10

10.write recursive descent parser for the grammar. E->E+T E->T T->T*F
T->F F->(E)/id.
The grammar on which we are going to do recursive descent parsing is:

E -> E+T | T
T -> T*F | F
F -> (E) | id
RD parser will verify whether the syntax of the input stream is correct by
checking each character from left to right. A basic operation necessary is
reading characters from the input stream and matching then with terminals from
the grammar that describes the syntax of the input.

The given grammar can accept all arithmetic equations involving +, * and ().
eg:
a+(a*a) a+a*a , (a), a , a+a+a*a+a.... etc are accepted
a++a, a***a, +a, a*, ((a . . . etc are rejected.

Solution:
First we have to avoid left recursion

E -> TE'
E' -> +TE' | ε
T -> FT'
T' -> *FT' | ε
F -> (E) | id

After eliminating Left recursion, we have to simply move from one character to
next by checking whether it follow the grammar. In this program, ε is indicated
as $.

recursive.c

#include <stdio.h>
#include <string.h>
#include <ctype.h>

char input[100]; // Increased size to allow longer input


int i, error;

void E();
void T();
void Eprime();
void Tprime();
void F();

int main() {
i = 0;
error = 0;

printf("Enter an arithmetic expression (e.g., a+a*a): ");


fgets(input, sizeof(input), stdin); // Use fgets for safety

// Remove trailing newline from fgets input


input[strcspn(input, "\n")] = '\0';

E();

if (strlen(input) == i && error == 0) {


printf("\nAccepted..!!!\n");
} else {
printf("\nRejected..!!!\n");
}

return 0;
}

void E() {
T();
Eprime();
}

void Eprime() {
if (input[i] == '+') {
i++;
T();
Eprime();
}
}

void T() {
F();
Tprime();
}

void Tprime() {
if (input[i] == '*') {
i++;
F();
Tprime();
}
}

void F() {
if (isalnum(input[i])) {
i++; // If it's a valid operand, move to the next character
}
else if (input[i] == '(') {
i++;
E();
if (input[i] == ')') {
i++; // Match closing parenthesis
} else {
error = 1; // Unmatched parentheses
}
}
else {
error = 1; // Invalid character
}
}
Output:
a+(a*a) a+a*a , (a), a , a+a+a*a+a.... etc are accepted
++a, a***a, +a, a*, ((a . . . etc are rejected.

You might also like