Commit 28eaf7de authored by Eckhart Arnold's avatar Eckhart Arnold
Browse files

- Extended Introduction

parent db9e1654
......@@ -85,7 +85,7 @@ from DHParser.syntaxtree import Node, traverse, remove_children_if, \\
remove_expendables, remove_empty, remove_tokens, flatten, is_whitespace, \\
is_empty, is_expendable, collapse, replace_content, WHITESPACE_PTYPE, TOKEN_PTYPE, \\
TransformationFunc, remove_parser, remove_content, remove_brackets, \\
keep_children, has_name, has_content, apply_if
keep_children, has_name, has_content, apply_if, remove_first, remove_last
......@@ -61,8 +61,8 @@ __all__ = ['WHITESPACE_PTYPE',
......@@ -827,6 +827,24 @@ remove_expendables = remove_children_if(is_expendable) # partial(remove_childre
remove_brackets = keep_children(slice(1,-1))
def remove_first(node, condition=lambda node: True):
"""Removes the first child if the condition is met.
Otherwise does nothing."""
if node.children:
if condition(node.children[0]):
node.result = node.result[1:]
def remove_last(node, condition=lambda node: True):
"""Removes the last child if the condition is met.
Otherwise does nothing."""
if node.children:
if condition(node.children[-1]):
node.result = node.result[:-1]
def remove_tokens(node, tokens: AbstractSet[str] = frozenset()):
"""Reomoves any among a particular set of tokens from the immediate
......@@ -18,4 +18,4 @@ permissions and limitations under the License.
import os
__version__ = '0.7.5' + '_dev' + str(os.stat(__file__).st_mtime)
__version__ = '0.7.6' # + '_dev' + str(os.stat(__file__).st_mtime)
Introduction to [DHParser](
*This is just an appetizer. Full documentation coming soon...*
Motto: **Computers enjoy XML, humans don't.**
Why use domain specific languages in the humanities?
Suppose you are a literary scientist and you would like to edit a poem
like Heinrich Heine's "Lyrisches Intermezzo". Usually, the technology
of choice would be XML and you would use an XML-Editor to write to
code something like this:
<?xml version="1.0" encoding="UTF-8" ?>
<autor gnd="118548018">Heinrich Heine</autor>
<werk href=""
Buch der Lieder
<ort gnd="4023118-5">Hamburg</ort>
<serie>Lyrisches Intermezzo</serie>
<vers>Wenn ich in deine Augen seh',</vers>
<vers>so schwindet all' mein Leid und Weh!</vers>
<vers>Doch wenn ich küsse deinen Mund,</vers>
<vers>so werd' ich ganz und gar gesund.</vers>
<vers>Wenn ich mich lehn' an deine Brust,</vers>
<vers>kommt's über mich wie Himmelslust,</vers>
<vers>doch wenn du sprichst: Ich liebe dich!</vers>
<vers>so muß ich weinen bitterlich.</vers>
Now, while you might think that this all works well enough, there are
a few drawbacks to this approach:
- The syntax is cumbersome and the encoding not very legible to humans
working with it. (And I did not even use
[TEI-XML](, yet...)
Editing and revising XML-encoded text is a pain. Just ask the
literary scientists who have to work with it.
- The XML encoding, especially TEI-XML, is often unintuitive. Only
experts understand it. Now, if you had the idea that you humanist
friend, who is not into digital technologies, might help you with
proof-reading, you better think about it again.
- There is an awful lot of typing to do: All those lengthy opening
and closing tags. This takes time...
- While looking for a good XML-Editor, you find that there hardly exist
any XML-Editors any more. (And for a reason, actually...) In
particular, there are no good open source XML-Editors.
One the other hand, there are good reasons why XML is used in the
humanities: Important encoding standards like TEI-XML are defined in
XML. It's strict syntax and the possibility to check data against a
schema help detecting and avoiding encoding errors. If the schema
is well defined, it is unambiguous, and it is easy to parse for a
computer. Most of these advantages, however, are on a technical level
and few of them are actually exclusive advantages of XML.
All in all this means, that while XML is a solid backend-technology,
it still is a pain to work with XML as a frontend-technology. This is
where DHParser comes in. It allows you to define your own domain
specific notation that is specifically tailored to your editing needs
and provides an infrastructure that - if you know a little
Python-programming - makes it very easy to convert your annotated
text into an XML-encoding of your choice. With DHParser, the same poem
above can be simply encoded like this:
Heinrich Heine <gnd:118548018>,
Buch der Lieder <urn:nbn:de:kobv:b4-200905192211>,
Hamburg <gnd:4023118-5>, 1927.
Lyrisches Intermezzo
Wenn ich in deine Augen seh',
so schwindet all' mein Leid und Weh!
Doch wenn ich küsse deinen Mund,
so werd' ich ganz und gar gesund.
Wenn ich mich lehn' an deine Brust,
kommt's über mich wie Himmelslust,
doch wenn du sprichst: Ich liebe dich!
so muß ich weinen bitterlich.
Yes, that's right. It is as simple as that. Observe, how much
more effacious a verse like "Wenn ich mich lehn' an deine Brust, /
kommt's über mich wie Himmelslust," can be if it is not cluttered with
XML tags ;-)
You might now wonder
whether the second version really does encode the same information
as the XML version. How, for example, would the computer know for
sure where a verse starts and ends or a stanza or what is
title and what stanza? Well, for all these matters there exist
conventions that poets have been using for several thousand years.
For example, a verse always starts and ends in one an the same
line. There is always a gap between stanzas. And the title is always
written above the poem and not in the middle of it. So, if there is
a title at all, we can be sure that what is written in the first
line is the title and not a stanza.
DHParser is able to exploit all those hints in order to gather much the
same information as was encoded in the XML-Version. Don't believe it?
You can try: Download DHParser from the
[gitlab-repository]( and enter
the directory `examples/Tutorial` on the command line interface (shell).
Just run `python` (you need to have installed
[Python]( Version 3.4 or higher on your computer).
The output will be something like this:
<namenfolge>Heinrich Heine</namenfolge>
<wortfolge>Buch der Lieder</wortfolge>
<serie>Lyrisches Intermezzo</serie>
<vers>Wenn ich in deine Augen seh',</vers>
<vers>so schwindet all' mein Leid und Weh!</vers>
<vers>Doch wenn ich küsse deinen Mund,</vers>
<vers>so werd' ich ganz und gar gesund.</vers>
<vers>Wenn ich mich lehn' an deine Brust,</vers>
<vers>kommt's über mich wie Himmelslust,</vers>
<vers>doch wenn du sprichst: Ich liebe dich!</vers>
<vers>so muß ich weinen bitterlich.</vers>
Now, you might notice that this is not exactly the XML-encoding as shown
above. (Can you spot the differences?) But you will probably believe me
without further proof that it can easily be converted into the other
version and contains all the information that the other version contains.
How does DHParser achieve this? Well, there is the rub. In order to convert
the poem in the domain specific version into the XML-version, DHParser
requires a structural description of the domain specific encoding. This
is a bit similar to a document type definition (DTD) in XML. This
structural description uses a slightly enhanced version of the
[Extended-Backus-Naur-Form (EBNF)](
that is a well established formalism for the structural description of
formal languages in computer sciences. And excerpt of the EBNF-definition
of our domain-specific encoding for the poem looks like this. (We leave out
the meta-data here. See
for the full EBNF):
gedicht = { LEERZEILE }+ [serie] §titel §text /\s*/ §ENDE
serie = !(titel vers NZ vers) { NZ zeile }+ { LEERZEILE }+
titel = { NZ zeile}+ { LEERZEILE }+
zeile = { ZEICHENFOLGE }+
text = { strophe {LEERZEILE} }+
strophe = { NZ vers }+
vers = { ZEICHENFOLGE }+
ZEICHENFOLGE = /[^ \n<>]+/~
NZ = /\n/~
LEERZEILE = /\n[ \t]*(?=\n)/~
ENDE = !/./
Now, without going into too much detail here, let me just explain a few basics of
this formal description: The slashes `/` enclose ordinary regular expressions.
Thus, `NZ` for ("Neue Zeile", German for: "new line") is defined as `/\n/~` which
is the newline-token `\n` in a regular expression, plus further horizontal
whitespace (signified by the tilde `~`), if there is any.
The braces `{` `}` enclose items that can be repeated zero or more times; with
a `+` appended to the closing brace it means one or more times. Now, look at the
definition of `text` in the 6th line: `{ strophe {LEERZEILE} }+`. This reads
as follows: The text of the poem consists of a sequence of stanzas, each of which
is followed by a sequence of empty lines (German: "Leerzeilen"). If you now
look a the structural definition of a stanza, you find that it consists of a
sequence of verses, each of which starts, i.e. is preceeded by a new line.
Can you figure out the rest? Hint: The angular brackets `[` and `]` mean that and
item is optional and the `§` sign means that it is obligatory. (Strictly speaking,
the §-signs are not necessary, because an item that is not optional is always
obligatory, but the §-signs help the converter to produce the right error
This should be enough for an introduction to the purpose of DSLs in the humanities.
It has shown the probably most important
use case of DHParser, i.e. as a frontend-technology form XML-encodings. Of course
it can just as well be used as a frontend for any other kind of structured data,
like SQL or graph-strcutured data. The latter is by the way is a very reasonable
alternative to XML for edition projects with a complex transmission history.
See Andreas Kuczera's Blog-entry on
["Graphdatenbanken für Historiker"](
Tutorial: First Steps with DHParser
Disclaimer: *You'll need to be able to use a shell and have some basic
knowledge of Python programming to be able to follow this section!*
In order to try the example above, you should fetch DHParsers from its
$ git clone
Now, if the enter the repo, you'll find three subdirectories:
The directory `DHParser` contains the Python modules of the DHParser-package, `test` - as you can
guess - contains the unit-tests for DHParser. Now, enter the `examples/Tutorial`-directory.
Presently, most other examples are pretty rudimentary. So, don't worry about them.
In this directory, you'll find a simple EBNF Grammar for poetry in the file `Lyrik.ebnf`. Have a
look at it. You'll find that is the same grammar (plus a few additions) that has been mentioned
just before. You'll also find a little script `` that is used to compile an
EBNF-Grammar into an executable Python-module that can be used to parse any piece of text that
this grammar is meant for; in this case poetry.
Any DHParser-Project needs such a script. The content of the script is pretty self-explanatory:
from DHParser.testing import recompile_grammar
if not recompile_grammar('.', force=True):
with open('Lyrik_ebnf_ERRORS.txt') as f:
The script simply (re-)compiles any EBNF grammar that it finds in the current directory. "Recompiling"
means that DHParser notices if a grammar has already been compiled and overwrites only that part of
the generated file that contains the actual parser. All other parts - we will come to that later
what these are - can safely be edited by you. Now just run `` from the command line:
$ python3
You'll find that `` has generated a new script with the name ``. This
script contains the Parser for the `Lyrik.ebnf`-grammar and some skeleton-code for a DSL->XML-Compiler
(or rather, a DSL-whatever compiler), which you can later fill in. Now let's see how this script works:
$ python3 Lyrisches_Intermezzo_IV.txt >result.xml
The file `Lyrisches_Intermezzo_IV.txt` contains the fourth part of Heinrich Heine's Lyrisches Intermezzo
encoded in our own human-readable poetry-DSL that has been shown above.
Since we have redirected the output to `result.xml`, you'll find a new file with this name in the
directory. If you look at it with an editor - preferably one that provides syntax-highlighting for
XML-files, you'll find that it look's pretty much like XML. However, this XML-code still looks
much more obfuscated than in the Introduction before. If you look closely, you can nonetheless see
that the poem itself has faithfully been preserved. For example, if you scroll down a few lines,
you'll find the, hardly recognizable first verse of the poem:
<:Whitespace> </:Whitespace>
<:Whitespace> </:Whitespace>
<:Whitespace> </:Whitespace>
<:Whitespace> </:Whitespace>
<:Whitespace> </:Whitespace>
How come it is so obfuscated, and where do all those pseudo-tags like `<:RegExp>` and
`<:Whitespace>` come from? Well, this is probably the right time to explain a bit about
parsing and compilation in general. Parsing and compilation of a text with DHParser
takes place in three strictly separated steps:
1. Parsing of the text and generation of the "concrete syntax tree" (CST)
2. Transformation of the CST into an "abstract syntax tree" (AST)
3. And, finally, compilation of the AST into valid XML, HTML, LaTeX or what you like.
DHParser automatically only generates a parser for the very first step. The other steps have
to be programmed by hand, though DHParser makes tries to make those parts as easy as possible.
What you have just seen in your editor is a Pseudo-XML-representation of the concrete syntax
tree. (The output of a parser always is a tree structure, just like XML.)
It is called concrete syntax tree, because it contains all the syntactic details that
have been described in the `Lyrik.ebnf`-grammar; and the grammar needs to describe all those
details, because otherwise it would not be possible to parse the text. On the other hand
most of these details do not carry any important information. This is the reason why in the
second step the transformation into an abstract syntax tree that leaves out the unimportant
details. There is now general rule of how to derive abstract syntax trees from concrete syntax
trees, and there cannot be, because it depends on the particular domain of application which
details are important and which not. For poems these might be different than, say, for a
catalogue entry. Therefore the AST-transformation has to be specified for each grammar
separately, just as the grammar has to be specified for each application domain.
Before I'll explain how to specify an AST-transformation for DHParser, you may want to
know what difference it makes. There is a script `` in the directory
where the AST-transformations are already included. Running the script
$ python Lyrisches_Intermezzo_IV.txt
yields the fairly clean Pseudo-XML-representation of the DSL-encoded poem that we have seen above.
Just as a teaser, you might want to look up, how the AST-transformation is specified with DHParser.
For this purpose, you can have a look in file ``. If you scrool down to
the AST section, you'll see something like this:
Lyrik_AST_transformation_table = {
# AST Transformations for the Lyrik-grammar
"+": remove_empty,
"bibliographisches": [remove_parser('NZ'), remove_tokens],
"autor": [],
"werk": [],
"untertitel": [],
"ort": [],
"jahr": [reduce_single_child],
"wortfolge": [flatten(has_name('WORT'), recursive=False), remove_last(is_whitespace), collapse],
"namenfolge": [flatten(has_name('NAME'), recursive=False), remove_last(is_whitespace), collapse],
"verknüpfung": [remove_tokens('<', '>'), reduce_single_child],
"ziel": reduce_single_child,
"gedicht, strophe, text": [flatten, remove_parser('LEERZEILE'), remove_parser('NZ')],
"titel, serie": [flatten, remove_parser('LEERZEILE'), remove_parser('NZ'), collapse],
"zeile": [],
"vers": collapse,
"WORT": [],
"NAME": [],
"ZEICHENFOLGE, NZ, JAHRESZAHL": reduce_single_child,
"ENDE": [],
":Whitespace": replace_content(lambda node : " "),
":Token, :RE": reduce_single_child,
"*": replace_by_single_child
As you can see, AST-transformations a specified in a declarative fashion (with the option to add
your own Python-programmed transformation rules). This keeps the specification of the AST-transformation
simple and concise. At the same, we avoid adding hints for the AST-transformation in the grammar
specification, which would render the grammar less readable.
Next, I am going to explain step by step, how a domain specific language for poems like Heine's
Lyrisches Intermezzo can be designed, specified, compiled and tested.
*to be continued, stay tuned...*
# EBNF-Syntax for the Testbed-minilanguage for DHParser
# Example for a Testbed. This example contains a few tests for the EBNF
# language
standard {"literal test double quoted"}
{'literal test single suoted'}
multiple lines
with gap'}
{'literal with right-attached whitespace' }
{ 'literal with left-attached whitespace'}
{quotation marks forgotten}
standard {letters}
whitespaced {whitespace }
{ no_whitespace_on_the_left}
{"with quotation marks it is a literal, not a symbol"}
standard {/\w+/}
{/whitespace/ }
whitespacemarker {~/\w+/~}
right_wsp {/\w+/~}
[{ symbol }]
DHParser-Testbed is a simple DSL to formulate Testcases for DSL development
with DHParser.
This diff is collapsed.
""" - unit tests for module ParserCombinators
Copyright 2016 by Eckhart Arnold
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
See the License for the specific language governing permissions and
limitations under the License.
import os
import re
import sys
from ParserCombinators import EBNFGrammar, EBNFTransTable, EBNFCompiler, full_compilation, Forward, RegExp, \
Alternative, Sequence, Token, compile_python_object, compileDSL, GrammarBase
arithmetic_EBNF = r"""
expression = term { ("+" | "-") term}
term = factor { ("*"|"/") factor}
factor = constant | variable | "(" expression ")"
variable = "x" | "y" | "z"
constant = digit {digit}
digit = "0" | "1" | "..." | "9"
test = digit constant variable
arithmetic_expected_result = """
class ArithmeticGrammar(ParserRoot):
constant = Forward()
digit = Forward()
expression = Forward()
variable = Forward()
wspc__ = mixin_comment(whitespace=r'\s*', comment=r'')
test = Sequence("test", digit, constant, variable)
digit.set(Alternative("digit", Token("0", wspcR=wspc__), Token("1", wspcR=wspc__), Token("...", wspcR=wspc__), Token("9", wspcR=wspc__)))
constant.set(Sequence("constant", digit, ZeroOrMore(None, digit)))
variable.set(Alternative("variable", Token("x", wspcR=wspc__), Token("y", wspcR=wspc__), Token("z", wspcR=wspc__)))
factor = Alternative("factor", constant, variable, Sequence(None, Token("(", wspcR=wspc__), expression, Token(")", wspcR=wspc__)))
term = Sequence("term", factor, ZeroOrMore(None, Series(None, Alternative(None, Token("*", wspcR=wspc__), Token("/", wspcR=wspc__)), factor)))
expression.set(Sequence("expression", term, ZeroOrMore(None, Series(None, Alternative(None, Token("+", wspcR=wspc__), Token("-", wspcR=wspc__)), term))))
root__ = expression
ebnf_EBNF = r"""
# Starting comment
@ whitespace = /\s*/ # '@' means the folowing assingment is a compiler directive
syntax = { production }
production = symbol "=" expression "."
expression = term { "|" term }
term = factor { factor }
factor = symbol
| literal
| regexp # regular expressions
| option
| repetition
| group
option = "[" expression "]"
repetition = "{" expression "}"
group = "(" expression ")"
symbol = ~/\w+/~
literal = ~/"(?:[^"]|\\")*"/~
| ~/'(?:[^']|\\')*'/~
regexp = ~/~\/(?:[^\/]|(?<=\\)\/)*\/~/~
| ~/\/(?:[^\/]|(?<=\\)\/)*\//~
# trailing whitespace and comments
ebnf_expected_result = r"""
class EBNFGrammar(ParserRoot):
expression = Forward()
wspc__ = mixin_comment(whitespace=r'\s*', comment=r'')
regexp = Alternative("regexp", RE('~/(?:[^/]|(?<=\\\\)/)*/~', wspcL=wspc__, wspcR=wspc__), RE('/(?:[^/]|(?<=\\\\)/)*/', wspcL=wspc__, wspcR=wspc__))
literal = Alternative("literal", RE('"(?:[^"]|\\\\")*"', wspcL=wspc__, wspcR=wspc__), RE("'(?:[^']|\\\\')*'", wspcL=wspc__, wspcR=wspc__))
symbol = RE('\\w+', "symbol", wspcL=wspc__, wspcR=wspc__)
group = Sequence("group", Token("(", wspcR=wspc__), expression, Token(")", wspcR=wspc__))
repetition = Sequence("repetition", Token("{", wspcR=wspc__), expression, Token("}", wspcR=wspc__))
option = Sequence("option", Token("[", wspcR=wspc__), expression, Token("]", wspcR=wspc__))
factor = Alternative("factor", symbol, literal, regexp, option, repetition, group)
term = Sequence("term", factor, ZeroOrMore(None, factor))
expression.set(Sequence("expression", term, ZeroOrMore(None, Series(None, Token("|", wspcR=wspc__), term))))
production = Sequence("production", symbol, Token("=", wspcR=wspc__), expression, Token(".", wspcR=wspc__))
syntax = ZeroOrMore("syntax", production)
root__ = syntax
class LeftRecursiveGrammar(GrammarBase):
"""formula = expr "."
expr = expr ("+"|"-") term | term
term = term ("*"|"/") factor | factor
factor = /[0-9]+/
expr = Forward()
term = Forward()
factor = RegExp("factor", '[0-9]+')
term.set(Alternative("term", Sequence(None, term, Alternative(None, Token("*"), Token("/")), factor), factor))
expr.set(Alternative("expr", Sequence(None, expr, Alternative(None, Token("+"), Token("-")), term), term))
formula = Sequence("formula", expr, Token("."))
root__ = formula
def rem_docstring(class_py):
return re.sub(r'r"""(?:.|\n)*"""\n ', '', class_py).strip()
class TestEBNFCompiler:
def test_EBNFGrammar(self):
assert (str(EBNFGrammar.root__) == str(EBNFGrammar.root__))
def test_arithmeticEBNF(self):
result, errors, syntax_tree = full_compilation(arithmetic_EBNF, EBNFGrammar(),
EBNFTransTable, EBNFCompiler('Arithmetic'))
assert result is not None, errors
assert arithmetic_expected_result.strip() == rem_docstring(result)
def test_ebnfEBNF(self):
result, errors, syntax_tree = full_compilation(ebnf_EBNF, EBNFGrammar(),
EBNFTransTable, EBNFCompiler('EBNF'))
assert not errors, str(errors)
assert ebnf_expected_result.strip() == rem_docstring(result)