Docsity

Compiler Design, Slides of Compiler Design

Partial preview of the text

Download Compiler Design and more Slides Compiler Design in PDF only on Docsity!

Compiler Design

18CS

Text book: Compilers, Techniques, and Tools

(Second Edition), Aho, Lam, Sethi, and

Ullman, Addison-Wesley, Pearson education,

Ref:https://nptel.ac.in/downloads/106108113/

Overview

  • (^) Language Processors
  • (^) Structure of Compiler with illustration
  • (^) Compiler Construction tools
  • (^) Science of building a compiler
  • (^) Applications of compiler technology
  • (^) Basics of lexical analysis

Why study Compiler Design?

  • (^) Computational thinking in language design
  • (^) Understanding how modern language and compiler technology

can be used to make software reliably, securely, and efficiently

  • (^) Motivation for new programming languages such as GO (

https://golang.org) and Clojure ( https://clojure.org/)

  • (^) Understanding modern program translation techniques and

tools

  • (^) Awareness of language and compiler issues in dealing with

parallelism and concurrency

  • (^) Get experience on a project in which you can explore in some

depth an advanced topic in programming languages and

compilers of your own choosing.

Compiler Design 7th Sem BE(CSE) NMIT 5

Language Processor

  • (^) If the target program is an executable machine language program it

can be called by the user to process inputs producing outputs.

Source program

Compiler

Errors

Target Program

Input

Target Program

Output

Running the

target program

Source program

Interpreter

Errors

Output

Input

  • (^) Interpreter directly executes the operations (one by one) specified

in the source program on inputs supplied by the user.

  • (^) Faster
  • (^) Limited

error

diagnostics

  • (^) Slower
  • (^) Better error

diagnostics

A Hybrid Compiler

  • (^) Java language processors combine compilation

& interpretation

Translator

Virtual

Machine

Source program

(byte code) Intermediate program

Input

Output

Compiler Design 7th Sem BE(CSE) NMIT 8

Language Processing System

  • (^) A preprocessor –The statements

which are processed before

compilation are known as

preprocessor statements. Any

preprocessor statement begins with

#.modifies the source program.

Contains code with no

preprocessing statements. Collects

modules of source program from

files, expands shortcuts, & macros.

Includes corresponding function

definition.eg-file

include(#include),macro(#define PI

3.14)

  • (^) Compiler processes the modified

source program and produces

assembly language program.

  • (^) Assembler produces relocatable

machine code as its output.

Phases of a Compiler

Front End

Lexical Analysis (LA)

Note:

Fahrenheit and

centigrade are

lexemes

mapped into

tokens <id,1>

and <id,2> resp.

*is a lexeme

mapped to

token

or <*>

Lexical Analysis

  • (^) LA can be generated automatically from regular

expression specifications

  • (^) LEX and Flex are two such tools
  • (^) LA is a deterministic finite state automaton
  • (^) Why is LA separate from parsing?
  • (^) Simplification of design - software engineering reason
  • (^) I/O issues are limited to LA alone
  • (^) LA based on finite automata are more efficient to

implement than pushdown automata used for parsing

(due to stack)

Grammars are Used for Specifying Syntax

The grammar S → aSbS | bSaS | ε generates all strings of a ’s and b ’s

with the same number of a ’s as b ’s.

This grammar is ambiguous: abab has two parse trees.

S

a

b S a S ε

S b S

ε ε

( ab )

n has parse trees 

n

n

n

S

ε a S b S

a S b S

ε ε

Programming Languages are not

Inherently Ambiguous

This grammar G generates the same language

S → aAbS | bBaS | ε

A → aAbA | ε

B → bBaB | ε

G is unambiguous and has

only one parse tree for

every sentence in L ( G ).

S

ε a A b S

a A b S

ε ε