0% found this document useful (0 votes)
11 views51 pages

CC Unit 3

The document provides an overview of syntax analysis in compilers, detailing the role of parsers in checking language syntax and the differences between parse trees and syntax trees. It discusses various types of parsers, error handling strategies, and methods for removing ambiguity in grammars. Additionally, it covers recursive descent parsing and provides examples of converting ambiguous grammars into unambiguous ones.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views51 pages

CC Unit 3

The document provides an overview of syntax analysis in compilers, detailing the role of parsers in checking language syntax and the differences between parse trees and syntax trees. It discusses various types of parsers, error handling strategies, and methods for removing ambiguity in grammars. Additionally, it covers recursive descent parsing and provides examples of converting ambiguous grammars into unambiguous ones.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 51

Unit 3

Syntax Analysis
Introduction to parser
• The syntax analyzer (parser) basically checks for the
syntax of the language.
• It takes a token from the lexical analyzer and group
them in such a way that some programming
structure (syntax) can be recognized.
• This overall process is called syntax checking of the
language.
Difference between syntax tree and parse tree

• Parse tree are much more detailed representation of the source


language than that of syntax tree.

• Syntax trees are smaller than parse trees and these are much
space and time efficient.

• Parse tree shows how the statement gets parsed according to


their syntactic specification.
Types of Parser
Top-down and Bottom-up parsers

• Top-down parsers build parse trees from the top (root) to the
bottom (leaves).
• Bottom-up parsers build parse trees from the leaves and work
up to the root.
• In both case input to the parser is scanned from left to right,
one symbol at a time.
• The output of the parser is some representation of the parse
tree for the stream of tokens.
Tasks conducted during parsing

There are number of tasks that might be conducted


during parsing. Such as;
o Collecting information about various tokens into the
symbol table.
o Performing type checking and other kinds of semantic
analysis.
o Generating intermediate code.
Syntax Error Handling:
• Planning the error handling right from the start can both simplify the structure

of a compiler and improve its response to errors.

• The program can contain errors at many different levels. e.g.,

§ Lexical – such as misspelling an identifier, keyword, or operator.

§ Syntax – such as an arithmetic expression with unbalanced parenthesis.

§ Semantic – such as an operator applied to an incompatible operand.

§ Logical – such as an infinitely recursive call.


Goals of the error handler in a parser

The error handler in a parser has simple goals:

o It should present errors clearly and


accurately.

o It should recover from each error quickly


enough to be able to detect subsequent errors.

o It should not significantly slow down the


processing of correct programs.
Error-Recovery Strategies:

There are many different general strategies that a


parser can employ to recover from a syntactic error.
§ Panic mode
§ Phrase level

§ Error production

§ Global correction
Panic mode

§ This is used by most parsing methods.

§ On discovering an error, the parser discards input symbols


one at a time until one of a designated set of synchronizing
tokens (delimiters; such as; semicolon or end) is found.

§ Panic mode correction often skips a considerable amount of


input without checking it for additional errors.

§ It is simple.
Phrase-level recovery
§ On discovering an error; the parser may perform local correction on the

remaining input; i.e., it may replace a prefix of the remaining input by

some string that allows the parser to continue.

§ e.g., local correction would be to replace a comma by a semicolon,

deleting an extraneous semicolon, or insert a missing semicolon.

§ Its major drawback is the difficulty it has in coping with situations in

which the actual error has occurred before the point of detection.
Error productions

§ If an error production is used by the parser,


can generate appropriate error diagnostics to
indicate the erroneous construct that has been
recognized in the input.
Global correction

Given an incorrect input string x and grammar


G, the algorithm will find a parse tree for a
related string y, such that the number of
insertions, deletions and changes of tokens
required to transform x into y is as small as
possible.
Global correction

• Unfortunately these methods are in general too


costly to implement in terms of time and space
so these techniques are currently only of
theoretical interest.
Writing grammars for context free environments

• In syntax analysis , the specification of syntax


