Two pass Compiler, Parser - Compiler Construction - Lecture Slides, Slides for Compiler Construction. Birla Institute of Technology and Science
ekavia
ekavia

Two pass Compiler, Parser - Compiler Construction - Lecture Slides, Slides for Compiler Construction. Birla Institute of Technology and Science

PDF (739 KB)
20 pages
15Number of download
1000+Number of visits
100%on 1 votesNumber of votes
Description
Two pass Compiler, Intermediate representation, Back end, Front end, NP Complete, Modules, Context Free Grammars are basic concepts discussed of course.
20 points
Download points needed to download
this document
Download the document
Preview3 pages / 20
This is only a preview
3 shown on 20 pages
Download the document
This is only a preview
3 shown on 20 pages
Download the document
This is only a preview
3 shown on 20 pages
Download the document
This is only a preview
3 shown on 20 pages
Download the document
What is a compiler?

Compiler Construction

Lecture 2

Docsity.com

2

Two-pass Compiler

Front End

Back End

source code

IR machine code

errors

Docsity.com

3

Two-pass Compiler  Use an intermediate

representation (IR)  Front end maps legal source

code into IR

Docsity.com

4

Two-pass Compiler  Back end maps IR into target

machine code  Admits multiple front ends &

multiple passes

Docsity.com

5

Two-pass Compiler  Front end is O(n) or

O(n log n)  Back end is NP-Complete

(NPC)

Docsity.com

6

Front End Recognizes legal (& illegal)

programs Report errors in a useful

way  Produce IR & preliminary

storage map Docsity.com

7

The Front-End

Modules  Scanner  Parser

scanner parser source code tokens IR

errors

Docsity.com

8

Scanner

scanner parser source code tokens IR

errors

Docsity.com

9

Scanner Maps character stream into

words – basic unit of syntax  Produces pairs –

• a word and • its part of speech

Docsity.com

10

Scanner  Example x = x + y becomes <id,x> <assign,=> <id,x> <op,+> <id,y>

token type word

<id,x>

Docsity.com

11

Scanner we call the pair

“<token type, word>” a “token”  typical tokens: number,

identifier, +, -, new, while, if

Docsity.com

12

Parser

scanner parser source code tokens IR

errors

Docsity.com

13

Parser Recognizes context-free

syntax and reports errorsGuides context-sensitive

(“semantic”) analysis  Builds IR for source

program Docsity.com

14

Context-Free Grammars  Context-free syntax is specified

with a grammar G=(S,N,T,P)  S is the start symbol  N is a set of non-terminal symbols  T is set of terminal symbols or words  P is a set of productions or rewrite

rules

Docsity.com

15

Context-Free Grammars Grammar for expressions 1. goalexpr 2. exprexpr op term 3. | term 4. term → number 5. | id 6. op + 7. | -

Docsity.com

16

The Front End  For this CFG

S = goal T = { number, id, +, -} N = { goal, expr, term, op} P = { 1, 2, 3, 4, 5, 6, 7}

Docsity.com

17

Context-Free Grammars  Given a CFG, we can derive

sentences by repeated substitution  Consider the sentence

(expression) x + 2 – y

Docsity.com

18

Derivation Production Result

goal 1 expr 2 exprop term 5 expr op y 7 expr – y 2 expr op term – y 4 expr op 2 – y 6 expr + 2 – y 3 term + 2 – y 5 x + 2 – y

Docsity.com

19

The Front End  To recognize a valid

sentence in some CFG, we reverse this process and build up a parse  A parse can be represented

by a tree: parse tree or syntax tree

Docsity.com

20

Parse Production Result

goal 1 expr 2 exprop term 5 expr op y 7 expr – y 2 expr op term – y 4 expr op 2 – y 6 expr + 2 – y 3 term + 2 – y 5 x + 2 – y

Docsity.com

no comments were posted
This is only a preview
3 shown on 20 pages
Download the document