LL(1) Parser Program in CIn this article, we will discuss the LL(1) parser program in C. But before discussing LL(1) parser implementation, we need to know about the LL(1) parser with its rules. What is LL(1) Parser?LL(1) It is a top-down parser. It deals with the LL(1) class of grammar. The first "L" indicates that the input is scanned from left to right, the second "L" indicates that the input is derivated to the left, and "1" indicates the number of lookaheads that were utilized to arrive at the decision. We must determine whether the grammar is LL(1) or not before creating an LL(1) parsing table. Building Rules for the LL(1) Parsing Table:We can use the following rules to understand the LL(1) parsing in C. Step 1: Grammar Evaluation: - Begin by examining a provided context-free grammar written in BNF (Backus-Naur Form).
- Make that the grammar is LL(1) parseable and not left-recursive. Correct transformations can be used to get rid of left recursion.
- Put the grammar in a format that will be useful for analysis, such as a table of parsing rules or a collection of production rules.
Step 2: The first and second sets: - For each non-terminal symbol in the grammar, determine the FIRST set. The entire terminal symbols that can begin a string derived from A are contained in the FIRST set of a non-terminal A.
- For each non-terminal sign in the grammar, determine the FOLLOW set. All the terminal symbols that can immediately follow a string deriving from A in a derivation are contained in the FOLLOW set of a non-terminal A.
Step 3: How to Build a Parsing Table: - In each instance where A is a non-terminal and is a string of terminals and/or non-terminals:
- Add A-> to [A, t] in the table for each terminal symbol t in the FIRST(α).
- If ε (epsilon) is in FIRST(α), for each terminal symbol t in FOLLOW(A), add A -> α to the table entry [A, t].
- Add A-> to the table entry [A, $] if it is in FIRST() and $ (end of input) is in FOLLOW(A).
- The grammar is not LL(1) and cannot be parsed using LL(1) parsing if there are any parsing table conflicts (different entries in the same cell).
Step 4: How to Use the Parsing Table: - First, create a parsing stack from scratch and place the grammar's start symbol on it.
- The current input token is read.
- Use the current input token as the column index and the top of the stack as the row index to consult the parsing table.
- If the table entry is blank or contains a mistake, the input contains a syntax error.
- Pop A from the stack and push in reverse order onto the stack if the table entry has a production A ->.
Program for LL(1) Parsing Table in C:In this program, the program reads a file named "text.txt" to read grammar. Epsilon is represented by the symbol "^". The program considers LL(1) to be the input grammar. text.txt Program:Output: FIRST OF E: ( t
FIRST OF A: + ^
FIRST OF T: ( t
FIRST OF B: * ^
FIRST OF F: ( t
FOLLOW OF E: $ )
FOLLOW OF A: $ )
FOLLOW OF T: + $ )
FOLLOW OF B: + $ )
FOLLOW OF F: * + ) $
FIRST OF TA: ( t
FIRST OF +TA: + ^
FIRST OF FB: ( t
FIRST OF *FB: * ^
FIRST OF t: t
FIRST OF (E): (
( ) * + t $
E TA TA
A ^ ^ ^ +TA ^
T FB FB
B ^ ^ *FB ^ ^
F (E) t
|