Author |
Message |
md
|
Posted: Sat Oct 10, 2009 12:21 am Post subject: Writing a parser in C++ |
|
|
As part of a project I've got going right now I've written a scanner/parser for the RedCode assembly language (http://en.wikipedia.org/wiki/Redcode). And as I have it working (after very very preliminary testing...) I figured I would share it with CS.ca as an example, and possibly inspire others to do something similar.
The code is available from my mercurial server (http://hg.nxor.org/Corewars) as a C++ eclipse project, and as a tarball at http://hg.nxor.org/hg.cgi/Corewars/archive/tip.tar.bz2. Unfortunately I haven't bothered to write a makefile for it, so if you don't use eclipse you'll have to write your own or use an automake tool. For those who just want to look at the code it is also available through your web browser at http://hg.nxor.org/hg.cgi/Corewars/file/a79931cdec89. I've made a branch, so the code will remain stable for a while as I continue to work on the Trunk.
The code consists of 5 major parts: a set of tokens, a scanner to read a file and return tokens, a finite state machine (FSM) to match some of the tokens, a parser to build a RedCode program construct, and the RedCode program construct itself. If there are questiosn about the code (which is not well documented yet...) post 'em here and I'll do my best to answer; right now I'm a wee bit tired to explain it in any more detail then that
[edit] Fixed broken links which a surprising number of people are following to point to the correct place
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
Clayton
|
Posted: Sun Oct 11, 2009 8:40 pm Post subject: RE:Writing a parser in C++ |
|
|
Your states! They make my eyes bleed. Is there really no better way of handling them?
|
|
|
|
|
|
apomb
|
Posted: Tue Oct 13, 2009 9:27 am Post subject: RE:Writing a parser in C++ |
|
|
Is this a project for school? or work? or just a personal project? I don't know enough about parsers to ask specific questions, but that's very impressive work indeed.
|
|
|
|
|
|
Tony
|
Posted: Tue Oct 13, 2009 9:38 am Post subject: RE:Writing a parser in C++ |
|
|
Quote:
Redcode is the name of the programming language used in the game Core War.
|
Tony's programming blog. DWITE - a programming contest. |
|
|
|
|
md
|
Posted: Wed Oct 14, 2009 8:17 pm Post subject: RE:Writing a parser in C++ |
|
|
For fun, and there are a couple of bugs in it currently - a memory leak for one (see if you can find it!)
The FSM could probably be done better (as in more readable) but certainly not as efficiently. It's only unreasonable because I chose to recognise alll the operator and operator modifiers with the FSM instead of a seperate table lookup after the fact. The later aproach is shorter line-wise, but makes slightly more complicated code. The finite state machine is actually absurdly simple to debug.
|
|
|
|
|
|
mirhagk
|
Posted: Mon Feb 08, 2010 2:23 pm Post subject: RE:Writing a parser in C++ |
|
|
i would just like to say thank you. I know that this is kinda a necro but i actually am currently working on a parser for school so now i have something to kinda see how a parser actually works.
|
|
|
|
|
|
md
|
Posted: Mon Feb 08, 2010 4:53 pm Post subject: RE:Writing a parser in C++ |
|
|
You're most welcome.
Unfortunately I haven't done much more work on that particular project due to time constraints; however I am reasonably sure that what I have already written is at least free of bugs.
You might find http://hg.nxor.org/hg.cgi/Corewars/file/a79931cdec89 to be a better reference, as there were a couple of bug fixes after the cs.ca version.
[edit] Fixed broken link (see above and below!)
|
|
|
|
|
|
md
|
Posted: Sat Feb 12, 2011 12:10 am Post subject: RE:Writing a parser in C++ |
|
|
As it seems that people still find this interesting and are looking for the code, I've re-hosted it as a mercurial repository here: http://hg.nxor.org/hg.cgi/Corewars/
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
QuantumPhysics
|
Posted: Thu Aug 30, 2012 12:44 pm Post subject: RE:Writing a parser in C++ |
|
|
The black mage has casted an evil Resurrection spell on this post.
I'm sorry but, would you happen to tell me where exactly the link is for the download? For some reason i cannot see it. I am really interested in see'ing the source code.
|
|
|
|
|
|
md
|
Posted: Thu Aug 30, 2012 9:18 pm Post subject: Re: Writing a parser in C++ |
|
|
Seems that on one of my re-installs I disabled the non-passworded access to my repos and removed this one anyway.
Here's an archive of the source, untouched since I last looked at it.
Note that as compsci limits file name extensions it's not actually a ZIP file, but a bz2'd tar archive. either rename to tar.bz2 or use a sane tool that doesn't care about file names.
Description: |
|
Download |
Filename: |
CoreWars Evolver.zip |
Filesize: |
872.85 KB |
Downloaded: |
1194 Time(s) |
|
|
|
|
|
|
|