25
UNIT 4: SYNTAX DIRECTED DEFINITIONS 1 Sadique Nayeem Asst. Professor Dept. of CSE Sitamarhi Institute of Technology, Sitamarhi

Compiler Design Unit 4. Syntax Directed Definitions › wp-content › uploads › 2020 › 05 › file… · 81,7 6

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

  • UNIT 4: SYNTAX DIRECTED DEFINITIONS

    1

    Sadique NayeemAsst. ProfessorDept. of CSE

    Sitamarhi Institute of Technology, Sitamarhi

  • Compilers need to know…

    Whether a variable has been declared?

    Are there variables which have not been declared?

    What is the type of the variable?

    Whether a variable is a scalar, an array, or a function? Whether a variable is a scalar, an array, or a function?

    If an expression is type consistent?

    How many arguments does a function take?

    Are all invocations of a function consistent with the declaration?

    If an operator/function is overloaded, which function is beinginvoked?

  • Semantic analysis

    Semantic Analysis makes sure that declarations and statements ofprogram are semantically correct.

    It is a collection of procedures which is called by parser as andwhen required by grammar.

    Both syntax tree of previous phase and symbol table are used toBoth syntax tree of previous phase and symbol table are used tocheck the consistency of the given code.

    It verifies “What is the meaning of the program?”

    Harder to give precise mathematical description

    Semantic checking is needed to make sure the program confirms tothe programming language description.

    Provide meaningful error messages to the user.

  • Two types of semantic of PL

    Static semantic

    Dynamic semantic

    Static semantic: Static semantic of a programming Static semantic: Static semantic of a programminglanguage is checked by the semantic analyzer Variables are declared before use

    Types match on both side of assignments

    Parameter types and number match in declaration and use

  • Example: Static semanticint dot_prod(int x[], inty[])

    {

    int d=0, i;

    for(i=0;i

  • Dynamic semantic

    Dynamic semantic: compiler can only generate code to check dynamic semantic of the programming languages at runtime. Whether an overflow will occur during arithmetic operation?

    Whether array limits will be crossed during execution?

    Whether recursion will cross stack limits?

    Whether heap memory will be insufficient?

  • Attributes

    int i;

    i’s value

    i’s type

    i’s location

    The properties of an entity is called as attributes.

    An attribute has a name and an associated value: a string, a number, a type, amemory location, an assigned register, strings.

    The attributes give meaning to the structure in the parse tree such as:

    Data type of an expression or variable

    Value of the constant

    Number and Types of parameter expected by the function

    Memory location of a variable or expression

    Size of an expression or variable

  • Following things are done in Semantic Analysis

    Type checking : The process of verifying and enforcing theconstraints of types is called type checking. This may occur either at compile-time (a static check) or run-time (a

    dynamic check). Static type checking is a primary task of the semanticanalysis carried out by a compiler.

    If type rules are enforced strongly (that is, generally allowing onlythose automatic type conversions which do not lose information), theprocess is called strongly typed, if not, weakly typed.

    Type coercion: If some kind of mixing of types is allowed. It isdone in languages which are not strongly typed. This can be donedynamically as well as statically.

  • Uniqueness checking: Whether a variable name is unique ornot, in its scope.

    Name Checks: Check whether any variable has a name whichis not allowed.is not allowed.

    Disambiguate Overloaded operators: If an operator isoverloaded, one would like to specify the meaning of thatparticular operator because from there one will go into codegeneration phase next.

  • Semantic analysis computes additional information related tothe meaning of the program once the syntactical structure isknown.

    In language ‘C’, semantic analysis involves adding informationto the symbol table and performing type checking.to the symbol table and performing type checking.

    As FSA defines the formalism for the lexical analysis and CFGfor syntax analysis, so, for semantic analysis we need arepresentation formalism and an implementationmechanism.

    So for semantic analysis, “representation formalism” is called“Syntax Directed Translation”

  • Syntax Directed Translation

    The principal of SDT states that the meaning of an input sentence isrelated to its syntactic structure i.e. Parse tree

    By SDT we indicate the formalism for specifying translations forprogramming language construct guided by CFG.

    Here we associate attributes to the grammar symbols representingthe language constructs and values for attributes are computed bysemantic rules associated with grammar productions.

    There are two notations for attaching semantic rules: Semantic Directed Definition(SDD): It is also called as Attribute

    Grammar. Translation Schemes: It indicates order in which semantic rules are

    to be evaluated.

  • Semantic Directed definition

    A syntax-directed definition (SDD) is a context-free grammartogether with attributes and rules.

    SDD generalizes a CFG by associating a set of attributes witheach node in a parse tree.

    Each attribute give some information about the node.Each attribute give some information about the node.

    For example: Attributes associated with an expression nodemay give its Value or its Type or its Memory Location.

    The attributes of the Non Terminal on the LHS are defined asfunctions of attributes of the symbols n RHS

    Example: A →XYZ A.t → f(X.t Y.t Z.t)

  • Attributes

    The properties of an entity are called attributes.

    It gives meaning to the sentence in the parse tree such as

    data type of an expression or variable,

    value of constant,

    Number and type of parameter expected by a fuction

    Memory location

    The attributes associated with the grammar are classified into two categories: Synthesized Attributes

    Inherited Attributes

  • Synthesized Attributes

    If the attributes of the parent depends on the attributes of thechildren then such attributes are called synthesized attributes. OR

    A attribute is said to be synthesized if its value at a parse treenode N is determined from the attributes values at the children ofN and N itself.N and N itself.

    Synthesized attributes have a desirable property that they can beevaluated during a single bottom up traversal of a parse tree.

  • Calculating synthesized attributes

    3*4+5 n

  • Inherited Attributes

    If the attributes of the children depends on the attributes of theparents then such attributes are called Inherited attributes. OR

    Inherited attributes have their value at a parse tree nodedetermined from attribute value at the node itself, its parent andits siblings in the parse tree.its siblings in the parse tree.

  • Calculating Inherited attributes

    id1, id2, id3

  • Evaluating an SDD at the Nodes of a Parse Tree

    Parse tree help us to visualize the translation specified by theSDD.

    The rules of an SDD are applied by first constructing a parsetree and then using the rules to evaluate all of the attributes ateach of the nodes of the parse tree.each of the nodes of the parse tree.

    A Parse tree showing the values of its attributes is called anannotated parse tree.

  • Classes of SDD

    Translations can be implemented using classes of SDD's thatguarantee an evaluation order, since they do not permitdependency graphs with cycles.

    S-Attributed Definitions S-Attributed Definitions

    L-Attributed Definitions

  • S-Attributed Definitions

    An SDD is S-attributed if every attribute is synthesized

    When an SDD is S-attributed, we can evaluate its attributes in any bottom up

    order of the nodes of the parse tree.

    It is often especially simple to evaluate the attributes by performing a post order

    traversal of the parse tree and evaluating the attributes at a node N when thetraversal of the parse tree and evaluating the attributes at a node N when the

    traversal leaves N for the last time.

    postorder(N)

    {for ( each child C of N, from the left )

    postorder(C);evaluate the attributes associated with node N;

    }

    S-attributed definitions can beimplemented during bottom-up parsing,since a bottom-up parse corresponds to apost-order traversal. Specifically, post-order corresponds exactly to the order inwhich an LR parser reduces a productionbody to its head.

  • L-Attributed Definitions

  • S-attributed SDT L-attributed SDT

    If an SDT uses only synthesized attributes, it is called as S-attributed SDT.

    If an SDT uses both synthesized attributes and inherited attributes with a restriction that inherited attribute can inherit values from left siblings only, it is called as L-siblings only, it is called as L-attributed SDT.

    S-attributed SDTs are evaluated in bottom-up parsing, as the values of the parent nodes depend upon the values of the child nodes.

    Attributes in L-attributed SDTs are evaluated by depth-first and left-to-right parsing manner.

    Semantic actions are placed in rightmost place of RHS.

    Semantic actions are placed anywhere in RHS.

  • 25

    THANK YOU!