liveBook · Manning

Textual domain-specific language

As explained in Building User-Friendly DSLs

A domain-specific language whose programs are represented as text and processed through textual syntax and parsing.

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

How Building User-Friendly DSLs covers it

How deep it goes
Explains notation, identifiers, grammar, and parsing with a worked Rental syntax, while deferring deeper language-engineering treatment.
Examples
Uses a textual Rental record declaration, its EBNF grammar, and an exercise comparing textual and alternative notations.
What you can do after
Build it from scratch by designing textual notation and defining a grammar capable of parsing domain declarations.
How it connects
Links parsing, grammars, external DSLs, software language engineering, and language workbenches.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building User-Friendly DSLs. The section links point to the parts of the book it draws on.