Parser Uses A CFG
Parser Uses A CFG
produce output for the next phase of the compiler. Output could be either a
parse tree or an abstract syntax tree. Now to interleave semantic analysis
with the syntax analysis phase of the compiler, we use Syntax Directed
Translation.
The above diagram shows how semantic analysis could happen. The flow of
information happens bottom-up and all the children’s attributes are
computed before parents, as discussed above. Right-hand side nodes are
sometimes annotated with subscript 1 to distinguish between children and
parents.
Additional Information
Synthesized Attributes are such attributes that depend only on the
attribute values of children nodes.
Thus [ E -> E+T { E.val = E.val + T.val } ] has a synthesized attribute val
corresponding to node E. If all the semantic attributes in an augmented
grammar are synthesized, one depth-first search traversal in any order is
sufficient for the semantic analysis phase.
Inherited Attributes are such attributes that depend on parent and/or
sibling’s attributes.
Thus [ Ep -> E+T { Ep.val = E.val + T.val, T.val = Ep.val } ], where E & Ep are
same production symbols annotated to differentiate between parent and
child, has an inherited attribute val corresponding to node T.