liveBook · Manning

Ambiguous grammar

As explained in Building User-Friendly DSLs

A grammar for which some text has more than one valid parse according to the grammar.

An ambiguous grammar allows some text to have more than one valid parse. Parsing matters when textual DSL content must be turned into a usable structure; the grammar defines which strings and arrangements count as valid. The supplied grammar describes the Rental record syntax but does not demonstrate a multiply parsed input or show an ambiguity-resolution strategy. section 15.1

The practical boundary of the treatment is:

  • Textual DSL notation uses strings, with identifiers, quoted names, keywords, whitespace, attribute references, types, and values. section 15.1
  • EBNF expresses the record-type structure through productions such as attribute, value, and recordType. section 15.1
  • Ambiguity analysis and parser-conflict handling remain unspecified; the grammar is presented as a simpler, portable syntax. 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 productions to see how textual Rental content is assigned a syntactic structure. section 15.1

How Building User-Friendly DSLs covers it

How deep it goes
Defines EBNF productions and syntax choices but stops short of demonstrating ambiguity detection or resolution.
Examples
Uses a textual Rental record type and an EBNF grammar containing recordType, attribute, value, and attributeReference.
What it assumes
Assumes familiarity with textual notation while defining identifiers, whitespace, attribute references, types, and values in context.
What you can do after
Evaluate/understand how grammar productions describe valid textual DSL content.
How it connects
Connects textual DSLs with parsing, grammar, identifiers, whitespace, and EBNF.

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.