Docsity

Homework 3 Problems - Programming Languages | CSCI 4430, Assignments of Programming Languages

Toggle sidebar
bg1
1
Spring 09 CSCI 4430, A Milanova/BG Ryder 1
Announcements
HW3
Due Friday, March 6th
Grading Schedule
Midterm Exam: Friday
Project 1: by Tuesday, March 17th
HW3: Friday, March 20th
Spring 09 CSCI 4430, A Milanova/BG Ryder 2
Last Class: Attribute Grammars
Attribute Grammars: generalization of Context-Free
Grammars
Synthesized Attributes
Attribute value computed from values of attributes of
children in tree and values of attributes of self
E.g., val attributes
E.g., nptr attributes
Other examples?
Inherited Attributes
Attribute value computed from values of attributes of
parent and siblings in tree
E.g., sub attributes
Spring 09 CSCI 4430, A Milanova/BG Ryder 3
Last Class: Attribute Grammars
S-Attributed Grammars
An attribute grammar is S-attributed if all attributes are
synthesized
A very special case of attribute grammars
The most important case in practice
Can be evaluated on-the-fly during an LR parse
L-Attributed Grammars
An attribute grammar is L-attributed if each inherited
attribute of
X
j
on the right-hand-side of
A →X
1
X
2
…X
j
…X
n
is computed only from
(1) the attributes to the left of
X
j
:
X
1
, X
2
,…, X
j-1
(2) the inherited attributes of
A
4
Attribute Grammars (finish)
Functional Programming
Finish reading: Scott, Chapter 4
Read: Scott, Chapter 10
Spring 09 CSCI 4430, A Milanova/BG Ryder 5
Lecture Outline
Attribute Grammars
Exercises
Functional Programming with Scheme
Functional Programming Languages
S-expressions
cons, car, cdr
Defining functions
6
Exercises: Problem 1
[From Aho, Sethi and Ullman] Give an attribute
grammar to differentiate expressions over +, *, x
and constants. E.g., 3*x+x*x
S →E
print(
E.d
)
E →E
1
+
T
(1)
E.d:=E
1
.d
+ ’+’ +
T.d
(2)
E.f:= E
1
.f
+ ‘+’ +
T.f
E →T
(1)
E.d:=T.d
(2)
E.f := T.f
T →T
1
*
F
(1)
T.d:= ‘
(
‘
+
T
1
.f
+ ’*’+
F.d
+ ’+’ +
F.f
+ ‘*’ +
T
1
.d
+’)’
(2)
T.f:= T
1
.f
+ ‘*’ +
F.f
T →F
(1)
T.d:=F.d
(2)
T.f := F.f
F →
num (1)
F.d:=
‘0’ (2)
F.f :=
num.val
F→
x(1)
F.d:=‘
1’ (2)
F.f
:= ‘x’
pf3
pf4
pf5

Partial preview of the text

Download Homework 3 Problems - Programming Languages | CSCI 4430 and more Assignments Programming Languages in PDF only on Docsity!

Spring 09 CSCI 4430, A Milanova/BG Ryder 1

Announcements

 HW

 Due Friday, March 6

th

 Grading Schedule

 Midterm Exam: Friday

 Project 1: by Tuesday, March 17th

 HW3: Friday, March 20

th

Spring 09 CSCI 4430, A Milanova/BG Ryder 2

Last Class: Attribute Grammars

 Attribute Grammars: generalization of Context-Free

Grammars

 Synthesized Attributes

 Attribute value computed from values of attributes of

children in tree and values of attributes of self

 E.g., val attributes

 E.g., nptr attributes

 Other examples?

 Inherited Attributes

 Attribute value computed from values of attributes of

parent and siblings in tree

 E.g., sub attributes

Spring 09 CSCI 4430, A Milanova/BG Ryder 3

Last Class: Attribute Grammars

 S-Attributed Grammars

 An attribute grammar is S-attributed if all attributes are

synthesized

 A very special case of attribute grammars

 The most important case in practice

 Can be evaluated on-the-fly during an LR parse

 L-Attributed Grammars

 An attribute grammar is L-attributed if each inherited

attribute of Xj on the right-hand-side of A → X 1 X 2 …Xj…Xn

is computed only from

 (1) the attributes to the left of Xj : X 1 , X 2 ,…, Xj-

 (2) the inherited attributes of A

4

Attribute Grammars (finish)

Functional Programming

Finish reading: Scott, Chapter 4

Read: Scott, Chapter 10

Spring 09 CSCI 4430, A Milanova/BG Ryder 5

