Compiler Construction Sohail Aslam Lecture 12 Parse Trees
- Slides: 54
Compiler Construction Sohail Aslam Lecture 12
Parse Trees Leftmost derivation G E E op x – E E op E 2 * y 2
Parse Trees G E E op x – evaluation order x–(2*y) E E op E 2 * y 3
Parse Trees G E E E x op – E 2 Rightmost derivation op E * y evaluation order (x – 2 ) * y 4
Precedence § These two derivations point out a problem with the grammar § It has no notion of precedence, or implied order of evaluation 5
Precedence § These two derivations point out a problem with the grammar § It has no notion of precedence, or implied order of evaluation 6
Precedence To add precedence § Create a non-terminal for each level of precedence § Isolate corresponding part of grammar 7
Precedence To add precedence § Create a non-terminal for each level of precedence § Isolate corresponding part of grammar 8
Precedence To add precedence § Create a non-terminal for each level of precedence § Isolate corresponding part of grammar 9
Precedence To add precedence § Force parser to recognize high precedence subexpressions first. 10
Precedence For algebraic expressions § Multiplication and division, first. (level one) § Subtraction and addition, next (level two) 11
Precedence For algebraic expressions § Multiplication and division, first. (level one) § Subtraction and addition, next (level two) 12
Precedence For algebraic expressions § Multiplication and division, first. (level one) § Subtraction and addition, next (level two) 13
1 2 3 4 5 6 7 8 9 Goal expr term factor → → | | → | expr + term expr – term factor term / factor number Id 14
level two level one 1 2 3 4 5 6 7 8 9 Goal expr → → | | term → | | factor → | expr + term expr – term factor term / factor number Id 15
Precedence This grammar is larger § Takes more rewriting to reach some of the terminal symbols § But it encodes expected precedence 16
Precedence This grammar is larger § Takes more rewriting to reach some of the terminal symbols § But it encodes expected precedence 17
Precedence This grammar is larger § Takes more rewriting to reach some of the terminal symbols § But it encodes expected precedence 18
Precedence § Produces same parse tree under leftmost and rightmost derivations § Let’s see how it parses x– 2*y 19
Precedence § Produces same parse tree under leftmost and rightmost derivations § Let’s see how it parses x– 2*y 20
Precedence Rule 1 3 5 9 7 Sentential Form Goal expr – term factor expr – term <id, y> expr – factor <id, y> 21
Derivations & Precedence Rule 8 4 7 9 Sentential Form expr – <num, 2> <id, y> term – <num, 2> <id, y> factor – <num, 2> <id, y> <id, x> – <num, 2> <id, y> The rightmost derivation 22
Parse Trees G evaluation order x–(2*y) E E T – T T F T <id, x> <num, 2> * F <id, y> 23
Parse Trees G evaluation order x–(2*y) E E T – T T F T <id, x> <num, 2> * F <id, y> 24
Precedence § Both leftmost and rightmost derivations give the same expression § Because the grammar directly encodes the desired precedence. 25
Precedence § Both leftmost and rightmost derivations give the same expression § Because the grammar directly encodes the desired precedence. 26
Ambiguous Grammars § If a grammar has more than one leftmost derivation for a single sentential form, the grammar is ambiguous 27
Ambiguous Grammars § If a grammar has more than one rightmost derivation for a single sentential form, the grammar is ambiguous 28
Ambiguous Grammars § The leftmost and rightmost derivations for a sentential form may differ, even in an unambiguous grammar § Let’s consider the classic if-then-else example 29
Ambiguous Grammars if-then-else problem Stmt → if Expr then Stmt | if Expr then Stmt else Stmt | … other stmts …. 30
Ambiguous Grammars § The following sentential form has two derivations: if E 1 then if E 2 then S else S 2 31
Ambiguity Production 1, then Production 2: if E 1 then if E 2 then S 1 else S 2 if E 1 E 2 then else if S 2 then S 1 32
Ambiguity Production 2, then Production 1: if E 1 then if E 2 then S 1 else S 2 if E 1 then if E 2 then else S 1 S 2 33
Ambiguity if E 1 E 2 if then else if S 2 then S 1 E 1 then if E 2 then else S 1 S 2 34
Removing Ambiguity § Must rewrite grammar to avoid generating the problem § Match each else to innermost umatched if 35
Removing Ambiguity § Must rewrite grammar to avoid generating the problem § Match each else to innermost umatched if 36
Removing Ambiguity 1. Stmt → If E then Stmt 2. | If E then With. Else else Stmt 3. | Assignment 4. With. Else → If E then With. Else else With. Else 5. | Assignment 37
Removing Ambiguity Let derive the following using the rewritten grammar: if E 1 then if E 2 then A 1 else A 2 38
Stmt if Expr then Stmt E 1 if Expr E 2 then Withelse A 1 else Stmt A 2 This binds the else controlling A 2 to inner if 39
Context-Free Grammars § We have been using the term context-free without explaining why such rules are in fact “free of context”. 40
Context-Free Grammars § The simple reason is that nonterminals appear by themselves to the left of the arrow in context-free rules: A →a 41
Context-Free Grammars § The rule A → a says that A may be replaced by a anywhere, regardless of where A occurs. 42
Context-Free Grammars § On the other hand, we could define a context as pair of strings b, g, such that a rule would apply only if b occurs before and g occurs after the nonterminal A. 43
Context-Sensitive Grammars § We would write this as b. Ag →bag § Such a rule in which a ≠ e is called a contextsensitive grammar rule 44
Context-Sensitive Grammars § We would write this as b. Ag →bag § Such a rule in which a ≠ e is called a contextsensitive grammar rule 45
Parsing Techniques
Parsing Techniques Top-down parsers § Start at the root of the parse tree and grow towards leaves. § Pick a production and try to match the input 47
Parsing Techniques Top-down parsers § Start at the root of the parse tree and grow towards leaves. ck a production and try to match the input 48
Parsing Techniques Top-down parsers § Start at the root of the parse tree and grow towards leaves. § Pick a production and try to match the input 49
Parsing Techniques Top-down parsers § Bad “pick” may need to backtrack § Some grammars are backtrack-free. 50
Parsing Techniques Top-down parsers § Bad “pick” may need to backtrack § Some grammars are backtrack-free. 51
Parsing Techniques Bottom-up parsers § Start at the leaves and grow toward root § As input is consumed, encode possibilities in an internal state. 52
Parsing Techniques Bottom-up parsers § Start at the leaves and grow toward root § As input is consumed, encode possibilities in an internal state. 53
Parsing Techniques Bottom-up parsers § Start at the leaves and grow toward root § As input is consumed, encode possibilities in an internal state. 54
- Dr sohail aslam
- Dr sohail lectures
- Aslam anis
- Neraca lajur perusahaan aslam
- Aslam has to study four books
- Yet another compiler compiler
- Cross compiler in compiler design
- Compiler lecture
- Compiler construction principles and practice
- Compiler vs interpreter advantages and disadvantages
- Front end of a compiler
- Lexical analysis
- Kenneth c. louden
- ------phase is known as the back-end of the compiler.
- Thompson construction in compiler design
- Type checking and type conversion in compiler design
- Preprocessor in compiler construction
- Phases of compiler construction
- 01:640:244 lecture notes - lecture 15: plat, idah, farad
- Software project management lecture notes
- Int.parse(console.readline());
- Parse tree
- Parse tree generator
- Pohon sintaks
- Coldfusion xml functions
- Parse computer science
- Ironiko kahulugan
- Parse tree generator
- Parse tree for if else statement
- Rizzo parse
- Left recursion
- Tradutor
- Parse xml in power automate
- Parse tree
- Parse tree propositional logic
- Creative commons license example
- Examples of human becoming theory
- Annotated parse tree for 9-5+2
- Annotated parse tree generator
- Parse computer science
- Failed to execute 'fetch' on 'window': failed to parse url
- Melodrama dula
- Fully attributed parse tree
- Immo parse
- Sma compiler
- Php bytecode compiler
- Nswi098
- Compiler
- Intermediate code example
- Subdivision of runtime memory
- Bagian dari program yang akan diabaikanoleh compiler adalah
- Next use information in compiler design
- Semantic analysis compiler
- What are language processing activities
- Machine independent compiler features