1 / 20

Chapter 3

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

love
Download Presentation

Chapter 3

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 3 • Attribute Grammars

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

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

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

  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. 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. Example (Synthesized Attribute) A simple expression:

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

  12. Example (Inherited Attribute) A positive integer:

  13. Parse Tree with an Inherited Attribute 825

  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. Dynamic Semantics • Informal Semantics • Translational Semantics • Operational Semantics • Denotational Semantics

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

  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. Translational Semantics (Example) LOAD M SUB N JF L3 LOAD N SUB M STO N L3 LABEL LOAD M ADD #5 STO M if m <> n then n := nm m := m+5

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

More Related