Results 1 to 7 of 7

Thread: Generic parser for source code, etc. to share

  1. #1

    Smile Generic parser for source code, etc. to share

    Hi all,
    During my work with my 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):
    unit_base_parser.pas.txt
    unit_base_parser_types.pas.txt

    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):
    parser_test.dpr.txt

    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

  2. #2
    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

  3. #3
    Here are the latest paser files:

    unit_base_parser_types.pas.txt
    unit_base_parser.pas.txt

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

    Enjoy!

    cheers,
    Paul

  4. #4
    PGD Staff code_glitch's Avatar
    Join Date
    Oct 2009
    Location
    UK (England, the bigger bit)
    Posts
    933
    Blog Entries
    45
    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.
    I once tried to change the world. But they wouldn't give me the source code. Damned evil cunning.

  5. #5
    Quote Originally Posted by code_glitch View Post
    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:

    Code:
    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

  6. #6
    PGD Staff code_glitch's Avatar
    Join Date
    Oct 2009
    Location
    UK (England, the bigger bit)
    Posts
    933
    Blog Entries
    45
    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
    I once tried to change the world. But they wouldn't give me the source code. Damned evil cunning.

  7. #7
    hahaha! OOOOPS!

    cheers,
    Paul

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •