Javatpoint Logo
Javatpoint Logo

Recursive Descent Parser Program in C

Introduction:

The parsing process involves determining whether or not the given program is legitimate. It examines a string as input to see if it follows to the established grammar.

Parsers come in two varieties:

1. Top-Down Parser:

These parsers start at the tree's root and work their way down to its leaf. The Recursive Descent Parser and LL parsers are two examples of top-down parsers.

2. Bottom-Up Parsers:

With this parsing technique, the entire program is reduced to the start symbol. The Bottom-Up parsers are Operator Precedence Parser, LR(0) Parser, SLR Parser, LALR Parser, and CLR Parser.

What is a Recursive Descent Parser?

Continuative Descent Top-down parsers, like Parser, construct the parse tree from the root to the leaf. It recognizes the input using recursive functions and may or may not employ backtracking. Predictive parsing is another name for the recursive descent parser variant that does not require backtracking.

Backtracking:

You can follow the several steps to utilize backtracking:

  1. It repeatedly scans the input until we locate the right path.
  2. The following qualities of the grammar must be present in order to create a recursive descent parser:
  3. It shouldn't be allowed to repeat.
  4. It needs to be left-factored. Alternatives shouldn't share a prefix.
  5. Recursion should be possible in languages.
  6. Backtracking will be required by the recursive descent parser if the grammar is not left-factored.

Example

Before removing left recursion After removing left recursion
E -> E + T | T
T -> T * F | F
F ->( E ) | id
E -> T E'
E' -> + T E' | e
T -> F T'
T' -> * F T' | e
F ->( E ) | id

Now, we are going to create a separate program for each variable in the recursive descent parser.

Program:

Output:

Enter the string

Input		 Action
--------------------------------
i+(i+i)*i        E -> T E'
i+(i+i)*i        T -> F T'
+(i+i)*i         F ->i
+(i+i)*i         T' -> $
+(i+i)*i         E' -> + T E'
(i+i)*i          T -> F T'
(i+i)*i          F -> ( E )
i+i)*i           E -> T E'
i+i)*i           T -> F T'
+i)*i            F ->i
+i)*i            T' -> $
+i)*i            E' -> + T E'
i)*i             T -> F T'
)*i              F ->i
)*i              T' -> $
)*i              E' -> $
*i               T' -> * F T'
                 F ->i
                 T' -> $
                 E' -> $
--------------------------------
String is successfully parsed

Recursive Descent Parser: Advantages and Drawbacks

There are several advantages and disadvantages of Recursive Descent Parser. Some main advantages and disadvantages of Recursive Descent Parser as follows:

Advantages:

  1. Implementing the Recursive Descent parsers is fairly simple. These are excellent for "quick and dirty"

Drawbacks:

  1. These parsers are slower than some of the other parsers. They are ineffective for parses requiring arbitrary long lookahead.
  2. Only languages that support recursive procedure calls can implement Left-recursion is a problem that it has.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA