Parser to read configuration file

T

Tech Id

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.
 
M

Martin Gregorie

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.
 
T

Tom Anderson

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
 
S

Stefan Ram

Tech Id said:
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).
 
R

Roedy Green

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.
 
A

Arne Vajhøj

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
 
S

Stefan Ram

Arne Vajhøj said:
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?
 
R

Roedy Green

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.
 
M

markspace

Stefan 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!

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.
 
R

RedGrittyBrick

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
 
M

Martin Gregorie

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
 
L

Lew

markspace said:
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.
 
T

Tom Anderson

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
 
M

markspace

Lew said:
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.
 
T

Tech Id

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!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top