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

Lesson 18

This document discusses techniques for writing grammars and parsing text, including: - Lexical vs syntactic analysis and eliminating ambiguity - Eliminating left recursion through substitution and producing non-left-recursive grammars - Left factoring to produce grammars suitable for predictive parsing - Top-down parsing techniques like recursive descent parsing and LL(k) grammars - Non-context-free language constructs that cannot be parsed with a context-free grammar

Uploaded by

sdfgedr4t
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)
75 views

Lesson 18

This document discusses techniques for writing grammars and parsing text, including: - Lexical vs syntactic analysis and eliminating ambiguity - Eliminating left recursion through substitution and producing non-left-recursive grammars - Left factoring to produce grammars suitable for predictive parsing - Top-down parsing techniques like recursive descent parsing and LL(k) grammars - Non-context-free language constructs that cannot be parsed with a context-free grammar

Uploaded by

sdfgedr4t
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/ 32

LESSON 18

Contents
 Writing a Grammar
 Lexical Vs Syntactic Analysis
 Eliminating Ambiguity
 Elimination of Left Recursion
 Left Factoring
 Non-Context-Free Language Constructs
 Top Down Parsing
 Recursive Decent Parsing
 FIRST & FOLLOW
 LL(1) Grammars

2
Elimination of Left Recursion
 A grammar is left recursive if it has a non-terminal A such that
there is a derivation A ⇒+ Aα for some string α

 Top-down parsing methods cannot handle left-recursive grammars, so


a transformation is needed to eliminate left recursion.

 We already seen removal of Immediate left recursion i.e

A → Aα + β A → βA’
A’ → αA’ | ɛ

3
Elimination of Left Recursion..
 Immediate left recursion can be eliminated by the following
technique, which works for any number of A-productions.

A → Aα1 | Aα2 | … | Aαm | β1 | β2 | … | βn

 Then the equivalent non-recursive grammar is

A → β1A’ | β2A’ | … | βnA’


A’ → α1A’ | α2A’ | … | αmA’ | ɛ

 The non-terminal A generates the same strings as before but is no


longer left recursive.
4
Elimination of Left Recursion...
 This procedure eliminates all left recursion from the A and A'
productions (provided no αi is ɛ) , but it does not eliminate left
recursion involving derivations of two or more steps.

 Ex. Consider the grammar:


S→Aa|b
A→Ac|Sd|ɛ

 The non-terminal S is left recursive because S ⇒ Aa ⇒ Sda , but it is


not immediately left recursive.

5
Elimination of Left Recursion...
 Now we will discuss an algorithm that systematically eliminates left
recursion from a grammar.

 It is guaranteed to work if the grammar has no cycles or ɛ-


productions.

INPUT:
Grammar G with no cycles or ɛ-productions.
OUTPUT:
An equivalent grammar with no left recursion.

* The resulting non-left-recursive grammar may have ɛ-productions.


6
Elimination of Left Recursion...
METHOD:

7
Elimination of Left Recursion...
Ex. S→Aa|b
A→Ac|Sd|ɛ

 Technically, the algorithm is not guaranteed to work, because of the


ɛ-production but in this case, the production A → ɛ turns out to be
harmless.

 We order the non-terminals S, A.

 For i = 1 nothing happens, because there is no immediate left


recursion among the S-productions.

8
Elimination of Left Recursion...
 For i = 2 we substitute for S in A → S d to obtain the following A-
productions.
A→Ac|Aad|bd|ɛ

 Eliminating the immediate left recursion among these A-


productions yields the following grammar:

S →Aa|b
A → b d A’ | A’
A’ → c A’ | a d A’ | ɛ

9
Elimination of Left Recursion...
1. Extract the left common factor of this grammar.
2. Can the transformation of extracting the left common factor make
this grammar suitable for top-down parsing techniques?
3. After extracting the left common factor, the left recursion is
eliminated from the original method.
4. Is the resulting grammar suitable for top-down grammatical analysis?
 S -> S S + | S S * | a

 Extract the left common factor


S -> S S A | a
A -> + | *

10
Elimination of Left Recursion...
 Eliminate left recursion

 1 ) S -> S S A | a
 2) A -> + | *
 // i = 1
 1) S -> a B
 2) B -> S A B | ε
 3) A -> + | *
 // i = 2, j = 1
 1) S -> a B
 2) B -> a B A B | ε
 3) A -> + | *
11
Elimination of Left Recursion...
• S -> + S S | * S S | a
• first(S) = [+, *, a]
• follow(S) = [+, *, a, $]

12
Left Factoring
 Left factoring is a grammar transformation that is useful for
producing a grammar suitable for predictive, or top-down, parsing.

 If two productions with the same LHS have their RHS beginning with
the same symbol (terminal or non-terminal), then the FIRST sets will
not be disjoint so predictive parsing will be impossible

 Top down parsing will be more difficult as a longer lookahead will be


needed to decide which production to use.

 Ex.

13
Left Factoring..
 if A → αβ1 | αβ2 are two A-productions

 Input begins with a nonempty string derived from α


 We do not know whether to expand A to αβ1 or αβ2
 However , we may defer the decision by expanding A to αA'
 After seeing the input derived from α we expand
A' to β1 or A' to β2.

 This is called left-factoring.


A → α A’
A' → β1| β2

