Advice On Python Parser Generators
I've been given a task where I have to create a parser for a simple C-like language. I can use any programming language and tools I wish to create the parser, but I'm learning Pyth
Solution 1:
PyParsing is a python tool to generate parsers. There are a lot of interesting examples.
Easy to get started:
from pyparsing import Word, alphas
# define grammar
greet = Word( alphas ) + "," + Word( alphas ) + "!"# input string
hello = "Hello, World!"# parse input stringprint hello, "->", greet.parseString( hello )
Solution 2:
I recommend that you check out Lark: https://github.com/erezsh/lark
It can parse ALL context-free grammars, it automatically builds an AST (with line & column numbers), and it accepts the grammar in EBNF format, which is simple to write and it's considered the standard.
Post a Comment for "Advice On Python Parser Generators"