Semantic Analysis Flashcards
(35 cards)
What are the two kinds of semantic (contextual) constraints?
Static and Dynamic
A static semantic constraint can be checked by…
a compiler performing static analysis.
How can a dynamic semantic (contextual) constraint be checked?
It can be checked by runtime code generated by a compiler.
What are some examples of static context constraints?
- Variables must be declared before used.
- Variables cannot be declared more than once in the same program unit.
- In x = y, x and y must be compatible.
- Every function must contain at least one return statement.
Static Analysis
Constraints can be described in terms of ______ or ______.
- Annotation
- Decoration of a Syntax Tree
What is the name of the compiler component that checks for constraints?
Semantic Analyzer
Do context free grammars impose contextual constraints on the sentences?
No.
Symbols in the grammar are associated with…
attributes.
Production rules in a grammar are associated with…
semantic rules indicating how the attribute values are computed.
What do attributes in a grammar do?
Capture correspondence between characters and mathematical values.
Example
Attribute val: The mathematical value that corresponds to the digit.
digit = 0 (character)
digit.val = 0 (digit)
What do conditions in a grammar do?
Constrain the sentences to those whose legal values are in a fixed and finite range.
What can semantic rules do?
Tells us how to compute the values of the attributes from the characters and other attributes.
What are synthesized attributes?
Attibute values that are calculated in productions where the associated symbol appears on the left hand side.
Dependencies point from child to parent in the parse tree.

(T/F) In inherited attributes, information always flow from child to parent in the parse tree.
False
Information may flow to a node from the parent OR siblings.

Analogous with CFG, attribute grammars are declarative.
Explain.
AGs define a set of valid trees, but don’t specify how to build or decorate them just like CFG define valid sentences, but not the parsing algorithm.
What are some characteristics of a well-defined attribute grammar?
They have rules that determine a unique set of values for every possible parse tree.
What are some characteristics of a non-circular attribute grammar?
A grammar that never yields a parse tree with cycles in the attribute flow graph.
What is a translation scheme?
An algorithm that decorates a tree in an order that respects the AGs attribute flow.
Describe Most Obvious Scheme
- Works with any well-defined AG, even circular
- Repeated passes over tree
- Compute attribute at each pass
- Terminate when values no longer change
- Oblivious
- Doesn’t use any knowledge of structure of tree or grammar
How do dynamic scheme translators work?
They tailor the evaluation order to the structure of the given parse tree.
The fastest translations schemes tend to be _______.
Static
What are S-Attributed grammars?
All attributes are synthesized.
How can S-attributed AGs be computed?
They can be computed by a bottom-up traversal of the syntax tree.
What is an L-Attributed grammar?
This occurs when A.s depends on B.t if B.t is ever passed to a semantic function that returns a value for A.s.






