-
-
Save Drvanon/b8266c83031fb71824d3fb06dce4d468 to your computer and use it in GitHub Desktop.
LALR try for vanon (1)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from lark import Lark | |
grammar = r""" | |
start: _NL* _section+ | |
_section: _HEADER _content* _HEADER | |
_HEADER: "--- SECTION ---" _NL | |
_content: number_list | |
| assign | |
| _other | |
number_list: NUMBER+ _NL | |
assign: CNAME "=" (NUMBER | CNAME) _NL | |
_other: (ANY | CNAME)+ _NL | |
ANY.-100: /.+/ | |
%import common.NEWLINE -> _NL | |
%import common (NUMBER, CNAME, WS_INLINE) | |
%ignore WS_INLINE | |
""" | |
text = r""" | |
--- SECTION --- | |
Structured content I want to capture: | |
123 123 432 132 | |
564 771 561 153 | |
--- SECTION --- | |
123123 123123 123123 123 | |
123123 123 123 123 | |
Unpredictably content | |
that I do not want to | |
capture (yet). | |
--- SECTION --- | |
content that won't be captured by this parser | |
--- SECTION --- | |
Random information | |
--- SECTION --- | |
Structured content I want to capture but of a different type | |
Value1 = 2 | |
String2 = asdasd | |
NowAFloat = 23123.1 | |
--- SECTION --- | |
""" | |
p = Lark(grammar, parser="lalr") | |
print(p.parse(text).pretty()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment