Docsity

Lecture 3: Simple Language IMP - Arithmetic and Logical Expressions, Study notes of Computer Science

Partial preview of the text

Download Lecture 3: Simple Language IMP - Arithmetic and Logical Expressions and more Study notes Computer Science in PDF only on Docsity!

8/29/

Simple Language IMP

An Example of a Simple

Language, IMP

Lecture 3CS 336

Tuesday,August 29, 2000

8/29/

Simple Language IMP

Reference

Textbook

Glynn Winskel, The Formal Semantics of

operational semantics1996, MIT Press, chapter 2, Introduction toProgramming Languages: An Introduction,

8/29/

Simple Language IMP

Outline

5. The IMP Language4. Statements3. Logical Expressions2. Relational Expressions1. Arithmetic Expressions

8/29/

Simple Language IMP

1. Arithmetic Expressions

DivisionMultiplicationSubtractionAddition

8/29/

Simple Language IMP

Subtraction of Integers

for each s in S does there exist an sโ€™ in S

only if we include negative integersinversesuch that s + sโ€™ = 0?

8/29/

Simple Language IMP

Inverse Elements for Addition

of Integers

if s is in S, then there is a unique sโ€™ in S

We write sโ€™ as - ssuch that s + sโ€™ = 0

We then define the subtraction operation

s1 - s2 = s1 + - s2by

8/29/

Simple Language IMP

Multiplication of Integers

closure: if s1 and s2 are in set S, then s1 * s2 is in S (Closure of multiplication gives us no more operations than does closure of addition with inverse.)

identify: there is a member of S, 1, such that s * 1 = 1 * s for all s in S

associativity: if s1, s2, and s3 are in set S, then (s1 * s2)

  • s3 = s1 * (s2 * s3)

commutativity: if s1 and s2 are in set S, then s1 * s2 = s2 * s

8/29/

Simple Language IMP

Inverse of Multiplication

Does each integer have an integer

No! Only 1 has an integer inverse.multiplicative inverse?

If we include the multiplicative inverses

numbers.we expand the set to be the rational

Note: 0 has no multiplicative inverse in

the rational numbers. 0 * a = a * 0 = 0

8/29/

Simple Language IMP

Parsing using Grammar 1

=> a - b * c + d=> a - b * c + LOC=> a - b * c + AEXP=> a - b * LOC + AEXP=> a - b * AEXP + AEXP=> a -b * AEXP=> a - LOC * AEXP=> a - AEXP * AEXP=> a - AEXP=> LOC - AEXPAEXP => AEXP - AEXP

8/29/

Simple Language IMP

Parsing Same Expression

using Grammar 1

=> a - b * c + d=> a - b * c + LOC=> a - b * c + AEXP=> a - b * LOC + AEXP=> a - b * AEXP + AEXP=> a - LOC * AEXP + AEXP=> a - AEXP * AEXP + AEXP=> a - AEXP + AEXP=> LOC - AEXP + AEXP=> AEXP - AEXP + AEXPAEXP => AEXP + AEXP

8/29/

Simple Language IMP

An Unambiguous Grammar 2

for Arithmetic Expressions

LOC ::= string of one or more letters followed by zero orNUM ::= string of one or more digitsAFACTOR ::= LOCAFACTOR ::= NUMATERM ::= ATERM * AFACTORA EXP ::= AEXP - ATERMAEXP ::= AEXP + ATERM more digits

8/29/

Simple Language IMP

Parsing using Grammar 2

8/29/

Simple Language IMP

Abstract Syntax versus

Concrete Syntax

Abstract Syntax of arithmetic expressions

how to build new arithmetic expressions

Concrete Syntax of arithmetic

how to add information for unique parsingexpressions

(through parentheses or precedence rules)

8/29/

Simple Language IMP

2. Relational Expressions

The Law of Trichotomy

for integers n1 and n2, exactly one of the

n1 > n2n1 < n2n1 = n2following is true: