Docsity

CS 403: Compilation vs. Interpretation & Syntax Specification with BNF, Study notes of Programming Languages

Toggle sidebar
bg1
1
Spring 2002 CS 403 Class Notes Page 1
Lecture 2 - Tuesday, 1/15/02
• Today
– Compilation vs. interpretation
• Chapter 1.4
– Specifying syntax
• Chapter 2.1
• Reading Assignments For Next Class
– Continue reading Chapter 2.1, 2.2
– Begin Chapter 3
• Assignments
– Assignment #1 will be due Tuesday, 1/22/02
Spring 2002 CS 403 Class Notes Page 2
Compilation vs. Interpretation
• Pure compilation: Translate program
into machine language (object code)
– Object code directly executable on target
machine
• Pure interpretation: No translation of
source code; interpreter runs on
platform machine and executes source
language statements
Spring 2002 CS 403 Class Notes Page 3
Pure Compilation
Example: Visual C++ is compiled.
Spring 2002 CS 403 Class Notes Page 4
Pure Interpretation
Example: Visual Basic can be run as interpreted
or compiled.
Spring 2002 CS 403 Class Notes Page 5
Hybrid Approach
Spring 2002 CS 403 Class Notes Page 6
Separate Compilation
• Definition: Different modules of the program
are compiled separately (i.e., at different
times)
– Sometimes difficult to see this in an integrated
programming environment (like Visual Studio)
• A separate object file is produced for each
module
• Linker must be run separately at the end to
produce the final executable
pf3
pf4

Partial preview of the text

Download CS 403: Compilation vs. Interpretation & Syntax Specification with BNF and more Study notes Programming Languages in PDF only on Docsity!

Spring 2002 CS 403 Class Notes Page 1

Lecture 2 - Tuesday, 1/15/

  • Today
    • Compilation vs. interpretation
      • Chapter 1.
    • Specifying syntax
      • Chapter 2.
  • Reading Assignments For Next Class
    • Continue reading Chapter 2.1, 2.
    • Begin Chapter 3
  • Assignments
    • Assignment #1 will be due Tuesday, 1/22/

Spring 2002 CS 403 Class Notes (^) Page 2

Compilation vs. Interpretation

  • Pure compilation: Translate program

into machine language (object code)

  • Object code directly executable on target machine
  • Pure interpretation: No translation of

source code; interpreter runs on

platform machine and executes source

language statements

Spring 2002 CS 403 Class Notes (^) Page 3

Pure Compilation

Example: Visual C++ is compiled.

Spring 2002 CS 403 Class Notes Page 4

Pure Interpretation

Example: Visual Basic can be run as interpreted

or compiled.

Spring 2002 CS 403 Class Notes (^) Page 5

Hybrid Approach

Spring 2002 CS 403 Class Notes (^) Page 6

Separate Compilation

  • Definition: Different modules of the program

are compiled separately (i.e., at different

times)

  • Sometimes difficult to see this in an integrated programming environment (like Visual Studio)
  • A separate object file is produced for each

module

  • Linker must be run separately at the end to

produce the final executable

Spring 2002 CS 403 Class Notes Page 7

Use of Libraries

Linker can link object files for user-written program modules, or can link pre-written library object files

Spring 2002 CS 403 Class Notes (^) Page 8

Case Study: Java

  • Java compiled into bytecodes
    • Standard format for distribution of Java programs
    • Allows programs to be transferred from one machine to another on the Internet and run on new machine
    • Requires presence of Java Virtual Machine (JVM)
  • Original paradigm: Bytecodes were interpreted by the JVM
  • More recent approach: JVM provides a “just-in-time” bytecode compiler - Actual machine language is then executed on the target machine

Spring 2002 CS 403 Class Notes (^) Page 9

New Topic: Syntax Specification

  • Practical problem: Describing to someone the

syntax of a C++ assignment statement:

  • Variable
  • =
  • Expression
  • But what is an expression?
  • Very difficult to capture unambiguously in English

Spring 2002 CS 403 Class Notes Page 10

Backus Naur Form (BNF)

  • BNF: Backus Naur Form – A notation for defining programming language syntax - Based on regular expressions and context free grammars
  • Define what strings are syntactically correct vs. incorrect
  • Some basic notation:
    • Terminal symbols: Actual items that are part of the language (0,1,+,-,*,/,etc.)
    • Non-Terminal symbols: , , etc.
    • Rule/production: A single line of BNF
    • Alternatives: Expressed with |
    • *: 0 or more occurrences
    • +:1 or more occurrences

Spring 2002 CS 403 Class Notes (^) Page 11

Defining Identifiers

  • Identifier: Typically used to name a variable,

constant or subprogram

  • Identifier:
    • = A|B|C|D|…|Z
    • = 0|1|2|3|4|5|…|9|
    • = |
    • = +
  • Slight modification:
    • Restrict identifiers to not start with digits
    • = *

Spring 2002 CS 403 Class Notes (^) Page 12

Another Approach to Repetition

  • Two approaches to repeated elements:
    • = + VS.
    • = |
  • The other version of identifier:
    • = * VS.
    • =
    • = |ε∴
  • In these situations, either approach works.
    • For some situations, only the recursive approach works.

Spring 2002 CS 403 Class Notes Page 19

A More Abstract Context

  • It’s difficult to make up lots of interesting problems with actual programming languages - Actual programming languages are complex - Hard to get all of the assumptions in
  • A More Abstract Context
    • Write BNF for the language of all strings consisting of any number of A’s, any number of B’s and exactly one C.
    • Two approaches:
      • = ABC OR
      • = C
      • = A | ε
      • = B | ε

Spring 2002 CS 403 Class Notes (^) Page 20

Group Work

= ab

= b | b

= a | a

  • Which of the following strings are in the

language defined by this BNF specification?

  • baab
  • bbbab
  • bbaaaaa
  • bbaab

Spring 2002 CS 403 Class Notes (^) Page 21

Homework Assignment

  • Due next Tuesday
  • See class Web site for details