0% found this document useful (0 votes)
39 views12 pages

Asif Khan (216104) Compiler Construction Presentation

The document presents a detailed overview of Context Free Grammars, including definitions of terminals, nonterminals, start symbols, and productions. It discusses derivations, parse trees, ambiguity, and methods for eliminating ambiguity and left recursion, as well as left factoring techniques. The content is aimed at understanding the structure and transformation of grammars for parsing in computer science.

Uploaded by

asafkhan3411
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)
39 views12 pages

Asif Khan (216104) Compiler Construction Presentation

The document presents a detailed overview of Context Free Grammars, including definitions of terminals, nonterminals, start symbols, and productions. It discusses derivations, parse trees, ambiguity, and methods for eliminating ambiguity and left recursion, as well as left factoring techniques. The content is aimed at understanding the structure and transformation of grammars for parsing in computer science.

Uploaded by

asafkhan3411
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

Name : Asif Khan

Roll No: 216104


Class : BS-CS-5TH (Morning)

Presentation Topic:
Context Free
Grammar
Context free grammars

 Terminals
 Nonterminals
 Start symbol
 productions

expression -> expression + term


expression -> expression – term
expression -> term
term -> term * factor
term -> term / factor
term -> factor
factor -> (expression)
factor -> id
Derivations

 Productions are treated as rewriting rules to


generate a string
 Rightmost and leftmost derivations
 E -> E + E | E * E | -E | (E) | id
 Derivations for –(id+id)
 E => -E => -(E) => -(E+E) => -(id+E)=>-(id+id)
Parse trees

 -(id+id)
 E => -E => -(E) => -(E+E) => -(id+E)=>-(id+id)
Ambiguity

 For some strings there exist more than one parse


tree
 Or more than one leftmost derivation
 Or more than one rightmost derivation
 Example: id+id*id
Elimination of ambiguity
Elimination of ambiguity
(cont.)
 Idea:
 A statement appearing between a then and an else
must be matched
Elimination of left
recursion
 A grammar is left recursive if it has a non-
+
terminal A such that there is a derivation A=>

 Top down parsing methods cant handle left-
recursive grammars
 A simple rule for direct left recursion elimination:
 For a rule like:
 A -> A α|β
 We may replace it with
 A -> β A’
 A’ -> α A’ | ɛ
Left recursion elimination (cont.)

 There are cases like following


 S -> Aa | b
 A -> Ac | Sd | ɛ
 Left recursion elimination algorithm:
 Arrange the nonterminal in some order A1,A2,…,An.
 For (each i from 1 to n) {
 For (each j from 1 to i-1) {
 Replace each production of the form Ai-> Aj γ by the
production Ai -> δ1 γ | δ2 γ | … |δk γ where Aj-> δ1 | δ2 |
… |δk are all current Aj productions
 }
 Eliminate left recursion among the Ai-productions
 }
Left factoring

 Left factoring is a grammar transformation that is


useful for producing a grammar suitable for
predictive or top-down parsing.
 Consider following grammar:
 Stmt -> if expr then stmt else stmt
 | if expr then stmt
 On seeing input if it is not clear for the parser which
production to use
 We can easily perform left factoring:
 If we have A->αβ1 | αβ2 then we replace it with
 A -> αA’
 A’ -> β1 | β2
Left factoring (cont.)

 Algorithm
 For each non-terminal A, find the longest prefix α
common to two or more of its alternatives. If α<> ɛ,
then replace all of A-productions A->αβ1 |αβ2 | … |
αβn | γ by
 A -> αA’ | γ
 A’ -> β1 |β2 | … | βn
 Example:
 S -> I E t S | i E t S e S | a
 E -> b
Thank You

You might also like