Docsity

basic Concepts of Compiler construction, Slides of Compiler Construction

Partial preview of the text

Download basic Concepts of Compiler construction and more Slides Compiler Construction in PDF only on Docsity!

CompilerCompiler

Construction Construction

Compiler Compiler

Construction Construction

Lecture 2

Two-pass CompilerTwo-pass CompilerTwo-pass Compiler Two-pass Compiler Front End Back End source code IR (^) machine code errors

Two-pass CompilerTwo-pass CompilerTwo-pass Compiler Two-pass Compiler ๏‚ง Back end maps IR into target machine code ๏‚ง Admits multiple front ends & multiple passes

Front EndFront EndFront End Front End

Recognizes legal (& illegal)

programs

Report errors in a useful

way

Produce IR & preliminary

storage map

ScannerScannerScanner Scanner

Compiler Mode

scanner parser source code tokens IR errors

ScannerScannerScanner Scanner

Maps character stream into

words โ€“ basic unit of syntax

Produces pairs โ€“

  • (^) a word and
  • (^) its part of speech

ScannerScannerScanner Scanner

we call the pair

โ€œ<token type, word>โ€ a

โ€œtokenโ€

typical tokens: number ,

identifier , +, -, new , while , if

ParserParserParser Parser scanner parser source code tokens IR errors

Context-Free GrammarsContext-Free GrammarsContext-Free Grammars 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

Context-Free GrammarsContext-Free GrammarsContext-Free Grammars Context-Free Grammars Grammar for expressions

  1. goal โ†’ expr
  2. expr โ†’ expr op term
  3. | term
  4. term โ†’ number
  5. | id
  6. op โ†’ +
  7. | -

Context-Free GrammarsContext-Free GrammarsContext-Free Grammars Context-Free Grammars

Given a CFG, we can derive

sentences by repeated

substitution

Consider the sentence

(expression)

x + 2 โ€“ y

DerivationDerivationDerivation Derivation Production Result goal 1 expr 2 expr^ op 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

ParseParseParse Parse Production Result goal 1 expr 2 expr^ op 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