A Cheat Sheet

How do I separate parsing logic from semantic logic (from code generation logic)?

=> Semantic Model

I’m using an internal DSL => Expression Builder + Semantic Model

Parsing

How do I hold symbols to cross-reference between different parts of the parse?

=> Symbol Table

How do I keep hierarchic context during the parse?

As much as possible, I keep it on the stack and use parameters and return values.

If I can’t => Context Variable

How do I gradually build up an immutable object?

=> Construction Builder

How do I collect and return multiple errors back to the caller of the parse?

=> Notification

External DSLs

How do I break up text into a parsing structure?

=> Syntax-Directed Translation + BNF

My structure is very simple => Delimiter-Directed Translation

How do I build a lexer?

=> Regex Table Lexer

If I’m using a Parser Generator and there is a lexer generator that fits well, I’ll use that.

How do I build a parser?

=> Parser Generator

My grammar is simple => Parser Combinator

There is no Parser Generator for my platform => Parser Combinator

I prefer control flow to composing objects => Recursive Descent Parser

How do I generate output?

My output maps cleanly onto the Semantic Model => Embedded Translation

My transformation is more complicated => Tree Construction

I want to execute simple DSL statements immediately => Embedded Interpretation

How do I handle arithmetic expressions, Boolean expressions, or similar structures?

=> Nested Operator Expression

How do I handle cases that are too rare to be worth extending the DSL? How do I weave non-DSL behavior into the DSL?

=> Foreign Code + Embedment Helper

I’m using Foreign Code, and my DSL has become overwhelmed and thus hard to see.

=> Embedment Helper

How I do I change my lexing rules in the middle of a parse?

=> Alternative Tokenization

How do I handle newlines that are part of the grammar when using Syntax-Directed Translation?

=> Newline Separators

Internal DSLs

How do I represent a sequence of high-level statements?

=> Function Sequence

How do I handle a fixed sequence of clauses?

The clauses are mandatory => Nested Function

There are optional clauses => Literal Map

I need optional clauses and don’t have literal map syntax => Method Chaining

How do I handle a variable sequence of clauses?

Each clause is the same => Literal List

The clauses are different => Method Chaining

Each clause should appear only once => Literal Map

How do I build simple expressions in method names?

How do I represent method parameters as method names?

=> Dynamic Reception

How do I start an expression with a number (or other literal)?

=> Literal Extension

How do I use bare function calls without using global data or functions?

=> Object Scoping

How do I control when I evaluate a clause?

The clause is inside a method => Nested Closure The clause is in a class definition => Annotation

How do I get static type checking or type-safe autocompletion?

=> Class Symbol Table

How do I make my DSL look less like the host language?

My changes are simple textual substitutions => Textual Polishing

If it’s more complicated, use an external DSL instead.

How do I populate my Semantic Model with expressions written in my host language without evaluating those expressions?

=> Parse Tree Manipulation

Code Generation

How do I drive producing the output code?

Most of the output is generated => Transformer Generation

There’s a lot of non-generated output => Templated Generation

How do I keep my templates readable using Templated Generation?

=> Embedment Helper

How do I structure my semantics in my target code?

=> Model-Aware Generation

The target language isn’t expressive enough => Model Ignorant Generation

How do I interweave generated and handwritten code?

Use calls between generated and handwritten objects.

I need to mix generated and handwritten code in the same object => Generation Gap

Alternative Computational Models

How do I arrange computation using a model different from my host language?

=> Adaptive Model

How do I represent conditional logic?

I have a composite conditional expression => Decision Table

I have a list of conditions to be evaluated => Production Rule System

How do I represent tasks that are computationally expensive and have prerequisite tasks that must be checked first and executed if necessary?

=> Dependency Network

How do I represent a machine that reacts differently to events depending on the state it’s in?

=> State Machine

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.105.159