0% found this document useful (0 votes)
5 views5 pages

Unit 2 CD

Uploaded by

Harsh Singhal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views5 pages

Unit 2 CD

Uploaded by

Harsh Singhal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Unit 2

Context-free grammar is the formal language which is used to


generate all possible patterns of string in a grammar

What is ambiguous grammar?


A Context-free grammar is said to be ambiguous if there exists more
than one derivation tree for the given input string i.e., more than one
Leftmost Derivation Tree (LMDT) or Rightmost Derivation Tree
(RMDT)
For Example:

Let us consider this grammar: E -> E+E| id


What is parser?
A parser is a key component of a compiler or interpreter that analyzes
the structure of source code according to the rules of a formal
grammar.
The main purpose of parser is to determine/ check the syntax of input
code is correct and to build a data structure that represents the
structure of the code, such as a parse tree.
Parsing Techniques
1. Top-down parser [ LL (1)]
Top-down parser used leftmost derivation concept
Generate parse tree by expanding variables and it start from start
symbol and end on terminal and generate the output, matching the
input tokens with the production rules of the grammar. It closely
follows the structure of the grammar rules and is commonly used in
recursive descent parsing.
Problems we face when designing the Top-Down Parser

Left Recursion:

Handling left-recursive grammar rules can be problematic for top-


down parsers. Left recursion occurs when a non-terminal symbol in a
grammar directly or indirectly derives itself as a leftmost symbol in
one of its productions. This can lead to infinite recursion during
parsing

Backtracking:

Imagine solving a puzzle but realizing you made a mistake a few


steps back. You have to go back and try a different approach. Top-
down parsers sometimes need to do this too, but too much
backtracking can slow them down.

Increase memory usage and slow them down


2. Bottom-up parser [ LR (1), LR (0)]
Bottom-up parser is used leftmost derivation concept, generate the
parse tree of given input string by the production rules of the grammar
by compressing variable and is commonly used in non-recursive
descent parsing.
It start from terminal and end on start symbol and generate the output

Recursive Predictive Parser:

A recursive predictive parser is a type of top-down parser that uses


recursive procedures (functions) to perform predictive parsing. It
predicts which production rule to apply based on the current input
symbol and the lookahead token without needing backtracking.

Process:

The parser starts with the start symbol of the grammar and recursively
calls parsing functions for each non-terminal symbol in the grammar.

At each step, the parser predicts which production rule to apply based
on the current non-terminal symbol and the lookahead token.

If the prediction is correct, the parser proceeds to parse the next


symbol. If not, it reports an error.

The parsing functions may call each other recursively to handle nested
non-terminal symbols, mirroring the structure of the grammar rules.

Advantages:

1. Simple to understand and implement, like following a recipe.


2. Works efficiently for simple grammars where each step is clear and
predictable.

Limitations:
Can get stuck in loops if the instructions are not clear.

May struggle with complex or ambiguous instructions.

Non-Recursive Predictive Parser:

A non-recursive predictive parser is a type of top-down parser that


avoids the use of recursion in favor of explicit stack-based parsing or
iterative parsing techniques. It predicts which production rule to apply
based on the current input symbol and the lookahead token without
using recursive function calls.

Process:

Instead of using recursive function calls, a non-recursive predictive


parser typically uses an explicit stack to keep track of the parsing
state.

The parser iteratively processes the input symbols, maintaining the


current parsing state on the stack.
At each step, the parser predicts which production rule to apply based
on the current input symbol and the lookahead token.
The parsing process continues until the entire input string is parsed,
reporting any errors encountered along the way.
Advantages:
Avoids getting stuck in loops or running out of memory due to
recursion.
Can handle more complex instructions and ambiguities more
effectively.

Limitations:
Requires more explicit control flow and management of parsing
state.

May be more complex to implement compared to recursive parsers.

Comparison between Recursive and Non-Recursive


Predictive Parsers
Feature Recursive Predictive Parser Non-Recursive Predictive
Parser
Definition Uses recursive functions to Uses an explicit stack or table
parse input for parsing
Implementation Implemented with recursive Implemented with iterative loops
function calls and stack
Control Flow Control flow is managed by the Control flow is managed
call stack explicitly by the parser
Ease of Easier to understand for simple Can be more complex due to
Understanding grammars explicit stack use
Stack Usage Uses the program's call stack Uses an explicit stack data
structure
Memory May cause stack overflow with Better memory management,
Management deep recursion less risk of overflow
Handling Left Cannot handle left-recursive Can handle left recursion with
Recursion grammars directly modifications
Error Reporting Error handling can be complex Can provide more detailed error
messages
Example of Use Suitable for small, simple Suitable for larger, more
grammars complex grammars

You might also like