Lecture2.6 - 9/10/06 DPL-2: impcore - an imperative...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
9/10/06 1 9/10/06 From Slides by N. Ramsey (Copyright) 1 DPL-2: impcore - an imperative mini-language Some Terminology: Concrete Syntax : the form in which people write programs Abstract Syntax : expresses the essential structure of programs Operational Semantics : explains the meaning of the abstract syntax by explaining how it should be executed Interpreter : interactive, parses concrete syntax, converts to abstract syntax, and executes it according to the operational semantics Environments : determine the meaning of identifiers, both in the operational semantics and in the interpreter. 9/10/06 From Slides by N. Ramsey (Copyright) 2 DPL-2: impcore - an imperative mini-language We are going to examine an interpreter for a small imperative language - we will be able to look at problems and solutions that apply to most imperative languages. Syntax: LISP -like (most trivial syntax known to man) The language will accept two kinds of inputs: function definitions and expressions . The impcore function: (define mod (m n) (- m (* n (/ m n)))) corresponds to the C function int mod (int m, int n) { return m - n*(m / n); } 9/10/06 From Slides by N. Ramsey (Copyright) 3 DPL-2: An expression-oriented language Expressions include control flow (no “statements”) Impcore C (if e 1 e 2 e 3 ) if (e 1 ) then e 2 ; else e 3 ; (while e 1 e 2 ) while (e 1 ) e 2 ; 0 (set x e) x = e (begin e 1 … e n ) ( e 1 ; e 2 ; …; e n ) (f e 1 e 2 … e n ) f(e 1 , e 2 , …, e n ) f may be primitive or defined via (define f …). The primitives include + - * / = < > print 9/10/06 From Slides by N. Ramsey (Copyright) 4 DPL-2: more impcore Datatypes: integer . Functions would also qualify, but they are not returned as values (not first class ), nor can they be passed as actual parameters. Scopes ( name-spaces, environments ) We have two levels only: global variables and formal parameters . Local variables are “simulated” by adding extra formal parameters (as in awk ). A third scope ( environment ) is used for functions - they don t share a name-space with any of the variables. 9/10/06 From Slides by N. Ramsey (Copyright) 5 DPL-2: separate name-spaces at work -> (val f 33) (* global variable *) 33 -> (define f (f) (+ f f)) (* function and formal parameter *) f -> (f f) (* function application - actual par. f *) 66 9/10/06 From Slides by N. Ramsey (Copyright) 6 DPL-2: impcore concrete syntax toplevel ::= expression | fundef | val-binding | ( use filename ) ::= ( define function-name formals expression ) formals ::= ( { parameter-name } ) ::= ( val variable-name ) ::= literal-value | ( if expression expression expression ) | ( while expression expression ) | ( set variable-name expression ) | ( begin { } ) | ( op { } ) op ::= function-name | + | - | * | / | = | < | > | print
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
9/10/06 2 9/10/06 From Slides by N. Ramsey (Copyright) 7 DPL-2: impcore concrete syntax literal-value ::= integer integer ::= digits, with optional - sign *-name ::=
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 6

Lecture2.6 - 9/10/06 DPL-2: impcore - an imperative...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online