CSC 1800 Organization of Programming Languages Semantics 1 - - PDF document

csc 1800 organization of programming languages
SMART_READER_LITE
LIVE PREVIEW

CSC 1800 Organization of Programming Languages Semantics 1 - - PDF document

CSC 1800 Organization of Programming Languages Semantics 1 Revisiting Syntax Syntax: look, form/structure notation: context free grammar, BNF Semantics: what programs do, their behavior and meaning no standard notation 2


slide-1
SLIDE 1

1

CSC 1800 Organization of Programming Languages

Semantics

2

Revisiting Syntax

⚫ Syntax: “look”, form/structure

– notation: context free grammar, BNF

⚫ Semantics: what programs do, their

behavior and meaning

– no standard notation

1 2

slide-2
SLIDE 2

2

3

Terminology: Tokens

⚫ Tokens are pieces of program text that we do not wish

to think of as being built from smaller pieces

⚫ Identifiers (count), keywords (if), operators (==),

constants (123.4), etc.

⚫ Programs stored in files are just sequences of

characters

⚫ How is such a file divided into a sequence of tokens? ⚫ Also know as… Terminals

(not Non-Terminals)

4

Syntax can use BNF & EBNF

::= is defined as |

  • r

< > syntactic category, grammatical category [ ] surround an optional part { } surround a repeated part, repeated 0 or more times ( ) are used to clarify hierarchy

3 4

slide-3
SLIDE 3

3

5

Examples of BNF & EBNF

⚫ BNF

<expr> ::= <expr> + <term> | <expr> - <term> | <term> <term> ::= <term> * <factor> | <term> / <factor> | <factor>

⚫ EBNF

<expr> ::= <term> {(+ | -) <term>} <term> ::= <factor> {(* | /) <factor>}

6

Epsilon or ε or /*empty*/

⚫ There are places where you want the grammar

to generate nothing

⚫ For example, this grammar defines a typical if-

then construct with an optional else part: <ifstmt> : if <expr> then <stmt> <elsepart> <elsepart> : else <stmt> | ε

5 6

slide-4
SLIDE 4

4

7

Associativity

⚫ Operator associativity can also be indicated by

a grammar

<expr> -> <expr> + <expr> | const (ambiguous) <expr> -> <expr> + const | const (unambiguous)

<expr> <expr> <expr> <expr> const const const + +

8

Moving Toward Semantics

⚫ Concrete syntax ⚫ Abstract syntax ⚫ Semantics: described in various ways

Operational

Denotational

Axiomatic

Program function (how it's done in the real world)

7 8

slide-5
SLIDE 5

5

9

Why Semantics?

⚫ Grammars cannot describe all of the syntax of

programming languages

⚫ Categories of constructs that are trouble:

  • Context-free, but cumbersome (e.g.,

types of operands in expressions)

  • Non-context-free (e.g., variables must

be declared before they are used)

10

Semantics

⚫ There is no single widely acceptable notation or

formalism for describing semantics

⚫ Several needs for a methodology and notation for

semantics:

– Programmers need to know what statements mean – Compiler writers must know exactly what language constructs do – Correctness proofs would be possible – Compiler generators would be possible – Designers could detect ambiguities and inconsistencies

9 10

slide-6
SLIDE 6

6

11

Operational Semantics

⚫ Operational Semantics

Describe the meaning of a program by executing its statements on a machine, either simulated or actual. The change in the state of the machine (memory, registers, etc.) defines the meaning of the statement ⚫ To use operational semantics for a high-level language,

a virtual machine is needed

⚫ A hardware pure interpreter would be too expensive ⚫ A software pure interpreter also has problems

The detailed characteristics of the particular computer would make actions difficult to understand

Such a semantic definition would be machine- dependent

12

Operational Semantics – Useful?

⚫ Advantages:

May be simple for small examples

Good if used informally

Useful for implementation ⚫ Disadvantages:

Very complex for large programs

Lacks mathematical rigor ⚫ Uses:

Vienna Definition Language (VDL) used to define PL/I

Compiler work

11 12

slide-7
SLIDE 7

7

13

Denotational Semantics

⚫ Based on recursive function theory ⚫ The most abstract semantics description method ⚫ Originally developed by Scott and Strachey (1970) ⚫ Building a denotational spec. for a language:

Define a mathematical object for each language entity

Define a function that maps instances of the language entities onto instances of the corresponding mathematical objects ⚫ The meaning of language constructs are defined by only

the values of the program's variables

14

Denotational Example: Decimal Numbers

<dec_num> → '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' | <dec_num> ('0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9') Mdec('0') = 0, Mdec ('1') = 1, …, Mdec ('9') = 9 Mdec (<dec_num> '0') = 10 * Mdec (<dec_num>) Mdec (<dec_num> '1’) = 10 * Mdec (<dec_num>) + 1 … Mdec (<dec_num> '9') = 10 * Mdec (<dec_num>) + 9

Not a human-friendly notation

13 14

slide-8
SLIDE 8

8

15

Denotational Semantics – Useful?

⚫ Can be used to prove the correctness of programs ⚫ Provides a rigorous way to think about programs ⚫ Can be an aid to language design ⚫ Has been used in compiler generation systems ⚫ Because of its complexity, it are of little use to language

users

16

Denotational vs. Operational

⚫ Denotational semantics is similar to operational

semantics except:

There is no virtual machine

Language is mathematics (lambda calculus) ⚫ Difference between denotational and operational

semantics:

In operational semantics, the state changes are defined by coded algorithms for a virtual machine

In denotational semantics, they are defined by rigorous mathematical functions

15 16

slide-9
SLIDE 9

9

17

Axiomatic Semantics

⚫ Based on formal logic (predicate calculus) ⚫ Original purpose: formal program verification ⚫ Axioms or inference rules are defined for each

statement type in the language (to allow transformations

  • f logic expressions into more formal logic expressions)

⚫ The logic expressions are called assertions

18

Axiomatic Semantics Form

⚫ Pre-, post form: {P} statement {Q} ⚫ An example

a = b + 1 {a > 1}

One possible precondition: {b > 10}

Weakest precondition: {b > 0}

17 18

slide-10
SLIDE 10

10

19

Axiomatic Semantics – Useful?

⚫ Developing axioms or inference rules for all of the

statements in a language is difficult

⚫ It is a good tool for correctness proofs, and an excellent

framework for reasoning about programs, but it is not as useful for language users and compiler writers

⚫ Its usefulness in describing the meaning of a

programming language is limited for language users or compiler writers

20

Program Function Semantics

⚫ How semantics are done in the real world ⚫ Describe in English (e.g.) as best as you can what each

particular syntax element in a language does, how you use it, what its meaning is in the language

19 20

slide-11
SLIDE 11

11

21

Example: Java If Statement

⚫ From the Java Language Specification:

14.9 The if Statement The if statement allows conditional execution of a statement or a conditional choice of two statements, executing one or the other but not both.

The Expression must have type boolean or Boolean, or a compile-time error occurs.

22

Program Function Semantics – Useful?

⚫ Much easier to create that other semantic forms ⚫ Probably the only form of semantics that is useful to a

wide range of programmers

⚫ Danger: ambiguity of natural language

21 22