Top Down Parser-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: Parser, Algorithm, key, Recursion, Expr, Term, Predictive, Transformations, Sentential

Typology: Study notes

2011/2012

Uploaded on 08/04/2012

qader.khan
qader.khan ๐Ÿ‡ต๐Ÿ‡ฐ

4.3

(6)

12 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
L
Le
ec
ct
tu
ur
re
e
1
13
3
Top-Down Parser
A top-down parser starts with the root of the parse tree. The root node is labeled with the
goal (start) symbol of the grammar. The top-down parsing algorithm proceeds as follows:
1. Construct the root node of the parse tree
2. Repeat until the fringe of the parse tree matches input string
a. At a node labeled A, select a production with A on its lhs
b. for each symbol on its rhs, construct the appropriate child
c. When a terminal symbol is added to the fringe and it does not match the
fringe, backtrack
The key is picking right production in step a. That choice should be guided by the input
string. Letโ€™s try parsing using this algorithm using the expression grammar.
x โ€“ 2 * y
P Sentential Form input
- Goal โ†‘x โ€“ 2 * y
1 expr โ†‘x โ€“ 2 * y
2 expr + term โ†‘x โ€“ 2 * y
4 term + term โ†‘x โ€“ 2 * y
7
factor
+ term โ†‘x โ€“ 2 * y
9 <id,x> + term โ†‘x โ€“ 2 * y
9 <id,x> + term x โ†‘โ€“ 2 * y
This worked well except that โ€œโ€“โ€ does not match โ€œ+โ€. The parser made the wrong choice
of production to use at step 2. The parser must backtrack and use a different production.
This time the โ€œโ€“โ€ and โ€œโ€“โ€ matched. We can advance past โ€œโ€“โ€ to look at โ€œ2โ€. Now, we
need to expand โ€œtermโ€
P Sentential Form input
- Goal โ†‘x โ€“ 2 * y
1 expr โ†‘x โ€“ 2 * y
3 expr โ€“ term โ†‘x โ€“ 2 * y
4 term โ€“ term โ†‘x โ€“ 2 * y
7
factor
โ€“ term โ†‘x โ€“ 2 * y
9 <id,x> โ€“ term โ†‘x โ€“ 2 * y
9 <id,x> โ€“ term x โ†‘โ€“ 2 * y
pf3
pf4

Partial preview of the text

Download Top Down Parser-Compiler Construction-Lecture Notes and more Study notes Compiler Construction in PDF only on Docsity!

LeLeccttuurree 1 133

Top-Down Parser

A top-down parser starts with the root of the parse tree. The root node is labeled with the goal (start) symbol of the grammar. The top-down parsing algorithm proceeds as follows:

  1. Construct the root node of the parse tree
  2. Repeat until the fringe of the parse tree matches input string a. At a node labeled A , select a production with A on its lhs b. for each symbol on its rhs, construct the appropriate child c. When a terminal symbol is added to the fringe and it does not match the fringe, backtrack

The key is picking right production in step a. That choice should be guided by the input string. Letโ€™s try parsing using this algorithm using the expression grammar.

x โ€“ 2 (^) * y

P Sentential Form input

  • Goal โ†‘ x โ€“ 2 * y 1 expr โ†‘ x โ€“ 2 * y 2 expr + term (^) โ†‘ x โ€“ 2 * y 4 term + term (^) โ†‘ x โ€“ 2 * y 7 factor + term (^) โ†‘ x โ€“ 2 * y 9 <id,x> + term (^) โ†‘ x โ€“ 2 * y 9 <id,x> + term (^) x โ†‘ โ€“ 2 * y

This worked well except that โ€œโ€“โ€ does not match โ€œ+โ€. The parser made the wrong choice of production to use at step 2. The parser must backtrack and use a different production.

This time the โ€œโ€“โ€ and โ€œโ€“โ€ matched. We can advance past โ€œ โ€“ โ€ to look at โ€œ 2 โ€. Now, we need to expand โ€œ term โ€

P Sentential Form input

  • Goal (^) โ†‘ x โ€“ 2 * y 1 expr (^) โ†‘ x โ€“ 2 * y (^3) expr โ€“ term โ†‘ x โ€“ 2 * y (^4) term โ€“ term โ†‘ x โ€“ 2 * y (^7) factor โ€“ term โ†‘ x โ€“ 2 * y (^9) <id,x> โ€“ term โ†‘ x โ€“ 2 * y 9 <id,x> โ€“ term x โ†‘ โ€“ 2 * y

The 2โ€™s match but the expansion terminated too soon because there is still unconsumed input and there are no non-terminals to expand in the sentential form โ‡’ Need to backtrack.

P Sentential Form input

  • <id,x> โ€“ term (^) x โ€“ โ†‘ 2 * y 5 <id,x> โ€“ term * factor (^) x โ€“ โ†‘ 2 * y 7 <id,x> โ€“ factor * factor (^) x โ€“ โ†‘ 2 * y 8 <id,x> โ€“ <num,2> * factor (^) x โ€“ โ†‘ 2 * y
  • <id,x> โ€“ <num,2> * factor (^) x โ€“ 2 โ†‘ * y
  • <id,x> โ€“ <num,2> * factor x โ€“ 2 * โ†‘ y 9 <id,x> โ€“ <num,2> * <id,y> x โ€“ 2 * โ†‘ y
  • <id,x> โ€“ <num,2> * <id,y> x โ€“ 2 * y โ†‘

This time the parser met with success. All of the input matched.

Left Recursion

Consider another possible parse:

Parser is using productions but no input is being consumed.

  • <id,x> โ€“ <num,2> x โ€“ 2 โ†‘ * y

9 <id,x> โ€“ <num,2> x โ€“ โ†‘ (^2) * y

7 <id,x> โ€“ factor x โ€“ โ†‘ (^2) * y

  • <id,x> โ€“ term x โ€“ โ†‘ (^2) * y

P Sentential Form input

2 expr + term + term + term +....^ โ†‘ x^ โ€“^2 * y

2 expr + term + term + term^ โ†‘ x^ โ€“^2 * y

2 expr + term + term^ โ†‘ x^ โ€“^2 * y

expr + term

expr

Goal

Sentential Form

2^ โ†‘ x^ โ€“^2 * y

1^ โ†‘ x^ โ€“^2 * y

  • โ†‘ x โ€“ (^2) * y

P input

Predictive Parsing

If a top down parser picks the wrong production, it may need to backtrack. Alternative is to look-ahead in input and use context to pick the production to use correctly. How much look-ahead is needed? In general, an arbitrarily large amount of look-ahead symbols are required.. Fortunately, large classes of CFGs can be parsed with limited lookahead. Most programming languages constructs fall in those subclasses

The basic idea in predictive parsing is: given A? ฮฑ | ฮฒ , the parser should be able to choose between ฮฑ and ฮฒ. To accomplish this, the parser needs FIRST and FOLLOW sets.

Definition: FIRST sets: for some rhs ฮฑ โˆˆ G , define FIRST( ฮฑ ) as the set of tokens that appear as the first symbol in some string that derives from ฮฑ. That is, x โˆˆ FIRST( ฮฑ ) iff ฮฑ โ‡’โˆ—^ x ฮณ, for some ฮณ.