A textual domain-specific language represents its programs as text, so its notation and serialization format coincide. Keywords, identifiers, strings, and numbers provide the main visual building blocks. Parsing extracts structured data from text that conforms to a grammar. section 15.1 section 15.5 section 1.4.1
A parsing-based textual DSL typically proceeds through these implementation concerns:
- Choose readable syntax, including identifiers or quoted strings for domain names and explicit markers for references. section 15.1
- Define a grammar that recognizes keywords, whitespace, names, types, values, attributes, and complete domain declarations. section 15.1
- Parse valid text into structured content that can support an external DSL implementation. section 15.1 section 15.5
Rental record type
A compact textual form can declare a Rental record with a date range, monetary attributes, a percentage discount, initial values, and a reference to another attribute. Comparing this form with a non-textual notation exposes trade-offs in intuitiveness, visual clutter, missing business rules, and implementation complexity. section 15.1
Listing 15.2 A grammar in EBNF that can parse listing 15.1
STRING: '"' [A-Za-z0-9 ]+ '"' ; #1
type: 'amount' | 'date range' | 'percentage' ; #2
WS: (' ' | '\t' | '\n')+ ; #3
attributeReference: '->' WS? STRING ;
number: [0-9]+ ('.' [0-9]+)? ;
value: attributeReference | number ;
attribute: STRING WS? ':' WS? type (WS 'initially' WS? value)? ;
recordType: WS? 'record type' WS? STRING WS?
'having attributes:' (WS? attribute)* WS? ;
Inspect the EBNF grammar that parses the Rental declaration, including lexical tokens, attribute references, values, attributes, and the record-type rule. section 15.1
