You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/grammar.md
+34-15Lines changed: 34 additions & 15 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -20,13 +20,13 @@ EBNF is basically a short-hand for common BNF patterns.
20
20
21
21
Optionals are expanded:
22
22
23
-
```ebnf
23
+
```ruby
24
24
a b? c -> (a c | a b c)
25
25
```
26
26
27
27
Repetition is extracted into a recursion:
28
28
29
-
```ebnf
29
+
```ruby
30
30
a: b*->a:_b_tag
31
31
_b_tag: (_b_tag b)?
32
32
```
@@ -35,11 +35,11 @@ And so on.
35
35
36
36
Lark grammars are composed of a list of definitions and directives, each on its own line. A definition is either a named rule, or a named terminal, with the following syntax, respectively:
37
37
38
-
```c
39
-
rule: <EBNFEXPRESSION>
38
+
```html
39
+
rule: <EBNF-EXPRESSION>
40
40
| etc.
41
41
42
-
TERM: <EBNFEXPRESSION> // Rules aren't allowed
42
+
TERM: <EBNF-EXPRESSION> // Rules aren't allowed
43
43
```
44
44
45
45
@@ -82,18 +82,18 @@ Templates are expanded when preprocessing the grammar.
82
82
83
83
Definition syntax:
84
84
85
-
```ebnf
85
+
```javascript
86
86
my_template{param1, param2, ...}:<EBNFEXPRESSION>
87
87
```
88
88
89
89
Use syntax:
90
90
91
-
```ebnf
91
+
```javascript
92
92
some_rule: my_template{arg1, arg2, ...}
93
93
```
94
94
95
95
Example:
96
-
```ebnf
96
+
```javascript
97
97
_separated{x, sep}:x (sep x)*// Define a sequence of 'x sep x sep x ...'
98
98
99
99
num_list:"[" _separated{NUMBER, ","} "]"// Will match "[1, 2, 3]" etc.
This is happening because Python's regex engine always returns the best matching option. There is no way to access the alternatives.
175
175
176
-
If you find yourself in this situation, the recommended solution is to use rules instead.
176
+
If you find yourself in this situation, the recommended solution is to either use the "dynamic_complete" lexer, or use rules instead.
177
177
178
-
Example:
178
+
Example using rules:
179
179
180
180
```python
181
181
>>> p = Lark("""start: (a | b)+
@@ -191,6 +191,25 @@ _ambig
191
191
b b
192
192
```
193
193
194
+
Example using dynamic-complete:
195
+
196
+
```python
197
+
>>> g = """
198
+
... start: (A | B)+
199
+
... A : "a" | "ab"
200
+
... B : "b"
201
+
... """
202
+
>>> p = Lark(g, ambiguity="explicit", lexer="dynamic_complete")
203
+
>>> rich.print(p.parse("ab"))
204
+
_ambig
205
+
├── start
206
+
│ └── ab
207
+
└── start
208
+
├── a
209
+
└── b
210
+
```
211
+
212
+
(note: the dynamic-complete lexer can significantly affect the performance of the parser)
194
213
195
214
## Rules
196
215
@@ -265,7 +284,7 @@ COMMENT: "#" /[^\n]/*
265
284
266
285
Allows one to import terminals and rules from lark grammars.
267
286
268
-
When importing rules, all their dependencies will be imported into a namespace, to avoid collisions. It'snot possible to override their dependencies (e.g. like you would when inheriting a class).
287
+
When importing rules, all their dependencies will be imported into a namespace, to avoid collisions. To override any of their dependencies (e.g. like you would override methods when inheriting a class), use the ``%override`` directive.
269
288
270
289
**Syntax:**
271
290
```html
@@ -276,19 +295,19 @@ When importing rules, all their dependencies will be imported into a namespace,
If the module path is absolute, Lark will attempt to load it from the built-in directory (which currently contains `common.lark`, `python.lark`, and`unicode.lark`).
298
+
If the module path is absolute, Lark will attempt to load it from the built-in directory (which currently contains `common.lark`, `lark.lark`, `python.lark`, and `unicode.lark`).
280
299
281
300
If the module path is relative, such as `.path.to.file`, Lark will attempt to load it from the current working directory. Grammars must have the `.lark` extension.
282
301
283
-
The rule or terminal can be imported under another name with the `->` syntax.
302
+
The rule or terminal can be imported under another name (an alias) with the `->` syntax.
284
303
285
304
**Example:**
286
305
```perl
287
306
%import common.NUMBER
288
307
289
308
%import .terminals_file (A, B, C)
290
309
291
-
%import .rules_file.rulea -> ruleb
310
+
%import .rules_file.rule_a -> rule_b
292
311
```
293
312
294
313
Note that `%ignore` directives cannot be imported. Imported rules will abide by the `%ignore` directives declared in the main grammar.
@@ -328,4 +347,4 @@ Can also be used to implement a plugin system where a core grammar is extended b
328
347
%extend NUMBER: /0x\w+/
329
348
```
330
349
331
-
For both `%extend`and`%override`, there is not requirement for a rule/terminal to come from another file, but that is probably the most common usecase
350
+
For both `%extend` and `%override`, there is not requirement for a rule/terminal to come from another file, but that is probably the most common use-case.
0 commit comments