parser
LR0Parser
LR1Parser
Parser
Bases: ABC
Base class of the LR parsers. Parses a stream of tokens with a parsing table,
and on each reduction computes the rule's value: with the callback registered
with execute_on, else with the rule's primitive from the grammar.
Source code in src/syntactes/parser/parser.py
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 | |
execute_on(rule)
Decorate a function to be executed when this parser reduces by rule.
The function is called with one token per right-hand side symbol of the rule, and returns the value of the left-hand side token. The decorated function is returned unchanged.
Raises ValueError if rule is not a rule of the parser's grammar.
Source code in src/syntactes/parser/parser.py
from_grammar(grammar)
classmethod
Create a parser for the given grammar.
Source code in src/syntactes/parser/parser.py
parse(stream)
Parses the given stream of tokens. Expects the EOF token as the last one.
On each reduction the rule's value becomes the value of the token
pushed for the left-hand side. The value is, in order of precedence:
- what the callback registered for the rule returns, called with one token per right-hand side symbol, in order;
- the result of the rule's primitive (
Grammar.primitives): a binary operation gets the values of the tokens atGrammar.operands,Nonegives None, and any other primitive converts its single token's value; - the value of the rule's token, if it has exactly one;
- None.
On accept the starting rule's value is computed the same way, from its right-hand side tokens except the trailing EOF, and returned. So a starting rule with neither a callback nor a primitive returns the start symbol's value.
Raises syntactes.parser.PrimitiveError if a primitive raises, with the
original exception as its cause. Exceptions raised by callbacks
propagate unchanged.
Raises syntactes.parser.UnexpectedTokenError if an unexpected token is
received.
Raises syntactes.parser.NotAcceptedError if the stream of tokens ends
before the parser receives an accept action.
Source code in src/syntactes/parser/parser.py
NotAcceptedError
Bases: ParserError
The parser did not receive an accept action. The stream of tokens is syntactically invalid.
ParserError
PrimitiveError
Bases: ParserError
A rule's primitive raised while the parser applied it, e.g. int on a
token whose value isn't a number. The rule and primitive are available as
rule and primitive, and the original exception as __cause__.
Source code in src/syntactes/parser/exception.py
UnexpectedTokenError
Bases: ParserError
A token was received that does not map to an action. The stream of tokens is syntactically invalid.