Lecture Outline

 Attribute Grammars

 Exercises

 Functional Programming with Scheme

 Functional Programming Languages

 S-expressions

 cons, car, cdr

 Defining functions

6

Exercises: Problem 1

 [From Aho, Sethi and Ullman] Give an attribute

grammar to differentiate expressions over +, *, x

and constants. E.g., 3x+xx

S → E print( E.d)

E → E 1 + T (1) E.d:=E 1 .d + ’ + ’ + T.d (2) E.f:= E 1 .f + ‘ + ’ + T.f

E → T (1) E.d:=T.d (2) E.f := T.f

T → T 1 * F (1) T.d:= ‘( ‘ + T 1 .f + ’ * ’+ F.d + ’ + ’ + F.f + ‘ * ’ + T 1 .d +’)’

(2) T.f:= T 1 .f + ‘ * ’ + F.f

T → F (1) T.d:=F.d (2) T.f := F.f

F → num (1) F.d:= ‘ 0 ’ (2) F.f := num. val

F → x (1) F.d:=‘ 1 ’ (2) F.f := ‘ x ’

Spring 09 CSCI 4430, A Milanova/BG Ryder 7

Problem 1 (ii)

3 x x^ x

x

x

x

T → T 1 * F (1) T.d:=T 1 .f + ’’+ F.d + ’+’ + F.f + ‘’ + T 1 .d

(2) T.f:= T 1 .f + ‘*’ + F.f

3x+xx**

3 * 1 + x * 0

3 * x

x * 1 + x * 1

x * x

3 * 1 + x * 0 + x * 1 + x * 1

3 * x + x * x

Spring 09 CSCI 4430, A Milanova/BG Ryder 8

Exercises. Problem 2

 Problem 5(b) from HW3: Grammar generates

floating point binary numbers. E.g., 101.101 (5.125).

S → L. L

S → L

L → L B

L → B

B → 0

B → 1

 Determine S.val with an attribute grammar for which the

only synthesized attribute of B is c which holds the

contribution of B to the total value.

9

Problem 2 (ii)

S

Li^.

Bi

B

i

L^ Bi

i

L

i

Lf

Bf

Bf

L^ Bf

f

Lf

Hint: Compute inherited attribute B.pow which holds

the power of B. Attribute B.c for B → 1 is synthesized

as B.c := 2 B.pow

Tree for 101.111:

Attributes of L:

L.restlen is inherited and holds the length of the sequence following L.

L.len is synthesized and holds the length of L.

L.dot is inherited and holds whether L is integer or fraction sequence.

Spring 09 CSCI 4430, A Milanova/BG Ryder 10

Exercises: Problem 3

 [From Aho, Sethi, Ullman] Give an attribute

grammar to translate infix expression into

infix expressions without redundant

parentheses. For example, since + and * are

left associative, ((a(b+c))(d)) can be

rewritten as a(b+c)d.

Spring 09 CSCI 4430, A Milanova/BG Ryder 11

Problem 3 (ii). Example ((ab)(c+d))

E

T

F

E 1

T 1

F 1

T 2 F 4

E 2

E 3

T 4

F 2

F 3

id:a

T 3

E 4 + T 6

id:b

T 5 F 6

F 5

id:c

id:d

n

n

n

n n

l*

l* n

n

l* l*

r*

r*

n

l+ r+

l+ l+

r+

Red attributes (denoted by .i)

  • inherited (i.e., passed top-down)
  • pass information about enclosing expression
  • possible values: n, l+, r+, l, r
  • examples:
    • T 2 has attribute l* meaning that the expression rooted at T 2 is the left operand of a multiplication
    • F 3 has attribute r* meaning that the expression rooted at F 3 is the right operand of a multiplication
    • E 1 , E 2 and E 3 have attribute n meaning that they are parenthesized

Spring 09 CSCI 4430, A Milanova/BG Ryder 12

Problem 3 (iii). Example ((ab)(c+d))

E

T

F

E 1

T 1

F 1

T 2 F 4

E 2

E 3

T 4

F 2

F 3

id:a

T 3

E 4 + T 6

id:b

T 5 F 6

F 5

id:c

id:d

n

n

n

n n

l*

l* n

n

l* l*

r*

r*

n

l+ r+

l+ l+

“ab”, r+

“ab”,

Blue attributes

  • synthesized (i.e., passed bottom-up)
  • 1st^ kind: pass the string value of expression (denoted by .val)
    • “ab”, or “ab*(c+d)”
  • 2nd^ kind: pass info about enclosed expression (denoted by .s)
    • possible values: *, +, n
    • examples:
      • T 3 has attribute * meaning that the expression rooted at T 3 is multiplication
      • F 4 has attribute n meaning that the expression is parenthesized

