Parse Trees-Compiler Construction-Lecture Notes, Study notes of Compiler Construction

Compile is software which translates high level programming language to computer basic language. Many compilers exist e.g. Borland, Java etc. Compiler construction have few steps which are taught in this course. This lecture includes: Parse, Derivation, Nodes, tree, Evaluation, Order, Precedence, Arithmatical, Expr, Ambiguous

Typology: Study notes

2011/2012

Uploaded on 08/04/2012

qader.khan
qader.khan 🇵🇰

4.3

(6)

12 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
L
Le
ec
ct
tu
ur
re
e
1
12
2
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
2
*
y
Leftmost
derivation
evaluation order
x ( 2
y )
G
E
op E
x
E
E
op
E
2
*
y
Rightmost
derivation
ev
aluation order
(x 2 ) * y
pf3
pf4
pf5

Partial preview of the text

Download Parse Trees-Compiler Construction-Lecture Notes and more Study notes Compiler Construction in PDF only on Docsity!

LeLeccttuurree 1 122

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

x – E op E

2 * y

Leftmost

derivation

evaluation order

x – ( 2 * y )

G

E

op E

x –

E

E op^ E

* y

Rightmost

derivation

evaluation order

(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 1 Expr 3 exprterm (^5) exprtermfactor (^9) exprterm ∗ <id,y> (^7) exprfactor ∗ <id,y> (^8) expr – <num,2> ∗ <id,y> (^4) term – <num,2> ∗ <id,y> (^7) factor – <num,2> ∗ <id,y> (^9) <id,x> – <num,2> ∗ <id,y>

This produces same parse tree under leftmost and rightmost derivations

9 | Id

8 factor? number

7 | factor

6 | term / factor

5 term?^ term^ ∗^ factor

4 | term

3 | exprterm

2 expr? expr + term

1 Goal? expr

level two

level one

The convention in most programming languages is to match the else with the most recent if.

Production 1, then Production 2: if E 1 then if E 2 then S 1 else S 2

E 1

if

then

if

then

else

S 1

S 2

E 2

E 1

if

then

if

then else

S 1 S^2

E 2

Production 2, then Production 1: if E 1 then if E 2 then S 1 else S 2

We can rewrite grammar to avoid generating the problem and match each else to innermost unmatched if:

1. Stmt? If E then Stmt 2. | If E then WithElse else Stmt 3. | _Assignment

  1. WithElse_? If E then WithElse else WithElse 5. | Assignment

Let derive the following using the rewritten grammar:

if E 1 then if E 2 then A 1 else A 2

Context-Free Grammars

We have been using the term context- free without explaining why such rules are in fact “free of context”. The simple reason is that non-terminals appear by themselves to the left of the arrow in context-free rules:

A? α

The rule A? α says that A may be replaced by α anywhere , regardless of where A occurs. On the other hand, we could define a context as pair of strings β , γ , such that a rule would apply only if β occurs before and γ occurs after the non-terminal A. We would write this as β A γ? β α γ

This binds the else controlling A 2 to inner if

E 1

Stmt

then

else

E 2 A 1 A 2

if Expr Stmt

if Expr then Withelse Stmt