COP4020 Programming Languages

Embed Size (px)

DESCRIPTION

COP4020 Programming Languages. Semantics Prof. Xin Yuan. Overview. Static semantics Dynamic semantics Attribute grammars Abstract syntax trees. Static Semantics. Syntax concerns the form of a valid program, while semantics concerns its meaning - PowerPoint PPT Presentation

Text of COP4020 Programming Languages

  • COP4020Programming LanguagesSemanticsProf. Xin Yuan

    COP4020 Spring 2011

  • COP4020 Spring 2014**OverviewStatic semantics

    Dynamic semantics

    Attribute grammars

    Abstract syntax trees

    COP4020 Spring 2014

  • COP4020 Spring 2014**Static SemanticsSyntax concerns the form of a valid program, while semantics concerns its meaningContext-free grammars are not powerful enough to describe certain rules, e.g. checking variable declaration with variable useStatic semantic rules are enforced by a compiler at compile timeImplemented in semantic analysis phase of the compilerExamples:Type checkingIdentifiers are used in appropriate contextCheck subroutine call argumentsCheck labels

    COP4020 Spring 2014

  • COP4020 Spring 2014**Dynamic SemanticsDynamic semantic rules are enforced by the compiler by generating code to perform the checks at run-timeExamples:Array subscript values are within boundsArithmetic errorsPointers are not dereferenced unless pointing to valid objectA variable is used but hasn't been initializedSome languages (Euclid, Eiffel) allow programmers to add explicit dynamic semantic checks in the form of assertions, e.g. assert denominator not= 0When a check fails at run time, an exception is raised

    COP4020 Spring 2014

  • COP4020 Spring 2014**Attribute GrammarsAn attribute grammar connects syntax with semanticsEach grammar production has a semantic rule with actions (e.g. assignments) to modify values of attributes of (non)terminalsA (non)terminal may have any number of attributesAttributes have values that hold information related to the (non)terminalGeneral form: Semantic rules are used by a compiler to enforce static semantics and/or to produce an abstract syntax tree while parsing tokensSyntax directed translation.Can also be used to build simple language interpretersproductionsemantic rule ::= A.a := ...; B.a := ...; C.a := ...

    COP4020 Spring 2014

  • COP4020 Spring 2014**Example Attributed GrammarThe val attribute of a (non)terminal holds the subtotal value of the subexpressionNonterminals are indexed in the attribute grammar to distinghuish multiple occurrences of the nonterminal in a production

    productionsemantic rule ::= + E1.val := E2.val + T.val::= - E1.val := E2.val - T.val::= E.val := T.val::= * T1.val := T2.val * F.val::= / T1.val := T2.val / F.val::= T.val := F.val::= - F1.val := -F2.val::= ( )F.val := E.val::= unsigned_intF.val := unsigned_int.val

    COP4020 Spring 2014

  • COP4020 Spring 2014**Decorated Parse Trees

    A parser produces a parse tree that is decorated with the attribute values

    Example decorated parse tree of (1+3)*2 with the val attributes

    COP4020 Spring 2014

  • Synthesized and inherited attributesEach grammar production A->a is associated with a set of semantic rules of the form b=f(c1, c2, , ck)

    If b is an attributed associated with A, it is called a synthesized attribute.If b is an attributed associated with a grammar symbol on the right side of the production, b is called an inherited attribute.

    COP4020 Spring 2014**

    COP4020 Spring 2014

  • COP4020 Spring 2014**Synthesized AttributesSynthesized attributes of a node hold values that are computed from attribute values of the child nodes in the parse tree and therefore information flows upwardsproductionsemantic rule ::= + E1.val := E2.val + T.val

    COP4020 Spring 2014

  • COP4020 Spring 2014**Inherited AttributesInherted attributes of child nodes are set by the parent node or sibling nodes and therefore information flows downwards

    production semantic rulesD ->T L L.in = T.typeT->int T.type = integerT->real T.type = realL->L1, id L1.in = L.in, addtype(id.entry, L.in)L->id addtype(id.entry, L.in)

    real id1, id2, id3

    COP4020 Spring 2014

  • COP4020 Spring 2014**Inherited AttributesAnother exampleproductionsemantic rule::= TT.st := T.val; E.val := TT.val::= + TT2.st := TT1.st + T.val; TT1.val := TT2.val::= TT.val := TT.st

    COP4020 Spring 2014

  • COP4020 Spring 2014**Attribute FlowAn attribute flow algorithm propagates attribute values through the parse tree by traversing the tree according to the set (write) and use (read) dependencies (an attribute must be set before it is used)productionsemantic rule ::= TT.st := T.val

    COP4020 Spring 2014

  • COP4020 Spring 2014**Attribute FlowAn attribute flow algorithm propagates attribute values through the parse tree by traversing the tree according to the set (write) and use (read) dependencies (an attribute must be set before it is used)productionsemantic rule ::= + TT2.st := TT1.st + T.val

    COP4020 Spring 2014

  • COP4020 Spring 2014**Attribute FlowAn attribute flow algorithm propagates attribute values through the parse tree by traversing the tree according to the set (write) and use (read) dependencies (an attribute must be set before it is used)productionsemantic rule ::= TT.val := TT.st

    COP4020 Spring 2014

  • COP4020 Spring 2014**Attribute FlowAn attribute flow algorithm propagates attribute values through the parse tree by traversing the tree according to the set (write) and use (read) dependencies (an attribute must be set before it is used)productionsemantic rule ::= + TT1.val := TT2.val

    COP4020 Spring 2014

  • COP4020 Spring 2014**Attribute FlowAn attribute flow algorithm propagates attribute values through the parse tree by traversing the tree according to the set (write) and use (read) dependencies (an attribute must be set before it is used)productionsemantic rule ::= E.val := TT.val

    COP4020 Spring 2014

  • COP4020 Spring 2014**S- and L-Attributed GrammarsA grammar is called S-attributed if all attributes are synthesized

    Production semantic rulesL ->E n print(E.val)E->E1 + T E.val = E1.val + T.valE->T E.val = T.valT->T1 * F T.val = T1.val * F.valT->F T.val = F.valF->(E) F.val = E.valF->digits F.val = digits.lexval

    COP4020 Spring 2014

  • COP4020 Spring 2014**S- and L-Attributed GrammarsA grammar is called L-attributed if the parse tree traversal to update attribute values is always left-to-right and depth-firstFor a production A -> X1 X2 X3 XnThe attributes of Xj (1
  • COP4020 Spring 2014**Example L-Attributed GrammarImplements a calculatorproductionsemantic rule ::= ::= + ::= - ::= ::= ::= * ::= / ::= ::= - ::= ( ) ::= unsigned_intTT.st := T.val; E.val := TT.valTT2.st := TT1.st + T.val; TT1.val := TT2.valTT2.st := TT1.st - T.val; TT1.val := TT2.valTT.val := TT.stFT.st := F.val; T.val := FT.valFT2.st := FT1.st * F.val; FT1.val := FT2.valFT2.st := FT1.st / F.val; FT1.val := FT2.valFT.val := FT.stF1.val := -F2.valF.val := E.valF.val := unsigned_int.val

    COP4020 Spring 2014

  • COP4020 Spring 2014**Constructing Abstract Syntax Trees with Attribute Grammars

    Three operations to create nodes for an AST tree that represents expressions:mk_bin_op(op, left, right): constructs a new node that contains a binary operator op and AST sub-trees left and right representing the operators operands and returns pointer to the new nodemk_un_op(op, node): constructs a new node that contains a unary operator op and sub-tree node representing the operators operand and returns pointer to the new nodemk_leaf(value): constructs an AST leaf that contains a value and returns pointer to the new node

    COP4020 Spring 2014

  • Constructing AST with attribute grammarE->E1+ E2 E.ptr = mk_bin_op(+, E1.ptr, E2.ptr);E->E1 E2 E.ptr = mk_bin_op(-, E1.ptr, E2.ptr);E->E1* E2 E.ptr = mk_bin_op(*, E1.ptr, E2.ptr);E->E1 / E2 E.ptr = mk_bin_op(/, E1.ptr, E2.ptr);E->(E1) E.ptr = E1.ptrE-> -E1 E.ptr = mk_un_op(-, E1.ptr);E->number E.ptr = mk_leaf(number.val);

    COP4020 Spring 2014**

    COP4020 Spring 2014

    COP4020 Fall 2006*COP4020 Fall 2006