Attribute Grammars - PowerPoint PPT Presentation

About This Presentation
Title:

Attribute Grammars

Description:

Title: Syntax-Directed Translation (Attribute Grammars) Author: Neng-Fa Zhou Last modified by: zhou Created Date: 3/29/2000 3:04:42 AM Document presentation format – PowerPoint PPT presentation

Number of Views:124
Avg rating:3.0/5.0
Slides: 12
Provided by: NengF1
Category:

less

Transcript and Presenter's Notes

Title: Attribute Grammars


1
Attribute Grammars
  • Recall the yacc program
  • Parse a given expression
  • Evaluate it

expr expr '' expr 1 3
expr '-' expr 1 - 3 expr ''
expr 1 3 expr '/' expr
1 / 3 '(' expr ')' 2
NUMBER
2
Attributes and Attribute Grammars
  • Attributes
  • variables (type,offset,first or last
    occurrence,...)
  • constants (type, value, scope, ...)
  • Attribute grammar(Syntax-directed definition)
  • A generalization of CFG in which each grammar
    symbol has an associated set of attributes
  • Semantics rules for computing attribute values

3
Synthesized Attributes
  • Synthesized Attributes
  • The value is computed from the values of
    attributes of the children
  • S-attributed grammar - synthesized attributes
    only
  • Bottom-up propagation
  • Example
  • values of expressions
  • types of expressions

4
S-Attributed GrammarsExample
5
The Annotated Parse Tree
6
Inherited Attributes
  • Inherited Attributes
  • The value is computed from the values of
    attributes of the siblings and parent
  • Top-down propagation
  • Example
  • type information
  • where does a variable occurs? lhs or rhs

7
Inherited AttributesExample
8
The Annotated Parser Tree
9
Converting Binary to Decimal
N BIN N.v BIN.v BIN.r 0 BIN0
BIN1 B BIN1.r BIN0.r 1 B.r
BIN0.r BIN0.v BIN1.vB.v BIN
B BIN.v B.v B.r BIN.r B '1' B.v
2B.r B '0' B.v 0
10
Dependency Graphs
  • Each node represents an attribute of a node in
    the parse tree
  • Each arc represents the dependence relationship
    (flow of values)

11
Computing Attribute Values
  • 1.Compute while parsing
  • Oblivious methods
  • Fast (one pass)
  • Complicated
  • Some attribute values cannot be computed
  • 2. Compute after parsing
  • Parse-tree methods (sort dependency graphs)
  • Rule-based methods (data-flow or fix-point)
Write a Comment
User Comments (0)
About PowerShow.com