Preface xix <p style="margin: 0px;"> </p> <p style="margin: 0px;">Part I: Narratives 1</p> <p style="margin: 0px;">Chapter 1: An Introductory Example 3</p> <p style="margin: 0px;">Gothic Security 3</p> <p style="margin: 0px;">The State Machine Model 5</p> <p style="margin: 0px;">Programming Miss Grant’s Controller 9</p> <p style="margin: 0px;">Languages and Semantic Model 16</p> <p style="margin: 0px;">Using Code Generation 19</p> <p style="margin: 0px;">Using Language Workbenches 22</p> <p style="margin: 0px;">Visualization 24</p> <p style="margin: 0px;"> </p> <p style="margin: 0px;">Chapter 2: Using Domain-Specific Languages 27</p> <p style="margin: 0px;">Defining Domain-Specific Languages 27</p> <p style="margin: 0px;">Why Use a DSL? 33</p> <p style="margin: 0px;">Problems with DSLs 36</p> <p style="margin: 0px;">Wider Language Processing 39</p> <p style="margin: 0px;">DSL Lifecycle 40</p> <p style="margin: 0px;">What Makes a Good DSL Design? 42</p> <p style="margin: 0px;"> </p> <p style="margin: 0px;">Chapter 3: Implementing DSLs 43</p> <p style="margin: 0px;">Architecture of DSL Processing 43</p> <p style="margin: 0px;">The Workings of a Parser 47</p> <p style="margin: 0px;">Grammars, Syntax, and Semantics 49</p> <p style="margin: 0px;">Parsing Data 50</p> <p style="margin: 0px;">Macros 52</p> <p style="margin: 0px;"> </p> <p style="margin: 0px;">Chapter 4: Implementing an Internal DSL 67</p> <p style="margin: 0px;">Fluent and Command-Query APIs 68</p> <p style="margin: 0px;">The Need for a Parsing Layer 71</p> <p style="margin: 0px;">Using Functions 72</p> <p style="margin: 0px;">Literal Collections 77</p> <p style="margin: 0px;">Using Grammars to Choose Internal Elements 79</p> <p style="margin: 0px;">Closures 80</p> <p style="margin: 0px;">Parse Tree Manipulation 82</p> <p style="margin: 0px;">Annotation 84</p> <p style="margin: 0px;">Literal Extension 85</p> <p style="margin: 0px;">Reducing the Syntactic Noise 85</p> <p style="margin: 0px;">Dynamic Reception 86</p> <p style="margin: 0px;">Providing Some Type Checking 87</p> <p style="margin: 0px;"> </p> <p style="margin: 0px;">Chapter 5: Implementing an External DSL 89</p> <p style="margin: 0px;">Syntactic Analysis Strategy 89</p> <p style="margin: 0px;">Output Production Strategy 92</p> <p style="margin: 0px;">Parsing Concepts 94</p> <p style="margin: 0px;">Mixing-in Another Language 100</p> <p style="margin: 0px;">XML DSLs 101</p> <p style="margin: 0px;"> </p> <p style="margin: 0px;">Chapter 6: Choosing between Internal and External DSLs 105</p> <p style="margin: 0px;">Learning Curve 105</p> <p style="margin: 0px;">Cost of Building 106</p> <p style="margin: 0px;">Programmer Familiarity 107</p> <p style="margin: 0px;">Communication with Domain Experts 108</p> <p style="margin: 0px;">Mixing In the Host Language 108</p> <p style="margin: 0px;">Strong Expressiveness Boundary 109</p> <p style="margin: 0px;">Runtime Configuration 110</p> <p style="margin: 0px;">Sliding into Generality 110</p> <p style="margin: 0px;">Composing DSLs 111</p> <p style="margin: 0px;">Summing Up 111</p> <p style="margin: 0px;"> </p> <p style="margin: 0px;">Chapter 7: Alternative Computational Models 113</p> <p style="margin: 0px;">A Few Alternative Models 116</p> <p style="margin: 0px;"> </p> <p style="margin: 0px;">Chapter 8: Code Generation 121</p> <p style="margin: 0px;">Choosing What to Generate 122</p> <p style="margin: 0px;">How to Generate 124</p> <p style="margin: 0px;">Mixing Generated and Handwritten Code 126</p> <p style="margin: 0px;">Generating Readable Code 127</p> <p style="margin: 0px;">Preparse Code Generation 128</p> <p style="margin: 0px;">Further Reading 128</p> <p style="margin: 0px;"> </p> <p style="margin: 0px;">Chapter 9: Language Workbenches 129</p> <p style="margin: 0px;">Elements of Language Workbenches 130</p> <p style="margin: 0px;">Schema Definition Languages and Meta-Models 131</p> <p style="margin: 0px;">Source and Projectional Editing 136</p> <p style="margin: 0px;">Illustrative Programming 138</p> <p style="margin: 0px;">Tools Tour 140</p> <p style="margin: 0px;">Language Workbenches and CASE tools 141</p> <p style="margin: 0px;">Should You Use a Language Workbench? 142</p> <p style="margin: 0px;"> </p> <p style="margin: 0px;">Part II: Common Topics 145</p> <p style="margin: 0px;">Chapter 10: A Zoo of DSLs 147</p> <p style="margin: 0px;">Graphviz 147</p> <p style="margin: 0px;">JMock 149</p> <p style="margin: 0px;">CSS 150</p> <p style="margin: 0px;">Hibernate Query Language (HQL) 151</p> <p style="margin: 0px;">XAML 152</p> <p style="margin: 0px;">FIT 155</p> <p style="margin: 0px;">Make et al. 156</p> <p style="margin: 0px;"> </p> <p style="margin: 0px;">Chapter 11: Semantic Model 159</p> <p style="margin: 0px;">How It Works 159</p> <p style="margin: 0px;">When to Use It 162</p> <p style="margin: 0px;">The Introductory Example (Java) 163</p> <p style="margin: 0px;"> </p> <p style="margin: 0px;">Chapter 12: Symbol Table 165</p> <p style="margin: 0px;">How It Works 166</p> <p style="margin: 0px;">When to Use It 168</p> <p style="margin: 0px;">Further Reading 168</p> <p style="margin: 0px;">Dependency Network in an External DSL (Java and ANTLR) 168</p> <p style="margin: 0px;">Using Symbolic Keys in an Internal DSL (Ruby) 170</p> <p style="margin: 0px;">Using Enums for Statically Typed Symbols (Java) 172</p> <p style="margin: 0px;"> </p> <p style="margin: 0px;">Chapter 13: Context Variable 175</p> <p style="margin: 0px;">How It Works 175</p> <p style="margin: 0px;">When to Use It 176</p> <p style="margin: 0px;">Reading an INI File (C#) 176</p> <p style="margin: 0px;"> </p> <p style="margin: 0px;">Chapter 14: Construction Builder 179</p> <p style="margin: 0px;">How It Works 179</p> <p style="margin: 0px;">When to Use It 180</p> <p style="margin: 0px;">Building Simple Flight Data (C#) 180</p> <p style="margin: 0px;"> </p> <p style="margin: 0px;">Chapter 15: Macro 183</p> <p style="margin: 0px;">How It Works 184</p> <p style="margin: 0px;">When to Use It 192</p> <p style="margin: 0px;"> </p> <p style="margin: 0px;">Chapter 16: Notification 193</p> <p style="margin: 0px;">How It Works 194</p> <p style="margin: 0px;">When to Use It 194</p> <p style="margin: 0px;">A Very Simple Notification (C#) 194</p> <p style="margin: 0px;">Parsing Notification (Java) 195</p> <p style="margin: 0px;"> </p> <p style="margin: 0px;">Part III: External DSL Topics 199</p> <p style="margin: 0px;">Chapter 17: Delimiter-Directed Translation 201</p> <p style="margin: 0px;">How It Works 201</p> <p style="margin: 0px;">When to Use It 204</p> <p style="margin: 0px;">Frequent Customer Points (C#) 205</p> <p style="margin: 0px;">Parsing Nonautonomous Statements with Miss Grant’s Controller (Java) 211</p> <p style="margin: 0px;"> </p> <p style="margin: 0px;">Chapter 18: Syntax-Directed Translation 219</p> <p style="margin: 0px;">How It Works 220</p> <p style="margin: 0px;">When to Use It 227</p> <p style="margin: 0px;">Further Reading 227</p> <p style="margin: 0px;"> </p> <p style="margin: 0px;">Chapter 19: BNF 229</p> <p style="margin: 0px;">How It Works 229</p> <p style="margin: 0px;">When to Use It 238</p> <p style="margin: 0px;"> </p> <p style="margin: 0px;">Chapter 20: Regex Table Lexer (by Rebecca Parsons) 239</p> <p style="margin: 0px;">How It Works 240</p> <p style="margin: 0px;">When to Use It 241</p> <p style="margin: 0px;">Lexing Miss Grant’s Controller (Java) 241</p> <p style="margin: 0px;"> </p> <p style="margin: 0px;">Chapter 21: Recursive Descent Parser (by Rebecca Parsons) 245</p> <p style="margin: 0px;">How It Works 246</p> <p style="margin: 0px;">When to Use It 249</p> <p style="margin: 0px;">Further Reading 249</p> <p style="margin: 0px;">Recursive Descent and Miss Grant’s Controller (Java) 250</p> <p style="margin: 0px;"> </p> <p style="margin: 0px;">Chapter 22: Parser Combinator (by Rebecca Parsons) 255</p> <p style="margin: 0px;">How It Works 256</p> <p style="margin: 0px;">When to Use It 261</p> <p style="margin: 0px;">Parser Combinators and Miss Grant’s Controller (Java) 261</p> <p style="margin: 0px;"> </p> <p style="margin: 0px;">Chapter 23: Parser Generator 269</p> <p style="margin: 0px;">How It Works 269</p> <p style="margin: 0px;">When to Use It 272</p> <p style="margin: 0px;">Hello World (Java and ANTLR) 272</p> <p style="margin: 0px;"> </p> <p style="margin: 0px;">Chapter 24: Tree Construction 281</p> <p style="margin: 0px;">How It Works 281</p> <p style="margin: 0px;">When to Use It 284</p> <p style="margin: 0px;">Using ANTLR’s Tree Construction Syntax (Java and ANTLR) 284</p> <p style="margin: 0px;">Tree Construction Using Code Actions (Java and ANTLR) 292</p> <p style="margin: 0px;"> </p> <p style="margin: 0px;">Chapter 25: Embedded Translation 299</p> <p style="margin: 0px;">How It Works 299</p> <p style="margin: 0px;">When to Use It 300</p> <p style="margin: 0px;">Miss Grant’s Controller (Java and ANTLR) 300</p> <p style="margin: 0px;"> </p> <p style="margin: 0px;">Chapter 26: Embedded Interpretation 305</p> <p style="margin: 0px;">How It Works 305</p> <p style="margin: 0px;">When to Use It 306</p> <p style="margin: 0px;">A Calculator (ANTLR and Java) 306</p> <p style="margin: 0px;"> </p> <p style="margin: 0px;">Chapter 27: Foreign Code 309</p> <p style="margin: 0px;">How It Works 309</p> <p style="margin: 0px;">When to Use It 311</p> <p style="margin: 0px;">Embedding Dynamic Code (ANTLR, Java, and Javascript) 311</p> <p style="margin: 0px;"> </p> <p style="margin: 0px;">Chapter 28: Alternative Tokenization 319</p> <p style="margin: 0px;">How It Works 319</p> <p style="margin: 0px;">When to Use It 326</p> <p style="margin: 0px;"> </p> <p style="margin: 0px;">Chapter 29: Nested Operator Expression 327</p> <p style="margin: 0px;">How It Works 327</p> <p style="margin: 0px;">When to Use It 331</p> <p style="margin: 0px;"> </p> <p style="margin: 0px;">Chapter 30: Newline Separators 333</p> <p style="margin: 0px;">How It Works 333</p> <p style="margin: 0px;">When to Use It 335</p> <p style="margin: 0px;"> </p> <p style="margin: 0px;">Chapter 31: External DSL Miscellany 337</p> <p style="margin: 0px;">Syntactic Indentation 337</p> <p style="margin: 0px;">Modular Grammars 339</p> <p style="margin: 0px;"> </p> <p style="margin: 0px;">Part IV: Internal DSL Topics 341</p> <p style="margin: 0px;">Chapter 32: Expression Builder 343</p> <p style="margin: 0px;">How It Works 344</p> <p style="margin: 0px;">When to Use It 344</p> <p style="margin: 0px;">A Fluent Calendar with and without a Builder (Java) 345</p> <p style="margin: 0px;">Using Multiple Builders for the Calendar (Java) 348</p> <p style="margin: 0px;"> </p> <p style="margin: 0px;">Chapter 33: Function Sequence 351</p> <p style="margin: 0px;">How It Works 351</p> <p style="margin: 0px;">When to Use It 352</p> <p style="margin: 0px;">Simple Computer Configuration (Java) 352</p> <p style="margin: 0px;"> </p> <p style="margin: 0px;">Chapter 34: Nested Function 357</p> <p style="margin: 0px;">How It Works 357</p> <p style="margin: 0px;">When to Use It 359</p> <p style="margin: 0px;">The Simple Computer Configuration Example (Java) 360</p> <p style="margin: 0px;">Handling Multiple Different Arguments with Tokens (C#) 361</p> <p style="margin: 0px;">Using Subtype Tokens for IDE Support (Java) 363</p> <p style="margin: 0px;">Using Object Initializers (C#) 365</p> <p style="margin: 0px;">Recurring Events (C#) 366</p> <p style="margin: 0px;"> </p> <p style="margin: 0px;">Chapter 35: Method Chaining 373</p> <p style="margin: 0px;">How It Works 373</p> <p style="margin: 0px;">When to Use It 377</p> <p style="margin: 0px;">The Simple Computer Configuration Example (Java) 378</p> <p style="margin: 0px;">Chaining with Properties (C#) 381</p> <p style="margin: 0px;">Progressive Interfaces (C#) 382</p> <p style="margin: 0px;"> </p> <p style="margin: 0px;">Chapter 36: Object Scoping 385</p> <p style="margin: 0px;">How It Works 386</p> <p style="margin: 0px;">When to Use It 386</p> <p style="margin: 0px;">Security Codes (C#) 387</p> <p style="margin: 0px;">Using Instance Evaluation (Ruby) 392</p> <p style="margin: 0px;">Using an Instance Initializer (Java) 394</p> <p style="margin: 0px;"> </p> <p style="margin: 0px;">Chapter 37: Closure 397</p> <p style="margin: 0px;">How It Works 397</p> <p style="margin: 0px;">When to Use It 402</p> <p style="margin: 0px;"> </p> <p style="margin: 0px;">Chapter 38: Nested Closure 403</p> <p style="margin: 0px;">How It Works 403</p> <p style="margin: 0px;">When to Use It 405</p> <p style="margin: 0px;">Wrapping a Function Sequence in a Nested Closure (Ruby) 405</p> <p style="margin: 0px;">Simple C# Example (C#) 408</p> <p style="margin: 0px;">Using Method Chaining (Ruby) 409</p> <p style="margin: 0px;">Function Sequence with Explicit Closure Arguments (Ruby 411</p> <p style="margin: 0px;">Using Instance Evaluation (Ruby) 412</p> <p style="margin: 0px;"> </p> <p style="margin: 0px;">Chapter 39: Literal List 417</p> <p style="margin: 0px;">How It Works 417</p> <p style="margin: 0px;">When to Use It 417</p> <p style="margin: 0px;"> </p> <p style="margin: 0px;">Chapter 40: Literal Map 419</p> <p style="margin: 0px;">How It Works 419</p> <p style="margin: 0px;">When to Use It 420</p> <p style="margin: 0px;">The Computer Configuration Using Lists and Maps (Ruby) 420</p> <p style="margin: 0px;">Evolving to Greenspun Form (Ruby) 422</p> <p style="margin: 0px;"> </p> <p style="margin: 0px;">Chapter 41: Dynamic Reception 427</p> <p style="margin: 0px;">How It Works 428</p> <p style="margin: 0px;">When to Use It 429</p> <p style="margin: 0px;">Promotion Points Using Parsed Method Names (Ruby) 430</p> <p style="margin: 0px;">Promotion Points Using Chaining (Ruby) 434</p> <p style="margin: 0px;">Removing Quoting in the Secret Panel Controller (JRuby) 438</p> <p style="margin: 0px;"> </p> <p style="margin: 0px;">Chapter 42: Annotation 445</p> <p style="margin: 0px;">How It Works 446</p> <p style="margin: 0px;">When to Use It 449</p> <p style="margin: 0px;">Custom Syntax with Runtime Processing (Java) 449</p> <p style="margin: 0px;">Using a Class Method (Ruby) 451</p> <p style="margin: 0px;">Dynamic Code Generation (Ruby) 452</p> <p style="margin: 0px;"> </p> <p style="margin: 0px;">Chapter 43: Parse Tree Manipulation 455</p> <p style="margin: 0px;">How It Works 455</p> <p style="margin: 0px;">When to Use It 456</p> <p style="margin: 0px;">Generating IMAP Queries from C# Conditions (C#) 457</p> <p style="margin: 0px;"> </p> <p style="margin: 0px;">Chapter 44: Class Symbol Table 467</p> <p style="margin: 0px;">How It Works 468</p> <p style="margin: 0px;">When to Use It 469</p> <p style="margin: 0px;">Statically Typed Class Symbol Table (Java) 469</p> <p style="margin: 0px;"> </p> <p style="margin: 0px;">Chapter 45: Textual Polishing 477</p> <p style="margin: 0px;">How It Works 477</p> <p style="margin: 0px;">When to Use It 478</p> <p style="margin: 0px;">Polished Discount Rules (Ruby) 478</p> <p style="margin: 0px;"> </p> <p style="margin: 0px;">Chapter 46: Literal Extension 481</p> <p style="margin: 0px;">How It Works 481</p> <p style="margin: 0px;">When to Use It 482</p> <p style="margin: 0px;">Recipe Ingredients (C#) 483</p> <p style="margin: 0px;"> </p> <p style="margin: 0px;">Part V: Alternative Computational Models 485</p> <p style="margin: 0px;">Chapter 47: Adaptive Model 487</p> <p style="margin: 0px;">How It Works 488</p> <p style="margin: 0px;">When to Use It 492</p> <p style="margin: 0px;"> </p> <p style="margin: 0px;">Chapter 48: Decision Table 495</p> <p style="margin: 0px;">How It Works 495</p> <p style="margin: 0px;">When to Use It 497</p> <p style="margin: 0px;">Calculating the Fee for an Order (C#) 497</p> <p style="margin: 0px;"> </p> <p style="margin: 0px;">Chapter 49: Dependency Network 505</p> <p style="margin: 0px;">How It Works 506</p> <p style="margin: 0px;">When to Use It 508</p> <p style="margin: 0px;">Analyzing Potions (C#) 508</p> <p style="margin: 0px;"> </p> <p style="margin: 0px;">Chapter 50: Production Rule System 513</p> <p style="margin: 0px;">How It Works 514</p> <p style="margin: 0px;">When to Use It 517</p> <p style="margin: 0px;">Validations for club membership (C#) 517</p> <p style="margin: 0px;">Eligibility Rules: extending the club membership (C#) 521</p> <p style="margin: 0px;"> </p> <p style="margin: 0px;">Chapter 51: State Machine 527</p> <p style="margin: 0px;">How It Works 527</p> <p style="margin: 0px;">When to Use It 529</p> <p style="margin: 0px;">Secret Panel Controller (Java) 530</p> <p style="margin: 0px;"> </p> <p style="margin: 0px;">Part VI: Code Generation 531</p> <p style="margin: 0px;">Chapter 52: Transformer Generation 533</p> <p style="margin: 0px;">How It Works 533</p> <p style="margin: 0px;">When to Use It 535</p> <p style="margin: 0px;">Secret Panel Controller (Java generating C) 535</p> <p style="margin: 0px;"> </p> <p style="margin: 0px;">Chapter 53: Templated Generation 539</p> <p style="margin: 0px;">How It Works 539</p> <p style="margin: 0px;">When to Use It 541</p> <p style="margin: 0px;">Generating the Secret Panel State Machine with Nested Conditionals (Velocity and Java generating C) 541</p> <p style="margin: 0px;"> </p> <p style="margin: 0px;">Chapter 54: Embedment Helper 547</p> <p style="margin: 0px;">How It Works 548</p> <p style="margin: 0px;">When to Use It 549</p> <p style="margin: 0px;">Secret Panel States (Java and ANTLR) 549</p> <p style="margin: 0px;">Should a Helper Generate HTML? (Java and Velocity) 552</p> <p style="margin: 0px;"> </p> <p style="margin: 0px;">Chapter 55: Model-Aware Generation 555</p> <p style="margin: 0px;">How It Works 556</p> <p style="margin: 0px;">When to Use It 556</p> <p style="margin: 0px;">Secret Panel State Machine (C) 557</p> <p style="margin: 0px;">Loading the State Machine Dynamically (C) 564</p> <p style="margin: 0px;"> </p> <p style="margin: 0px;">Chapter 56: Model Ignorant Generation 567</p> <p style="margin: 0px;">How It Works 567</p> <p style="margin: 0px;">When to Use It 568</p> <p style="margin: 0px;">Secret Panel State Machine as Nested Conditionals (C) 568</p> <p style="margin: 0px;"> </p> <p style="margin: 0px;">Chapter 57: Generation Gap 571</p> <p style="margin: 0px;">How It Works 571</p> <p style="margin: 0px;">When to Use It 573</p> <p style="margin: 0px;">Generating Classes from a Data Schema (Java and a Little Ruby) 573</p> <p style="margin: 0px;"> </p> <p style="margin: 0px;"></p> <p style="margin: 0px;">Bibliography 579</p> <p style="margin: 0px;">Index 581</p> <p style="margin: 0px;"></p>