Construct a Parser

Problem

You have an application that stores certain data in a table. Your task is to add a new feature to this application to import that data from a file format that your application does not yet support. There are no off-the-shelf parsers available for this file format. You will have to roll your own.

The rules of the file format you need to parse are as follows:

  1. The keyword table begins a new table. A file can have an unlimited number of tables, and must have at least one.

  2. Any strings that follow the table keyword form the table’s caption. A table does not need to have a caption.

  3. The keyword row begins a new row. A row cannot exist outside of a table. A table can have an unlimited number of rows, and must have at least one.

  4. The row keyword cannot be followed by a string.

  5. The keyword cell begins a new cell. A cell cannot exist outside of a row. A row can have an unlimited number of cells, but does not need any. Different rows in the same table can have different numbers of cells.

  6. Any strings that follow the cell keyword form the content of the cell. A cell does not need to have any content.

  7. A string is a sequence of zero or more characters enclosed by percentage signs. A string with nothing between the percentage signs is an empty string. Two sequential percentage signs in a character string denote a single character, a percentage sign. No characters other than the percentage sign have a special meaning in strings. Line breaks and other control characters that appear ...

Get Regular Expressions Cookbook, 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.