0% found this document useful (0 votes)
6 views17 pages

Compi Desi CHP 03 p2

The document discusses syntax analysis in compiler design, focusing on bottom-up parsing techniques such as shift-reduce parsing and LR parsers. It outlines the differences between LL and LR parsing methods, error detection and recovery processes, and various types of compile-time errors with their recovery strategies. Additionally, it details error handling methods like panic mode recovery, statement mode recovery, error production, and global correction.

Uploaded by

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

Compi Desi CHP 03 p2

The document discusses syntax analysis in compiler design, focusing on bottom-up parsing techniques such as shift-reduce parsing and LR parsers. It outlines the differences between LL and LR parsing methods, error detection and recovery processes, and various types of compile-time errors with their recovery strategies. Additionally, it details error handling methods like panic mode recovery, statement mode recovery, error production, and global correction.

Uploaded by

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

Course Name:

Compiler Design

1
CHAPTER 3

Syntax Analysis
Part 2

2
Bottom-Up Parser
 Bottom-up parsing starts from the leaf nodes of a tree and works in upward direction till
it reaches the root node.
 Here, we start from a sentence and then apply production rules in reverse manner in
order to reach the start symbol.
 The image given below depicts the bottom-up parsers available.
Shift-Reduce Parsing
 Shift-reduce parsing uses two unique steps for bottom-up parsing.

 These steps are known as shift-step and reduce-step.

 Shift step: The shift step refers to the advancement of the input pointer to the next input
symbol, which is called the shifted symbol. This symbol is pushed onto the stack. The
shifted symbol is treated as a single node of the parse tree.
 Reduce step: When the parser finds a complete grammar rule (RHS) and replaces it to
(LHS), it is known as reduce-step. This occurs when the top of the stack contains a handle.
To reduce, a POP function is performed on the stack which pops off the handle and replaces it
with LHS non-terminal symbol.
LR Parser
 The LR parser is a non-recursive, shift-reduce, bottom-up parser.

 It uses a wide class of context-free grammar which makes it the most efficient
syntax analysis technique.
 LR parsers are also known as LR(k) parsers, where,

 L stands for left-to-right scanning of the input stream;

 R stands for the construction of right-most derivation in reverse, and

 k denotes the number of lookahead symbols to make decisions.


…LR Parser
 There are three widely used algorithms available for constructing an LR parser:
 SLR(1) – Simple LR Parser:
 Works on smallest class of grammar
 Few number of states, hence very small table
 Simple and fast construction
 LR(1) – LR Parser:
 Works on complete set of LR(1) Grammar
 Generates large table and large number of states
 Slow construction
 LALR(1) – Look-Ahead LR Parser:
 Works on intermediate size of grammar
 Number of states are same as in SLR(1)
LL vs. LR
LL LR
Does a leftmost derivation. Does a rightmost derivation in reverse.
Starts with the root nonterminal on the Ends with the root nonterminal on the
stack. stack.
Ends when the stack is empty. Starts with an empty stack.
Uses the stack for designating what is still to Uses the stack for designating what is
be expected. already seen.
Builds the parse tree top-down. Builds the parse tree bottom-up.
Continuously pops a nonterminal off the Tries to recognize a right hand side on the
stack, and pushes the corresponding right stack, pops it, and pushes the corresponding
hand side. nonterminal.
Expands the non-terminals. Reduces the non-terminals.
Reads the terminals when it pops one off the Reads the terminals while it pushes them on
stack. the stack.
Pre-order traversal of the parse tree. Post-order traversal of the parse tree.
Error detection and Recovery
 In this phase of compilation, all possible errors made by the user are detected and
reported to the user in form of error messages.
 This process of locating errors and reporting them to users is called the Error
Handling process.
 Functions of an Error handler.
 Detection
 Reporting
 Recovery
Classification of Errors
Compile-time errors
Lexical phase errors:
 These errors are detected during the lexical analysis phase.

 Typical lexical errors are:


 Exceeding length of identifier or numeric constants.
 The appearance of illegal characters
 Unmatched string
…Compile-time errors
Error recovery for lexical errors
 Panic Mode Recovery
 In this method, successive characters from the input are removed one at a time until
a designated set of synchronizing tokens is found.
 Synchronizing tokens are delimiters such as; or }
Advantage:
 It is easy to implement and guarantees not to go into an infinite loop

Disadvantage:
a considerable amount of input is skipped without checking it for additional
errors
…Compile-time errors
Syntactic phase errors:
 These errors are detected during the syntax analysis phase.

 Typical syntax errors are:


 Errors in structure
 Missing operator
 Misspelled keywords
 Unbalanced parenthesis
…Compile-time errors
Error recovery for Syntactic phase errors

1. Panic Mode Recovery


 In this method, successive characters from input are removed one at a time
until a designated set of synchronizing tokens is found. Synchronizing
tokens are deli-meters such as ; or }
 Advantage is that its easy to implement and guarantees not to go to infinite
loop
 Disadvantage is that a considerable amount of input is skipped without
checking it for additional errors
…Compile-time errors
2. Statement Mode recovery
 In this method, when a parser encounters an error, it performs necessary correction
on remaining input so that the rest of input statement allow the parser to parse
ahead.
 The correction can be deletion of extra semicolons, replacing comma by semicolon
or inserting missing semicolon.
 While performing correction, at most care should be taken for not going in infinite
loop.
Disadvantage:
 It finds difficult to handle situations where actual error
occurred before point of detection.
…Compile-time errors
3. Error production
 If user has knowledge of common errors that can be encountered then,
these errors can be incorporated by augmenting the grammar with
error productions that generate erroneous constructs.
 If this is used then, during parsing appropriate error messages can be
generated and parsing can be continued.

Disadvantage:
 its difficult to maintain.
…Compile-time errors
4. Global Correction
 The parser examines the whole program and tries to find out the closest
match for it which is error free.
 The closest match program has less number of insertions, deletions and
changes of tokens to recover from erroneous input.

 Due to high time and space complexity, this method is not implemented
practically.
End of Chapter
3
17

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy