PDA

View Full Version : Generic parser for source code, etc. to share



paul_nicholls
01-02-2011, 10:51 AM
Hi all,
During my work with my Pas2C64 (http://www.pascalgamedevelopment.com/showthread.php?6660-Pas2C64) parser I have been working on, I decided to create a generic parser class that anyone can use as a starting point, and sub-class it to make more specific parsers :)

Here is the base parser and base parser type units (with .txt after the .pas to allow uploading):
214
215

I have based it on the recursive descent parser shown here (C code):
http://en.wikipedia.org/wiki/Recursive_descent_parser

You must subclass the parser to use it. See the attached file below (with .txt after the .dpr):
216

You Register all generic tokens and keyword tokens with the parser (see the base parser file for an example; this can be moved into a concrete parser types file (or similar) if you prefer).

You then test for the token values in the parser just like in the attached program example :)

Enjoy!

cheers,
Paul

paul_nicholls
01-02-2011, 07:37 PM
I just realized that I need to update the parser files a little bit, due to subclassing issuses...so I will do this a bit later today

paul_nicholls
01-02-2011, 10:36 PM
Here are the latest paser files:

217
218

I have updated the parser so one can override what tokens are being used for generic tokens, operators, and keywords...

Enjoy!

cheers,
Paul

code_glitch
02-02-2011, 10:53 PM
Just read through that - wow that is some darn nice code. Nothing like the SVN of prometheus ;) you have 400 odd lines of perfection, I give you 1200 lines of functionality held together with duct tape ARGGGH! No not really, I'm re-organising the source (cant make heads or tails of it any more XD) But I must say I can't wait to see how this project turns out. So good luck.

paul_nicholls
02-02-2011, 11:09 PM
Just read through that - wow that is some darn nice code. Nothing like the SVN of prometheus ;) you have 400 odd lines of perfection, I give you 1200 lines of functionality held together with duct tape ARGGGH! No not really, I'm re-organising the source (cant make heads or tails of it any more XD) But I must say I can't wait to see how this project turns out. So good luck.

:-[ Wow! Thanks for the nice comments mate :)

I don't think my code is THAT good LOL

I do like your description - "1200 lines of functionality held together with duct tape", but I am sure it isn't that bad :)

Good luck with Prometheus man...looking forward to more of your project :)

PS, I have discovered a small bug in the TBasePaser.Execute method where I was calling GetToken once too many times! D'OH!

Here is the correct version of the method:


function TBaseParser.Execute(const aSrcStream,aDstStream: TStream): Boolean;
begin
FSrcStream := aSrcStream;
FDstStream := aDstStream;

FErrorMsg := '';
Result := True;

// initialize parser
GetCharacter;
GetToken;

try
ParseInput; // override this to make a proper parser
except
on E:Exception do
begin
FErrorMsg := E.Message;
Result := False;
end;
end;
end;


cheers,
Paul

code_glitch
03-02-2011, 12:45 PM
Ah, object oriented code or (OOP because when it screws up you go OOOOPS) nah just kidding. Anyway, like I said, good luck and it's looking good so far - might even be using this one day :D

paul_nicholls
03-02-2011, 09:42 PM
hahaha! OOOOPS! :)

cheers,
Paul