Search code examples
pythonregexparsinggrammarpeg

Parsing a table using preferably grammar or regex


few days i am stuck; i would like to parse a big document with many page amongts things it has tables like this one :

                Col1  Col2  Col3   Col4

                TXT1       TTT     Fnam1, LNam1

                TXT2       TEE     Fnam2, Mnam LNam2

                TXT1

                TXT5       ART     Fnam3, LNam3

                     TXT6  BGT     Fnam4, LNam4

I have written the grammar in arpeggio (python) i can parse the table if all cells are not empty but everything falls a part if there is one or more empty cells. Anyone has an idea how i can do it without using ambiguous grammar ? Thanks in advance


Solution

  • i found the solution thanks to the hints from @JeffC by asking about the number of spaces. Here is the grammar:

    from __future__ import print_function, unicode_literals
    
    import os
    from arpeggio import ParserPython
    from arpeggio import Optional, ZeroOrMore, OneOrMore, EOF,  PTNodeVisitor, ParserPython, visit_parse_tree, Combine
    from arpeggio.export import PMDOTExporter, PTDOTExporter
    from arpeggio import RegExMatch as _
    
    
    def wsx() : return _(r"[ \t]*")
    def ws2() : return _(r"[ \t]{2}")
    def ws16() : return _(r"[ \t]{16}")
    def code(): return _(r"[a-zA-Z0-9]{2,}")
    def col4_title(): return "Col4"
    def col3_title(): return "Col3"
    def col2_title(): return "Col2"
    def col1_title(): return "Col1"
    def cell_col4(): return [Combine(wsx,OneOrMore(word,Optional(ws2)),Optional(",", OneOrMore(ws2,word))),wsx]
    def cell_col3(): return [code,wsx]
    def cell_col2(): return [code,wsx]
    def cell_col1(): return [code,wsx]
    def team_row(): return ws16, Optional(cell_col1),Optional(ws2),Optional(cell_col2),Optional(ws2),Optional(cell_col3),Optional(ws2),Optional(cell_col4),Optional(wsx), nl
    def team_table_body(): return wsx, OneOrMore(nl,team_row),
    def team_table_title(): return "testtab"
    def team_table_header(): return wsx, team_table_title, wsx, col1_title, wsx, col2_title, wsx, col3_title, wsx, col4_title,Optional(wsx),nl
    def team_block(): return wsx, team_table_header, team_table_body