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 2 1
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) 3 3 Syntax can use BNF & EBNF ::= is defined as | or < > syntactic category, grammatical category [ ] surround an optional part { } surround a repeated part, repeated 0 or more times ( ) are used to clarify hierarchy 4 4 2
Examples of BNF & EBNF ⚫ BNF <expr> ::= <expr> + <term> | <expr> - <term> | <term> <term> ::= <term> * <factor> | <term> / <factor> | <factor> ⚫ EBNF <expr> ::= <term> {(+ | -) <term>} <term> ::= <factor> {(* | /) <factor>} 5 5 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> | ε 6 6 3
Associativity ⚫ Operator associativity can also be indicated by a grammar <expr> -> <expr> + <expr> | const (ambiguous) <expr> -> <expr> + const | const (unambiguous) <expr> <expr> <expr> + const <expr> + const const 7 7 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) 8 8 4
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) 9 9 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 10 10 5
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 – 11 11 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 12 12 6
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 13 13 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') M dec ('0') = 0, M dec ('1') = 1, …, M dec ('9') = 9 M dec (<dec_num> '0') = 10 * M dec (<dec_num>) M dec (<dec_num> '1’) = 10 * M dec (<dec_num>) + 1 … M dec (<dec_num> '9') = 10 * M dec (<dec_num>) + 9 Not a human-friendly notation 14 14 7
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 15 15 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 16 16 8
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 of logic expressions into more formal logic expressions) ⚫ The logic expressions are called assertions 17 17 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} – 18 18 9
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 19 19 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 20 20 10
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. 21 21 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 22 22 11
Recommend
More recommend