|
8 | 8 | Python parser generator |
9 | 9 |
|
10 | 10 | ## Quick start |
| 11 | + |
| 12 | +### Creating a parsing table |
11 | 13 | ```py |
12 | 14 | from syntactes import Grammar, Rule, SLRGenerator, Token |
13 | 15 |
|
@@ -54,16 +56,86 @@ SLR PARSING TABLE |
54 | 56 | ------------------------------------------------- |
55 | 57 | | | $ | + | E | S | T | x | |
56 | 58 | ------------------------------------------------- |
57 | | -| 1 | -- | -- | s3 | -- | s4 | s2 | |
| 59 | +| 1 | -- | -- | s4 | -- | s2 | s3 | |
58 | 60 | ------------------------------------------------- |
59 | | -| 2 | r4 | r4 | -- | -- | -- | -- | |
| 61 | +| 2 | r2 | s5 | -- | -- | -- | -- | |
60 | 62 | ------------------------------------------------- |
61 | | -| 3 | a | -- | -- | -- | -- | -- | |
62 | | ------------------------------------------------- |
63 | | -| 4 | r2 | s5 | -- | -- | -- | -- | |
| 63 | +| 3 | r4 | r4 | -- | -- | -- | -- | |
64 | 64 | ------------------------------------------------- |
65 | | -| 5 | -- | -- | s6 | -- | s4 | s2 | |
| 65 | +| 4 | a | -- | -- | -- | -- | -- | |
| 66 | +------------------------------------------------ |
| 67 | +| 5 | -- | -- | s6 | -- | s2 | s3 | |
66 | 68 | ------------------------------------------------- |
67 | 69 | | 6 | r1 | -- | -- | -- | -- | -- | |
68 | 70 | ------------------------------------------------- |
69 | 71 | ``` |
| 72 | + |
| 73 | +### Parsing |
| 74 | + |
| 75 | +```py |
| 76 | +from syntactes import Grammar, Rule, Token |
| 77 | +from syntactes.parser import ParserError, SLRParser, execute_on |
| 78 | + |
| 79 | +EOF = Token.eof() |
| 80 | +S = Token("S", is_terminal=False) |
| 81 | +E = Token("E", False) |
| 82 | +T = Token("T", False) |
| 83 | +x = Token("x", True, 1) # value of token is 1 |
| 84 | +PLUS = Token("+", True) |
| 85 | + |
| 86 | +tokens = {EOF, S, E, T, x, PLUS} |
| 87 | + |
| 88 | +# 0. S -> E $ |
| 89 | +# 1. E -> T + E |
| 90 | +# 2. E -> T |
| 91 | +# 3. T -> x |
| 92 | +rule_1 = Rule(0, S, E, EOF) |
| 93 | +rule_2 = Rule(1, E, T, PLUS, E) |
| 94 | +rule_3 = Rule(2, E, T) |
| 95 | +rule_4 = Rule(4, T, x) |
| 96 | + |
| 97 | +rules = (rule_1, rule_2, rule_3, rule_4) |
| 98 | + |
| 99 | +grammar = Grammar(rule_1, rules, tokens) |
| 100 | + |
| 101 | +parser = SLRParser.from_grammar(grammar) |
| 102 | + |
| 103 | + |
| 104 | +@execute_on(rule_4) |
| 105 | +def push_value(x_token): |
| 106 | + # Add and argument for every token on the right-hand side of the rule. |
| 107 | + print( |
| 108 | + f"received token {x_token} with value: {x_token.value}, reducing by rule: {rule_4}" |
| 109 | + ) |
| 110 | + |
| 111 | + |
| 112 | +@execute_on(rule_2) |
| 113 | +def add(left, plus, right): |
| 114 | + print(f"received tokens {left}, {plus}, {right}, reducing by rule: {rule_2}") |
| 115 | + |
| 116 | + |
| 117 | +print("Parsing stream: x + x + x $\n") |
| 118 | +parser.parse([x, PLUS, x, PLUS, x, EOF]) |
| 119 | + |
| 120 | +print("\nParsing stream: x + $\n") |
| 121 | +try: |
| 122 | + parser.parse([x, PLUS, EOF]) |
| 123 | +except ParserError as e: |
| 124 | + print("ParserError:", e) |
| 125 | +``` |
| 126 | + |
| 127 | +Running the above example produces this output: |
| 128 | +``` |
| 129 | +Parsing stream: x + x + x $ |
| 130 | +
|
| 131 | +received token x with value: 1, reducing by rule: T -> x |
| 132 | +received token x with value: 1, reducing by rule: T -> x |
| 133 | +received token x with value: 1, reducing by rule: T -> x |
| 134 | +received tokens E, +, T, reducing by rule: E -> T + E |
| 135 | +received tokens E, +, T, reducing by rule: E -> T + E |
| 136 | +
|
| 137 | +Parsing stream: x + $ |
| 138 | +
|
| 139 | +received token x with value: 1, reducing by rule: T -> x |
| 140 | +ParserError: Received token: $; expected one of: ['x', 'T', 'E'] |
| 141 | +``` |
0 commit comments