How do I parse indents and dedents with pyparsing?

后端 未结 1 1059

Here is a subset of the Python grammar:

single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE

stmt: simple_stmt | compound_stmt
simple_stmt: small_stm         


        
相关标签:
1条回答
  • 2020-12-28 10:36

    There are a couple of examples on the pyparsing wiki Examples page that could give you some insights:

    • pythonGrammarParser.py
    • indentedGrammarExample.py

    To use pyparsing's indentedBlock, I think you would define suite as:

    indentstack = [1]
    suite = indentedBlock(stmt, indentstack, True)
    

    Note that indentedGrammarExample.py pre-dates the inclusion of indentedBlock in pyparsing, so does its own implemention of indent parsing.

    0 讨论(0)
提交回复
热议问题