Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Parser to read configuration file

Reply
Thread Tools

Parser to read configuration file

 
 
Tech Id
Guest
Posts: n/a
 
      05-13-2010
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.
 
Reply With Quote
 
 
 
 
Martin Gregorie
Guest
Posts: n/a
 
      05-13-2010
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 |
 
Reply With Quote
 
 
 
 
Tom Anderson
Guest
Posts: n/a
 
      05-13-2010
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
 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      05-13-2010
Tech Id <(E-Mail Removed)> 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).

 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      05-13-2010
On Thu, 13 May 2010 01:10:28 -0700 (PDT), Tech Id
<(E-Mail Removed)> 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.
 
Reply With Quote
 
Arne Vajh°j
Guest
Posts: n/a
 
      05-14-2010
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
 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      05-14-2010
Arne Vajh°j <(E-Mail Removed)> 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?

 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      05-14-2010
On 14 May 2010 00:49:04 GMT, http://www.velocityreviews.com/forums/(E-Mail Removed)-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.
 
Reply With Quote
 
markspace
Guest
Posts: n/a
 
      05-14-2010
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.
 
Reply With Quote
 
RedGrittyBrick
Guest
Posts: n/a
 
      05-14-2010
On 14/05/2010 01:49, Stefan Ram wrote:
> Arne Vajh├Şj<(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
File.read(fname) vs. File.read(fname,File.size(fname)) Alex Dowad Ruby 4 05-01-2010 08:20 AM
configuration file parser like Config::General for perl Tammo Tjarks Ruby 3 09-05-2007 10:39 AM
Caught Exception: System.Configuration.ConfigurationErrorsException: An error occurred loading a configuration file: Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicK Mike ASP .Net 5 08-15-2007 08:57 AM
configuration file parser library Damiano G. Preatoni C++ 4 05-05-2005 05:04 PM
XMLparser: Difference between parser.setErrorHandler() vs. parser.setContentHandler() Bernd Oninger Java 0 06-09-2004 01:26 AM



Advertisments