- Type
- Quiz
- Course
- CS606 Compiler Construction
- Size
- PDF 40 KB
- Updated
- 23 Oct 2026
You get 3 free files a day on the website. Add the Chrome extensionSign in with the extension for 30 a day.
Preview text
Text from pages 1, 2, 3 of 6. Download the file for all 6 pages.
Lecture 12
Parse Trees
The derivations can be represented in a tree-like fashion. The interior nodes contain the non-terminals used during the derivation
Precedence
These two derivations point out a problem with the grammar. It has no notion of precedence, or implied order of evaluation. The normal arithmetic rules say that multiplication has higher precedence than subtraction. To add precedence, create a non- G
E
E op E
E op E x
y Leftmost derivation
x – ( 2 * y )
G
E
op E
x E
E op E y Rightmost derivation
(x – 2 ) * y terminal for each level of precedence. Isolate corresponding part of grammar to force parser to recognize high precedence sub-expressions first. Here is the revised grammar:
This grammar is larger and requires more rewriting to reach some of the terminal symbols. But it encodes expected precedence. Let’s see how it parses
x – 2 * y
Rule Sentential Form Goal Expr expr – term expr – term ∗ factor expr – term ∗ <id,y> expr – factor ∗ <id,y> expr – <num,2> ∗ <id,y> term – <num,2> ∗ <id,y> factor – <num,2> ∗ <id,y> <id,x> – <num,2> ∗ <id,y>
This produces same parse tree under leftmost and rightmost derivations Id number factor factor term / factor term ∗ factor term term expr – term expr + term expr expr Goal
level two
level one Both leftmost and rightmost derivations give the same expression because the grammar directly encodes the desired precedence.
Ambiguous Grammars
If a grammar has more than one leftmost derivation for a single sentential form, the grammar is ambiguous. 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
Stmt ?
if Expr then Stmt | if Expr then Stmt else Stmt | … other stmts ….
The following sentential form has two derivations:
if E1 then if E2 then S else S2 G
E
F T
T F
<id,x> <id,y> T E
T
<num,2> x – ( 2 * y )
Related CS606 files
All CS606 files →Get more out of VUBuddy, for free
- Unlimited downloads
- The explanation behind every MCQ answer
- Study offline on slow data
- 30 downloads a day instead of 3
- Sign in and buy Pro Semester here
One Pro Semester membership works in both the extension and the app.