Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

2.

Explain the terms constructing LL((1) parser and Constructing recursive descent Parsing
with examples?

LL(1) Parsers

LL(1) stands for "Left-to-right, Leftmost derivation, one Lookahead". These parsers are a type of "top-
down parsing" technique that analyzes an input string from left to right and constructs a parse tree
based on a context-free grammar (CFG).

Key Features:

Left-to-right processing: The input is scanned from left to right.

Leftmost derivation: The parser follows the leftmost derivation rule for the grammar.

One Lookahead: The parser uses only the next symbol (lookahead) in the input string to make
decisions about which production rule to apply.

Construction:

1. Grammar Preparation: The CFG must be LL(1). This means it cannot have left recursion or
ambiguity where the same lookahead symbol could lead to different productions. You might need to
remove left recursion or perform left factoring on the grammar.

2. FIRST and FOLLOW Sets: These sets are calculated for each non-terminal in the grammar.

FIRST(X): The set of terminal symbols that can appear as the first symbol in a string derived from
non-terminal X.

FOLLOW(X): The set of terminal symbols (including the end-of-input symbol $) that can follow an X
on the stack during parsing.

3. Parsing Table Construction: A table is created with rows for non-terminals and columns for
terminal symbols and $. Each entry in the table specifies the production rule to apply based on the non-
terminal on the stack and the lookahead symbol. The FIRST and FOLLOW sets are used to populate this
table without conflicts (multiple productions for the same combination).

Example:

Consider the grammar for simple arithmetic expressions:

E -> T E'

E' -> + T E' | ε

T -> F T'
T' -> * F T' | ε

F -> (E) | id

After calculating FIRST and FOLLOW sets and resolving any grammar issues, the parsing table might look
like this:

| | id | + | * | ( | ) | $ |

|-------|---|---|---|---|---|---|

|E |E | | |E | | |

| E' | | + | * | | ) | $ |

|T |T | | |T | | |

| T' | | | * | | ) | $ |

|F | id | | | ( | | |

Recursive Descent Parsing

Recursive descent parsing is a top-down parsing technique that uses a set of recursive functions, one for
each non-terminal in the grammar. Each function attempts to match the current input string with the
production rules defined for its corresponding non-terminal.

Key Features:

Recursion: Functions call themselves for parsing sub-expressions based on grammar rules.

Backtracking (Optional): If a match fails, some recursive descent parsers might backtrack and try
alternative production rules. LL(1) parsing, however, typically avoids backtracking due to its one-
lookahead nature.

Construction:

1. Function for Each Non-Terminal: Write a recursive function for each non-terminal in the grammar.

2. Function Logic: Each function takes the current input string as input and attempts to match it
against the production rules for its non-terminal.

- It checks if the first symbol in the input matches the first symbol of a production rule.

- If so, it recursively calls the appropriate function(s) for the remaining symbols in the production
rule, passing the remaining input string.

- If a match is successful, the function returns a successful indication.


- If a match fails at any point, some parsers might backtrack and try other production rules (unless
it's an LL(1) parser that can make deterministic decisions without backtracking).

Example (Simplified):

```python

def parse_E(input_string):

if input_string[0].isdigit():

# Handle digit as a factor

...

return parse_E'(input_string[1:])

elif input_string[0] == '(':

value = parse_E(input_string[1:])

if input_string[len(value)+1] == ')':

# Handle parenthesized expression

...

return parse_E'(input_string[len(value)+2:])

You might also like