Upload
wilfred-bishop
View
217
Download
0
Embed Size (px)
Citation preview
March 5, 2004 1 ICE 1341 – Programming Languages (Lecture #4) In-Young Ko
Programming LanguagesProgramming Languages(ICE 1341)(ICE 1341)
Lecture #4Lecture #4 March 5, 2004
In-Young Koiko .AT. icu.ac.kr
Information and Communications University (ICU)
March 5, 2004 2 ICE 1341 – Programming Languages (Lecture #4) In-Young Ko
AnnouncementsAnnouncements
Confirming the class registrationConfirming the class registration Rescheduling of the class on March 10Rescheduling of the class on March 10 thth
Monday, March 8Monday, March 8thth 4:00pm-5:00pm, L401 4:00pm-5:00pm, L401
March 5, 2004 3 ICE 1341 – Programming Languages (Lecture #4) In-Young Ko
Review of the Previous LectureReview of the Previous Lecture
Compilation ProcessCompilation Process Syntax and SemanticsSyntax and Semantics Formal Ways to Define LanguagesFormal Ways to Define Languages
Language RecognizersLanguage Recognizers Language GeneratorsLanguage Generators
Context-free GrammarsContext-free Grammars Chomsky HierarchyChomsky Hierarchy
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
e.g.,e.g., Grammar:Grammar: <expr> <expr> <expr> <op> <expr> | const <expr> <op> <expr> | const
<op> <op> // | | ––
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) ?
e.g.,e.g., Grammar:Grammar: <expr> <expr> const <op> <expr> | const const <op> <expr> | const
<op> <op> // | | ––
<expr>
<expr>
<expr>
<op><op>
<op>
const const const- /
Then, how about the case Then, how about the case of parsing the following of parsing the following sentence?sentence?
cconst / const - constonst / const - const
* AW Lecture Notes
March 5, 2004 6 ICE 1341 – Programming Languages (Lecture #4) In-Young Ko
Operator PrecedenceOperator Precedence
Specify Specify operator orderingsoperator orderings in a grammar to in a grammar to prevent the grammar ambiguityprevent the grammar ambiguity
<expr>
<expr> <term>
<term> <term>
const const const/–
e.g.,e.g., Grammar:Grammar: <expr> <expr> <expr> <expr> –– <term> | <term> <term> | <term> <term> <term> <term> / const | const <term> / const | const
Sentence to parse: Sentence to parse: const const –– const / cont const / cont
Derivation:Derivation:<expr> <expr> <expr> <expr> –– <term> <term> <term> <term> –– <term> <term> const const –– <term> <term> const const –– <term> / const <term> / const const const –– const / const const / const
* AW Lecture Notes
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
e.g.2, <expr> e.g.2, <expr> const ** <expr> | const const ** <expr> | const
e.g.,e.g., Grammar:Grammar: <expr> <expr> <expr> <expr> ++ <expr> | const <expr> | const Sentence to parse: Sentence to parse: const const ++ const + cont const + cont
AmbiguousAmbiguous Can it be solved by specifying operator precedence?Can it be solved by specifying operator precedence?
March 5, 2004 8 ICE 1341 – Programming Languages (Lecture #4) In-Young Ko
Extended BNF (EBNF)Extended BNF (EBNF)
Optional partsOptional partsee.g., <selection> .g., <selection> if (<expression>) <statement> [ else <statement>] if (<expression>) <statement> [ else <statement>]
RepetitionRepetitionee.g., <ident_list> .g., <ident_list> <identifier> {, <identifier>} <identifier> {, <identifier>}
Multiple choicesMultiple choicesee.g., <term> .g., <term> <term> ( * | / | % ) <factor> <term> ( * | / | % ) <factor>
BNFBNF <expr> <expr> <expr> + <term> <expr> + <term>
| <expr> | <expr> –– <term> <term> | <term>| <term> <term> <term> <term> * <factor> <term> * <factor> | <term> / | <term> /
<factor><factor> | <factor>| <factor>
EBNFEBNF <expr> <expr> <term> {(+ | -) <term>} <term> {(+ | -) <term>}
<term> <term> <factor> {(* | /) <factor>} <factor> {(* | /) <factor>}
* AW Lecture Notes
March 5, 2004 9 ICE 1341 – Programming Languages (Lecture #4) In-Young Ko
Attribute Grammars Attribute Grammars –– Problem DescriptionProblem Description
Example Problem: Example Problem: Type Compatibility RuleType Compatibility Rule<assign> <assign> <var> = <expr> <var> = <expr>
<expr> <expr> <var> + <var> | <var> <var> + <var> | <var>
<var> <var> A | B | C A | B | C
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
Attribute Grammars Attribute Grammars ––DefinitionDefinition
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
Attributes: Attributes: actual_typeactual_type (synthesized attr.), (synthesized attr.), expected_typeexpected_type (inherited attr.) (inherited attr.)
1.1. Syntax rule:Syntax rule: <assign> <assign> <var> = <expr> <var> = <expr>Semantic rule:Semantic rule: <expr>. <expr>.expected_typeexpected_type <var>. <var>.actual_typeactual_type
2.2. Syntax rule:Syntax rule: <expr> <expr> <var>[2] + <var>[3] <var>[2] + <var>[3]Semantic rule:Semantic rule: <expr>. <expr>.actual_typeactual_type
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
Predicate:Predicate: <expr>. <expr>.actual_typeactual_type = <expr>. = <expr>.expected_typeexpected_type3.3. Syntax rule:Syntax rule: <expr> <expr> <var> <var>
Semantic rule:Semantic rule: <expr>. <expr>.actual_typeactual_type <var>. <var>.actual_typeactual_typePredicate:Predicate: <expr>. <expr>.actual_typeactual_type = <expr>. = <expr>.expected_typeexpected_type
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
Computing Attribute Values Computing Attribute Values –– Evaluating AttributesEvaluating Attributes
Sentence: Sentence: A = A + BA = A + B
1. <var>.1. <var>.actual_typeactual_type look-up(A) (Rule 4) look-up(A) (Rule 4)
2. <expr>.2. <expr>.expected_typeexpected_type <var>. <var>.actual_typeactual_type (Rule 1) (Rule 1)
3. <var>[2].3. <var>[2].actual_typeactual_type look-up(A) (Rule 4) look-up(A) (Rule 4)
<var>[3].<var>[3].actual_typeactual_type look-up(B) (Rule 4) look-up(B) (Rule 4)
4. <expr>.4. <expr>.actual_typeactual_type either int or real (Rule 2)either int or real (Rule 2)
5. <expr>.5. <expr>.expected_typeexpected_type = =
<expr>.<expr>.actual_typeactual_type is is
either TRUE or FALSE (Rule 2)either TRUE or FALSE (Rule 2)
Grammar:Grammar:<assign> <assign> <var> = <var> = <expr><expr><expr> <expr> <var> + <var> | <var> + <var> | <var> <var><var> <var> A | B | C A | B | C
March 5, 2004 13 ICE 1341 – Programming Languages (Lecture #4) In-Young Ko
Computing Attribute Values Computing Attribute Values –– Flow of Attributes in a Parse TreeFlow of Attributes in a Parse Tree
An Inherited An Inherited AttributeAttribute
A Synthesized A Synthesized AttributeAttribute
An Intrinsic An Intrinsic AttributeAttribute
March 5, 2004 14 ICE 1341 – Programming Languages (Lecture #4) In-Young Ko
Computing Attribute Values Computing Attribute Values –– Fully Attributed Parse TreeFully Attributed Parse Tree
March 5, 2004 15 ICE 1341 – Programming Languages (Lecture #4) In-Young Ko
Homework #2Homework #2
Chapter 3 Problem Set (pp. 157 Chapter 3 Problem Set (pp. 157 –– 158) 158) #2 #2 –– a, b a, b #5#5 #6 #6 –– a a #8#8 #13#13 #14#14