“(c+d)”,n “ab”, “c+d”,+

“ab”,

“ab(c+d)”,*

“ab(c+d)”,*

“ab(c+d)”,*

19

Attribute Grammars (finish)

Functional Programming

Finish read: Scott, Chapter 4

Read: Scott, Chapter 10

Spring 09 CSCI 4430, A Milanova/BG Ryder 20

Lecture Outline

 Functional Programming with Scheme

 Functional PLs

 S-expressions

 cons, car, cdr

 Defining functions

Spring 09 CSCI 4430, A Milanova/BG Ryder 21

First, Imperative Languages

 The concept of assignment is central

 X := 5; Y := f(X); X := 10; Y := f(X);

 Side effects

 Program semantics (i.e., how the program

works): state-transition semantics

 A program is a sequence of statements and their

effect on memory (i.e., state)

1. C := 0;

2. for I := 1 step 1 until N do

3. C:=C + a[I] * b[I];

Spring 09 CSCI 4430, A Milanova/BG Ryder 22

Imperative Languages (ii)

 Functions are usually not first-class values

 Functions have side effects:

 Informally, the value of a function application

depends on the visible state; thus, it is not

independent of the context of application

 Also, function application affects visible state;

thus, it affects execution of other functions

 Unintended side effects are the biggest problem

with imperative programming

Spring 09 CSCI 4430, A Milanova/BG Ryder 23

Imperative Languages (iii)

 Functions are usually not first-class values

 A first-class value is one that can be passed as

argument and returned as value from functions

 In a language with assignments, can be assigned into

a variable or structure

 Are C and C++ functions first-class values?

 Are Java functions first-class values?

Spring 09 CSCI 4430, A Milanova/BG Ryder 24

Imperative Languages (iv)

 Imperative languages are often compiled

 Imperative languages often use manual

memory management

 Imperative languages have complex syntax

Spring 09 CSCI 4430, A Milanova/BG Ryder 25

Functional Languages

 In pure functional languages, there is no

concept of assignment

 Variables are bound to values only through

parameter associations

 No side effects

 Referential transparency

 Value of an application is independent of context

where the function application occurs

 Thus, all variables in a function body must be

local to that function. Why?

Spring 09 CSCI 4430, A Milanova/BG Ryder 26

Functional Languages (ii)

 Program semantics: reduction semantics

 A program is a set of function definitions and

their application to arguments

Def IP = (Insert +) º (ApplyToAll *) º Transpose

IP <<1,2,3>,<6,5,4>> =

(Insert +) ((ApplyToAll *) (Transpose

(Insert +) ((ApplyToAll *) <<1,6>,<2,5>,<3,4>>)

(Insert +) <6,10,12>

Function composition

Spring 09 CSCI 4430, A Milanova/BG Ryder 27

Functional Languages (iii)

 Functions are first-class values

 Can be returned as value of a function application

 Can be passed as an argument

 In a language with assignment, can be assigned into

variables and structures and saved

 Unnamed functions exist as values

 Functional languages are usually interpreted

 Functional languages have implicit memory

management, i.e., garbage collection

 Functional languages usually have simple (or at

least much simpler) syntax

Spring 09 CSCI 4430, A Milanova/BG Ryder 28

Functional Languages (iv)

 Lisp designed for symbolic computing

 Simple syntax

 Data and programs have same syntactic form

 S-expression

 Function application written in prefix form

(e1 e2 e3 … ek) means

 Evaluate e1 to a function value

 Evaluate each of e2,…,ek to values

 Apply the function to these values

(+ 1 3) evaluates to 4

Spring 09 CSCI 4430, A Milanova/BG Ryder 29

History

Lisp

1950’s

John McCarthy

Scheme

Guy Steele

Gerald Sussman

Common Lisp

dynamic scoping

lexical scoping

functions as first class values

standardized PL

Spring 09 CSCI 4430, A Milanova/BG Ryder 30

S-expressions

S-expr ::= Name | Number | ( { S-expr } )

 Name is a symbolic constant, some string of chars which

starts off with anything that can’t start a Number

 Number is an integer or real number

 E.g., (a (b c) (d)) is an S-expr (a list)

 car selects the first element

 car of this S-expr is a

 cdr selects the rest of the list

 cdr of this S-expr is ((b c) (d))

a

b

c ( )

d ( )^

Spring 09 CSCI 4430, A Milanova/BG Ryder 37

PLT Scheme

 Download PLT Scheme (aka DrScheme)

 http://plt-scheme.org/

 choose language R5RS