140 likes | 329 Views
3.2 Semantics . Semantics. Attribute Grammars The Meanings of Programs: Semantics Sebesta Chapter 3. Attribute Grammars. CFG cannot describe all necessary information An attribute grammar is a CFG G = (S, N, T, P) with the following additions:
E N D
Semantics • Attribute Grammars • The Meanings of Programs: Semantics • Sebesta Chapter 3
Attribute Grammars • CFG cannot describe all necessary information • An attribute grammar is • a CFG G = (S, N, T, P) with the following additions: • For each grammar symbol xthere is a set A(x) of attribute values • Each rule has a set of functionsthat 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 (cont.) • Invented by Donald Knuth • Can carry complex syntactic and some semantic information along through parse trees • e.g. complex syntax rule (static semantics): a variable must be defined before it is used • Primary value of AGs: • static semantics specification • compiler design (static semantics checking)
Describing Semantics • There is no single widely accepted notation or formalism for semantics • Different formalisms are used: • Operational Semantics • Axiomatic Semantics • Denotational Semantics
Operational Semantics • Describes the meaning of a program by executing its statements on a machine, either simulated or actual. • The meaning of a statement is defined by the change in the state of the machine (memory, registers, etc.)
Operational Semantics Limitations • A virtual machine is needed • A hardware pure interpreter • too expensive for a high-level language • A software pure interpreter • too difficult to understand the actions because of the detailed characteristics of the particular computer used • machine-dependent
Operational Semantics • A better alternative: A complete computer simulation • The process: • Build a translator which translates source code to the machine code of an idealized computer • Build a simulator for the idealized computer • Evaluation of operational semantics: • Good if used informally (language manuals, etc.) • Extremely complex if used formally (e.g., VDL)
Axiomatic Semantics • Based on formal logic - predicate calculus • Original purpose: formal program verification • Approach: • Define axioms or inference rules for each statement type in the language (to allow transformations of expressions to other expressions) • The expressions are called assertions
Prolog • Prolog – logic programming language • Axiomatic semantics • Contains facts (assertions) • Contains inference rules • Inference process answers a query by generating new facts from known facts and rules • We will learn Prolog in this course to practice the logic programming paradigm
Axiomatic Semantics Evaluation • Good for correctness proofs and for reasoning about programs • Too difficult to develop axioms and inference rules for all of the statements in a high-level language • Too difficult to understand the axioms and rules • It is not very useful for • language design • compiler/interpreter writers • programmers
Denotational Semantics • Based on recursive function theory • The process: • 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 the values of the program's variables • The state changes are defined by mathematical functions • (operational semantics defines them in program code)
Denotational Semantics Evaluation • The most abstract method of describing semantics • Can be used to prove the correctness of programs • Provides a rigorous way to think about programs • Can aid language design • Has been used in compiler generators • Too complex to be useful to programmers
Summary • EBNF is an established formalism to define the syntax of PLs • There is no single commonly used way to define the semantics – all formalisms have disadvantages