14
Left Factoring…
INPUT: Grammar G.
OUTPUT: An equivalent left-factored grammar.
METHOD:
 For each non-terminal A, find the longest prefix α common to two or
more of its alternatives.
 If α ≠ ɛ i.e., there is a nontrivial common prefix.
• Replace all of the A-productions A → αβ1 | αβ2 … | αβn | γ by
A → α A’ | γ
A' → β1| β2| …. | βn

• γ represents all alternatives that do not begin with α

15
Left Factoring…
 Ex Dangling else grammar:

 Here i, t, and e stand for if, then, and else


E and S stand for "conditional expression" and "statement."

 Left-factored, this grammar becomes:

16
Left Factoring…
1. Extract the left common factor of this grammar.
2. Can the transformation of extracting the left common factor make
this grammar suitable for top-down parsing techniques?
3. After extracting the left common factor, the left recursion is
eliminated from the original method.
 Is the resulting grammar suitable for top-down grammatical
analysis? S -> 0 S 1 | 0 1

 Extract the left common factor


 S -> 0 A
 A -> S 1 | 1

17
Left Factoring…
 Eliminate left recursion

 // initial status
 1) S -> 0 A
 2) A -> S 1 | 1

 // i = 1
 // nothing changed

 // i = 2, j = 1
 1) S -> 0 A
 2) A -> 0 A 1 | 1
18
Non-CFL Constructs
 Although grammars are powerful, but they are not all-powerful to
specify all language constructs.

 Lets see an example to understand this

 The language in this example abstracts the problem of checking that


identifiers are declared before they are used in a program.

 The language consists of strings of the form wcw, where


 the first w represents the declaration of an identifier w.
 c represents an intervening program fragment.
 the second w represents the use of the identifier.

19
Non-CFL Constructs..
 The abstract language is L1 = {wcw | w is in (a|b)*}

 L1 consists of all words composed of a repeated string of a's and b's


separated by c, such as aabcaab.

 The non-context- freedom of L1 directly implies the non-context-


freedom of programming languages like C and Java, which require
declaration of identifiers before their use and which allow identifiers
of arbitrary length.

 For this reason, a grammar for C or Java does not distinguish among
identifiers that are different character strings.
20
Top Down Parsing
 Top-down parsing can be viewed as the problem of constructing a
parse tree for the input string, starting from the root and creating
the nodes of the parse tree in preorder (DFT).

 If this is our grammar then the steps involved in construction of a


parse tree are

21
Top Down Parsing..
 Top Down Parsing for id + id * id

22
Top Down Parsing...

 Consider a node labeled E' .


 At the first E' node (in preorder) , the production E’ → +TE’ is chosen;
at the second E’ node, the production E’ → ɛ is chosen.
 A predictive parser can choose between E’-productions by looking at
the next input symbol.

23
Top Down Parsing...
 The class of grammars for which we can construct predictive
parsers looking k symbols ahead in the input is sometimes called
the LL(k) class.

 LL parser is a top-down parser for a subset of the context-free


grammars.
 It parses the input from Left to right, and constructs a Leftmost
derivation of the sentence.

 LR parser constructs a rightmost derivation. 

24
Recursive Decent Parsing
 Recursive Descent Parsing

 It is a top-down process in which the parser attempts to verify that


the syntax of the input stream is correct as it is read from left to right.

 A basic operation necessary for this involves reading characters from


the input stream and matching then with terminals from the
grammar that describes the syntax of the input.

 Recursive descent parsers will look ahead one character and advance
the input stream reading pointer when proper matches occur. 
25
Recursive Decent Parsing..
 The following procedure accomplishes matching and reading process.

 The variable called 'next' looks ahead and always provides the next
character that will be read from the input stream.
 This feature is essential if we wish our parsers to be able to predict what
is due to arrive as input.

26
Recursive Decent Parsing...
 What a recursive descent parser actually does is to perform a
depth-first search of the derivation tree for the string being parsed.
 This provides the 'descent' portion of the name.

 The 'recursive' portion comes from the parser's form, a collection


of recursive procedures.

 As our first example, consider the simple grammar


E → id + T
T → (E)
T → id

27
Recursive Decent Parsing...
 Derivation tree for the expression id+(id+id)

28
Recursive Decent Parsing…
 A recursive descent parser traverses the tree by first calling a
procedure to recognize an E.

 This procedure reads an 'x' and a '+' and then calls a procedure to
recognize a T.

 Note that 'errorhandler' is a procedure that notifies the user that a


syntax error has been made and then possibly terminates execution.

29
Recursive Decent Parsing...
 In order to recognize a T, the parser must figure out which of the
productions to execute.

 In this routine, the parser determines whether T had the form (E)
or x.
 If not then the error routine was called, otherwise the appropriate
terminals and non-terminals were recognized.

30
Recursive Decent Parsing...
 So, all one needs to write a recursive descent parser is a nice
grammar.

 But, what exactly is a 'nice' grammar?

 STAY TUNED TILL NEXT LESSON.

31
Recursive Decent Parsing...
 S -> 0 S 1 | 0 1
 Step1.Extract the left common factor
 S -> 0 A
 A -> S 1 | 1
 step2. Eliminate left recursion

 S -> 0 A
 A -> 0 A 1 | 1
 step3. Forecast analysis table

32

You might also like