Docsity

Introduction to Compilers: LL Parsing and AST Construction, Exams of Compiler Design

Toggle sidebar
bg1
1
CS 412/413 Spring 2007 Introduction to Compilers 1
CS412/CS413
Introduction to Compilers
Tim Teitelbaum
Lecture 7: LL parsing and AST construction
February 5, 2007
CS 412/413 Spring 2007 Introduction to Compilers 2
LL(1) Parsing
•Last time:
– how to build a parsing table for an LL(1)
grammar (use FIRST/FOLLOW sets)
– how to construct a recursive-descent parser
from the parsing table
• Grammars may not be LL(1)
– Use left factoring when grammar has multiple
productions starting with the same symbol.
– Other problematic cases?
CS 412/413 Spring 2007 Introduction to Compilers 3
if-then-else
• How to write a grammar for if stmts?
S →if (E) S
S →if (E) S else S
S →other
Is this grammar ok?
CS 412/413 Spring 2007 Introduction to Compilers 4
No—Ambiguous!
•How to parse?
if (E1) if (E2)S1 else S2
Which “if” is the “else” attached to?
S→if (E) S
→if (E) if (E) S else S
S→if (E) Selse S
→if (E) if (E) S else S
S →if (E) S
S →if (E) S else S
S →other
if
E1if
E2S1S2
if
E1if S2
E2S1
CS 412/413 Spring 2007 Introduction to Compilers 5
Grammar for Closest-if Rule
• Want to rule out if (E) if (E) S else S
• Impose that unmatched “if” statements
occur only on the “else” clauses
statement →matched | unmatched
matched →if (E) _______ else __________
| other
unmatched →if (E) ________
| if (E) _______ else __________
CS 412/413 Spring 2007 Introduction to Compilers 6
Grammar for Closest-if Rule
• Want to rule out if (E) if (E) S else S
• Impose that unmatched “if” statements
occur only on the “else” clauses
statement →matched | unmatched
matched →if (E) matched else matched
| other
unmatched →if (E) ________
| if (E) _______ else __________
pf3
pf4

Partial preview of the text

Download Introduction to Compilers: LL Parsing and AST Construction and more Exams Compiler Design in PDF only on Docsity!

CS 412/413 Spring 2007 Introduction to Compilers 1

CS412/CS

Introduction to Compilers

Tim Teitelbaum

Lecture 7: LL parsing and AST construction

February 5, 2007

CS 412/413 Spring 2007 Introduction to Compilers 2

LL(1) Parsing

• Last time:

– how to build a parsing table for an LL(1)

grammar (use FIRST/FOLLOW sets)

– how to construct a recursive-descent parser

from the parsing table

• Grammars may not be LL(1)

– Use left factoring when grammar has multiple

productions starting with the same symbol.

– Other problematic cases?

CS 412/413 Spring 2007 Introduction to Compilers 3

if-then-else

• How to write a grammar for if stmts?

S → if (E) S

S → if (E) S else S

S → other

Is this grammar ok?

CS 412/413 Spring 2007 Introduction to Compilers 4

No—Ambiguous!

• How to parse?

if (E 1 ) if (E 2 ) S 1 else S 2

Which “if” is the “else” attached to?

S → if (E) S → if (E) if (E) S else S

S → if (E) S else S → if (E) if (E) S else S

S → if (E) S S → if (E) S else S S → other

if E 1 if E 2 S 1 S (^2) if E 1 if S (^2) E 2 S (^1)

CS 412/413 Spring 2007 Introduction to Compilers 5

Grammar for Closest-if Rule

• Want to rule out if (E) if (E) S else S

• Impose that unmatched “if” statements

occur only on the “else” clauses

statement → matched | unmatched matched → if (E) _______ else __________ | other unmatched → if (E) ________ | if (E) _______ else __________

CS 412/413 Spring 2007 Introduction to Compilers 6

Grammar for Closest-if Rule

• Want to rule out if (E) if (E) S else S

