0

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

ibstelix
  • 103
  • 7
  • 1
    What characters show up in the empty cells? e.g. in the first row, is it two/three tabs in Col2 or a bunch of spaces? If it's spaces, is it always a consistent number of spaces in any column when that cell is empty? How many spaces? – JeffC Mar 26 '21 at 03:28
  • Can you show the grammar that you've written so far? – Josh Voigts Mar 26 '21 at 13:19
  • The space between cells is not consistent and it is whitespace – ibstelix Mar 26 '21 at 14:56

1 Answers1

0

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
ibstelix
  • 103
  • 7