is given by context free grammar.
• CFG is a collection of : G=(V,T,S,P)
• V= set of non terminal
• T= set of terminal
• S= start symbol
• P= set of production rules
Example
• Let G be a context free grammar for which the
production rules are given as below.

S→ aB | bA
A → a | aS | bAA
B → b | bS | aBB

derive the string ‘ aaabbabbba’ using above


grammar.
Ambiguity
• A grammar that produces more than one parse
tree for some sentence is said to be ambiguous.
• In another word we can say that an ambiguous
grammar is one that produces more than one
leftmost or rightmost derivation for the same
sentence
Ambiguous grammar example
• E→ E+E | E*E | (E) | id

• This grammar can permits more than one parse


tree for expressions like ,

id+id*id
Removing Ambiguity by precedence and Associativity rules-

• An ambiguous grammar may be converted


into an unambiguous grammar by
implementing the precedence and
Associativity constraints.

• These constraints are implemented using the


following rules-
Removing Ambiguity by precedence and Associativity rules-

• Rule-01:

• The precedence of operators is implemented using following

rules-

• The level at which the production is present defines the priority

of the operator contained in it.

• The higher the level of the production, the lower the priority of

operator.

• The lower the level of the production, the higher the priority of

operator.
Removing Ambiguity by precedence and Associativity rules-

• Rule-02:

• The Associativity of operators is implemented using following

rules

• If the operator is left associative, induce left recursion in its

production.

• If the operator is right associative, induce right recursion in its

production.
Problem-01:

• Convert the following ambiguous grammar


into unambiguous grammar-
• R → R + R / R . R / R* / a / b
• where * is kleen closure and . is concatenation
• Solution-

• The given grammar is ambiguous. It can be converted into


unambiguous grammar by implementing the precedence and
Associativity constraints.

• Given grammar consists of the following operators-


• +,.,*
• Given grammar consists of the following operands-
• a,b
• The priority order will be-
• (a , b) > * > . > +
• where-
• . operator is left associative
• + operator is left associative
• Now,
• On the basis of priority and Associativity of operators, we can write the corresponding unambiguous
grammar as-

• E→E+T/T
• T→T.F/F
• F → F* / G
• G→a/b
• Unambiguous Grammar

• OR

• E→E+T/T
• T→T.F/F
• F → F* / a / b
• Problem-02:

• Convert the following ambiguous grammar
into unambiguous grammar-
• bexp → bexp or bexp / bexp and bexp / not
bexp / T / F
• where bexp represents Boolean expression, T
represents True and F represents False
• Solution-

• The given grammar is ambiguous. It can be converted
into unambiguous grammar by implementing the
precedence and associativity constraints.

• Given grammar consists of the following operators-
• or , and , not
• Given grammar consists of the following operands-
• The priority order will be-
• (T , F) > not > and > or
• where-
• and operator is left associative
• or operator is left associative
• Now,
• On the basis of priority and associativity of operators, we can write the
corresponding unambiguous grammar as-

• bexp → bexp or M / M
• M → M and N / N
• N → not N / G
• G→T/F
• Unambiguous Grammar

• OR

• bexp → bexp or M / M
• M → M and N / N
• N → not N / T / F
Top Down Parsing for id+id*id
Top Down Parsing for id+id*id
Top Down Parsing for id+id*id
Top Down Parsing for id+id*id
Recursive Descent parser

• Recursive Descent Parser uses the technique of Top-Down Parsing without backtracking.

• It can be defined as a Parser that uses the various recursive procedure to process the input

string with no backtracking.

• It can be simply performed using a Recursive language.

• The first symbol of the string of R.H.S of production will uniquely determine the correct

alternative to choose.

• The major approach of recursive-descent parsing is to relate each non-terminal with a

procedure.

• One of major drawback or recursive-descent parsing is that it can be implemented only for

those languages which support recursive procedure calls and it suffers from the problem of

left-recursion.
Recursive Descent parser
• Example − Write down the algorithm using
Recursive procedures to implement the following
Grammar.
• E → TE′
• E′ → +TE′
• T → FT′
• T′ →∗ FT′|ε
• F → (E)|id

You might also like