0% found this document useful (0 votes)
16 views

SPCC Module 5 Lect 5 Syntax Analysis Part 1.Pptx

This document provides an introduction to syntax analysis, also known as parsing, which is the second phase of a compiler that generates a parse tree. It discusses the role of a parser, error handling techniques, and context-free grammar, outlining various types of errors and recovery methods. The document also explains the structure of context-free grammar and demonstrates rightmost and leftmost derivations for generating valid strings.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

SPCC Module 5 Lect 5 Syntax Analysis Part 1.Pptx

This document provides an introduction to syntax analysis, also known as parsing, which is the second phase of a compiler that generates a parse tree. It discusses the role of a parser, error handling techniques, and context-free grammar, outlining various types of errors and recovery methods. The document also explains the structure of context-free grammar and demonstrates rightmost and leftmost derivations for generating valid strings.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

SYNTAX ANALYSIS

LECTURE 1

Prepared By: Vaibhav Ambhire


◼ Introduction
◼ Role of a parser
CONTENT ◼ Error Handling Techniques
◼ Context Free Grammar

Prepared By: Vaibhav Ambhire


◼ Syntax analysis is the second phase of the compiler

◼ It is also called as parsing and it generates parse tree

INTRODUCTION ◼ Parser: It is the program that takes tokens and


grammar (context- free grammar -CFG) as input and
validates the input token against the grammar

Prepared By: Vaibhav Ambhire


ROLE OF THE PARSER

Source
Program Parse Intermediate
token
Tree Representation
Lexical Semantic
Parser
Analyzer Analyzer

get next token

◼ It obtains a string of tokens from the lexical analyzer and verifies that
Symbol the string can be generated by the grammar for the source language
Table
◼ It also reports any syntax errors

◼ It also recovers from commonly occurring errors so that it can continue


processing the remaining input
Prepared By: Vaibhav Ambhire
◼ Lexical: such as misspelling a keyword.

◼ Syntactic: such as an arithmetic expression with

TYPE OF unbalanced parentheses.

ERRORS ◼ Semantic, such as an operator applied to an


incompatible operand.

◼ Logical: such as an infinitely recursive call.

Prepared By: Vaibhav Ambhire


Goals of Error handler:

◼ Report the presence of errors clearly and accurately


ERROR
◼ Recover from each error quickly enough to detect
HANDLER IN
subsequent errors
PARSER
◼ Add minimal overhead to the processing of correct
programs

Prepared By: Vaibhav Ambhire


1. Panic Mode recovery
ERROR 2. Phrase Level
RECOVERY
3. Error Production
TECHNIQUES
4. Global Correction

Prepared By: Vaibhav Ambhire


◼ Panic mode error recovery is based on the idea of
discarding input symbols one at a time until one of the
designated set of synchronized tokens is found

Panic ◼ The synchronizing tokens are usually delimiters, such as


semicolon or end
mode
recovery ◼ Advantage of simplicity and does not go into an infinite
loop.

◼ Useful when multiple errors in the same statement are


rare

Prepared By: Vaibhav Ambhire


◼ On discovering error, a parser may perform a local fix to
allow the parser to continue and simultaneously report
error

◼ In phrase level recovery mode each empty entry in the


Phrase parsing table is filled with a pointer to a specific error
Level routine to take care of that error

Recovery ◼ These error routine may be:

1. Change , insert, or delete input symbol

2. Issue an appropriate error message

3. Pop items from the stack

Prepared By: Vaibhav Ambhire


◼ Error production adds rules to the grammar that describes
the erroneous syntax .

◼ This strategy can resolve many , but not all potential


errors

◼ It includes production for common errors and we can

Error augment the grammar for the production rules that

Production generates the erroneous constructs

◼ A parser constructed from an augmented grammar by


these error productions detects the anticipated error
production is used during parsing

◼ Since it is almost impossible to know all the errors that can


be made by the programmers , this method is not practical

Prepared By: Vaibhav Ambhire


◼ Replace incorrect input with input that is correct and require

the fewer changes to create

◼ This requires expensive techniques that are costly in terms of

time and space

Global The algorithm states that:

Correction ◼ For a given grammar G give an incorrect input string X

◼ Now find a parse tree for a related string y with the help of an

algorithm such that the number of insertions , deletion and


changes of token require to change x into y is as small as
possible

Prepared By: Vaibhav Ambhire


A context free grammar has four tuple <V, T, P, S> where,

◼ V: set if non terminal symbols for writing the grammar


Context ◼ T: Set of terminal symbol T, used as token for the language
Free
◼ P: Set of production rule
Grammar
◼ S: A special non terminal which is start

Prepared By: Vaibhav Ambhire


E → E + E | E * E | ( E ) | - E | id
To generate a valid string: - ( id + id * id )

Steps:
Rightmost Derivation
Here, rightmost Non-Terminal is replaced in every step.
Context
Free E→(E)
Grammar E → (E+E)
E→ (E+E*E)
E→ (E+E*id)
E→(E+id*id)
E→(id+id*id)

Prepared By: Vaibhav Ambhire


E → E + E | E * E | ( E ) | - E | id
To generate a valid string: - ( id + id * id )

Steps:
Leftmost Derivation

Context Here, leftmost Non-Terminal is replaced in every step.

Free
Grammar E→(E)
E→(E*E)
E→ (E+E*E)
E → (id+E*E)
E → (id+id*E)
E→(id+id*id)

Prepared By: Vaibhav Ambhire

You might also like