سؤال

I'm trying to make a parser for a Java Program's command line arguments, as not to re-invent the wheel I tried looking for a generator that would generate something like that for me, I found people suggesting CLI and JSAP, they seem like great tools but they create objects in run-time, I need my parsing to be static as to not clutter the code with unncessary capabilities (and memory), so what I'm really looking for is a command line parser generator, in that thread I found XTC, Rats, and JavaCC, but they seem like a lot of work and I think that what I need is simpler.

In The end I decided to make my parser using JavaCC, this question is just to make sure that there is no one out there that knows a simpler way...

Thanks

Edit: the JavaCC thread is a dud since it works on character by character, not really suitable for command line parsing

هل كانت مفيدة؟

المحلول

This related SO question has many useful suggestions for command line parser libraries. I want to focus on why a classic parser generator is not a good way to solve the problem.

There are two fundamental reasons:

  1. A command line syntax needs to be simple, easy to remember, consistent with other command line syntaxes in general style / conventions, and has a minimum of syntactic noise. The problem with conventional PGS's is that each grammar is distinct from others, and is burdened with the need for unambiguous parses ... which tends to lead to syntactic noisiness. (OK, if you are disciplined you can avoid these traps, but ...)

    By contrast, the APIs of a typical argument parser library encourage the programmer to use a consistent style; e.g. -x versus --longForm, options first, -- means no more options, and so on. And because the "language" doesn't care about ambiguity, the programmer is free to sort this out informally (without excessive syntax) or simplify the syntax.

  2. Parser generators either produce parse trees or they require you to embed your code into the grammar. Neither of these is ideal for command line parsing, because they both add complexity to the application code to deal with this.

    By contrast, a typical argument parser library creates or populates a flat data structure, which is simpler to deal with.


You also seem to be overly worried about performance and memory usage:

... I need my parsing to be static as to not clutter the code with unncessary capabilities (and memory)

First observation is that it probably doesn't matter. The number and size of the runtime objects is most likely trivial compared to rest of the application ... and all of the hidden stuff that happens during normal JVM startup before the arguments are parsed ... and afterwards.

Second observation is that a PGS-based solution possibly has comparable overheads. In addition to generating a parse tree (for instance), the generated parser also needs to initialize and retain a bunch of grammar-specific parser tables. And of course, the generated parser code tends to be large as well.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top