Attribute Grammar
Principles of Programming Language
S.Venkatesan
Reference: Robert W. Sebesta, Concepts of Programming Languages, 10th Edition, Pearson
Use
• To describe more of the structure of a
programming language that can be described
with a context-free grammar.
• It is an extension to a context-free grammar.
• The extension allows certain language rules to
be conveniently described, such as compatibility.
Static Semantics
• Some characteristics of the structure of the programming
languages that are difficult to describe and some impossible.
• For example, in Java. Assigning float to integer variable but reverse
is legal.
• This can be done with BNF, however it needs additional terminals
and rules. In such case, the grammar of a language will be too
large.
• The size of the grammar determines the size of syntax analyser.
Not possible
• All variables must be declared before they are
referenced.
Static Semantic Rules
• Indirectly related to the meaning of programs during
execution; rather it has to do with the legal forms of
programs (syntax rather than semantics).
• In many languages it is for the type constraints.
• It is named static because to be done at the time compilation.
• To describe static semantics with BNF, attribute grammar was
designed (Knuth 1968).
Attribute Grammar
• For describing and checking the correctness of the static semantic rules of a
program.
• It is a CFG with an added attributes, attribute computation functions, and
predicate functions.
• Attributes are associated with the grammar symbols (T and NT), are similar
to variables in the sense that they can have values assigned to them.
– Attributed computation functions – semantic function associated with the grammar
rules.
– Predicate functions – state the static semantic rules of the language, are associated
with grammar rules.
Features
• Synthesized (actual type) and Inherited
attributes (expected type).
• Semantic functions.
• Predicate function – true if associated NT is
legal and false is illegal.
Attribute Grammar
Parse Tree
Computing Attribute Values
Dynamic Semantics
• This is to create an appropriate intermediate
language.
• Operational semantics – describe the meaning
of the statement.
– Natural
– Structural
Denotational Semantics
• It is based on recursive function theory.
• It has a domain and range
– Doman is the collection of values that are
legitimate parameters to the function.
– The range is the collection of objects to which the
parameters are mapped.
– Syntactic domain - domain
– Semantic domain - range
Difference
• In operational semantics - programming language
constructs are translated into simpler programming
language constructs, which becomes the basis of the
meaning of the construct. Step by step processing of
programs
• In denotational semantics – programming language
constructs are mapped to mathematical objects,
either sets, or more often, functions. No step by step
processing of programs.
Example
Axiomatic Semantics
• Specifies, what can be proven about the program.
• Here, it is more about the relationship of the
variables and constants.
• Applications
– Program Verification
– Program Semantics Specification
Assertions
• Constraints on the program variables at that point in the program.
– Pre and Post Condition
sum = 2 * x + 1 {sum > 1}
• Weakest Pre-condition
• Inference Rule – top->antecedent and bottom -> consequent
S1,S2,S3…Sn
----------------
S
Program Proofs