Menu

howto parse nested files (e.g. c's #include)

2007-10-04
2013-05-14
  • Larry Evans

    Larry Evans - 2007-10-04

    So far, I've seen how to parse a string and a complete file; however, I haven't
    seen how to parse something like c's #include "file" and then start parsing
    file until it's done, and then resume parsing after the #include.

    What's the best way to do this?

     
    • Paul McGuire

      Paul McGuire - 2007-10-08

      One approach is to do an initial pass to handle all of the preprocessor instructions, such as #define, #include, etc. and create a composite file with all of the preprocessor instructions and references already resolved.  However, if you need to retain some traceability of what came from the original source, and what came from the include file, this would not work as well.

      Something else to try would be to use a parse action attached to the #include expression, and have the parse action parse the header file before returning to parse the remainder of the source file.

      In either case, you would need to guard against recursive inclusion taking you down an infinite loop of #includes.

      HTH,
      -- Paul

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.