Skip to content
VUBuddy

CS606 Short Notes Lecture 12

Compiler Construction PDF 40 KBUpdated 23 Oct 2026
Quiz CS606 Short Notes Lecture 12 PDF 40 KB
vubuddy.com
Type
Quiz
Course
CS606 Compiler Construction
Size
PDF 40 KB
Updated
23 Oct 2026
Free download 0 of 3 used today
Download free · 40 KB

You get 3 free files a day on the website. Add the Chrome extension for 30 a day.

Unlimited downloads in the Android app Get it free on Google Play

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 )

All CS606 files →
CS606 Compiler Construction All CS606 handouts, past papers, quizzes and MCQs in one place. Open course page

Get more out of VUBuddy, for free

Android app Free on Google Play
  • Unlimited downloads
  • The explanation behind every MCQ answer
  • Study offline on slow data
Get the Android app
Chrome extension Free on the Chrome Web Store
  • 30 downloads a day instead of 3
  • Sign in and buy Pro Semester here
Add to Chrome

One Pro Semester membership works in both the extension and the app.