compiler construction 1. objectives given a context-free grammar, g, and the grammar- independent...

10
Compiler Construction 1

Upload: bryce-payne

Post on 11-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Compiler Construction 1. Objectives Given a context-free grammar, G, and the grammar- independent functions for a recursive-descent parser, complete the

Compiler Construction

1

Page 2: Compiler Construction 1. Objectives Given a context-free grammar, G, and the grammar- independent functions for a recursive-descent parser, complete the

Objectives• Given a context-free grammar, G, and the grammar-

independent functions for a recursive-descent parser, complete the recursive-descent parser by adding the grammar-dependent functions.

• Given naive intermediate code for a C loop, hand-optimize the code, reducing by at least 20% the number of intermediate code instructions needed while maintaining semantic correctness of the program.

• Define "register assignment" in the context of a compiler. Explain why register assignment is an important compiler optimization.

2

Page 3: Compiler Construction 1. Objectives Given a context-free grammar, G, and the grammar- independent functions for a recursive-descent parser, complete the

Compiler Tasks

3

Source

Lexor

Intermediate

Parser Optimizer

Tokens Intermediate

Assembler orBinary orSource

CodeGeneration

Page 4: Compiler Construction 1. Objectives Given a context-free grammar, G, and the grammar- independent functions for a recursive-descent parser, complete the

CFGs: ExampleSimplified description of English grammar:

G =S NP VPNP N | Adj NPN car | dogAdj big | greenVP V | V NPV is | eats

Example derivation:S G NP VP G Adj NP VP G Adj N VP G Adj N V

G Adj N eats G big N eats G big dog eats4

Production form:LHS = variable.RHS = string of variables and/or terminals.

Page 5: Compiler Construction 1. Objectives Given a context-free grammar, G, and the grammar- independent functions for a recursive-descent parser, complete the

CFGs: Formal Definition

G = (V, , P, S)

V = variables a finite set = alphabet or terminals a finite setP = productions a finite setS = start variable SV

Productions’ form, where AV, (V)*:• A

5

Page 6: Compiler Construction 1. Objectives Given a context-free grammar, G, and the grammar- independent functions for a recursive-descent parser, complete the

CFGs: Example 1{an bn | n0}

S | a S b

Formally: G = ({S}, {a,b}, {S , S a S b}, S)

6

Page 7: Compiler Construction 1. Objectives Given a context-free grammar, G, and the grammar- independent functions for a recursive-descent parser, complete the

CFGs & CFLs: Example 2

all strings of balanced parentheses

A core idea of most programming languages.

7

P | ( P ) | P P

Page 8: Compiler Construction 1. Objectives Given a context-free grammar, G, and the grammar- independent functions for a recursive-descent parser, complete the

CFGs: Example 2Language of Assignment Statements

P S PP SS id = E;E expressions (expressions left as “exercise”)

8

Page 9: Compiler Construction 1. Objectives Given a context-free grammar, G, and the grammar- independent functions for a recursive-descent parser, complete the

Example Languages

• Set of strings over {a,b} with an even number of a’s

• Arithmetic expressions using +, *, /, -, (,) Assume that all operands are called “id”

9

Page 10: Compiler Construction 1. Objectives Given a context-free grammar, G, and the grammar- independent functions for a recursive-descent parser, complete the

Recursive Descent Parser• Built from a context-free grammar• Simple rules build recursive program• Grammar independent part (see pdf file)• Grammar dependent

– Include a function for each non-terminal (lhs)– Generate code for each rule, left to right

• “Call” non-terminals• “Match” terminals• Use if statement to choose between multiple rules with

the same lhs (non-terminal) symbol

10