Skip to content

Commit 24d0cf7

Browse files
committed
Docs: Small fixes in grammar.md
1 parent 5faea92 commit 24d0cf7

File tree

1 file changed

+34
-15
lines changed

1 file changed

+34
-15
lines changed

docs/grammar.md

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ EBNF is basically a short-hand for common BNF patterns.
2020

2121
Optionals are expanded:
2222

23-
```ebnf
23+
```ruby
2424
a b? c -> (a c | a b c)
2525
```
2626

2727
Repetition is extracted into a recursion:
2828

29-
```ebnf
29+
```ruby
3030
a: b* -> a: _b_tag
3131
_b_tag: (_b_tag b)?
3232
```
@@ -35,11 +35,11 @@ And so on.
3535

3636
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:
3737

38-
```c
39-
rule: <EBNF EXPRESSION>
38+
```html
39+
rule: <EBNF-EXPRESSION>
4040
| etc.
4141

42-
TERM: <EBNF EXPRESSION> // Rules aren't allowed
42+
TERM: <EBNF-EXPRESSION> // Rules aren't allowed
4343
```
4444

4545

@@ -82,18 +82,18 @@ Templates are expanded when preprocessing the grammar.
8282

8383
Definition syntax:
8484

85-
```ebnf
85+
```javascript
8686
my_template{param1, param2, ...}: <EBNF EXPRESSION>
8787
```
8888

8989
Use syntax:
9090

91-
```ebnf
91+
```javascript
9292
some_rule: my_template{arg1, arg2, ...}
9393
```
9494

9595
Example:
96-
```ebnf
96+
```javascript
9797
_separated{x, sep}: x (sep x)* // Define a sequence of 'x sep x sep x ...'
9898

9999
num_list: "[" _separated{NUMBER, ","} "]" // Will match "[1, 2, 3]" etc.
@@ -173,9 +173,9 @@ Tree('start', [Token('A', 'ab')])
173173

174174
This is happening because Python's regex engine always returns the best matching option. There is no way to access the alternatives.
175175
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.
177177
178-
Example:
178+
Example using rules:
179179
180180
```python
181181
>>> p = Lark("""start: (a | b)+
@@ -191,6 +191,25 @@ _ambig
191191
b b
192192
```
193193
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)
194213
195214
## Rules
196215
@@ -265,7 +284,7 @@ COMMENT: "#" /[^\n]/*
265284
266285
Allows one to import terminals and rules from lark grammars.
267286
268-
When importing rules, all their dependencies will be imported into a namespace, to avoid collisions. It's not 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.
269288
270289
**Syntax:**
271290
```html
@@ -276,19 +295,19 @@ When importing rules, all their dependencies will be imported into a namespace,
276295
%import <module> (<TERM1>, <TERM2>, <rule1>, <rule2>)
277296
```
278297
279-
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`).
280299
281300
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.
282301
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.
284303
285304
**Example:**
286305
```perl
287306
%import common.NUMBER
288307
289308
%import .terminals_file (A, B, C)
290309
291-
%import .rules_file.rulea -> ruleb
310+
%import .rules_file.rule_a -> rule_b
292311
```
293312
294313
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
328347
%extend NUMBER: /0x\w+/
329348
```
330349
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

Comments
 (0)