0% found this document useful (0 votes)
78 views

EECS 321 Programming Languages: Instructor

This document provides an overview of the EECS 321 Programming Languages course taught by Robby Findler in Winter 2012. It discusses how the course will teach programming language concepts by implementing interpreters and using Racket. It explains the difference between interpreters and compilers, with interpreters taking a program as input and producing a result, while compilers take a program as input and produce a new program. The document also provides an example grammar for an algebra language in Backus-Naur Form and shows how it can be used to generate valid algebra programs and evaluate them by defining evaluation rules.

Uploaded by

taocort
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views

EECS 321 Programming Languages: Instructor

This document provides an overview of the EECS 321 Programming Languages course taught by Robby Findler in Winter 2012. It discusses how the course will teach programming language concepts by implementing interpreters and using Racket. It explains the difference between interpreters and compilers, with interpreters taking a program as input and producing a result, while compilers take a program as input and produce a new program. The document also provides an example grammar for an algebra language in Backus-Naur Form and shows how it can be used to generate valid algebra programs and evaluate them by defining evaluation rules.

Uploaded by

taocort
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

EECS 321 Programming Languages

Winter 2012

Instructor: Robby Findler

Course Details

http://www.eecs.northwestern.edu/~robby/ courses/321-2012-winter/ (or google ndler and follow the links)

Programming Language Concepts

This course teaches concepts in two ways: By implementing interpreters new concept ! new interpreter By using Racket and variants we dont assume that you already know Racket

Interpreters vs Compilers
An interpreter takes a program and produces a result DrRacket x86 processor desktop calculator bash Algebra student

Interpreters vs Compilers
An interpreter takes a program and produces a result DrRacket x86 processor desktop calculator bash Algebra student A compiler takes a program and produces a program DrRacket x86 processor gcc javac

Interpreters vs Compilers
An interpreter takes a program and produces a result DrRacket x86 processor desktop calculator bash Algebra student Good for understanding program behavior, easy to implement

A compiler takes a program and produces a program DrRacket x86 processor gcc javac Good for speed, more complex (come back next quarter)

Interpreters vs Compilers
An interpreter takes a program and produces a result DrRacket x86 processor desktop calculator bash Algebra student Good for understanding program behavior, easy to implement

A compiler takes a program and produces a program DrRacket x86 processor gcc javac Good for speed, more complex (come back next quarter) So, whats a program?
7

A Grammar for Algebra Programs


A grammar of Algebra in BNF (Backus-Naur Form): !prog" !defn" !expr" ::= ::= ::= | | | | ::= ::= !defn"* !expr" !id"(!id") = !expr" (!expr" + !expr") (!expr" - !expr") !id"(!expr") !id" !num" a variable name: f, x, y, z, ... a number: 1, 42, 17, ...

!id" !num"

A Grammar for Algebra Programs


A grammar of Algebra in BNF (Backus-Naur Form): !prog" !defn" !expr" ::= ::= ::= | | | | ::= ::= !defn"* !expr" !id"(!id") = !expr" (!expr" + !expr") (!expr" - !expr") !id"(!expr") !id" !num" a variable name: f, x, y, z, ... a number: 1, 42, 17, ...

!id" !num"

Each meta-variable, such as !prog", denes a set

Using a BNF Grammar


!id" !num" ::= ::= a variable name: f, x, y, z, ... a number: 1, 42, 17, ...

The set !id" is the set of all variable names The set !num" is the set of all numbers

10

Using a BNF Grammar


!id" !num" ::= ::= a variable name: f, x, y, z, ... a number: 1, 42, 17, ...

The set !id" is the set of all variable names The set !num" is the set of all numbers To make an example member of !num", simply pick an element from the set

11

Using a BNF Grammar


!id" !num" ::= ::= a variable name: f, x, y, z, ... a number: 1, 42, 17, ...

The set !id" is the set of all variable names The set !num" is the set of all numbers To make an example member of !num", simply pick an element from the set 2 " !num" 298 " !num"

12

Using a BNF Grammar


