You are on page 1of 2

Ex 4

Program to implement recursive descent parsing method for simple expression.


#include <stdio.h> void t_prime() {
#include <string.h> if (ip_sym[ip_ptr] == '*') {
#include <stdlib.h> printf("T' -> *FT'\n");
#include <ctype.h> advance();
f();
char ip_sym[15]; t_prime();
int ip_ptr = 0; } else {
printf("T' -> ε\n");
void advance(); }
void e(); }
void e_prime();
void t(); void f() {
void t_prime(); if (ip_sym[ip_ptr] == '(') {
void f(); printf("F -> (E)\n");
advance();
void advance() { e();
ip_ptr++; if (ip_sym[ip_ptr] == ')') {
} advance();
} else {
void e() { printf("Syntax error: Expected ')'\n");
printf("E -> TE'\n"); exit(1);
t(); }
e_prime(); } else if (isalpha(ip_sym[ip_ptr])) {
} printf("F -> i\n");
advance();
void e_prime() { } else {
if (ip_sym[ip_ptr] == '+') { printf("Syntax error: Invalid symbol\n");
printf("E' -> +TE'\n"); exit(1);
advance(); }
t(); }
e_prime();
} else { int main() {
printf("E' -> ε\n"); printf("Grammar without left recursion:\n");
} printf("\tE -> TE'\n\tE' -> +TE' | ε\n\tT ->
} FT'\n\tT' -> *FT' | ε\n\tF -> (E) | i\n");

void t() { printf("Enter the input expression: ");


printf("T -> FT'\n"); fgets(ip_sym, sizeof(ip_sym), stdin);
f(); ip_sym[strcspn(ip_sym, "\n")] = '\0'; //
t_prime(); Remove newline character from input
}
printf("Productions:\n");
Ex 4

e(); printf("Input expression parsed


successfully.\n");
if (ip_sym[ip_ptr] != '\0') { }
printf("Syntax error: Extra symbols after
parsing\n"); return 0;
} else { }

You might also like