3b. LMD & RMD.pdf

1,527 views 24 slides Nov 16, 2022
Slide 1
Slide 1 of 24
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24

About This Presentation

LMD & RMD


Slide Content

Derivation and Ambiguity
1
Course Name: Compiler Design
Course Code: CSE331
Level:3, Term:3
Department of Computer Science and Engineering
Daffodil International University

Derivation
Aderivationisbasicallyasequenceofproductionrules,inordertogetthe
inputstring.Duringparsing,wetaketwodecisionsforsomesententialform
ofinput:
•Decidingthenon-terminalwhichistobereplaced.
•Decidingtheproductionrule,bywhich,thenon-terminalwillbereplaced.
•Todecidewhichnon-terminaltobereplacedwithproductionrule,wecan
havetwooptions.
2

Derivation Types
•Left-mostDerivation
Ifthesententialformofaninputisscannedandreplacedfromleftto
right,itiscalledleft-mostderivation.Thesententialformderivedbytheleft-
mostderivationiscalledtheleft-sententialform.
•Right-mostDerivation
Ifwescanandreplacetheinputwithproductionrules,fromrightto
left,itisknownasright-mostderivation.Thesententialformderivedfromthe
right-mostderivationiscalledtheright-sententialform.
3

4
•Production rules:
E → E + E
E → E * E
E → id
The left-most derivation is:
E → E * E
E → E + E * E
E → id + E * E
E → id + id * E
E → id + id * id
The right-most derivation is:
E → E + E
E → E + E * E
E → E + E * id
E → E + id * id
E → id + id * id
Input string: id + id * id
Example

Parse Tree
•A parse tree is a graphical depiction of a derivation.
•It is convenient to see how strings are derived from the start symbol.
•The start symbol of the derivation becomes the root of the parse tree.
5

Constructing the Parse Tree
6
•Step 1:
E → E * E
Step 2:
E → E + E * E

•Step 3:
E → id + E * E
Constructing the Parse Tree …
Step 4:
E → id + id * E
7

•In a parse tree:
-All leaf nodes are terminals.
-All interior nodes are non-terminals.
-In-order traversal gives original input string
Step 5:
E → id + id * id
Constructing the Parse Tree …
8

Ambiguity
•AgrammarGissaidtobeambiguousifithasmorethanoneparsetree(leftorright
derivation)foratleastonestring.
Example: E→E+E
E→E–E
E→id
Forthestringid+id–id,theabovegrammargeneratestwoparsetrees:
9

•Parsing that we have seen so far.
Top-Down Parsing
10

•Parsingistheprocessofdeterminingifastringoftokenscanbegeneratedbya
grammar.
•Foranycontext-freegrammarthereisaparserthattakesatmostΟ(n
3
)timeto
parseastringofntokens.
•Top-downparsersbuildparsetreesfromthetop(root)tothebottom(leaves).
•Twotop-downparsingaretobediscussed:
oRecursiveDescentParsing
oPredictiveParsingAnefficientnon-backtrackingparsingcalledforLL(1)
grammars.
Top-Down Parsing
11

Considerthegrammar
S→cAd
A→ab|a
Inputstringw=cad
Toconstructaparsetreeforthisstringusingtop-downapproach,
initiallycreateatreeconsistingofasinglenodelabeledS.
Example of Top-Down Parsing
12

Example of Top-Down Parsing
S → cAd
A → ab | a
Input string w = cad
13

Procedure of Top-down Parsing
•Aninputpointerpointstoc,thefirstsymbolofw.
•ThenusethefirstproductionforStoexpandthetreeandobtainthetree.
•Theleftmostleaf,labeledc,matchesthefirstsymbolofw.
•Nextinputpointertoa,thesecondsymbolofw.
•Considerthenextleaf,labeledA.
•ExpandAusingthefirstalternativeforAtoobtainthetree.
14

•Nowhaveamatchforthesecondinputsymbol.Thenadvancetothenext
inputpointerd,thethirdinputsymbolandcomparedagainstthenextleaf,
labeledb.Sincebdoesnotmatchd,reportfailureandgobacktoAtosee
whetherthereisanotheralternative.(Backtrackingtakesplace).
•IfthereisanotheralternativeforA,substituteandcomparetheinputsymbol
withleaf.
•RepeatthestepforallthealternativesofAtofindamatchusingbacktracking.
Ifmatchfound,thenthestringisacceptedbythegrammar.Elsereportfailure.
•Aleft-recursivegrammarcancausearecursive-descentparser,evenonewith
backtracking,togointoaninfiniteloop.
•Asdiscussedabove,aneasywaytoimplementarecursivedescentparsingwith
backtrackingistocreateaprocedureforeachnon-terminals.
Procedure of Top-down Parsing
15

Transition Diagram for Predictive Parsers
16

Example: Build the parse tree for the arithmetic expression 4 + 2 * 3 using the expression grammar:
E → E + T | E -T | T
T → T * F | F
F → a | ( E )
where a represents an operand of some type, be it a number or variable. The trees are grouped by height.
17

E → E + T | E -T | T
T → T * F | F
F → a | ( E )
18

Grammer:
E → E+E | a
E ⇒E+Ě
⇒Ě+E+E
⇒a+Ě+E
⇒a+a+Ě
⇒a+a+a
19
Derivation:

Example: Build the parse tree for the arithmetic expression “a+a+a” using the
expression grammar:
E → E+E | a
20

Consider again, the grammar specifying only addition in expression:
E → E+E | a
E ⇒Ě+E
⇒Ě+E+E
⇒a+Ě+E
⇒a+a+Ě
⇒a+a+a
E ⇒E+Ě
⇒E+E+Ě
⇒E+Ě+a
⇒Ě+a+a
⇒a+a+a
AMBIGUOUS
21
Left Most Derivation(LMD):
Right Most Derivation(LMD):

The grammar is: S ⇾S S| (S) | ε
Consider the string "()()"
S ⇒Š S
⇒( S ̌) S
⇒( ) Š
⇒( ) ( S ̌)
⇒( ) ( )
S ⇒S Š
⇒S ̌( S )
⇒( S ) ( Š )
⇒( Š ) ( )
⇒( ) ( )
NOT AMBIGUOUS
22
UNAMBIGUOUS

The grammar is: S ⇾S S| (S) | ε
Consider the string "(())()"
S ⇒ŠS
⇒(Š)S
⇒((Š))S
⇒(())Š
⇒(())(Š)
⇒(())()
S ⇒ŠS
⇒SŠS
⇒S(Š)S
⇒S((Š))S
⇒S(())Š
⇒S(())(Š)
⇒Š(())()
⇒(())()
AMBIGUOUS
23

24
THANK YOU