200 likes | 329 Views
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).
E N D
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).
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.
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
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.
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.
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
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.
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.
Example (Synthesized Attribute) A simple expression:
Example (Inherited Attribute) A positive integer:
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.
Dynamic Semantics • Informal Semantics • Translational Semantics • Operational Semantics • Denotational Semantics
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.
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.
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 := nm m := m+5
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.
Operational Semantics (Example) if m<>n then n := nm m := m+5 if (m-n=0) goto L1 n n - m L1: m m + 5