Parse a small language with Prolog DCGs
8 steps · about 3 h 25 min · Intermediate
- Learn definite-clause grammar notation (guide, SWI-Prolog manual, 30 min)
Start with the grammar notation and its relationship to ordinary Prolog predicates.
- Run a grammar over complete input (reference, SWI-Prolog manual, 20 min)
Require the initial examples to consume their entire token list.
- Inspect unconsumed input (reference, SWI-Prolog manual, 20 min)
Use the remainder explicitly when composing tokenizers and parsers or reporting trailing text.
- Inspect grammar expansion (reference, SWI-Prolog manual, 20 min)
Connect DCG notation to the predicates Prolog actually executes when debugging a rule.
- Compose terminals and nonterminals (guide, SWI-Prolog manual, 30 min)
Build the language incrementally from tokens into nested command forms.
- Reuse higher-order grammar patterns (guide, SWI-Prolog manual, 25 min)
Factor repeated separated sequences and optional forms without obscuring the language rules.
- Tokenize text with standard DCG helpers (reference, SWI-Prolog library documentation, 25 min)
Use maintained primitives for blanks, numbers and text instead of fragile character handling.
- Test valid and invalid sentences (reference, SWI-Prolog manual, 35 min)
Lock down complete parses, rejected input and deliberately ambiguous cases with plunit.