Python c header file parser


















Add a comment. Active Oldest Votes. Improve this answer. Eli Bendersky k 83 83 gold badges silver badges bronze badges. Matt Anderson Matt Anderson While ostensibly ancient history, this answer deserves at least 2 more up votes -- it provides the ability to do exactly what the original post requests. You can also write your own parser using any of these tools: pyparsing ply lepl and a lot more Brunisboy 91 17 17 bronze badges.

Vijay Mathew Vijay Mathew Great references: I didn't know about a bunch of those modules. I'd like to know how full-featured they are compared to pygccxml which clearly has a leg-up since it's backed by GCC. Depends on pyparsing and, as far as I understand, it is pure python. Davide Davide 1 1 silver badge 4 4 bronze badges. The Overflow Blog. Podcast Helping communities build their own LTE networks. Podcast Making Agile work for data science. Finally, it's worth pointing out that if this sort of switch appears more than once in your program then you get to use my personal favourite refactoring, 'Replace Conditional With Polymorphism'.

This basically involves moving the repeated conditional logic into a single factory function, which returns different implementations of an interface. This looks like a good application for the Chain Of Responsiblity pattern.

The essence of a COR is a that you create a lot of little classes 'handlers' that look at a stream of inputs. Each class in the chain checks to see if it can handle the input, and if so it parses it and does what ever it needs to do with it; otherwise it just passes the input along to the next handler in the chain.

It's a common way to break up complex parsing tasks into a series of smaller chunks - as the other comments note, this is a fiddly problem with lots of details that will always have lots of nitpicky code in it; however by breaking your parsing tasks into lots of smaller chunks instead a long nested series of ifs or a switch you can make the code much easier to read, maintain and expand. In the classic COR pattern each handler has a link to the next, and requests are passed along the chain until one handler gets an answer which is passed back up the chain.

There's a simpler alternative in which you just loop over a list of handlers until you get a result. The deciding factor between the two approaches would be the need for implementing pre- and or post- answer functioniality.

If each handler is atomic, a simple list of parsers will suffice. If handlers have to prepare for or postprocess each other's results then the linked-list method applies. In your case it looks like all of your code paths are mutually eclusive so I'd stick with the simpler list-of-handler's method.

From the looks of your code above this could also be implemented without classes by just creating a list of parsing functions. However you'd then have to retype the signature check logic in all of the functions and if future requirements change to something where state was relevant, you'd have a harder time refactoring. Sign up to join this community. The best answers are voted up and rise to the top. Stack Overflow for Teams — Collaborate and share knowledge with a private group.

Create a free Team What is Teams? Learn more. Using pycparser to parse C header files Ask Question. Asked 8 years, 1 month ago. Active 7 years, 9 months ago. Viewed 12k times. UnknownIdentifier id regular fields, i. TODO -- very similar to code above Improve this question. Ben Osment Ben Osment 63 1 1 gold badge 1 1 silver badge 3 3 bronze badges. This is a nice complement to the header to binary Python code I wrote.

I find it amusing the setup code in main is larger than the actual conversion code. The main function that does the conversion. This really should be called with a variable name in order to avoid conflicts when running this over multiple files. First we need to make a list that will hold each line of the output. This gives us the data split into a list with elements that the size we need. The last one might be smaller which is fine. Now we go though each element which represents a line.

One thing to keep in mind is the join does not add a comma after the last element. It will only put a comma between elements.



0コメント

  • 1000 / 1000