Preface xix Part I: Narratives 1 Chapter 1: An Introductory Example 3 Gothic Security 3 The State Machine Model 5 Programming Miss Grant''s Controller 9 Languages and Semantic Model 16 Using Code Generation 19 Using Language Workbenches 22 Visualization 24 Chapter 2: Using Domain-Specific Languages 27 Defining Domain-Specific Languages 27 Why Use a DSL? 33 Problems with DSLs 36 Wider Language Processing 39 DSL Lifecycle 40 What Makes a Good DSL Design? 42 Chapter 3: Implementing DSLs 43 Architecture of DSL Processing 43 The Workings of a Parser 47 Grammars, Syntax, and Semantics 49 Parsing Data 50 Macros 52 Chapter 4: Implementing an Internal DSL 67 Fluent and Command-Query APIs 68 The Need for a Parsing Layer 71 Using Functions 72 Literal Collections 77 Using Grammars to Choose Internal Elements 79 Closures 80 Parse Tree Manipulation 82 Annotation 84 Literal Extension 85 Reducing the Syntactic Noise 85 Dynamic Reception 86 Providing Some Type Checking 87 Chapter 5: Implementing an External DSL 89 Syntactic Analysis Strategy 89 Output Production Strategy 92 Parsing Concepts 94 Mixing-in Another Language 100 XML DSLs 101 Chapter 6: Choosing between Internal and External DSLs 105 Learning Curve 105 Cost of Building 106 Programmer Familiarity 107 Communication with Domain Experts 108 Mixing In the Host Language 108 Strong Expressiveness Boundary 109 Runtime Configuration 110 Sliding into Generality 110 Composing DSLs 111 Summing Up 111 Chapter 7: Alternative Computational Models 113 A Few Alternative Models 116 Chapter 8: Code Generation 121 Choosing What to Generate 122 How to Generate 124 Mixing Generated and Handwritten Code 126 Generating Readable Code 127 Preparse Code Generation 128 Further Reading 128 Chapter 9: Language Workbenches 129 Elements of Language Workbenches 130 Schema Definition Languages and Meta-Models 131 Source and Projectional Editing 136 Illustrative Programming 138 Tools Tour 140 Language Workbenches and CASE tools 141 Should You Use a Language Workbench? 142 Part II: Common Topics 145 Chapter 10: A Zoo of DSLs 147 Graphviz 147 JMock 149 CSS 150 Hibernate Query Language (HQL) 151 XAML 152 FIT 155 Make et al. 156 Chapter 11: Semantic Model 159 How It Works 159 When to Use It 162 The Introductory Example (Java) 163 Chapter 12: Symbol Table 165 How It Works 166 When to Use It 168 Further Reading 168 Dependency Network in an External DSL (Java and ANTLR) 168 Using Symbolic Keys in an Internal DSL (Ruby) 170 Using Enums for Statically Typed Symbols (Java) 172 Chapter 13: Context Variable 175 How It Works 175 When to Use It 176 Reading an INI File (C#) 176 Chapter 14: Construction Builder 179 How It Works 179 When to Use It 180 Building Simple Flight Data (C#) 180 Chapter 15: Macro 183 How It Works 184 When to Use It 192 Chapter 16: Notification 193 How It Works 194 When to Use It 194 A Very Simple Notification (C#) 194 Parsing Notification (Java) 195 Part III: External DSL Topics 199 Chapter 17: Delimiter-Directed Translation 201 How It Works 201 When to Use It 204 Frequent Customer Points (C#) 205 Parsing Nonautonomous Statements with Miss Grant''s Controller (Java) 211 Chapter 18: Syntax-Directed Translation 219 How It Works 220 When to Use It 227 Further Reading 227 Chapter 19: BNF 229 How It Works 229 When to Use It 238 Chapter 20: Regex Table Lexer (by Rebecca Parsons) 239 How It Works 240 When to Use It 241 Lexing Miss Grant''s Controller (Java) 241 Chapter 21: Recursive Descent Parser (by Rebecca Parsons) 245 How It Works 246 When to Use It 249 Further Reading 249 Recursive Descent and Miss Grant''s Controller (Java) 250 Chapter 22: Parser Combinator (by Rebecca Parsons) 255 How It Works 256 When to Use It 261 Parser Combinators and Miss Grant''s Controller (Java) 261 Chapter 23: Parser Generator 269 How It Works 269 When to Use It 272 Hello World (Java and ANTLR) 272 Chapter 24: Tree Construction 281 How It Works 281 When to Use It 284 Using ANTLR''s Tree Construction Syntax (Java and ANTLR) 284 Tree Construction Using Code Actions (Java and ANTLR) 292 Chapter 25: Embedded Translation 299 How It Works 299 When to Use It 300 Miss Grant''s Controller (Java and ANTLR) 300 Chapter 26: Embedded Interpretation 305 How It Works 305 When to Use It 306 A Calculator (ANTLR and Java) 306 Chapter 27: Foreign Code 309 How It Works 309 When to Use It 311 Embedding Dynamic Code (ANTLR, Java, and Javascript) 311 Chapter 28: Alternative Tokenization 319 How It Works 319 When to Use It 326 Chapter 29: Nested Operator Expressio.
Domain-Specific Languages, Portable Documents