1

I have a non-standard configuration file, for which I want to write a python parser.

What is the best approach to writing a parser from scratch?

Example of a configuration file:

// Comment 
conf OPTION_NAME {
     (
       ( option1:"string"
         option2:"14"
       )
     )
}


// Comment2 
conf OPTION_NAME2 {
     (
       ( option1:"string2"
         option2:"15"
       )
     )
}
6
  • 1
    Is the format of the configuration file under your control? Commented Feb 27, 2012 at 15:28
  • Unfortunately, no , it's a legacy file, not under my control. But this is a good exercise for any non-standard conf file/code. Commented Feb 27, 2012 at 15:29
  • There's not enough information here. What is the grammar of this language? Commented Feb 27, 2012 at 15:31
  • 1
    Write a BNF grammar first, (if you can't change the config so you can). Perhaps this SO post might help. stackoverflow.com/questions/264262/grammar-writing-tools Commented Feb 27, 2012 at 15:32
  • 2
    I've used PyParsing the few times I've had to write a parser in Python. The first thing you should do is write down the grammar of your language. Commented Feb 27, 2012 at 15:48

1 Answer 1

2

I would personally use PLY: http://www.dabeaz.com/ply/

Here's a simple example:

http://www.dabeaz.com/ply/example.html

Here's an example from one of my own projects:

https://github.com/fogleman/FeedNotifier/blob/master/filters.py

Alternatively, since the files look very simple, I might just use a handmade Finite State Machine to do the parsing.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.