OBSOLETE_left_recursion_test.py 1.06 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/usr/bin/python3

import sys

LOGGING = True

sys.path.extend(['../../', '../', './'])

from DHParser import grammar_provider, logging, CONFIG_PRESET


CONFIG_PRESET['ast_serialization'] = "S-expression"
CONFIG_PRESET['test_parallelization'] = False
CONFIG_PRESET['left_recursion_depth'] = 2


arithmetic_syntax = """
    expression     = addition | subtraction
    addition       = (expression | term) "+" (expression | term)
    subtraction    = (expression | term) "-" (expression | term)
    term           = multiplication | division
    multiplication = (term | factor) "*" (term | factor)
    division       = (term | factor) "/" (term | factor)
    factor         = [SIGN] ( NUMBER | VARIABLE | group ) { VARIABLE | group }
    group          = "(" §expression ")"
    SIGN           = /[+-]/
    NUMBER         = /(?:0|(?:[1-9]\d*))(?:\.\d+)?/~
    VARIABLE       = /[A-Za-z]/~        
    """

if __name__ == "__main__":
    arithmetic = grammar_provider(arithmetic_syntax)()
    assert arithmetic
    with logging():
        syntax_tree = arithmetic("(a + b) * (a - b)")