• Impose that unmatched “if” statements

occur only on the “else” clauses

statement → matched | unmatched matched → if (E) matched else matched | other unmatched → if (E) ________ | if (E) _______ else __________

CS 412/413 Spring 2007 Introduction to Compilers 7

Grammar for Closest-if Rule

• Want to rule out if (E) if (E) S else S

• Impose that unmatched “if” statements

occur only on the “else” clauses

statement → matched | unmatched matched → if (E) matched else matched | other unmatched → if (E) ________ | if (E) matched else unmatched

CS 412/413 Spring 2007 Introduction to Compilers 8

Grammar for Closest-if Rule

• Want to rule out if (E) if (E) S else S

• Impose that unmatched “if” statements

occur only on the “else” clauses

statement → matched | unmatched matched → if (E) matched else matched | other unmatched → if (E) statement | if (E) matched else unmatched

CS 412/413 Spring 2007 Introduction to Compilers 9

LL(1) if-then-else?

statement → if (E) matched optional-tail | other

matched → if (E) matched else matched | other

optional-tail → else tail | ε tail → if (E) tail | other

CS 412/413 Spring 2007 Introduction to Compilers 10

LL(1) if-then-else?

statement → if (E) matched optional-tail | other matched → if (E) matched else matched | other optional-tail → else tail | ε tail → if (E) tail | other

CS 412/413 Spring 2007 Introduction to Compilers 11

Left-Recursive Grammars

• Left-recursive grammars are not LL(1)!

S → S α

S → β

• FIRST(β) ⊆ FIRST(Sα)

• Both productions will appear in the predictive

table, at row S in all the columns corresponding

to symbols in FIRST(β)

CS 412/413 Spring 2007 Introduction to Compilers 12

Eliminate Left Recursion

• Method for left-recursion elimination:

Replace

A → A α 1 | … | A αm

A → β 1 | … | βn

with

A → β 1 A’ | … | βn A’

A’ → α 1 A’ | … | αm A’ | ε

  • (See the complete algorithm in the Dragon Book)

CS 412/413 Spring 2007 Introduction to Compilers 19

AST Creation: parse_S

Expr parse_S() { switch (token) { case num: case ‘(’: Expr left = parse_E(); Expr right = parse_S’(); if (right == null) return left; else return new Add(left, right); default : throw new ParseError(); } }

S → ES'

S' → ε | +S E → num | (S)

CS 412/413 Spring 2007 Introduction to Compilers 20

EBNF: Extended BNF Notation

• Extended Backus-Naur Form = a form of

specifying grammars which allows some regular

expression syntax on RHS

*, +, ( ),? operators (also [X] means X?)

S → ES’

S’ → ε |+S

• EBNF version: agnostic on + associativity

S → E(+E)*

CS 412/413 Spring 2007 Introduction to Compilers 21

Top-down Parsing EBNF

• Recursive-descent code can directly implement

the EBNF grammar:

S → E(+E)*

void parse_S () { // parses sequence of E+E+E ... parse_E (); while (true) { switch (token) { case ‘+’: token = input.read(); parse_E(); break ; case ‘)’: case EOF: return ; default : throw new ParseError(); } }

CS 412/413 Spring 2007 Introduction to Compilers 22

Reassociating the AST

Expr parse_S() { Expr result = parse_E(); while ( true ) { switch (token) { case ‘+’: token = input.read(); result = new Add(result, parse_E()); break ; case ‘)’: case EOF: return result; default : throw new ParseError(); } }

CS 412/413 Spring 2007 Introduction to Compilers 23

Top-Down Parsing Summary

Language grammar

LL(1) grammar

predictive parsing table

recursive-descent parser

parser with AST generation

Left-recursion elimination Left-factoring

CS 412/413 Spring 2007 Introduction to Compilers 24

Exercises

• Which of the following are LL(1)?

A → aACd | b C → c | ε

A → aACd | b C → A | ε

A → aAC | b C → c | ε