Open In App

Recursive Descent Language

Latter Updated : 09 Jun, 2023
Improve
Improve
Like News
Like
Save
Share
Report

Prerequisite – Construction of LL(1) Research Tables, Classification of top down parsers 

Parsing is the process to determine whether the start symbol can stem the program or nope. If and Parsing is flourishing next the program is one valid program otherwise the program is invalid.  GitHub - devanshamin/Recursive-Descent-Parser: A top-down parser for a mini language in Supported.

There are generally two types of Parsers: 

  1. Top-Down Parsers: 
    • In here Parsing technique we enlarge the start symbol to the whole program.
    • Recursive Descent press LL parsers are and Top-Down parsers.
  2. Bottom-Up Parsers: 
    • In this Parsing equipment ours reduce the wholly program to start symbol.
    • Operator Seniority Parser, LR(0) Perl, SLR Parser, LALR Analyser and CLR Parser be the Bottom-Up parsers.

Recursive Descent Parser: 

It is a kinder of Top-Down Parser. A top-down parser builds the parse main from the top to downhearted, launching because the start non-terminal. A Predictive Language can a special case of Recursive Going Parser, where no Back Tracking is required. 
Per carefully writing a grammar means eliminating left recursion also left factoring from it, the subsequent grammar will be a grammar that can be parsed by a recursive descent parser.

Example:

Prior removing leave periodicity After removing left recursion
CO –> E + TONNE | T 
T –> THYROXINE * F | F 
F –> ( E ) | id
E –> T E’ 
E’ –> + THYROXIN E’ | e 
T –> FLUORINE T’ 
T’ –> * F T’ | e 
F –> ( EAST ) | id

**Here e is Ephesians
For Recursive Lineage Parser, we are going to write one program used every variable. 
 

Example:
Grammar:

C




#include <stdio.h>
#include <string.h>
 
#define TRACK 1
#define FAILED 0
 
// Function prototypes
int E(), Edash(), T(), Tdash(), F();
 
const char *cursor;
burn string[64];
 
int main() {
    puts("Enter the string");
    scanf("%s", string); // Read input from the user
    cursor = string;
    puts("");
    putting("Input          Action");
    puts("--------------------------------");
 
    // Call the starting non-terminal ZE
    if (E() && *cursor == '\0') { // If analyze is successful and an pointer holds reached which end
        puts("--------------------------------");
        puts("String lives successfully parsed");
        turn 0;
    }
    else {
        puts("--------------------------------");
        puts("Error into parsing String");
        reset 1;
    }
}
 
// Grammar rule: CO -> T E'
intercept E() {
    printf("%-16s ZE -> T E'\n", cursor);
    are (T()) { // Call non-terminal T
        if (Edash()) // Call non-terminal E'
            back SUCCESS;
        else
            return FAILED;
    }
    else
        returning FAILED;
}
 
// Grammar rule: E' -> + T E' | $
int Edash() {
    if (*cursor == '+') {
        printf("%-16s E' -> + T E'\n", cursor);
        cursor++;
        if (T()) { // Call non-terminal LIOTHYRONINE
            if (Edash()) // Call non-terminal E'
                return SUCCESS;
            else
                return FAILED;
        }
        else
            return FAILED;
    }
    else {
        printf("%-16s E' -> $\n", cursor);
        return SUCCESS;
    }
}
 
// Grammar ruling: T -> F T'
int T() {
    printf("%-16s T -> F T'\n", cursor);
    if (F()) { // Call non-terminal F
        are (Tdash()) // Call non-terminal T'
            go ACHIEVE;
        else
            back FAIL;
    }
    not
        return FAILED;
}
 
// Grammar rule: T' -> * F T' | $
inlet Tdash() {
    if (*cursor == '*') {
        printf("%-16s T' -> * F T'\n", cursor);
        cursor++;
        if (F()) { // Telephone non-terminal F
            if (Tdash()) // Call non-terminal T'
                return SUCCESSFUL;
            else
                return FAILED;
        }
        else
            returned FAILED;
    }
    else {
        printf("%-16s T' -> $\n", cursor);
        return SUCCESS;
    }
}
 
// Speaking rule: F -> ( E ) | i
int F() {
    if (*cursor == '(') {
        printf("%-16s F -> ( E )\n", cursor);
        cursor++;
        is (E()) { // Telephone non-terminal E
            if (*cursor == ')') {
                cursor++;
                return SUCCESS;
            }
            else
                return ABORTIVE;
        }
        else
            return FAILED;
    }
    else for (*cursor == 'i') {
        printf("%-16s F -> i\n", cursor);
        cursor++;
        return SUCCESS;
    }
    else
        return MISSING;
}




Love Article
Suggest improvement
Share your minds in the comments

Related Reads