Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Changed some wording at the end prior to adding error management strategies

...

  • You have ANTLR and TestNG installed. (You can use the same ideas with other unit testing frameworks such as JUnit)
  • You can run ANTLR to generate Java sources from a grammar.
  • You can build and run Java code.
  • (optional) You have JDK 1.5 or later installed. If not, you won't be able to use some of the constructs in this example and will need to translate back to 1.4 or earlier.

Develop

...

a basic CSV parser

Basic setup

Every CSV line ends in a newline (line feed) or carriage return + line feed. Let's set that up as our basic grammar.

...

No Format
line 1:3 no viable alternative at character ' '
line 1:4 no viable alternative at character ' '
line 1:6 no viable alternative at character ' '
line 1:7 no viable alternative at character ' '
line 1:8 no viable alternative at character ' '
line 1:15 no viable alternative at character ' '

(warning) ANTLR 3's error recovery is taking care of this for us .I'm inclined to make any production code do the right thing anyway, if for no other reason than to make the grammar self-documenting. So I will leave you with the following challenges:by skipping the space characters (which are unrecognized by the UNUOTED rule. If we want to catch the error instead, we'll have to alter the error recovery mechanism.

Panel
borderColor#ccc
bgColor#FFFFCE
titleBGColor#F7D6C1
titleThings to do
borderStyledashed
  1. Consider disabling automatic error recovery while developing and testing your grammar. Otherwise, you may miss problems that crop up during automated unit testing (e.g. when using CruiseControl).
  2. Strip leading and trailing spaces around commas (but nowhere else)