Attribute Grammars for Modular Disambiguation Valentin David 1 Akim Demaille 2 Olivier Gournet 2 1 Bergen University — Norway 2 EPITA Research and Development Laboratory (LRDE) — France International Conference on Intelligent Computer Communication and Processing 2006 V. David et al. (Bergen U. & LRDE) AGs for Disambiguation ICCP’06 1 / 25
Attribute Grammars for Modular Disambiguation Modular Front-Ends 1 Attribute Grammars for Disambiguation 2 Conclusion 3 V. David et al. (Bergen U. & LRDE) AGs for Disambiguation ICCP’06 2 / 25
Language Extensions domain specific extensions [4] embedded SQL design by contract (pre-/post-conditions) [1] syntactic sugar [5, 7] language evolution prototyping etc. and composition of them! V. David et al. (Bergen U. & LRDE) AGs for Disambiguation ICCP’06 3 / 25
Language Extensions domain specific extensions [4] embedded SQL design by contract (pre-/post-conditions) [1] syntactic sugar [5, 7] language evolution prototyping etc. and composition of them! V. David et al. (Bergen U. & LRDE) AGs for Disambiguation ICCP’06 3 / 25
Language Extensions domain specific extensions [4] embedded SQL design by contract (pre-/post-conditions) [1] syntactic sugar [5, 7] language evolution prototyping etc. and composition of them! V. David et al. (Bergen U. & LRDE) AGs for Disambiguation ICCP’06 3 / 25
Language Extensions domain specific extensions [4] embedded SQL design by contract (pre-/post-conditions) [1] syntactic sugar [5, 7] language evolution prototyping etc. and composition of them! V. David et al. (Bergen U. & LRDE) AGs for Disambiguation ICCP’06 3 / 25
C/C ++ Have Bad Syntactic Properties What’s this? a * b; V. David et al. (Bergen U. & LRDE) AGs for Disambiguation ICCP’06 4 / 25
C/C ++ Have Bad Syntactic Properties What’s this? a * b; A value product int a, b; a * b; V. David et al. (Bergen U. & LRDE) AGs for Disambiguation ICCP’06 4 / 25
C/C ++ Have Bad Syntactic Properties What’s this? a * b; A value product A variable declaration typedef int a; int a, b; a * b; a * b; V. David et al. (Bergen U. & LRDE) AGs for Disambiguation ICCP’06 4 / 25
C/C ++ Have Bad Syntactic Properties What’s this? (a) - (b); V. David et al. (Bergen U. & LRDE) AGs for Disambiguation ICCP’06 5 / 25
C/C ++ Have Bad Syntactic Properties What’s this? (a) - (b); A subtraction int a, b; (a) - (b); V. David et al. (Bergen U. & LRDE) AGs for Disambiguation ICCP’06 5 / 25
C/C ++ Have Bad Syntactic Properties What’s this? (a) - (b); A subtraction A cast int a, b; typedef int a; (a) - (b); (a) - (b); V. David et al. (Bergen U. & LRDE) AGs for Disambiguation ICCP’06 5 / 25
Modular Parser Generation To overcome context sensitivity, use “lexical tie-ins” [6] Use LALR(1) generators Use LR(1) generators None of these techniques is closed under union! So use GLR [10, 11] V. David et al. (Bergen U. & LRDE) AGs for Disambiguation ICCP’06 6 / 25
Modular Parser Generation To overcome context sensitivity, use “lexical tie-ins” [6] Use LALR(1) generators Use LR(1) generators None of these techniques is closed under union! So use GLR [10, 11] V. David et al. (Bergen U. & LRDE) AGs for Disambiguation ICCP’06 6 / 25
Modular Parser Generation To overcome context sensitivity, use “lexical tie-ins” [6] Use LALR(1) generators Use LR(1) generators None of these techniques is closed under union! So use GLR [10, 11] V. David et al. (Bergen U. & LRDE) AGs for Disambiguation ICCP’06 6 / 25
Modular Parser Generation To overcome context sensitivity, use “lexical tie-ins” [6] Use LALR(1) generators Use LR(1) generators None of these techniques is closed under union! So use GLR [10, 11] V. David et al. (Bergen U. & LRDE) AGs for Disambiguation ICCP’06 6 / 25
Generalized LR Parsing accepts the full class of context-free languages including ambiguous grammars so accept a superset of context-sensitive languages and filter the resulting “parse-forest” V. David et al. (Bergen U. & LRDE) AGs for Disambiguation ICCP’06 7 / 25
Generalized LR Parsing accepts the full class of context-free languages including ambiguous grammars so accept a superset of context-sensitive languages and filter the resulting “parse-forest” V. David et al. (Bergen U. & LRDE) AGs for Disambiguation ICCP’06 7 / 25
Generalized LR Parsing accepts the full class of context-free languages including ambiguous grammars so accept a superset of context-sensitive languages and filter the resulting “parse-forest” V. David et al. (Bergen U. & LRDE) AGs for Disambiguation ICCP’06 7 / 25
The Big Picture C SGLR Parse Parse Disambiguation source parser forest tree Concrete syntax to abstract syntax Pretty C Standard AST AST Transformations printer source C compiler V. David et al. (Bergen U. & LRDE) AGs for Disambiguation ICCP’06 8 / 25
The Big Picture C SGLR Parse Parse Disambiguation source parser forest tree Concrete syntax to abstract syntax Pretty C Standard AST AST Transformations printer source C compiler Need for modular disambiguation. V. David et al. (Bergen U. & LRDE) AGs for Disambiguation ICCP’06 8 / 25
Attribute Grammars for Modular Disambiguation Modular Front-Ends 1 Attribute Grammars for Disambiguation 2 Conclusion 3 V. David et al. (Bergen U. & LRDE) AGs for Disambiguation ICCP’06 9 / 25
A Simple Ambiguous Grammar context -free syntax "true" -> Bool "false" -> Bool Bool "|" Bool -> Bool Figure: Boolean Expressions (Ambiguous) [3] V. David et al. (Bergen U. & LRDE) AGs for Disambiguation ICCP’06 10 / 25
Parse Forest for true | true | true Bool Bool Bool | Bool Bool | Bool Bool | Bool true true Bool | Bool true true true true V. David et al. (Bergen U. & LRDE) AGs for Disambiguation ICCP’06 11 / 25
Techniques for Disambiguation Dedicated Code Poor modularity, more imperative, less declarative Algebraic Specification [2] Modular, declarative, too hard to use Attribute Grammars (AGs) [9] Modular, declarative, easy to use V. David et al. (Bergen U. & LRDE) AGs for Disambiguation ICCP’06 12 / 25
Techniques for Disambiguation Dedicated Code Poor modularity, more imperative, less declarative Algebraic Specification [2] Modular, declarative, too hard to use Attribute Grammars (AGs) [9] Modular, declarative, easy to use V. David et al. (Bergen U. & LRDE) AGs for Disambiguation ICCP’06 12 / 25
Techniques for Disambiguation Dedicated Code Poor modularity, more imperative, less declarative Algebraic Specification [2] Modular, declarative, too hard to use Attribute Grammars (AGs) [9] Modular, declarative, easy to use V. David et al. (Bergen U. & LRDE) AGs for Disambiguation ICCP’06 12 / 25
A Simple AG Example context -free syntax "true" | "false" -> Bool {attributes (assoc: root.is_atomic := true )} lhs:Bool "|" rhs:Bool -> Bool {attributes (assoc: root.is_atomic := false root.ok := rhs.is_atomic )} Figure: Boolean Expressions Disambiguated V. David et al. (Bergen U. & LRDE) AGs for Disambiguation ICCP’06 13 / 25
Disambiguated Parse Forest Bool Bool Bool | Bool Bool | Bool Bool | Bool true true Bool | Bool true true true true V. David et al. (Bergen U. & LRDE) AGs for Disambiguation ICCP’06 14 / 25
Application to ISO C99 [8] 126 symbols 356 rules 53 modules 10 attribute kinds 190 attribute rules completed to 1183 rules V. David et al. (Bergen U. & LRDE) AGs for Disambiguation ICCP’06 15 / 25
Application to ISO C99 [8] 126 symbols 356 rules 53 modules 10 attribute kinds 190 attribute rules completed to 1183 rules V. David et al. (Bergen U. & LRDE) AGs for Disambiguation ICCP’06 15 / 25
Application to ISO C99 [8] 126 symbols 356 rules 53 modules 10 attribute kinds 190 attribute rules completed to 1183 rules V. David et al. (Bergen U. & LRDE) AGs for Disambiguation ICCP’06 15 / 25
C Disambiguation HelloW Lemon Eval Lines of code 448 4 135 28 392 Ambiguities 103 6 410 68 195 Duration (s) 3.8 28.0 322.5 V. David et al. (Bergen U. & LRDE) AGs for Disambiguation ICCP’06 16 / 25
Attribute Grammars for Modular Disambiguation Modular Front-Ends 1 Attribute Grammars for Disambiguation 2 Conclusion 3 V. David et al. (Bergen U. & LRDE) AGs for Disambiguation ICCP’06 17 / 25
Attribute Grammars for Modular Disambiguation Pros Cons Declarativity Slow Modularity Hard to debug Simplicity Poor genericity External data V. David et al. (Bergen U. & LRDE) AGs for Disambiguation ICCP’06 18 / 25
Attribute Grammars for Modular Disambiguation Pros Cons Declarativity Slow Modularity Hard to debug Simplicity Poor genericity External data V. David et al. (Bergen U. & LRDE) AGs for Disambiguation ICCP’06 18 / 25
Future Work Completion of C ++ tool-chain Various C ++ syntactic sugar Parse time disambiguation V. David et al. (Bergen U. & LRDE) AGs for Disambiguation ICCP’06 19 / 25
Recommend
More recommend