Chapter 3 Attribute Grammars 1 Attribute Grammars Certain

  • Slides: 20
Download presentation
Chapter 3 • Attribute Grammars 1

Chapter 3 • Attribute Grammars 1

Attribute Grammars • Certain language structures cannot be described using EBNF. • Attribute grammars

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). 2

Examples of certain conditions • The type of the left-hand side must be the

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. 3

Static Semantics • Forms of programs and constraints which can be evaluated at compile

Static Semantics • Forms of programs and constraints which can be evaluated at compile time. • Static semantics mostly deal with type constrains. Syntax rule: <assign> <var> = <expr> Semantic rule: <var>. type = <expr>. type Attribute Grammar 4

Usefulness of Attribute Grammars • Attribute grammars provide more meaningful description than just the

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. 5

An example (Semantic rule) Syntax rule: <expr>[1] <ident> + <expr>[2] Semantic rule: <expr>[1]. value

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

An example (Condition) A grammar for sentances of the form: abc aabbcc aaabbbccc …

An example (Condition) A grammar for sentances of the form: abc aabbcc aaabbbccc … <sequence> <a_seq> <b_seq> <c_seq> <a_seq> {a} <b_seq> {b} <c_seq> {c} Condition: <a_seq>. size = <b_seq>. size = <c_seq>. size 7

Categories of Attributes (1) • Synthesized Attributes – LHS attributes depend on RHS attributes

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. 8

Categories of Attributes (2) • Inherited Attributes – RHS attributes depend on LHS attributes

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. 9

Example (Synthesized Attribute) A simple expression: 10

Example (Synthesized Attribute) A simple expression: 10

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

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

Example (Inherited Attribute) A positive integer: 12

Example (Inherited Attribute) A positive integer: 12

Parse Tree with an Inherited Attribute 825 13

Parse Tree with an Inherited Attribute 825 13

Dynamic Semantics • Attribute grammars are inadequate in describing the meaning of program units

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. 14

Dynamic Semantics • Informal Semantics • Translational Semantics • Operational Semantics • Denotational Semantics

Dynamic Semantics • Informal Semantics • Translational Semantics • Operational Semantics • Denotational Semantics 15

Informal Semantics • Usually defined informally in English, by attaching explanations and examples to

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

Translational Semantics • Meaning of language constructs defined in terms of what the program

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. 17

Translational Semantics (Example) if m <> n then m : = m+5 n :

Translational Semantics (Example) if m <> n then m : = m+5 n : = n m L 3 LOAD M SUB N JF L 3 LOAD N SUB M STO N LABEL LOAD M ADD #5 STO M 18

Operational Semantics • The main problem with translational semantics is that the low level

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. 19

Operational Semantics (Example) if m<>n m : = m+5 L 1: then n :

Operational Semantics (Example) if m<>n m : = m+5 L 1: then n : = n m if (m-n=0) goto L 1 n n - m m m + 5 20