CSC 1800 Organization of Programming Languages Semantics Revisiting
- Slides: 22
CSC 1800 Organization of Programming Languages Semantics
Revisiting Syntax l Syntax: “look”, form/structure – l notation: context free grammar, BNF Semantics: what programs do, their behavior and meaning – no standard notation 2
Terminology: Tokens l 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? l Also know as… Terminals l l l (not Non-Terminals) 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
Examples of BNF & EBNF l l BNF <expr> : : = <expr> + <term> | <expr> - <term> | <term> : : = <term> * <factor> | <term> / <factor> | <factor> EBNF <expr> : : = <term> {(+ | -) <term>} <term> : : = <factor> {(* | /) <factor>} 5
Epsilon or l l ε or /*empty*/ There are places where you want the grammar to generate nothing For example, this grammar defines a typical ifthen construct with an optional else part: <ifstmt> : if <expr> then <stmt> <elsepart> : else <stmt> | ε 6
Associativity l Operator associativity can also be indicated by a grammar <expr> -> <expr> + <expr> | <expr> -> <expr> + const | const (ambiguous) (unambiguous) <expr> + const 7
Moving Toward Semantics l l l Concrete syntax Abstract syntax Semantics: described in various ways – – Operational Denotational Axiomatic Program function (how it's done in the real world) 8
Why Semantics? l l 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
Semantics l l 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
Operational Semantics l 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 l To use operational semantics for a high-level language, a virtual machine is needed l A hardware pure interpreter would be too expensive A software pure interpreter also has problems l – – The detailed characteristics of the particular computer would make actions difficult to understand Such a semantic definition would be machine- dependent 11
Operational Semantics – Useful? l Advantages: – – – l Disadvantages: – – l May be simple for small examples Good if used informally Useful for implementation Very complex for large programs Lacks mathematical rigor Uses: – – Vienna Definition Language (VDL) used to define PL/I Compiler work 12
Denotational Semantics l l 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: – – l 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
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 (<dec_num> … Mdec (<dec_num> Mdec ('1') = 1, …, Mdec ('9') = 9 '0') = 10 * Mdec (<dec_num>) '1’) = 10 * Mdec (<dec_num>) + 1 '9') = 10 * Mdec (<dec_num>) + 9 Not a human-friendly notation 14
Denotational Semantics – Useful? l l l 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
Denotational vs. Operational l Denotational semantics is similar to operational semantics except: – – l 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
Axiomatic Semantics l l 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
Axiomatic Semantics Form l Pre-, post form: {P} statement {Q} l An example – – – a = b + 1 {a > 1} One possible precondition: {b > 10} Weakest precondition: {b > 0} 18
Axiomatic Semantics – Useful? l l l 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
Program Function Semantics l l 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
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. l The Expression must have type boolean or Boolean, or a compile-time error occurs. 21
Program Function Semantics – Useful? l l l 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
- Compare procedural semantics and declarative semantics.
- Real-time systems and programming languages
- Elsa gunter uiuc
- Multithreaded programming languages
- Programming languages levels
- Introduction to programming languages
- Plc
- Procedural programming languages
- Comparative programming languages
- Alternative programming languages
- Strongly typed vs weakly typed
- Transmission programming languages
- Adam doupe cse 340
- Types of programming languages
- Xenia programming languages
- Advantages of application software
- Mainstream programming languages
- Cse 340 principles of programming languages
- Programing languages
- Programming languages
- Programming languages
- Programming languages
- Tiny programming language