Parsing Comma-Separated Values
While we have already seen a basic CSV grammar in âPattern: Sequenceâ, letâs beef it up with the notion of a header row and allow empty columns. Hereâs a representative input file:
â | Details,Month,Amount |
â | Mid Bonus,June,"$2,000" |
â | ,January,"""zippo""" |
â | Total Bonuses,"","$5,000" |
Header rows are no different from regular rows; we simply interpret the column value as the column header name. Rather than using a row+ ANTLR fragment to match rows as well as the header row, we match it separately. When building a real application based upon this grammar, weâd probably want to treat the header differently. This way, we can get a handle on the first special row. Hereâs the first ...
Get The Definitive ANTLR 4 Reference, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.