Parsing c files in python
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.
Add a comment. That might looks something like this:. Stack Overflow for Teams — Collaborate and share knowledge with a private group.
Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Asked 9 years, 5 months ago. Active 4 years, 8 months ago. Viewed k times. Being a novice in Python, I got stuck at this point. Any input would be appreciated!
You should attach a copy of the file you need to parse and the expected output, that way it will be easier to help you. Add a comment. Active Oldest Votes. Christopher Bottoms Joe Day Joe Day 6, 4 4 gold badges 22 22 silver badges 26 26 bronze badges.
To admit, I could not find a way to do it, that's why I decided to read the whole thing. Should I write the file except the first 4 lines to another file to run the loop you have above? I bet there should be an easier way though. Now, I would be very glad to learn about the "read line-parse-write line line-by-line " way of doing things. Could you update your answer with a second example? Dec 29, Dec 27, Aug 15, Jun 16, Feb 4, Oct 16, May 21, Mar 7, Feb 18, Download the file for your platform.
If you're not sure which to choose, learn more about installing packages. Warning Some features may not work without JavaScript. Please try enabling it if you encounter problems. Search PyPI Search.
0コメント