190 likes | 438 Views
Chapter 4: Language Semantics. The need for language semantics: a. for the programmer - to know how to use language constructs b. for the implementor - to know how to implement the language. Semantics.
E N D
Chapter 4: Language Semantics The need for language semantics: a.for the programmer - to know how to use language constructs b.for the implementor - to know how to implement the language
Semantics Semantics:the study or analysis of the relationships between linguistic symbols and their meanings
Semantic descriptions Informal descriptionsof language semantics - provided in verbal form in the language manuals. May be ambiguous. Formal description: studied theoretically, no satisfactory models have been produced so far.
Formal semantic models • Grammatical models • Operational (imperative) models • Axiomatic models • Applicative (functional) models • Specification models
Grammatical models ·grammar rules are paired with semantic rules. Resulting grammars are called attribute grammars Rule Attribute E E + T value (E1) = value (E2) + value(T) E T value (E) = value (T) T T * P value (T1) = value (T2) * value (P) T P value (T) = value (P) P I value (P) = value of number I P (E) value (P) = value (E)
Operational models Describe the meaning of the language constructs in terms of machine states, i.e. memory and register contents. Operational semantics – describes what has to be done.
Applicative models • Treat programs as functions. • Denotational semantics (functional semantics) • Lambda calculus
Denotational semantics • Specifies the meaning of expressions by their denotation • The domain of interpretation defines the denotation of composite expressions in terms of the denotations of their components. • -> Compositional semantics
Lambda calculus Formal mathematical system devised by Alonzo Church to investigate functions, function application and recursion
Example illustrating lambda calculus A function accepts input and produces an output. Suppose we have a "chocolate-covering" function that produces the following outputs for the corresponding inputs: INPUT OUTPUT peanuts -> chocolate-covered peanuts raisins -> chocolate-covered raisins ants -> chocolate-covered ants
Lambda functions We can use Lambda-calculus to describe such a function: Lx . chocolate - covered x If we want to apply the function to an argument, we use the following syntax: (Lx.chocolate-covered x)peanuts -> chocolate-covered peanuts
Using Lambda expressions to get new functions Functions can also be the result of applying a lambda-expression Ly.Lx. y - covered x We can use this to create a caramel-covering function: (Ly.Lx.y-covered x) caramel -> Lx. caramel-covered x
Functions as inputs Functions can also be the inputs to other functions, as with this "apply-to-ants" function: Lf.(f) ants We can feed the chocolate-covering function to the "apply-to-ants" function: (Lf.(f)ants)Lx.chocolate-covered x -> (Lx.chocolate-covered x)ants -> chocolate-covered ants
Axiomatic models Describe the meaning as pre-conditions and post-conditions Used in program verifications
Specification models Describe the relationship among various functions that implement a program. Example of a specification model: Algebraic data types - describe the meaning in terms of algebraic operations, e.g. pop(push(S,x)) = S