Docsity

Simple One Pass Compiler - Programming Language Translation - Lecture Slides, Slides of Programming Languages

Partial preview of the text

Download Simple One Pass Compiler - Programming Language Translation - Lecture Slides and more Slides Programming Languages in PDF only on Docsity!

Chapter 2: A Simple One Pass

Compiler

The Entire Compilation Process

 Grammars for Syntax Definition

 Syntax-Directed Translation

 Parsing - Top Down & Predictive

 Pulling Together the Pieces

 The Lexical Analysis Process

 Symbol Table Considerations

 A Brief Look at Code Generation

 Concluding Remarks/Looking Ahead

Grammars for Syntax Definition

Example CFG

list → list + digit

list → list - digit

list → digit

digit → 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

(the ā€œ|ā€ means OR)

(So we could have written

list → list + digit | list - digit | digit )

Grammars are Used to Derive Strings:

Using the CFG defined on the previous slide, we can derive the string: 9 - 5 + 2 as follows:

list → list + digit

→ list - digit + digit → digit - digit + digit → 9 - digit + digit

→ 9 - 5 + digit → 9 - 5 + 2

P1 : list → list + digit P2 : list → list - digit P3 : list → digit P4 : digit → 9

P4 : digit → 5 P4 : digit → 2

A More Complex Grammar

What is this grammar for?

What does ā€œāˆˆā€ represent?

What kind of production rule is this?

block → begin opt_stmts end opt_stmts → stmt_list | ∈ stmt_list → stmt_list ; stmt | stmt

Defining a Parse Tree

 More Formally, a Parse Tree for a CFG Has the

Following Properties:

 Root Is Labeled With the Start Symbol

 Leaf Node Is a Token or ∈

 Interior Node (Now Leaf) Is a Non-Terminal

 If A → x1x2…xn, Then A Is an Interior;

x1x2…xn Are Children of A and May Be Non-

Terminals or Tokens

Other Important Concepts

Associativity of Operators

Left vs. Right

right letter

letter

right

letter

right

c

b

a (^) -

right → letter = right | letter letter → a | b | c | …| z

list digit

digit

list

digit

list

9

5

-^2

Other Important Concepts

Operator Precedence

What does

mean?

Typically

is precedence order

This can be

incorporated

into a grammar

via rules:

expr → expr + term | expr – term | term term → term * factor | term / factor | factor factor → digit | ( expr ) digit → 0 | 1 | 2 | 3 | … | 9

Precedemce Achieved by: expr & term for each precedence level

Rules for each are left recursive or associate to the left

Syntax-Directed Definition: (2 parts)

 Each Production Has a Set of Semantic Rules

 Each Grammar Symbol Has a Set of Attributes

 For the Following Example, String Attribute ā€œ t ā€ is

Associated With Each Grammar Symbol, i.e.,

 What is a Derivation for 9 + 5 - 2?

expr → expr – term | expr + term | term term → 0 | 1 | 2 | 3 | … | 9

Syntax-Directed Definition: (2 parts)

 Each Production Rule of the CFG Has a Semantic

Rule

 Note: Semantic Rules for expr Use Synthesized

Attributes Which Obtain Their Values From Other

Rules.

Production Semantic Rule expr → expr + term expr.t := expr.t || term.t || ā€˜+’ expr → expr – term expr.t := expr.t || term.t || ’-’ expr → term expr.t := term.t term → 0 term.t := ā€˜ 0 ’ term → 1 term.t := ā€˜ 1 ’ …. …. term → 9 term.t := ā€˜ 9 ’

Examples

rest → + term rest ⇒ rest → + term { print (ā€˜+’)} rest (Print ā€˜+’ After term for postfix translation)

expr → expr + term { print (ā€˜+’)}

→ expr - term { print (ā€˜-’)} → term

term → 0 { print (ā€˜0’)}

term → 1 { print (ā€˜1’)}

…

term → 9 { print (ā€˜9’)}

term

term

expr term

expr

expr

9

5

-^2

{ print (ā€˜-’)}

{ print (ā€˜9’)}

{ print (ā€˜5’)}

{ print (ā€˜2’)}

{ print (ā€˜+’)}

Parsing – Top-Down & Predictive

 Top-Down Parsing ⇒ Parse tree / derivation of a token string occurs in a top down fashion.

 For Example, Consider:

type → simple | ↑ id | array [ simple ] of type simple → integer | char | num dotdot num

Suppose input is : array [ num dotdot num ] of integer The parse would begin with type → array [ simple ] of type

Top-Down Parse (type = start symbol)

Input : array [ num dotdot num ] of integer

array [ simple ] of type

type

num dotdot num simple

array [ simple ] of type

type

num dotdot num simple integer

Top-Down Process

Recursive Descent or Predictive Parsing

 Parser Operates by Attempting to Match Tokens in

the Input Stream

 Utilize both Grammar and Input Below to Motivate

Code for Algorithm

array [ num dotdot num ] of integer

type → simple

| ↑ id | array [ simple ] of type

simple → integer

| char | num dotdot num

procedure match ( t : token ) ; begin if lookahead = t then lookahead : = nexttoken else error end ;