简体   繁体   中英

Easy grammar parsing in Python?

I'm building a project and I came across a problem. I should basically be able to parse JavaDoc-style (for example) comments. I know that there are already tools for parsing JavaDoc, but it was just an example. The format of my data would be something like:

// Function: Divide
//
// Divides two integers.
//
// Parameters:
//
//    x - The first integer.
//    y - The second integer.
//
// Returns:
//
//    The two integers divided.
//
// See Also:
//
//    <Multiply>

I should be able to set the keywords (Funciton, Parameters, Returns and etc). Also, the comment symbols may not always be // ( ** or ; are also possible). I know I should not try to parse this using regular expression, but with some kind of a grammar parser. My question is based on your experience, which one will be most suitable for my case?

If you need to make a parser off the cuff I recommend checking out antlr. It is very easy to use (you just need to write rules) and it will output all the code you need to parse according to your rules.

http://www.antlr.org/wiki/display/ANTLR3/Python+runtime

I believe is the current documentation for the Python implementation. Java is the main language it is used for but Python should be supported.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM