Professional Documents
Culture Documents
Sarthak - 13th July
Sarthak - 13th July
13 July 2023
tt
tt
ti
Solution:-
Code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char inp[100];
int ptr = 0;
int er_f = 0;
void S();
void T();
void match(char expected) {
if (inp[ptr] == expected) {
ptr++;
} else {
er_f = 1;
}
}
void S() {
if (inp[ptr] == 'a') {
match('a');
S();
Screenshot:-
Output:-
2. Code for the predictive parser for the given grammar and parse the sentence
(((a,a),↑,(a),a) .
S-> a|↑|(T)
T->T¸S|S
Code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char input[100];
int ptr= 0;
int er_f = 0;
void S();
void T();
void S() {
if (input[pointer] == 'a') {
match('a');
prin ("S -> a\n");
} else if (input[pointer] == '^') {
match('^');
prin ("S -> ^\n");
} else if (input[pointer] == '(') {
match('(');
T();
match(')');
prin ("S -> (T)\n");
} else {
er_f = 1;
}
tf
tf
tf
void T() {
S();
if (input[pointer] == ',') {
match(',');
T();
prin ("T -> T,S\n");
} else {
prin ("T -> S\n");
}
}
int main() {
prin ("Sarthak Goyal 21BCE0968\n\n");
prin ("Enter the sentence to parse: ");
fgets(input, sizeof(input), stdin);
input[strcspn(input, "\n")] = '\0';
S();
return 0;
}
tf
tf
tf
tf
tf
tf
tf
Screenshot:-
Output:-
Thank You