Presentation is loading. Please wait.

Presentation is loading. Please wait.

Slide 1 Chapter 3 Attribute Grammars. Slide 2 Attribute Grammars Certain language structures cannot be described using EBNF. Attribute grammars are extensions.

Similar presentations


Presentation on theme: "Slide 1 Chapter 3 Attribute Grammars. Slide 2 Attribute Grammars Certain language structures cannot be described using EBNF. Attribute grammars are extensions."— Presentation transcript:

1 Slide 1 Chapter 3 Attribute Grammars

2 Slide 2 Attribute Grammars Certain language structures cannot be described using EBNF. Attribute grammars are extensions of context-free grammars. Attributes are certain characteristics or functions of non-terminals (Eg. Type of an identifier).

3 Slide 3 Examples of certain conditions The type of the left-hand side must be the same as the type of the right-hand side. All variables must be declared before they are referenced. The number of characters in a sequence must be a certain value.

4 Slide 4 Static Semantics Forms of programs and constraints which can be evaluated at compile time. Static semantics mostly deal with type constrains. Syntax rule:  = Semantic rule:.type =.type Attribute Grammar

5 Slide 5 Usefulness of Attribute Grammars Attribute grammars provide more meaningful description than just the form of program units. Attribute grammars can include: –attributes –semantic rules –conditions.

6 Slide 6 An example (Semantic rule) Syntax rule: [1]  + [2] Semantic rule: [1].value =.value + [2].value If there is more than one occurrence of a non-terminal, they are subscripted with brackets to distinguish them.

7 Slide 7 An example (Condition) A grammar for sentances of the form: abc aabbcc aaabbbccc …   {a}  {b}  {c} Condition:.size =.size =.size

8 Slide 8 Categories of Attributes (1) Synthesized Attributes –LHS attributes depend on RHS attributes –These attributes pass information up the parse tree from children to the root –Usually initialized at the leaf nodes.

9 Slide 9 Categories of Attributes (2) Inherited Attributes –RHS attributes depend on LHS attributes –These attributes pass information down the parse tree from root to the children –Usually initialized at the root.

10 Slide 10 Example (Synthesized Attribute) A simple expression:

11 Slide 11 Parse Tree with a Synthesized Attribute B*(C+A)

12 Slide 12 Example (Inherited Attribute) A positive integer:

13 Slide 13 Parse Tree with an Inherited Attribute 825

14 Slide 14 Dynamic Semantics Attribute grammars are inadequate in describing the meaning of program units Dynamic semantics (or simply, “Semantics”) refers to the description of the meaning of expressions, statements and program units. There is no universally accepted notation.

15 Slide 15 Dynamic Semantics Informal Semantics Translational Semantics Operational Semantics Denotational Semantics

16 Slide 16 Informal Semantics Usually defined informally in English, by attaching explanations and examples to syntax rules. Commonly used in reference manuals. Eg: if ( ) Semantics: will be executed only when evaluates to a non-zero value.

17 Slide 17 Translational Semantics Meaning of language constructs defined in terms of what the program does at the lower level of code. Based on the fact that the semantics is preserved when a language is translated to a low level code. The translated language contains less number of constructs and is closely related to the machine architecture.

18 Slide 18 Translational Semantics (Example) if m <> n then n := n  m m := m+5 LOAD M SUB N JF L3 LOAD N SUB M STO N L3LABEL LOAD M ADD #5 STO M

19 Slide 19 Operational Semantics The main problem with translational semantics is that the low level instructions are often difficult to understand. Operational semantics replaces the real computer with a virtual computer, where the execution flow is simulated.

20 Slide 20 Operational Semantics (Example) if m<>n then n := n  m m := m+5 if (m-n=0) goto L1 n  n - m L1:m  m + 5


Download ppt "Slide 1 Chapter 3 Attribute Grammars. Slide 2 Attribute Grammars Certain language structures cannot be described using EBNF. Attribute grammars are extensions."

Similar presentations


Ads by Google