| 1 |
Generally Useful RPython Modules |
| 2 |
================================ |
| 3 |
|
| 4 |
.. contents:: |
| 5 |
|
| 6 |
|
| 7 |
This page lists some of the modules in :source:`rpython/rlib` together with some hints |
| 8 |
for what they can be used for. The modules here will make up some general |
| 9 |
library useful for RPython programs (since most of the standard library modules |
| 10 |
are not RPython). Most of these modules are somewhat rough still and are likely |
| 11 |
to change at some point. Usually it is useful to look at the tests in |
| 12 |
:source:`rpython/rlib/test` to get an impression of how to use a module. |
| 13 |
|
| 14 |
|
| 15 |
listsort |
| 16 |
-------- |
| 17 |
|
| 18 |
The :source:`rpython/rlib/listsort.py` module contains an implementation |
| 19 |
of the timsort sorting algorithm (the sort method of lists is not |
| 20 |
RPython). To use it, make (globally) one class by calling ``MySort = |
| 21 |
listsort.make_timsort_class(lt=my_comparison_func)``. There are also |
| 22 |
other optional arguments, but usually you give with ``lt=...`` a |
| 23 |
function that compares two objects from your lists. You need one class |
| 24 |
per "type" of list and per comparison function. |
| 25 |
|
| 26 |
The constructor of ``MySort`` takes a list as an argument, which will be |
| 27 |
sorted in place when the ``sort`` method of the ``MySort`` instance is |
| 28 |
called. |
| 29 |
|
| 30 |
|
| 31 |
nonconst |
| 32 |
-------- |
| 33 |
|
| 34 |
The :source:`rpython/rlib/nonconst.py` module is useful mostly for tests. The :ref:`flow graph builder<flow-graphs>` and |
| 35 |
the :ref:`annotator <annotator>` do quite some constant folding, which is sometimes not desired |
| 36 |
in a test. To prevent constant folding on a certain value, use the ``NonConst`` |
| 37 |
class. The constructor of ``NonConst`` takes an arbitrary value. The instance of |
| 38 |
``NonConst`` will behave during annotation like that value, but no constant |
| 39 |
folding will happen. |
| 40 |
|
| 41 |
|
| 42 |
objectmodel |
| 43 |
----------- |
| 44 |
|
| 45 |
The :source:`rpython/rlib/objectmodel.py` module is a mixed bag of various functionality. Some of the |
| 46 |
more useful ones are: |
| 47 |
|
| 48 |
``ComputedIntSymbolic``: |
| 49 |
Instances of ``ComputedIntSymbolic`` are treated like integers of unknown |
| 50 |
value by the annotator. The value is determined by a no-argument function |
| 51 |
(which needs to be passed into the constructor of the class). When the |
| 52 |
backend emits code, the function is called to determine the value. |
| 53 |
|
| 54 |
``CDefinedIntSymbolic``: |
| 55 |
Instances of ``CDefinedIntSymbolic`` are also treated like integers of |
| 56 |
unknown value by the annotator. When C code is emitted they will be |
| 57 |
represented by the attribute ``expr`` of the symbolic (which is also the |
| 58 |
first argument of the constructor). |
| 59 |
|
| 60 |
``r_dict``: |
| 61 |
An RPython dict-like object. The constructor of r_dict takes two functions: |
| 62 |
``key_eq`` and ``key_hash`` which are used for comparing and hashing the |
| 63 |
entries in the dictionary. |
| 64 |
|
| 65 |
``instantiate(cls)``: |
| 66 |
Instantiate class ``cls`` without calling ``__init__``. |
| 67 |
|
| 68 |
``we_are_translated()``: |
| 69 |
This function returns ``False`` when run on top of CPython, but the |
| 70 |
annotator thinks its return value is ``True``. Therefore it can be used to |
| 71 |
do different things on top of CPython than after translation. This should be |
| 72 |
used extremely sparingly (mostly for optimizations or debug code). |
| 73 |
|
| 74 |
``cast_object_to_weakaddress(obj)``: |
| 75 |
Returns a sort of "weak reference" to obj, just without any convenience. The |
| 76 |
weak address that it returns is not invalidated if the object dies, so you |
| 77 |
need to take care yourself to know when the object dies. Use with extreme |
| 78 |
care. |
| 79 |
|
| 80 |
``cast_weakadress_to_object(obj)``: |
| 81 |
Inverse of the previous function. If the object died then a segfault will |
| 82 |
ensue. |
| 83 |
|
| 84 |
``UnboxedValue``: |
| 85 |
This is a class which should be used as a base class for a class which |
| 86 |
carries exactly one integer field. The class should have ``__slots__`` |
| 87 |
with exactly one entry defined. After translation, instances of this class |
| 88 |
won't be allocated but represented by *tagged pointers**, that is pointers |
| 89 |
that have the lowest bit set. |
| 90 |
|
| 91 |
|
| 92 |
rarithmetic |
| 93 |
----------- |
| 94 |
|
| 95 |
The :source:`rpython/rlib/rarithmetic.py` module contains functionality to handle the small differences |
| 96 |
in the behaviour of arithmetic code in regular Python and RPython code. Most of |
| 97 |
them are already described in the :doc:`RPython description <rpython>`. |
| 98 |
|
| 99 |
|
| 100 |
rbigint |
| 101 |
------- |
| 102 |
|
| 103 |
The :source:`rpython/rlib/rbigint.py` module contains a full RPython implementation of the Python ``long`` |
| 104 |
type (which itself is not supported in RPython). The ``rbigint`` class contains |
| 105 |
that implementation. To construct ``rbigint`` instances use the static methods |
| 106 |
``fromint``, ``frombool``, ``fromfloat`` and ``fromdecimalstr``. To convert back |
| 107 |
to other types use the methods ``toint``, ``tobool``, ``touint`` and |
| 108 |
``tofloat``. Since RPython does not support operator overloading, all the |
| 109 |
special methods of ``rbigint`` that would normally start and end with "__" have |
| 110 |
these underscores left out for better readability (so ``a.add(b)`` can be used |
| 111 |
to add two rbigint instances). |
| 112 |
|
| 113 |
|
| 114 |
rrandom |
| 115 |
------- |
| 116 |
|
| 117 |
The :source:`rpython/rlib/rrandom.py` module contains an implementation of the mersenne twister random |
| 118 |
number generator. It contains one class ``Random`` which most importantly has a |
| 119 |
``random`` method which returns a pseudo-random floating point number between |
| 120 |
0.0 and 1.0. |
| 121 |
|
| 122 |
|
| 123 |
rsocket |
| 124 |
------- |
| 125 |
|
| 126 |
The :source:`rpython/rlib/rsocket.py` module contains an RPython implementation of the functionality of |
| 127 |
the socket standard library with a slightly different interface. The |
| 128 |
difficulty with the Python socket API is that addresses are not "well-typed" |
| 129 |
objects: depending on the address family they are tuples, or strings, and |
| 130 |
so on, which is not suitable for RPython. Instead, ``rsocket`` contains |
| 131 |
a hierarchy of Address classes, in a typical static-OO-programming style. |
| 132 |
|
| 133 |
|
| 134 |
rstrategies |
| 135 |
----------- |
| 136 |
|
| 137 |
The :source:`rpython/rlib/rstrategies` module contains a library to implement storage strategies in |
| 138 |
RPython VMs. The library is language-independent and extensible. |
| 139 |
More details and examples can be found in the :doc:`rstrategies documentation <rstrategies>`. |
| 140 |
|
| 141 |
|
| 142 |
streamio |
| 143 |
-------- |
| 144 |
|
| 145 |
The :source:`rpython/rlib/streamio.py` contains an RPython stream I/O implementation (which was started |
| 146 |
by Guido van Rossum as `sio.py`_ in the CPython sandbox as a prototype for the |
| 147 |
upcoming new file implementation in Python 3000). |
| 148 |
|
| 149 |
.. _sio.py: http://svn.python.org/view/sandbox/trunk/sio/sio.py |
| 150 |
|
| 151 |
|
| 152 |
unroll |
| 153 |
------ |
| 154 |
|
| 155 |
The :source:`rpython/rlib/unroll.py` module most importantly contains the function ``unrolling_iterable`` |
| 156 |
which wraps an iterator. Looping over the iterator in RPython code will not |
| 157 |
produce a loop in the resulting flow graph but will unroll the loop instead. |
| 158 |
|
| 159 |
|
| 160 |
rsre |
| 161 |
---- |
| 162 |
|
| 163 |
The implementation of regular expressions we use for PyPy. Note that it |
| 164 |
is hard to reuse in other languages: in Python, regular expressions are |
| 165 |
first compiled into a bytecode format by pure Python code from the |
| 166 |
standard library. This lower-level module only understands this |
| 167 |
bytecode format. Without a complete Python interpreter you can't |
| 168 |
translate the regexp syntax to the bytecode format. (There are hacks |
| 169 |
for limited use cases where you have only static regexps: they can be |
| 170 |
precompiled during translation. Alternatively, you could imagine |
| 171 |
executing a Python subprocess just to translate a regexp at runtime...) |
| 172 |
|
| 173 |
|
| 174 |
parsing |
| 175 |
------- |
| 176 |
|
| 177 |
The :source:`rpython/rlib/parsing/` module is a still in-development module to generate tokenizers and |
| 178 |
parsers in RPython. It is still highly experimental and only really used by the |
| 179 |
`Prolog interpreter`_ (although in slightly non-standard ways). The easiest way |
| 180 |
to specify a tokenizer/grammar is to write it down using regular expressions and |
| 181 |
simple EBNF format. |
| 182 |
|
| 183 |
The regular expressions are implemented using finite automatons. The parsing |
| 184 |
engine uses `packrat parsing`_, which has O(n) parsing time but is more |
| 185 |
powerful than LL(n) and LR(n) grammars. |
| 186 |
|
| 187 |
.. _packrat parsing: http://pdos.csail.mit.edu/~baford/packrat/ |
| 188 |
|
| 189 |
|
| 190 |
Regular Expressions |
| 191 |
------------------- |
| 192 |
|
| 193 |
The regular expression syntax is mostly a subset of the syntax of the `re`_ |
| 194 |
module. *Note: this is different from rlib.rsre.* |
| 195 |
By default, non-special characters match themselves. If you concatenate |
| 196 |
regular expressions the result will match the concatenation of strings matched |
| 197 |
by the single regular expressions. |
| 198 |
|
| 199 |
``|`` |
| 200 |
``R|S`` matches any string that *either* matches R or matches S. |
| 201 |
|
| 202 |
``*`` |
| 203 |
``R*`` matches 0 or more repetitions of R. |
| 204 |
|
| 205 |
``+`` |
| 206 |
``R+`` matches 1 or more repetitions of R. |
| 207 |
|
| 208 |
``?`` |
| 209 |
``R?`` matches 0 or 1 repetition of R. |
| 210 |
|
| 211 |
``(...)`` |
| 212 |
Parenthesis can be used to group regular expressions (note that in contrast |
| 213 |
to Python's re module you cannot later match the content of this group). |
| 214 |
|
| 215 |
``{m}`` |
| 216 |
``R{m}`` matches exactly m repetitions of R. |
| 217 |
|
| 218 |
``{m, n}`` |
| 219 |
``R{m, n}`` matches between m and n repetitions of R (including m and n). |
| 220 |
|
| 221 |
``[]`` |
| 222 |
Matches a set of characters. The characters to be matched can be listed |
| 223 |
sequentially. A range of characters can be specified using ``-``. For |
| 224 |
examples ``[ac-eg]`` matches the characters a, c, d, e and g. |
| 225 |
The whole set can be inverted by starting it with ``^``. So [^a] matches |
| 226 |
anything except a. |
| 227 |
|
| 228 |
To parse a regular expression and to get a matcher for it, you can use the |
| 229 |
function ``make_runner(s)`` in the ``rpython.rlib.parsing.regexparse`` module. It |
| 230 |
returns a object with a ``recognize(input)`` method that returns True or False |
| 231 |
depending on whether ``input`` matches the string or not. |
| 232 |
|
| 233 |
.. _re: http://docs.python.org/library/re.html |
| 234 |
|
| 235 |
|
| 236 |
EBNF |
| 237 |
---- |
| 238 |
|
| 239 |
To describe a tokenizer and a grammar the ``rpython.rlib.parsing.ebnfparse`` |
| 240 |
defines a syntax for doing that. |
| 241 |
|
| 242 |
The syntax file contains a sequence or rules. Every rule either describes a |
| 243 |
regular expression or a grammar rule. |
| 244 |
|
| 245 |
Regular expressions rules have the form:: |
| 246 |
|
| 247 |
NAME: "regex"; |
| 248 |
|
| 249 |
NAME is the name of the token that the regular expression |
| 250 |
produces (it has to consist of upper-case letters), ``regex`` is a regular |
| 251 |
expression with the syntax described above. One token name is special-cased: a |
| 252 |
token called ``IGNORE`` will be filtered out of the token stream before being |
| 253 |
passed on to the parser and can thus be used to match comments or |
| 254 |
non-significant whitespace. |
| 255 |
|
| 256 |
Grammar rules have the form:: |
| 257 |
|
| 258 |
name: expansion_1 | expansion_2 | ... | expansion_n; |
| 259 |
|
| 260 |
Where ``expansion_i`` is a sequence of nonterminal or token names:: |
| 261 |
|
| 262 |
symbol_1 symbol_2 symbol_3 ... symbol_n |
| 263 |
|
| 264 |
This means that the nonterminal symbol ``name`` (which has to consist of |
| 265 |
lower-case letters) can be expanded into any of the expansions. The expansions |
| 266 |
can consist of a sequence of token names, nonterminal names or literals, which |
| 267 |
are strings in quotes that are matched literally. |
| 268 |
|
| 269 |
An example to make this clearer:: |
| 270 |
|
| 271 |
IGNORE: " "; |
| 272 |
DECIMAL: "0|[1-9][0-9]*"; |
| 273 |
additive: multitive "+" additive | |
| 274 |
multitive; |
| 275 |
multitive: primary "*" multitive | |
| 276 |
primary; |
| 277 |
primary: "(" additive ")" | DECIMAL; |
| 278 |
|
| 279 |
This grammar describes the syntax of arithmetic impressions involving addition |
| 280 |
and multiplication. The tokenizer |
| 281 |
produces a stream of either DECIMAL tokens or tokens that have matched one of |
| 282 |
the literals "+", "*", "(" or ")". Any space will be ignored. The grammar |
| 283 |
produces a syntax tree that follows the precedence of the operators. For example |
| 284 |
the expression ``12 + 4 * 5`` is parsed into the following tree: |
| 285 |
|
| 286 |
.. graphviz:: |
| 287 |
|
| 288 |
digraph G{ |
| 289 |
"-1213931828" [label="additive"]; |
| 290 |
"-1213931828" -> "-1213951956"; |
| 291 |
"-1213951956" [label="multitive"]; |
| 292 |
"-1213951956" -> "-1213949172"; |
| 293 |
"-1213949172" [label="primary"]; |
| 294 |
"-1213949172" -> "-1213949812"; |
| 295 |
"-1213949812" [shape=box,label="DECIMAL\l'12'"]; |
| 296 |
"-1213931828" -> "-1213935220"; |
| 297 |
"-1213935220" [shape=box,label="__0_+\l'+'"]; |
| 298 |
"-1213931828" -> "-1213951316"; |
| 299 |
"-1213951316" [label="additive"]; |
| 300 |
"-1213951316" -> "-1213948180"; |
| 301 |
"-1213948180" [label="multitive"]; |
| 302 |
"-1213948180" -> "-1213951380"; |
| 303 |
"-1213951380" [label="primary"]; |
| 304 |
"-1213951380" -> "-1213951508"; |
| 305 |
"-1213951508" [shape=box,label="DECIMAL\l'4'"]; |
| 306 |
"-1213948180" -> "-1213948788"; |
| 307 |
"-1213948788" [shape=box,label="__1_*\l'*'"]; |
| 308 |
"-1213948180" -> "-1213951060"; |
| 309 |
"-1213951060" [label="multitive"]; |
| 310 |
"-1213951060" -> "-1213948980"; |
| 311 |
"-1213948980" [label="primary"]; |
| 312 |
"-1213948980" -> "-1213950420"; |
| 313 |
"-1213950420" [shape=box,label="DECIMAL\l'5'"]; |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
Parse Trees |
| 318 |
----------- |
| 319 |
|
| 320 |
The parsing process builds up a tree consisting of instances of ``Symbol`` and |
| 321 |
``Nonterminal``, the former corresponding to tokens, the latter to nonterminal |
| 322 |
symbols. Both classes live in the :source:`rpython/rlib/parsing/tree.py` module. You can use |
| 323 |
the ``view()`` method ``Nonterminal`` instances to get a pygame view of the |
| 324 |
parse tree. |
| 325 |
|
| 326 |
``Symbol`` instances have the following attributes: ``symbol``, which is the |
| 327 |
name of the token and ``additional_info`` which is the matched source. |
| 328 |
|
| 329 |
``Nonterminal`` instances have the following attributes: ``symbol`` is the name |
| 330 |
of the nonterminal and ``children`` which is a list of the children attributes. |
| 331 |
|
| 332 |
|
| 333 |
Visitors |
| 334 |
~~~~~~~~ |
| 335 |
|
| 336 |
To write tree visitors for the parse trees that are RPython, there is a special |
| 337 |
baseclass ``RPythonVisitor`` in :source:`rpython/rlib/parsing/tree.py` to use. If your |
| 338 |
class uses this, it will grow a ``dispatch(node)`` method, that calls an |
| 339 |
appropriate ``visit_<symbol>`` method, depending on the ``node`` argument. Here |
| 340 |
the <symbol> is replaced by the ``symbol`` attribute of the visited node. |
| 341 |
|
| 342 |
For the visitor to be RPython, the return values of all the visit methods need |
| 343 |
to be of the same type. |
| 344 |
|
| 345 |
|
| 346 |
Tree Transformations |
| 347 |
-------------------- |
| 348 |
|
| 349 |
As the tree of arithmetic example above shows, by default the parse tree |
| 350 |
contains a lot of nodes that are not really conveying useful information. |
| 351 |
To get rid of some of them, there is some support in the grammar format to |
| 352 |
automatically create a visitor that transforms the tree to remove the additional |
| 353 |
nodes. The simplest such transformation just removes nodes, but there are |
| 354 |
more complex ones. |
| 355 |
|
| 356 |
The syntax for these transformations is to enclose symbols in expansions of a |
| 357 |
nonterminal by [...], <...> or >...<. |
| 358 |
|
| 359 |
|
| 360 |
[symbol_1 symbol_2 ... symbol_n] |
| 361 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 362 |
|
| 363 |
This will produce a transformer that completely removes the enclosed symbols |
| 364 |
from the tree. |
| 365 |
|
| 366 |
Example:: |
| 367 |
|
| 368 |
IGNORE: " "; |
| 369 |
n: "A" [","] n | "A"; |
| 370 |
|
| 371 |
Parsing the string "A, A, A" gives the tree: |
| 372 |
|
| 373 |
.. graphviz:: |
| 374 |
|
| 375 |
digraph G{ |
| 376 |
"-1213678004" [label="n"]; |
| 377 |
"-1213678004" -> "-1213681108"; |
| 378 |
"-1213681108" [shape=box,label="__0_A\n'A'"]; |
| 379 |
"-1213678004" -> "-1213681332"; |
| 380 |
"-1213681332" [shape=box,label="__1_,\n','"]; |
| 381 |
"-1213678004" -> "-1213837780"; |
| 382 |
"-1213837780" [label="n"]; |
| 383 |
"-1213837780" -> "-1213837716"; |
| 384 |
"-1213837716" [shape=box,label="__0_A\n'A'"]; |
| 385 |
"-1213837780" -> "-1213839476"; |
| 386 |
"-1213839476" [shape=box,label="__1_,\n','"]; |
| 387 |
"-1213837780" -> "-1213839956"; |
| 388 |
"-1213839956" [label="n"]; |
| 389 |
"-1213839956" -> "-1213840948"; |
| 390 |
"-1213840948" [shape=box,label="__0_A\n'A'"]; |
| 391 |
} |
| 392 |
|
| 393 |
After transformation the tree has the "," nodes removed: |
| 394 |
|
| 395 |
.. graphviz:: |
| 396 |
|
| 397 |
digraph G{ |
| 398 |
"-1219325716" [label="n"]; |
| 399 |
"-1219325716" -> "-1219325844"; |
| 400 |
"-1219325844" [shape=box,label="__0_A\n'A'"]; |
| 401 |
"-1219325716" -> "-1219324372"; |
| 402 |
"-1219324372" [label="n"]; |
| 403 |
"-1219324372" -> "-1219325524"; |
| 404 |
"-1219325524" [shape=box,label="__0_A\n'A'"]; |
| 405 |
"-1219324372" -> "-1219324308"; |
| 406 |
"-1219324308" [label="n"]; |
| 407 |
"-1219324308" -> "-1219325492"; |
| 408 |
"-1219325492" [shape=box,label="__0_A\n'A'"]; |
| 409 |
} |
| 410 |
|
| 411 |
|
| 412 |
<symbol> |
| 413 |
~~~~~~~~ |
| 414 |
|
| 415 |
This will replace the parent with symbol. Every expansion can contain at most |
| 416 |
one symbol that is enclosed by <...>, because the parent can only be replaced |
| 417 |
once, obviously. |
| 418 |
|
| 419 |
Example:: |
| 420 |
|
| 421 |
IGNORE: " "; |
| 422 |
n: "a" "b" "c" m; |
| 423 |
m: "(" <n> ")" | "d"; |
| 424 |
|
| 425 |
Parsing the string "a b c (a b c d)" gives the tree: |
| 426 |
|
| 427 |
.. graphviz:: |
| 428 |
|
| 429 |
digraph G{ |
| 430 |
"-1214029460" [label="n"]; |
| 431 |
"-1214029460" -> "-1214026452"; |
| 432 |
"-1214026452" [shape=box,label="__0_a\n'a'"]; |
| 433 |
"-1214029460" -> "-1214028276"; |
| 434 |
"-1214028276" [shape=box,label="__1_b\n'b'"]; |
| 435 |
"-1214029460" -> "-1214027316"; |
| 436 |
"-1214027316" [shape=box,label="__2_c\n'c'"]; |
| 437 |
"-1214029460" -> "-1214026868"; |
| 438 |
"-1214026868" [label="m"]; |
| 439 |
"-1214026868" -> "-1214140436"; |
| 440 |
"-1214140436" [shape=box,label="__3_(\n'('"]; |
| 441 |
"-1214026868" -> "-1214143508"; |
| 442 |
"-1214143508" [label="n"]; |
| 443 |
"-1214143508" -> "-1214141364"; |
| 444 |
"-1214141364" [shape=box,label="__0_a\n'a'"]; |
| 445 |
"-1214143508" -> "-1214141748"; |
| 446 |
"-1214141748" [shape=box,label="__1_b\n'b'"]; |
| 447 |
"-1214143508" -> "-1214140756"; |
| 448 |
"-1214140756" [shape=box,label="__2_c\n'c'"]; |
| 449 |
"-1214143508" -> "-1214144468"; |
| 450 |
"-1214144468" [label="m"]; |
| 451 |
"-1214144468" -> "-1214414868"; |
| 452 |
"-1214414868" [shape=box,label="__5_d\n'd'"]; |
| 453 |
"-1214026868" -> "-1214141492"; |
| 454 |
"-1214141492" [shape=box,label="__4_)\n')'"]; |
| 455 |
} |
| 456 |
|
| 457 |
After transformation the tree looks like this: |
| 458 |
|
| 459 |
.. graphviz:: |
| 460 |
|
| 461 |
digraph G{ |
| 462 |
"-1219949908" [label="n"]; |
| 463 |
"-1219949908" -> "-1214026452"; |
| 464 |
"-1214026452" [shape=box,label="__0_a\n'a'"]; |
| 465 |
"-1219949908" -> "-1214028276"; |
| 466 |
"-1214028276" [shape=box,label="__1_b\n'b'"]; |
| 467 |
"-1219949908" -> "-1214027316"; |
| 468 |
"-1214027316" [shape=box,label="__2_c\n'c'"]; |
| 469 |
"-1219949908" -> "-1219949876"; |
| 470 |
"-1219949876" [label="n"]; |
| 471 |
"-1219949876" -> "-1214141364"; |
| 472 |
"-1214141364" [shape=box,label="__0_a\n'a'"]; |
| 473 |
"-1219949876" -> "-1214141748"; |
| 474 |
"-1214141748" [shape=box,label="__1_b\n'b'"]; |
| 475 |
"-1219949876" -> "-1214140756"; |
| 476 |
"-1214140756" [shape=box,label="__2_c\n'c'"]; |
| 477 |
"-1219949876" -> "-1219949748"; |
| 478 |
"-1219949748" [label="m"]; |
| 479 |
"-1219949748" -> "-1214414868"; |
| 480 |
"-1214414868" [shape=box,label="__5_d\n'd'"]; |
| 481 |
} |
| 482 |
|
| 483 |
|
| 484 |
>nonterminal_1 nonterminal_2 ... nonterminal_n< |
| 485 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 486 |
|
| 487 |
This replaces the nodes nonterminal_1 to nonterminal_n by their children. |
| 488 |
|
| 489 |
Example:: |
| 490 |
|
| 491 |
IGNORE: " "; |
| 492 |
DECIMAL: "0|[1-9][0-9]*"; |
| 493 |
list: DECIMAL >list< | DECIMAL; |
| 494 |
|
| 495 |
Parsing the string "1 2" gives the tree: |
| 496 |
|
| 497 |
.. graphviz:: |
| 498 |
|
| 499 |
digraph G{ |
| 500 |
"-1213518708" [label="list"]; |
| 501 |
"-1213518708" -> "-1213518196"; |
| 502 |
"-1213518196" [shape=box,label="DECIMAL\n'1'"]; |
| 503 |
"-1213518708" -> "-1213518260"; |
| 504 |
"-1213518260" [label="list"]; |
| 505 |
"-1213518260" -> "-1213520308"; |
| 506 |
"-1213520308" [shape=box,label="DECIMAL\n'2'"]; |
| 507 |
} |
| 508 |
|
| 509 |
after the transformation the tree looks like: |
| 510 |
|
| 511 |
.. graphviz:: |
| 512 |
|
| 513 |
digraph G{ |
| 514 |
"-1219505652" [label="list"]; |
| 515 |
"-1219505652" -> "-1213518196"; |
| 516 |
"-1213518196" [shape=box,label="DECIMAL\n'1'"]; |
| 517 |
"-1219505652" -> "-1213520308"; |
| 518 |
"-1213520308" [shape=box,label="DECIMAL\n'2'"]; |
| 519 |
} |
| 520 |
|
| 521 |
Note that the transformation works recursively. That means that the following |
| 522 |
also works: if the string "1 2 3 4 5" is parsed the tree at first looks like |
| 523 |
this: |
| 524 |
|
| 525 |
.. graphviz:: |
| 526 |
|
| 527 |
digraph G{ |
| 528 |
"-1213611892" [label="list"]; |
| 529 |
"-1213611892" -> "-1213608980"; |
| 530 |
"-1213608980" [shape=box,label="DECIMAL\n'1'"]; |
| 531 |
"-1213611892" -> "-1213623476"; |
| 532 |
"-1213623476" [label="list"]; |
| 533 |
"-1213623476" -> "-1213623380"; |
| 534 |
"-1213623380" [shape=box,label="DECIMAL\n'2'"]; |
| 535 |
"-1213623476" -> "-1213442868"; |
| 536 |
"-1213442868" [label="list"]; |
| 537 |
"-1213442868" -> "-1213441652"; |
| 538 |
"-1213441652" [shape=box,label="DECIMAL\n'3'"]; |
| 539 |
"-1213442868" -> "-1213441332"; |
| 540 |
"-1213441332" [label="list"]; |
| 541 |
"-1213441332" -> "-1213441620"; |
| 542 |
"-1213441620" [shape=box,label="DECIMAL\n'4'"]; |
| 543 |
"-1213441332" -> "-1213443060"; |
| 544 |
"-1213443060" [label="list"]; |
| 545 |
"-1213443060" -> "-1213442100"; |
| 546 |
"-1213442100" [shape=box,label="DECIMAL\n'5'"]; |
| 547 |
} |
| 548 |
|
| 549 |
But after transformation the whole thing collapses to one node with a lot of |
| 550 |
children: |
| 551 |
|
| 552 |
.. graphviz:: |
| 553 |
|
| 554 |
digraph G{ |
| 555 |
"-1219430228" [label="list"]; |
| 556 |
"-1219430228" -> "-1213608980"; |
| 557 |
"-1213608980" [shape=box,label="DECIMAL\n'1'"]; |
| 558 |
"-1219430228" -> "-1213623380"; |
| 559 |
"-1213623380" [shape=box,label="DECIMAL\n'2'"]; |
| 560 |
"-1219430228" -> "-1213441652"; |
| 561 |
"-1213441652" [shape=box,label="DECIMAL\n'3'"]; |
| 562 |
"-1219430228" -> "-1213441620"; |
| 563 |
"-1213441620" [shape=box,label="DECIMAL\n'4'"]; |
| 564 |
"-1219430228" -> "-1213442100"; |
| 565 |
"-1213442100" [shape=box,label="DECIMAL\n'5'"]; |
| 566 |
} |
| 567 |
|
| 568 |
|
| 569 |
Extensions to the EBNF grammar format |
| 570 |
------------------------------------- |
| 571 |
|
| 572 |
There are some extensions to the EBNF grammar format that are really only |
| 573 |
syntactic sugar but make writing grammars less tedious. These are: |
| 574 |
|
| 575 |
``symbol?``: |
| 576 |
matches 0 or 1 repetitions of symbol |
| 577 |
|
| 578 |
``symbol*``: |
| 579 |
matches 0 or more repetitions of symbol. After the tree transformation all |
| 580 |
these repetitions are children of the current symbol. |
| 581 |
|
| 582 |
``symbol+``: |
| 583 |
matches 1 or more repetitions of symbol. After the tree transformation all |
| 584 |
these repetitions are children of the current symbol. |
| 585 |
|
| 586 |
These are implemented by adding some more rules to the grammar in the correct |
| 587 |
way. Examples: the grammar:: |
| 588 |
|
| 589 |
s: a b? c; |
| 590 |
|
| 591 |
is transformed to look like this:: |
| 592 |
|
| 593 |
s: a >_maybe_symbol_0_< c | a c; |
| 594 |
_maybe_symbol_0_: b; |
| 595 |
|
| 596 |
The grammar:: |
| 597 |
|
| 598 |
s: a b* c; |
| 599 |
|
| 600 |
is transformed to look like this:: |
| 601 |
|
| 602 |
s: a >_star_symbol_0< c | a c; |
| 603 |
_star_symbol_0: b >_symbol_star_0< | b; |
| 604 |
|
| 605 |
The grammar:: |
| 606 |
|
| 607 |
s: a b+ c; |
| 608 |
|
| 609 |
is transformed to look like this:: |
| 610 |
|
| 611 |
s: a >_plus_symbol_0< c; |
| 612 |
_plus_symbol_0: b >_plus_symbol_0< | b; |
| 613 |
|
| 614 |
|
| 615 |
Full Example |
| 616 |
------------ |
| 617 |
|
| 618 |
A semi-complete parser for the `json format`_:: |
| 619 |
|
| 620 |
STRING: "\\"[^\\\\"]*\\""; |
| 621 |
NUMBER: "\-?(0|[1-9][0-9]*)(\.[0-9]+)?([eE][\+\-]?[0-9]+)?"; |
| 622 |
IGNORE: " |\n"; |
| 623 |
value: <STRING> | <NUMBER> | <object> | <array> | <"null"> | |
| 624 |
<"true"> | <"false">; |
| 625 |
object: ["{"] (entry [","])* entry ["}"]; |
| 626 |
array: ["["] (value [","])* value ["]"]; |
| 627 |
entry: STRING [":"] value; |
| 628 |
|
| 629 |
|
| 630 |
The resulting tree for parsing the string:: |
| 631 |
|
| 632 |
{"a": "5", "b": [1, null, 3, true, {"f": "g", "h": 6}]} |
| 633 |
|
| 634 |
looks like this: |
| 635 |
|
| 636 |
.. graphviz:: |
| 637 |
|
| 638 |
digraph G{ |
| 639 |
"-1220061652" [label="object"]; |
| 640 |
"-1220061652" -> "-1220127636"; |
| 641 |
"-1220127636" [label="entry"]; |
| 642 |
"-1220127636" -> "-1213915636"; |
| 643 |
"-1213915636" [shape=box,label="STRING\n'a'"]; |
| 644 |
"-1220127636" -> "-1214251156"; |
| 645 |
"-1214251156" [shape=box,label="STRING\n'5'"]; |
| 646 |
"-1220061652" -> "-1220063188"; |
| 647 |
"-1220063188" [label="entry"]; |
| 648 |
"-1220063188" -> "-1214253076"; |
| 649 |
"-1214253076" [shape=box,label="STRING\n'b'"]; |
| 650 |
"-1220063188" -> "-1220059444"; |
| 651 |
"-1220059444" [label="array"]; |
| 652 |
"-1220059444" -> "-1214253364"; |
| 653 |
"-1214253364" [shape=box,label="NUMBER\n'1'"]; |
| 654 |
"-1220059444" -> "-1214254292"; |
| 655 |
"-1214254292" [shape=box,label="__0_null\n'null'"]; |
| 656 |
"-1220059444" -> "-1214253268"; |
| 657 |
"-1214253268" [shape=box,label="NUMBER\n'3'"]; |
| 658 |
"-1220059444" -> "-1214252596"; |
| 659 |
"-1214252596" [shape=box,label="__1_true\n'true'"]; |
| 660 |
"-1220059444" -> "-1220062260"; |
| 661 |
"-1220062260" [label="object"]; |
| 662 |
"-1220062260" -> "-1220060116"; |
| 663 |
"-1220060116" [label="entry"]; |
| 664 |
"-1220060116" -> "-1214211860"; |
| 665 |
"-1214211860" [shape=box,label="STRING\n'f'"]; |
| 666 |
"-1220060116" -> "-1214210132"; |
| 667 |
"-1214210132" [shape=box,label="STRING\n'g'"]; |
| 668 |
"-1220062260" -> "-1220062868"; |
| 669 |
"-1220062868" [label="entry"]; |
| 670 |
"-1220062868" -> "-1214211956"; |
| 671 |
"-1214211956" [shape=box,label="STRING\n'h'"]; |
| 672 |
"-1220062868" -> "-1214212308"; |
| 673 |
"-1214212308" [shape=box,label="NUMBER\n'6'"]; |
| 674 |
} |
| 675 |
|
| 676 |
|
| 677 |
.. _Prolog interpreter: https://bitbucket.org/cfbolz/pyrolog/ |
| 678 |
.. _json format: http://www.json.org |