SlideServe
Menu
Préférences
© SlideServe · v2026

Understanding Attribute Grammars and Their Applications in Expression Evaluation

herbst avatar
11 diapositives · 237 Views · Sep 08, 2014

This article explores the concept of attribute grammars, a generalization of context-free grammars (CFG), where each grammar symbol has associated attributes. It covers both synthesized and inherited attributes, detailing their roles in computing values and expressions. The discussion includes examples such as the evaluation of arithmetic expressions and the conversion of binary numbers to decimal. Additionally, it highlights methods for computing attribute values during or after parsing, emphasizing the use of dependency graphs and rule-based techniques. This comprehensive overview aims to deepen your understanding of attribute grammars.

Understanding Attribute Grammars and Their Applications in Expression Evaluation - slide 1
1 / 11

Télécharger la présentation

Understanding Attribute Grammars and Their Applications in Expression Evaluation
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  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 ; by Neng-Fa Zhou

  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 by Neng-Fa Zhou

  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 by Neng-Fa Zhou

  4. S-Attributed GrammarsExample by Neng-Fa Zhou

  5. The Annotated Parse Tree by Neng-Fa Zhou

  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 by Neng-Fa Zhou

  7. Inherited AttributesExample by Neng-Fa Zhou

  8. The Annotated Parser Tree by Neng-Fa Zhou

  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.v+B.v BIN ::= B BIN.v = B.v B.r = BIN.r B ::= '1' B.v = 2B.r B ::= '0' B.v = 0 by Neng-Fa Zhou

  10. Dependency Graphs • Each node represents an attribute of a node in the parse tree • Each arc represents the dependence relationship (flow of values) by Neng-Fa Zhou

  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) by Neng-Fa Zhou