March 5, 2004 1 ICE 1341 – Programming Languages (Lecture #4) In-Young Ko Programming Languages Programming Languages (ICE 1341) (ICE 1341) Lecture #4 Lecture #4 March 5, 2004 In-Young Ko iko .AT. i cu . ac.kr Information and Communications University (ICU)
15
Embed
March 5, 2004 1 ICE 1341 – Programming Languages (Lecture #4) In-Young Ko Programming Languages (ICE 1341) Lecture #4 Programming Languages (ICE 1341)
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
March 5, 2004 1 ICE 1341 – Programming Languages (Lecture #4) In-Young Ko
March 5, 2004 4 ICE 1341 – Programming Languages (Lecture #4) In-Young Ko
Ambiguity in a GrammarAmbiguity in a Grammar
A grammar is A grammar is ambiguousambiguous iff it generates a sentential form that has iff it generates a sentential form that has two or more distinct parse treestwo or more distinct parse trees
The meaning (semantics) of an ambiguous grammar cannot be The meaning (semantics) of an ambiguous grammar cannot be determined uniquelydetermined uniquely
Sentence to parse: Sentence to parse: const const –– const / cont const / cont
<expr>
<expr> <expr>
<expr> <expr><op>
const const const– /
<op>
<expr>
<expr> <expr>
<expr> <expr>
<op><op>
<op>
const const const– /
* AW Lecture Notes
March 5, 2004 5 ICE 1341 – Programming Languages (Lecture #4) In-Young Ko
How to Remove Ambiguity?How to Remove Ambiguity?
Make the grammar grow only on one direction Make the grammar grow only on one direction (similar to a regular grammar) ?(similar to a regular grammar) ?
March 5, 2004 7 ICE 1341 – Programming Languages (Lecture #4) In-Young Ko
Associativity of OperatorsAssociativity of Operators
A A left recursiveleft recursive grammar (e.g., S grammar (e.g., S Sw) specifies Sw) specifies left associativityleft associativitye.g., <expr> e.g., <expr> <expr> <expr> ++ const | const const | const
AA right recursive grammar right recursive grammar (e.g., S (e.g., S wS) specifies wS) specifies right associativityright associativitye.g.1, <expr> e.g.1, <expr> const const ++ <expr> | const <expr> | const
The type of an expression The type of an expression when the operand types are not the when the operand types are not the samesame is always real. is always real.
When they are the sameWhen they are the same, the expression type is that of the , the expression type is that of the operands.operands.
The type of the left side of the assignment must The type of the left side of the assignment must match the typematch the type of the right side.of the right side.
This rule represents the This rule represents the static semanticsstatic semantics that needs to that needs to be checked at compile time (c.f., dynamic semantics)be checked at compile time (c.f., dynamic semantics)
This rule cannot be represented in BNFThis rule cannot be represented in BNF
March 5, 2004 10 ICE 1341 – Programming Languages (Lecture #4) In-Young Ko
Designed by Designed by Donald E.Donald E. KnuthKnuth, 1968, 1968 A formal approach to both A formal approach to both describing and checking the describing and checking the
correctness of the static semanticscorrectness of the static semantics of a program of a program Main elementsMain elements
AttributesAttributes: associated with grammar symbols: associated with grammar symbols Synthesized AttributesSynthesized Attributes: computed based on the attributes in a : computed based on the attributes in a
parse treeparse tree Inherited AttributesInherited Attributes: inherited from parents in a parse tree: inherited from parents in a parse tree Intrinsic AttributesIntrinsic Attributes: synthesized attributes of leaf nodes: synthesized attributes of leaf nodes
Attribute computation functionsAttribute computation functions ( (semantic functionssemantic functions): ): associated with grammar rules (productions)associated with grammar rules (productions)
Predicate functionsPredicate functions: syntactic and semantic rules of a : syntactic and semantic rules of a language, associated with grammar ruleslanguage, associated with grammar rules
March 5, 2004 11 ICE 1341 – Programming Languages (Lecture #4) In-Young Ko
Attribute Grammars Attribute Grammars ––An ExampleAn Example
if (<var>[2].if (<var>[2].actual_typeactual_type = int) and = int) and (<var>[3].(<var>[3].actual_typeactual_type = int) then int = int) then intelse realelse realendifendif
4.4. Syntax rule:Syntax rule: <var> <var> A | B | C A | B | CSemantic rule:Semantic rule: <var>. <var>.actual_typeactual_type look-up(<var>. look-up(<var>.stringstring))
March 5, 2004 12 ICE 1341 – Programming Languages (Lecture #4) In-Young Ko