0% found this document useful (0 votes)
8 views30 pages

Lecture 5-6

The document discusses the role of syntax analyzers (parsers) in compiler construction, detailing how they verify token strings against grammar and report syntax errors. It covers context-free grammars, derivation trees, ambiguity in grammars, and various grammar transformations such as left factoring and left recursion elimination. Additionally, it outlines parsing methods, including top-down and bottom-up parsing, and presents different types of parsers.
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)
8 views30 pages

Lecture 5-6

The document discusses the role of syntax analyzers (parsers) in compiler construction, detailing how they verify token strings against grammar and report syntax errors. It covers context-free grammars, derivation trees, ambiguity in grammars, and various grammar transformations such as left factoring and left recursion elimination. Additionally, it outlines parsing methods, including top-down and bottom-up parsing, and presents different types of parsers.
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/ 30

Compiler Construction

Lecture 5-6: Parsing


BITS Pilani
Hyderabad Campus
NEED AND ROLE OF SYNTAX ANALYZER (Parser)

1. The parser obtains a string of tokens from the lexical analyzer and
verifies that the string of token names can be generated by the
grammar for the source language.

2. We expect the parser to report any syntax errors in an intelligible


fashion and to recover from commonly occurring errors to continue
processing the remainder of the program.

BITS Pilani, Hyderabad Campus


How to describe language syntax
 Regular Expressions ??

 Context-free grammars

 Captures syntax structures of programming languages

 Cannot handle the problems:


To check whether variables are of types on which
operations are allowed.
To check whether a variable has been declared before use.
To check whether a variable has been initialized.
BITS Pilani, Hyderabad Campus
Context Free Grammar (CFG)
 where
 the set of variables
 the set of terminals
 the set of productions
• each production of the form where and
 start variable
 Example:
 Example: G2:
G1:
Context Free Grammar (CFG)
 Notation:
If , , …, are the productions for the variable of some
grammar, then we may express them by the notation

 Example:

|
Context Free Language (CFL)
 The language generated by a grammar


If is a context free grammar, then we call
a context free language.

where
Example:

is a CFG.
Thus, is a context free language.
Derivation (Parse) Tree
 Consider the following CFG for balanced parentheses
Let

Derivation 1:

Derivation 2:

 Pictorial representation of
both derivations for :
Derivation (Parse) Tree (formal definition)
 Parse tree for each .

 If is a rule, then the parse tree is

 If is a rule, then
Derivation (Parse) Tree (formal definition)
 Example: Consider a CFG with productions

.

 String
Left most and Right most derivations.
 If at each step in a derivation a production is applied to the
leftmost variable, then the derivation is said to be leftmost.

 If at each step in a derivation a production is applied to the


rightmost variable, then the derivation is said to be rightmost.

 Example: ,
 String:
Leftmost derivation:
rightmost derivation
Ambiguity
 A CFG such that some word has two distinct parse trees (two
distinct leftmost derivations / two distinct rightmost derivations)
is said to be ambiguous.

Example: Consider a CFG with productions

String:
Ambiguity
Example: Consider a CFG with productions

Show that the CFG is ambiguous.


Cont..

 Dangling else problem

 String:

BITS Pilani, Hyderabad Campus


Some important Terms

 Parse tree / syntax tree

 Letmost derivation and Rightmost derivation

 Ambiguous grammars

BITS Pilani, Hyderabad Campus


Grammar transformations
• Since it is sometimes convenient to have a grammar that
satisfies a particular property for a language, we would like to
be able to transform grammars into other grammars that
generate the same language, but that possibly satisfy different
properties.

• Forms of grammar transformations:


• Removing unreachable variables.
• Removing Left factoring.
• Removing left recursion.

BITS Pilani, Hyderabad Campus


Unreachable variable

BITS Pilani, Hyderabad Campus


Left factoring

• Left factoring is a grammar transformation that is applicable


when two productions left factoring for the same nonterminal
start with the same sequence of (terminal and/or
nonterminal) symbols.

