Describing Syntax and Semantics Chapter 3 Describing Syntax and Semantics
Chapter 3 Topics Introduction The General Problem of Describing Syntax Formal Methods of Describing Syntax Attribute Grammars Describing the Meanings of Programs: Dynamic Semantics
Introduction Language description includes two main components Syntax The form of expressions, statements, and program units Semantics The meaning Syntax and semantics provide a language’s definition
Introduction Describing syntax is easier than describing semantics Universally-accepted notations can be used to describe syntax. No universally-accepted systems have been created for describing semantics.
The General Problem of Describing Syntax: Terminology Definition: A sentence is an ordered string of characters over some alphabet Definition: A language is a set of sentences Definition: A lexeme is the lowest level syntactic unit of a language (e.g., *, sum, begin) Definition: A token is a category of lexemes (e.g., identifier)
Formal Grammars and Formal Languages A formal grammar G = (N,Σ,P,S) is a quad-tuple such that N is a finite set of nonterminal symbols Σ is a finite set of terminal symbols, disjoint from N P is a finite set of production rules of the form αNβ → γ S Є N the start symbol The language of a formal grammar G, denoted as L(G), is the set of all strings over Σ that can be generated by starting with the start symbol S and then applying the production rules in P until no nonterminal symbols are present.
Formal Methods of Describing Syntax The most widely known methods for describing programming language syntax: Backus-Naur Form (BNF) Context-Free Grammars Extended BNF (EBNF) Improves readability and writability Grammars and Recognizers
Context-Free Grammars Developed by Noam Chomsky mid-1950s Language generators meant to describe the syntax of natural languages Defines a class of languages called context-free languages
Formal Definition of Languages Recognizers A device that reads input strings of the language and decides whether the input strings belong to the language Generators A device that generates sentences of a language which are used to compare with the syntax of a particular sentence
Backus-Naur Form (BNF) Invented by John Backus and Peter Naur Equivalent to context-free grammars A metalanguage used to describe another language Abstractions are used to represent classes of syntactic structures act like syntactic variables called nonterminal symbols
BNF Fundamentals Non-terminals: abstractions Terminals: lexemes and tokens Grammar: a collection of rules Examples of BNF rules: <id_list> -> ident | ident, <id_list> <if_stmt> -> if <logic_expr> then <stmt>
BNF Rules A rule has a left-hand side (LHS) a right-hand side (RHS) consists of terminal and nonterminal symbols A grammar is a finite nonempty set of rules An abstraction (or nonterminal symbol) can have more than one RHS <stmt> -> <single_stmt> | begin <stmt_list> end
Describing Lists Syntactic lists are described using recursion <id_list> -> ident | ident, <id_list> A derivation is a repeated application of rules, starting with the start symbol, and ending with a sentence all terminal symbols
An Example Grammar <program> -> <stmts> <stmts> -> <stmt> | <stmt> ; <stmts> <stmt> -> <var> = <expr> <var> -> a | b | c | d <expr> -> <term> + <term> |<term> - <term> <term> -> <var> | const
An Example Derivation <program> => <stmts> => <var> = <expr> => a = <expr> => a = <term> + <term> => a = <var> + <term> => a = b + <term> => a = b + const
Derivation Every string of symbols in the derivation is a sentential form A sentence is a sentential form that has only terminal symbols A leftmost derivation is one in which the leftmost nonterminal in each sentential form is the one that is expanded A derivation may be neither leftmost nor rightmost
A hierarchical representation of a derivation Parse Tree A hierarchical representation of a derivation <program> <stmts> <stmt> <var> = <expr> a <term> + <term> <var> const b
Ambiguity in Grammars A grammar is ambiguous if and only if it generates a sentential form that has two or more distinct parse trees
An Ambiguous Expression Grammar <expr> <expr> <op> <expr> | const <op> / | - <expr> <expr> <expr> <op> <expr> <expr> <op> <op> <expr> <expr> <op> <expr> <expr> <op> <expr> const - const / const const - const / const
An Unambiguous Expression Grammar If we use the parse tree to indicate precedence levels of the operators, we cannot have ambiguity <expr> <expr> - <term> | <term> <term> <term> / const | const <expr> <expr> - <term> <term> <term> / const const const
Associativity of Operators Operator associativity can also be indicated by a grammar. ambiguous: <expr> -> <expr> + <expr> | const unambiguous: <expr> -> <expr> + const | const <expr> <expr> <expr> + const <expr> + const const
Extended BNF Optional parts are placed in brackets [] <proc_call> -> ident [(<expr_list>)] Alternative parts of RHSs are placed inside parentheses and separated via vertical bars <term> → <term> (+|-) const Repetitions (0 or more) are placed inside braces {} <ident> → letter {letter|digit}
BNF and EBNF BNF <expr> -> <expr> + <term> <term> -> <term> * <factor> | <term> / <factor> | <factor> EBNF <expr> -> <term> {(+|-) <term> } <term> -> <factor> {(*|/) <factor> }
The Chomsky Hierarchy Grammar Languages Automaton Production Rules Type-0 unrestricted Recursively enumerable Turing machine No restrictions Type-1 context-sensitive Context-sensitive Linear-bounded non-deterministic Turing machine αAβ -> αγβ Type-2 context-free Context-free Non-deterministic pushdown automaton A -> γ Type-3 regular Regular Finite state automaton A -> aB A -> a
Semantics The meaning, not the form Need a language to describe the semantics of languages Assorted mathematical formalisms Static Semantics Attribute Grammars Dynamic Semantics Operational Semantics Axiomatic Semantics Denotational Semantics
Static Semantics Static semantics of a language are those non-syntactic rules which 1) define legality of a program 2) can be analyzed at compile time Consider a rule which states “a variable must be declared before it is referenced” Cannot be specified in a context-free grammar Can be tested at compile time Some rules can be specified in a grammar, but unnecessarily complicate the grammar
Attribute Grammars Describes more than can be described with a context-free grammar (Knuth, 1968) Uses three additional elements to CFGs to carry some semantic information along parse trees Attributes value holders which describe language symbols Attribute computation functions describe how attribute values are determined also called semantic functions Predicate functions describe language rules, associated with grammar rules
Attribute Grammars Most attribute grammar work uses the parse trees: Inherited attributes pass information down a parse tree Synthesized attributes pass information up a parse tree Intrinsic attributes are synthesized attributes for parse tree leaves
Attribute Grammars Definition: An attribute grammar is a context-free grammar G = (N,Σ,P,S) with the following additions: For each grammar symbol x there is a set A(x) of attribute values Each rule has a set of functions that define certain attributes of the nonterminals in the rule Each rule has a (possibly empty) set of predicates to check for attribute consistency
Attribute Grammars Definitions Let X0 X1 ... Xn be a rule Functions of the form S(X0) = f(A(X1), ... , A(Xn)) define synthesized attributes I(Xj) = f(A(X0), ... , A(Xn)), for i <= j <= n, define inherited attributes Initially, there are intrinsic attributes on the leaves
Attribute Grammar Example Syntax <expr> -> <var> + <var> | <var> <var> -> A|B|C Attributes actual_type synthesized for <var> and <expr> expected_type inherited for <expr>
Attribute Grammar Example 1. Syntax rule: <expr> <var>[1] + <var>[2] Semantic rules: <expr>.actual_type <var>[1].actual_type Predicate: <var>[1].actual_type == <var>[2].actual_type <expr>.expected_type == <expr>.actual_type 2. Syntax rule: <var> id Semantic rule: <var>.actual_type lookup (<var>.string)
How are attribute values computed? Attribute Grammars How are attribute values computed? If all attributes were inherited, the tree could be decorated in top-down order. If all attributes were synthesized, the tree could be decorated in bottom-up order. In many cases, both kinds of attributes are used, and it is some combination of top-down and bottom-up that must be used.
Attribute Grammars <expr>.expected_type inherited from parent <var>[1].actual_type lookup (A) <var>[2].actual_type lookup (B) <var>[1].actual_type =? <var>[2].actual_type <expr>.actual_type <var>[1].actual_type <expr>.actual_type =? <expr>.expected_type
Attribute Grammars Primary value of AGs: 1. Static semantics specification 2. Static semantics checking
Dynamic Semantics There is no single widely acceptable notation or formalism for describing 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 each statement.
Dynamic Semantics The dynamic semantics of a program is the meaning of its expressions, statements, and program units Accurately describing semantics is essential Semantics are often described in English: thus, ambiguity! Three different methods are commonly used to describe semantics formally
Operational Semantics Denotational Semantics Dynamic Semantics Operational Semantics Axiomatic Semantics Denotational 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
Operational Semantics 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
Operational Semantics A better alternative: A complete computer simulation The process: Build a translator (translates source code to the machine code of an idealized computer) Build a simulator for the idealized computer
Uses of Operational Semantics compiler writing rigorously proving properties of the program program modeling for model checking the “gold standard” for validating other semantics
Axiomatic Semantics Based on formal logic (predicate calculus) Original purpose: formal program verification Define axioms or inference rules for each statement type in the language allowa transformations of expressions to other expressions The expressions are called assertions
Axiomatic Semantics An assertion before a statement is a precondition states the relationships and constraints among variables that are true at that point in execution An assertion following a statement is a postcondition The weakest precondition least restrictive precondition that guarantees the postcondition Pre-post form: {P} statement {Q}
Axiomatic Semantics Program proof process The postcondition for the whole program is the desired results Work back through the program to the first statement If the precondition on the first statement is the same as the program spec, then the program is correct
Axiomatic Semantics An axiom for assignment statements (x = E): {Qx->E} x = E {Q} The Rule of Consequence: {P} S {Q}, P' => P, Q => Q' {P’} S {Q’}
Axiomatic Semantics An inference rule for statement sequences: For a sequence S1;S2: {P1} S1 {P2} {P2} S2 {P3} the inference rule is: {P1} S1 {P2}, {P2} S2 {P3} {P1} S1; S2 {P3}
Axiomatic Semantics An inference rule for logical pretest loops: For the loop construct {P} while B do S end {Q} the inference rule is {I and B} S {I} {I} while B do S {I and (not B)} where I is the loop invariant
Characteristics of the the following conditions: Loop Invariant It must meet the following conditions: 1. P => I 2. {I} B {I} 3. {I and B} S {I} 4. (I and (not B)) => Q 5. The loop terminates
Loop Invariant The loop invariant I is a weakened version of the loop postcondition, and it is also a precondition. I must be weak enough to be satisfied prior to the beginning of the loop, I must be strong enough (combined with the loop exit condition) to force the truth of the postcondition
Evaluation of Axiomatic Semantics 1. DIFFICULT: developing axioms or inference rules for all of the statements in a language 2. A GOOD TOOL: for correctness proofs, and an excellent framework for reasoning about programs 3. NOT VERY USEFUL: for language users and compiler writers
Denotational Semantics Based on recursive function theory The most abstract semantics description method The most rigorous formal technique which is widely known Its complexity makes it of little use to language users
Denotational Semantics The process of building a denotational spec for a language: 1. Define a mathematical object for each language entity 2. Define a function that maps instances of the language entities onto instances of the corresponding mathematical objects
Denotational Semantics The difference between denotational and operational semantics: In operational semantics, state changes are defined by coded algorithms In denotational semantics, rigorous mathematical functions
Denotational Semantics The state of a program is the values of all its current variables s = {<i1, v1>, <i2, v2>, …, <in, vn>} where ik is a variable name and vk is the value of the variable. Define VARMAP(ij, s) = vj a function mapping values vj to names ij in a state s
Denotational Semantics Decimal Numbers <digit> => 0|1|2|3|4|5|6|7|8|9 <dec_num> => <digit>|<dec_num><digit> 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
Denotational Semantics Expressions: Map expressions onto Z {ERROR} assume <b> is <binary_expr>, <l> is <left_expr>, <r> is <right_expr> Me(<expr>,s) = case <expr> of <dec_num> => Mdec(<dec_num>,s) <var> => if VARMAP(<var>,s) == UNDEF then ERROR else VARMAP(<var>,s) <b> => if ( Me(<b>.<l>,s) == UNDEF or Me(<b>.<r>,s) == UNDEF) then ERROR elseif <b>.<operator> == ‘+’ then Me(<b>.<l>,s) + Me(<b>.<r>,s) else Me(<b>.<l>,s) * Me(<b>.<r>,s)
Denotational Semantics Assignment Statements: Maps state sets to state sets Ma(x := E,s) = if Me(E,s) == ERROR then ERROR else s’={<i1’,v1’>,<i2’,v2’>,...,<in’,vn’>}, where for j = 1, 2, ..., n, if ij <> x, vj’ = VARMAP(ij,s) if ij == x, vj’ = Me(E,s)
Denotational Semantics Logical Pretest Loops: Maps state sets to state sets Ml(while B do L, s) = if Mb(B,s) == UNDEF then ERROR elseif Mb(B,s) == false then s elseif Msl(L,s) == ERROR then ERROR else Ml(while B do L, Msl(L, s))
The Meaning of a Loop The meaning of the loop is: the value of the program variables after the statements in the loop have been executed the prescribed number of times, assuming there have been no errors In essence, the loop has been converted from iteration to recursion, where the recursive control is mathematically defined by other recursive state mapping functions Recursion, when compared to iteration, is easier to describe with mathematical rigor
Evaluation of Denotational Semantics USEFUL: for proving the correctness of programs can be an aid to language design RIGOROUS: provides a way to think about programs has been used in compiler generation systems COMPLEX: of little use to language users
Summary BNF and context-free grammars are equivalent meta-languages Well-suited for describing the syntax of programming languages An attribute grammar is a descriptive formalism that can describe both the syntax and the semantics of a language Three primary methods of dynamic semantics description Operation, axiomatic, denotational