Parser to read configuration file

Discussion in 'Java' started by Tech Id, May 13, 2010.

  1. Tech Id

    Tech Id Guest

    Hi,

    I need to read a configuration file and create an Object Model in
    memory.
    The configuration file uses around 7-8 keywords and symbols like '(',
    '[', ']' and ')' and quoted strings.

    My question is:
    Should I use a simple tokenizer to parse the above file or use some
    kind of parser?
    If parser is recommended, which one should be best?
    ANTLR?
    Flex/Bison?

    There is not much need of error recovery since the file is produced by
    another tool and rarely edited by hand.

    Thanks in advance for help.
     
    Tech Id, May 13, 2010
    #1
    1. Advertising

  2. On Thu, 13 May 2010 01:10:28 -0700, Tech Id wrote:

    > Hi,
    >
    > I need to read a configuration file and create an Object Model in
    > memory.
    > The configuration file uses around 7-8 keywords and symbols like '(',
    > '[', ']' and ')' and quoted strings.
    >
    > My question is:
    > Should I use a simple tokenizer to parse the above file or use some kind
    > of parser?
    > If parser is recommended, which one should be best? ANTLR?
    > Flex/Bison?
    >

    Coco/R is another Java parser generator - written in Java and generates
    Java though there are other flavours for other languages.

    I found it pretty easy to use: unlike Flex/Bison all its input is in one
    file and its also easy to edit the framework files as I discovered when I
    needed to make it parse source in a string.


    --
    martin@ | Martin Gregorie
    gregorie. | Essex, UK
    org |
     
    Martin Gregorie, May 13, 2010
    #2
    1. Advertising

  3. Tech Id

    Tom Anderson Guest

    On Thu, 13 May 2010, Tech Id wrote:

    > I need to read a configuration file and create an Object Model in
    > memory. The configuration file uses around 7-8 keywords and symbols like
    > '(', '[', ']' and ')' and quoted strings.
    >
    > My question is: Should I use a simple tokenizer to parse the above file
    > or use some kind of parser?


    Impossible to say without knowing more about the language. Can you give us
    an example?

    > If parser is recommended, which one should be best? ANTLR? Flex/Bison?


    I like JavaCC:

    https://javacc.dev.java.net/

    Partly because i prefer LL(k) to LALR, but that's a matter of taste.
    JavaCC certainly makes it very easy to write grammars, and very easy to
    hang your custom code off those grammars. It has a facility for building
    parse trees (generating code for building them, i think), but i never used
    it - i found it easier in the long run to put my code right in the action
    blocks. I used something that looks a bit like a Visitor or Builder
    pattern as a facade that the parser could talk to.

    tom

    --
    The art of medicine consists in amusing the patient while nature cures
    the disease. -- Voltaire
     
    Tom Anderson, May 13, 2010
    #3
  4. Tech Id

    Stefan Ram Guest

    Tech Id <> writes:
    >If parser is recommended, which one should be best?
    >ANTLR?
    >Flex/Bison?


    This might depend on the grammar.

    If the grammar is not too complicated, it might be
    best to use the tool you already know best (in my
    case that would mean not to use any library beyond
    Java SE, because that's what I know best).
     
    Stefan Ram, May 13, 2010
    #4
  5. Tech Id

    Roedy Green Guest

    On Thu, 13 May 2010 01:10:28 -0700 (PDT), Tech Id
    <> wrote, quoted or indirectly quoted someone
    who said :

    >
    >There is not much need of error recovery since the file is produced by
    >another tool and rarely edited by hand.


    See http://mindprod.com/jgloss/parser.html

    To make it programmer friendly, so others can use the file, consider
    XML, even though it is ugly and fluffy.

    http://mindprod.com/jgloss/xml.html
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com

    Beauty is our business.
    ~ Edsger Wybe Dijkstra (born: 1930-05-11 died: 2002-08-06 at age: 72)

    Referring to computer science.
     
    Roedy Green, May 13, 2010
    #5
  6. Tech Id

    Arne Vajhøj Guest

    On 13-05-2010 04:10, Tech Id wrote:
    > I need to read a configuration file and create an Object Model in
    > memory.
    > The configuration file uses around 7-8 keywords and symbols like '(',
    > '[', ']' and ')' and quoted strings.
    >
    > My question is:
    > Should I use a simple tokenizer to parse the above file or use some
    > kind of parser?
    > If parser is recommended, which one should be best?
    > ANTLR?
    > Flex/Bison?
    >
    > There is not much need of error recovery since the file is produced by
    > another tool and rarely edited by hand.


    if not too complex then
    use regex
    else
    use parser generator
    end if

    It is not possible to evaluate the complexity based on the
    information given.

    In general I would say that if you could get the format
    changed to something better supported like a Java
    properties file or a XML format that can be mapped
    via JAXB, then you would be much better of.

    Arne
     
    Arne Vajhøj, May 14, 2010
    #6
  7. Tech Id

    Stefan Ram Guest

    Arne Vajhøj <> writes:
    >It is not possible to evaluate the complexity based on the
    >information given.


    customer: Could you write a parser for us?
    I: Ok, just send me the grammar.
    customer: The what?
    I: Some kind of EBNF or so.
    customer: Well, I can't write EBNF. Could you write the grammar, too?
    We'll send you some examples of the language.
    I: Ok.
    (time passes.)
    I: So here is the grammar. You just need to read it and sign this
    confirmation that it really describes the language you want me
    to write the parser for, then I can go on and write the actual parser.
    customer: But I can't read EBNF!

    What then?
     
    Stefan Ram, May 14, 2010
    #7
  8. Tech Id

    Roedy Green Guest

    On 14 May 2010 00:49:04 GMT, -berlin.de (Stefan Ram)
    wrote, quoted or indirectly quoted someone who said :

    > I: So here is the grammar. You just need to read it and sign this
    > confirmation that it really describes the language you want me
    > to write the parser for, then I can go on and write the actual parser.
    > customer: But I can't read EBNF!


    Then you did what Ian Eason did back in circa 1968. He generated
    large numbers of conforming random sentences.
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com

    Beauty is our business.
    ~ Edsger Wybe Dijkstra (born: 1930-05-11 died: 2002-08-06 at age: 72)

    Referring to computer science.
     
    Roedy Green, May 14, 2010
    #8
  9. Tech Id

    markspace Guest

    Stefan Ram wrote:

    > I: So here is the grammar. You just need to read it and sign this
    > confirmation that it really describes the language you want me
    > to write the parser for, then I can go on and write the actual parser.
    > customer: But I can't read EBNF!
    >
    > What then?
    >



    That's kinda a good question. What do you do when the customer can't
    evaluate the product they are buying? I only see two choices

    1. They're going to have to trust you when you tell them something is
    correct.

    2. They're going to have to hire a second technologist or business
    analyst who can advise them on what is best for their company.

    On a slightly more practical level, you might have to demonstrate, or
    give assurances in writing, that the grammar you provide will parse
    certain examples which they can review.
     
    markspace, May 14, 2010
    #9
  10. On 14/05/2010 01:49, Stefan Ram wrote:
    > Arne Vajhøj<> writes:
    >> It is not possible to evaluate the complexity based on the
    >> information given.

    >
    > customer: Could you write a parser for us?
    > I: Ok, just send me the grammar.
    > customer: The what?
    > I: Some kind of EBNF or so.
    > customer: Well, I can't write EBNF. Could you write the grammar, too?
    > We'll send you some examples of the language.
    > I: Ok.
    > (time passes.)
    > I: So here is the grammar. You just need to read it and sign this
    > confirmation that it really describes the language you want me
    > to write the parser for, then I can go on and write the actual parser.
    > customer: But I can't read EBNF!
    >
    > What then?
    >


    Generate a railroad diagram from the EBNF and get them to sign that?
    http://en.wikipedia.org/wiki/Syntax_diagram

    --
    RGB
     
    RedGrittyBrick, May 14, 2010
    #10
  11. On Fri, 14 May 2010 00:49:04 +0000, Stefan Ram wrote:

    > Arne Vajhøj <> writes:
    >>It is not possible to evaluate the complexity based on the information
    >>given.

    >
    > customer: Could you write a parser for us? I: Ok, just send me the
    > grammar.
    > customer: The what?
    > I: Some kind of EBNF or so.
    > customer: Well, I can't write EBNF. Could you write the grammar, too?
    > We'll send you some examples of the language. I: Ok.
    > (time passes.)
    > I: So here is the grammar. You just need to read it and sign this
    > confirmation that it really describes the language you want me to
    > write the parser for, then I can go on and write the actual parser.
    > customer: But I can't read EBNF!
    >
    > What then?


    Write a few examples of realistic configurations that match your grammar,
    preferably examples of:
    - a really simple configuration
    - what you expect a typical configuration would look like.
    - the most complex configuration the client might want


    --
    martin@ | Martin Gregorie
    gregorie. | Essex, UK
    org |
     
    Martin Gregorie, May 14, 2010
    #11
  12. Tech Id

    Lew Guest

    markspace wrote:
    > That's kinda a good question. What do you do when the customer can't
    > evaluate the product they are buying? I only see two choices
    >
    > 1. They're going to have to trust you when you tell them something is
    > correct.
    >
    > 2. They're going to have to hire a second technologist or business
    > analyst who can advise them on what is best for their company.
    >
    > On a slightly more practical level, you might have to demonstrate, or
    > give assurances in writing, that the grammar you provide will parse
    > certain examples which they can review.


    Clearly you are defining the conditions of satisfaction incorrectly in this case.

    The client should not sign off on the implementation, e.g., the parsing
    grammar. The client should sign off on the behavior, within the universe of
    discourse of their business, e.g., that the business transaction confers the
    proper information/cash/results.

    I don't care whether you take a plane, train, or automobile as long as you're
    in Albequerque for the meeting on time and under budget.

    --
    Lew
     
    Lew, May 14, 2010
    #12
  13. Tech Id

    Tom Anderson Guest

    On Fri, 14 May 2010, Stefan Ram wrote:

    > Arne Vajh?j <> writes:
    >> It is not possible to evaluate the complexity based on the
    >> information given.

    >
    > customer: Could you write a parser for us?
    > I: Ok, just send me the grammar.
    > customer: The what?
    > I: Some kind of EBNF or so.
    > customer: Well, I can't write EBNF. Could you write the grammar, too?
    > We'll send you some examples of the language.
    > I: Ok.
    > (time passes.)
    > I: So here is the grammar. You just need to read it and sign this
    > confirmation that it really describes the language you want me
    > to write the parser for, then I can go on and write the actual parser.
    > customer: But I can't read EBNF!


    You write the parser, ship it to them, charge them for maintenance when it
    breaks, and tell the anecdote afterwards on usenet"

    tom

    --
    inspired by forty-rod whiskey
     
    Tom Anderson, May 14, 2010
    #13
  14. Tech Id

    markspace Guest

    Lew wrote:

    >
    > The client should not sign off on the implementation, e.g., the parsing
    > grammar. The client should sign off on the behavior, within the
    > universe of discourse of their business, e.g., that the business
    > transaction confers the proper information/cash/results.



    Well, I agree, but how do you get the client to understand what it is
    they are actually getting?

    At this point you might have to write a users' manual for the product,
    which should contain examples of the config file and how it works, with
    some do's an don't's. That might satisfy the customer, with out
    specifying the grammar. But this sort of "examples" is what I was
    trying to get at in the post you replied to.
     
    markspace, May 14, 2010
    #14
  15. Tech Id

    Tech Id Guest

    I tried my hands on JavaCC after fiddling with some crashes on ANTLR
    And it worked really well.

    Thanks to an Eclipse plug-in, My work is pertty much done!
     
    Tech Id, May 23, 2010
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark
    Replies:
    0
    Views:
    863
  2. Damiano G. Preatoni

    configuration file parser library

    Damiano G. Preatoni, May 5, 2005, in forum: C++
    Replies:
    4
    Views:
    9,006
    __PPS__
    May 5, 2005
  3. Karthikesh Raju

    best way to read a configuration file

    Karthikesh Raju, Mar 3, 2004, in forum: Python
    Replies:
    5
    Views:
    462
    Kevin Dahlhausen
    Mar 3, 2004
  4. Tammo Tjarks
    Replies:
    3
    Views:
    270
    Trans
    Sep 5, 2007
  5. Alex Dowad
    Replies:
    4
    Views:
    323
    Michel Demazure
    May 1, 2010
Loading...

Share This Page