may be transformed into

where is a new nonterminal.

In general, find the longest prefix common to two or more of its alternatives.

BITS Pilani, Hyderabad Campus


Example

Left factor the following grammar

2)

BITS Pilani, Hyderabad Campus


Left recursion

Left recursive Grammar


A -> A α | β (left most symbol in RHS is equal to the symbol in RHS)
A A
A

A α
β A α
Since the variable
A α A calls A without doing
β anything this might lead to
an infinite loop
Language generated by this grammar is βα* A α

BITS Pilani, Hyderabad Campus


Left Recursion Elimination

BITS Pilani, Hyderabad Campus


Example

BITS Pilani, Hyderabad Campus


Left Recursion general case

To eliminate left recursion (general form)


A  A1 | A2 | ... | Am | 1 | 2 | ... | n

with
A  1A’ | 2A’ | ... | nA’
A’  1A’ | 2A’ | ... | m A’ | 

BITS Pilani, Hyderabad Campus


Left Recursion

 There are three types of left recursion:


direct (A  A x)
indirect (A  B C, B  A )
hidden (A  B A, B  )

BITS Pilani, Hyderabad Campus


Eliminating indirect Left
Recursion
Consider the grammar:
S  Aa
A  Sb | c
Here the grammar does not have direct left recursion .. but
has a left recursion because S =>Aa => Sba

Rewrite the above grammar,


S  Aa
A  Aab|c

Replace S with Aa in the


second production.
BITS Pilani, Hyderabad Campus
Eliminating indirect left
recursion
ordering: S, E, T, F i=S i=E i=T, j=E
SE SE SE SE
E  E+T E  E+T E  TE' E  TE'
ET ET E'+TE'| E'+TE'|
T  E-T T  E-T T  E-T T  TE'-T
TF TF TF TF
F  E*F F  E*F F  E*F F  E*F
F  id F  id F  id F  id

Algorithm for eliminating indirect recursion


SE
List the nonterminals in some order A1, A2, ...,An
E  TE'
for i=1 to n E'+TE'|
for j=1 to i-1 T  FT'
if there is a production AiAj, T'  E'-TT'|
replace Aj with its rhs F  E*F
eliminate any direct left recursion on Ai F  id
BITS Pilani, Hyderabad Campus
Eliminating indirect left
recursion
i=F, j=E i=F, j=T

SE SE SE


E  TE' E  TE' E  TE'
E'+TE'| E'+TE'| E'+TE'|
T  FT' T  FT' T  FT'
T'  E'-TT'| T'  E'-TT'| T'  E'-TT'|
F  TE'*F F  FT'E'*F F  idF'
F  id F  id F'  T'E'*FF'|

BITS Pilani, Hyderabad Campus


Parsing methods

 Top-Down parsing
 Construction of the parse tree starts at the root (from the start
symbol) and proceeds towards leaves (token or terminals)
 Can be viewed as finding a leftmost derivation for an input string.

 Bottom-up parsing
 Construction of the parse tree starts from the leaf nodes and
proceeds towards root (start symbol).
 Order is that of the reverse of a rightmost derivation

BITS Pilani, Hyderabad Campus


Top down and bottom-up parsers
S -> aABe Input string: abbcde
A -> Abc | b
Top-Down parsing:
B -> d
 Can be viewed as finding a leftmost derivation for
an input string.
 Main Task is to make a decision to use the right
production for deriving the string

Bottom-up parsing:
 Order is that of the reverse of a rightmost derivation.
 Main Task is to make a decision of whether to shift or reduce.

BITS Pilani, Hyderabad Campus


Types of parsers

Parsers

Top Down Bottom Up

Operator LR
Backtracking Predictive
Precedence

Recursive
LL(1) LR(0) SLR(1) CLR(1) LALR(1)
Descent

BITS Pilani, Hyderabad Campus


Thank you

BITS Pilani, Hyderabad Campus

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