Semantic analysis Flashcards
what grammar symbols associated with?
attributes
how are these attributes evaluated?
semantic rules
Evaluation of these semantic rules depends on :
Generate intermediate codes put information on the symbol table perform type checking issues error messages perform almost any activities
A_________checks the source program for semantic errors
semantic analyzer
A semantic analyzer collects information for ____________
Code Generation
________________ is an important part of the semantic analyzer
Type-checking
SDT
Syntax directed translation
how are attributes stored in SDT?
The attributes are attached to the grammar symbols
How are the values of the attributes calculated?
Semantic rules associated with grammar productions
The flow of the semantic compiler
input string->parse tree-> dependency graph->semantic rule evaluation order
Attribute values may represent :
Numbers strings memory locations A data type for checking expressions scoping information for declaration A data type for checking expressions scoping information for local declarations Intermediate program representations
Name two ways to represent semantic rules:
Syntax directed definitions.
& Translation Schemes
SDD are ____ level
Translation schemes are closer to an _____________
higher implementation
Explain SDD
syntax-directed definitions
Each production of A->alpha
is associated with the set of semantic rules
b=f(c1,c2,…….cn) where f is a function
b=> is synthesized attribute of A and c1,c2,c3,,,,,,,,,,cn are attributes of the grammar symbols in the production
b=>inherited attribute,one of the grammar symbols in aplha are attributes of the grammar symbols in the production (A-> alpha)
in either of the cases, we say b depends on c1,c2,c3,,,,,,,,,,,ck.
what are Synthesized attributes
Synthesized attributes are the depends only on the attributes of the children
They are the most common attribute type
Synthesized attributes never take values from their parents nodes or any sibling nodes
the non terminal concerned must be in the head of the production
Terminals have synthesised attributes which are the lexical values
what has Inherited attributes?
An Attributes’ node ‘s parents and/or siblings .They cannot be defined in terms of attributes value at children nodes
Inherited attributes are often used in compilers for passing contextual information forward ,for example,the type keyword in a variable declaration statement
Attribute Grammar
A semantic rule=f(c1,c2,c3,,,,,,,,,cn) indicates that the attribute b depends on attributes c1,c2,…………cn.
Syntax-directed definition a semantic rule may just evaluate a value of an attribute it it may have some side effects such as printing values
Side effects
Side effects:
Evaluation of semantic rules may generate intermediate codes,may perform type checking and may issue error messages.
These are known as side effects
what are semantic rules with controlled side effects
Attribute grammars have no side
effects and allow any evaluation order consistent with dependency graph
whereas translation schemes impose left-to-right evaluation and allow
schematic actions to contain any program fragment
ways to control side effects:
- Permit incidental side effects that do not disturb attribute evaluation.
- Impose restrictions on allowable evaluation orders, so that the same
translation is produced for any allowable order
what is the need for a dependency graph?
An SDD with both and synthesized attributes does not ensure any guaranteed order ,even it may not have an order at all
non terminals A and B with synthesized and inherited attributes A.s and B.i respectively along with the production and rules as:
Production
A →B
Semantic Rule
A.s = B.i
B.i= A.s+1
what is the Dependency graph?
1.Dependency graph predicts the flow of information among the attribute instances in a particular parse tree
2.The inter dependencies among the inherited and synthesized attributes
at the node in the parse tree is depicted by dependency graph.
Visualize a Dependency Graph
- Each attribute is a node
2. We add edges from the node for attribute c to the node for the attribute