!expr" ::= | | | | (!expr" + !expr") (!expr" - !expr") !id"(!expr") !id" !num"

The set !expr" is dened in terms of other sets

13

Using a BNF Grammar


!expr" ::= | | | | (!expr" + !expr") (!expr" - !expr") !id"(!expr") !id" !num"

To make an example !expr": choose one case in the grammar pick an example for each meta-variable combine the examples with literal text

14

Using a BNF Grammar


!expr" ::= | | | | (!expr" + !expr") (!expr" - !expr") !id"(!expr") !id" !num"

To make an example !expr": choose one case in the grammar pick an example for each meta-variable combine the examples with literal text

15

Using a BNF Grammar


!expr" ::= | | | | (!expr" + !expr") (!expr" - !expr") !id"(!expr") !id" !num"

To make an example !expr": choose one case in the grammar pick an example for each meta-variable 7 " !num" combine the examples with literal text

16

Using a BNF Grammar


!expr" ::= | | | | (!expr" + !expr") (!expr" - !expr") !id"(!expr") !id" !num"

To make an example !expr": choose one case in the grammar pick an example for each meta-variable 7 " !num" combine the examples with literal text 7 " !expr"
17

Using a BNF Grammar


!expr" ::= | | | | (!expr" + !expr") (!expr" - !expr") !id"(!expr") !id" !num"

To make an example !expr": choose one case in the grammar pick an example for each meta-variable combine the examples with literal text

18

Using a BNF Grammar


!expr" ::= | | | | (!expr" + !expr") (!expr" - !expr") !id"(!expr") !id" !num"

To make an example !expr": choose one case in the grammar pick an example for each meta-variable f " !id" combine the examples with literal text

19

Using a BNF Grammar


!expr" ::= | | | | (!expr" + !expr") (!expr" - !expr") !id"(!expr") !id" !num"

To make an example !expr": choose one case in the grammar pick an example for each meta-variable f " !id" 7 " !expr"

combine the examples with literal text

20

Using a BNF Grammar


!expr" ::= | | | | (!expr" + !expr") (!expr" - !expr") !id"(!expr") !id" !num"

To make an example !expr": choose one case in the grammar pick an example for each meta-variable f " !id" 7 " !expr"

combine the examples with literal text f(7) " !expr"


21

Using a BNF Grammar


!expr" ::= | | | | (!expr" + !expr") (!expr" - !expr") !id"(!expr") !id" !num"

To make an example !expr": choose one case in the grammar pick an example for each meta-variable f " !id" f(7) " !expr"

combine the examples with literal text

22

Using a BNF Grammar


!expr" ::= | | | | (!expr" + !expr") (!expr" - !expr") !id"(!expr") !id" !num"

To make an example !expr": choose one case in the grammar pick an example for each meta-variable f " !id" f(7) " !expr"

combine the examples with literal text f(f(7)) " !expr"


23

Using a BNF Grammar


!prog" !defn" ::= ::= !defn"* !expr" !id"(!id") = !expr"

f(x) = (x + 1) " !defn"

24

Using a BNF Grammar


!prog" !defn" ::= ::= !defn"* !expr" !id"(!id") = !expr"

f(x) = (x + 1) " !defn" To make a !prog" pick some number of !defn"s (x + y) " !prog" f(x) = (x + 1) g(y) = f((y - 2)) " !prog" g(7)

25

Programming Language

A programming language is dened by a grammar for programs rules for evaluating any program to produce a result

26

Programming Language

A programming language is dened by a grammar for programs rules for evaluating any program to produce a result For example, Algebra evaluation is dened in terms of evaluation steps: (2 + (7 - 4)) # (2 + 3) # 5

27

Programming Language

A programming language is dened by a grammar for programs rules for evaluating any program to produce a result For example, Algebra evaluation is dened in terms of evaluation steps: f(x) = (x + 1) f(10) # (10 + 1) # 11

28

Evaluation
Evaluation # is dened by a set of pattern-matching rules: (2 + (7 - 4)) # (2 + 3)

due to the pattern rule ... (7 - 4) ... # ... 3 ...

29

Evaluation
Evaluation # is dened by a set of pattern-matching rules: f(x) = (x + 1) f(10) # (10 + 1) due to the pattern rule ... !id"1(!id"2) = !expr"1 ... ... !id"1(!expr"2) ... # ... !expr"3 ...

where !expr"3 is !expr"1 with !id"2 replaced by !expr"2

30

Rules for Evaluation


Rule 1 - one pattern ... !id"1(!id"2) = !expr"1 ... ... !id"1(!expr"2) ... # ... !expr"3 ...

where !expr"3 is !expr"1 with !id"2 replaced by !expr"2

31

Rules for Evaluation


Rule 1 - one pattern ... !id"1(!id"2) = !expr"1 ... ... !id"1(!expr"2) ... # ... !expr"3 ...

where !expr"3 is !expr"1 with !id"2 replaced by !expr"2 Rules 2 - $ special cases ... (0 + 0) ... # ... 0 ... ... (1 + 0) ... # ... 1 ... ... (2 + 0) ... # ... 2 ... etc. ... (0 - 0) ... # ... 0 ... ... (1 - 0) ... # ... 1 ... ... (2 - 0) ... # ... 2 ... etc.

32

Rules for Evaluation


Rule 1 - one pattern ... !id"1(!id"2) = !expr"1 ... ... !id"1(!expr"2) ... # ... !expr"3 ...

where !expr"3 is !expr"1 with !id"2 replaced by !expr"2 Rules 2 - $ special cases ... (0 + 0) ... # ... 0 ... ... (1 + 0) ... # ... 1 ... ... (2 + 0) ... # ... 2 ... etc. ... (0 - 0) ... # ... 0 ... ... (1 - 0) ... # ... 1 ... ... (2 - 0) ... # ... 2 ... etc.

When the interpreter is a program instead of an Algebra student, the rules look a little different
33

HW 1

On the course web page: Finger exercises in Racket

Assignment is due Friday

34

You might also like