|
| 1 | + |
| 2 | +[](https://pypi.python.org/pypi/syntactes) |
| 3 | +[](https://opensource.org/license/mit/) |
| 4 | +[](https://pypi.python.org/pypi/syntactes) |
| 5 | +[](https://github.com/Maxcode123/syntactes/actions/workflows/test-package.yml?query=branch%3Amain) |
| 6 | +--- |
| 7 | +# syntactes |
| 8 | +Python parser generator |
| 9 | + |
| 10 | +## Quick start |
| 11 | +```py |
| 12 | +from syntactes import Grammar, Rule, SLRGenerator, Token |
| 13 | + |
| 14 | +EOF = Token.eof() |
| 15 | +S = Token("S", is_terminal=False) |
| 16 | +E = Token("E", False) |
| 17 | +T = Token("T", False) |
| 18 | +x = Token("x", True) |
| 19 | +PLUS = Token("+", True) |
| 20 | + |
| 21 | +tokens = {EOF, S, E, T, x, PLUS} |
| 22 | + |
| 23 | +# 0. S -> E $ |
| 24 | +# 1. E -> T + E |
| 25 | +# 2. E -> T |
| 26 | +# 3. T -> x |
| 27 | +rule_1 = Rule(0, S, E, EOF) |
| 28 | +rule_2 = Rule(1, E, T, PLUS, E) |
| 29 | +rule_3 = Rule(2, E, T) |
| 30 | +rule_4 = Rule(4, T, x) |
| 31 | + |
| 32 | +rules = (rule_1, rule_2, rule_3, rule_4) |
| 33 | + |
| 34 | +grammar = Grammar(rule_1, rules, tokens) |
| 35 | + |
| 36 | +generator = SLRGenerator(grammar) |
| 37 | + |
| 38 | +parsing_table = generator.generate() |
| 39 | + |
| 40 | +print(parsing_table.pretty_str()) |
| 41 | +``` |
| 42 | + |
| 43 | +Running the above example produces this output: |
| 44 | +``` |
| 45 | +GRAMMAR RULES |
| 46 | +------------- |
| 47 | +0. S -> E $ |
| 48 | +1. E -> T + E |
| 49 | +2. E -> T |
| 50 | +3. T -> x |
| 51 | +------------- |
| 52 | +
|
| 53 | +LR0 PARSING TABLE |
| 54 | +------------------------------------------------- |
| 55 | +| | $ | + | E | S | T | x | |
| 56 | +------------------------------------------------- |
| 57 | +| 1 | -- | -- | s3 | -- | s4 | s2 | |
| 58 | +------------------------------------------------- |
| 59 | +| 2 | r4 | r4 | -- | -- | -- | -- | |
| 60 | +------------------------------------------------- |
| 61 | +| 3 | a | -- | -- | -- | -- | -- | |
| 62 | +------------------------------------------------ |
| 63 | +| 4 | r2 | s5 | -- | -- | -- | -- | |
| 64 | +------------------------------------------------- |
| 65 | +| 5 | -- | -- | s6 | -- | s4 | s2 | |
| 66 | +------------------------------------------------- |
| 67 | +| 6 | r1 | -- | -- | -- | -- | -- | |
| 68 | +------------------------------------------------- |
| 69 | +``` |
0 commit comments