0% found this document useful (0 votes)
34 views2 pages

Ex 4

Uploaded by

sushant kamble
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)
34 views2 pages

Ex 4

Uploaded by

sushant kamble
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
You are on page 1/ 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