home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pccts.zip / pccts / NOTES.visual.C++ < prev    next >
C/C++ Source or Header  |  1994-03-31  |  4KB  |  108 lines

  1.          How to port PCCTS 1.10 (and 1.20 hopefully) to Visual C++
  2.  
  3.                                    By
  4.  
  5.                        John Hall <jhall@ivy.wpi.edu>
  6.  
  7. Here is how to compile an ANTLR grammar in Visual C++.  These steps
  8. describe how to have your ANTLR grammar parse the input file the user
  9. selects when they choose File Open in your Windows application.  (Even
  10. if you aren't using Visual C++, the steps should be portable enough to
  11. other compilers.)
  12.  
  13.  * Make sure that ANTLR and DLG generate ANSI code (use the -ga
  14.    switch).
  15.  
  16.  * Set the following compiler flags in Visual C++ (these are in the
  17.    Memory Model category of the compiler options in the Project
  18.    Options menu):
  19.  
  20.    FLAG MEANING
  21.    ==== ==============================================================
  22.    /AL    Large memory model (multiple data segments; data items must be
  23.     smaller than 64K).
  24.  
  25.    /Gtn    Allocates all items whose size is greater than or equal to n
  26.     in a new data segment.  (I let n be 256: /Gt256.)
  27.  
  28.    /Gx-    All references to data items are done with far addressing in
  29.     case they are placed in a far segment.
  30.  
  31.  * Add the following member variable to the attributes section of your
  32.    derived CDocument class (you will need to make sure you also
  33.    include stdio.h):
  34.  
  35.    FILE *fp;
  36.  
  37.  * Add the following method to your derived CDocument class:
  38.  
  39.    BOOL CAppDoc::OnOpenDocument(const char* pszPathName)
  40.    {
  41.        // Call CDocument's OnOpenDocument to do housekeeping for us
  42.        // DON'T add anything to the loading section of Serialize
  43.        if (!CDocument::OnOpenDocument(pszPathName))
  44.            return FALSE;
  45.    
  46.        // Open input file
  47.        if ((fp = fopen(pszPathName, "r")) == NULL)
  48.            return FALSE;
  49.    
  50.        // Parse input file
  51.        ANTLR(start(), fp);
  52.    
  53.        // Close input file
  54.        fclose(fp);
  55.        return TRUE;
  56.    }
  57.  
  58.    (Note: additional code may be necessary, depending on your parser.
  59.    For example, if your parser uses PCCTS's symbol table library, you
  60.    will need to insert calls to zzs_init and zzs_done.)
  61.  
  62.  * Compile the generated C files as C++ files.  (I renamed the files
  63.    to have a .CPP extension to fool Visual C++ into thinking they were
  64.    C++ files.  One might also use the /Tp switch, but that switch
  65.    requires you separately include the filename.)  [I used this step
  66.    as an easy out for all the external linking errors I was getting
  67.    that I couldn't fix by declaring things extern "C".]
  68.  
  69.  * Make sure the __STDC__ portion of the generated files gets
  70.    compiled.  (Either define __STDC__ yourself or else change all
  71.    occurrences of __STDC__ to __cplusplus in the generated files.  You
  72.    can define __STDC__ in the Preprocessor category of the compiler
  73.    options.)
  74.  
  75. That last step is important for Visual C++, but may not apply to other
  76. compilers.  For C++ compilers, whether __STDC__ is defined is
  77. implementation dependent (ARM, page 379).  Apparently, Visual C++ does
  78. not to define it; it also does not support "old style" C function
  79. definitions (which is okay, according to page 404 of the ARM).  Those
  80. two things together caused problems when trying to port the code.
  81. When it saw this:
  82.  
  83. #ifdef __STDC__
  84. void
  85. globals(AST **_root)
  86. #else
  87. globals(_root)
  88. AST **_root;
  89. #endif
  90.  
  91. it skipped the __STDC__ section and tried to process the "old style"
  92. function definition, where it choked.
  93.  
  94. When you finally get your parser to compile and link without error,
  95. you may get General Protection Fault errors at run time.  The problem
  96. I had was that a NULL was passed to a variable argument function
  97. without an explicit cast.  The function grabbed a pointer (32-bits)
  98. off the stack using va_arg, but the NULL was passed silently as the
  99. integer 0 (16 bits), making the resulting pointer was invalid.  (This
  100. was in PCCTS's sample C parser.)
  101.  
  102. There is one other thing I might suggest to help you avoid a run-time
  103. error.  Make sure you redefine the default error reporting function,
  104. zzsyn.  To do this, put "#define USER_ZZSYN" in your #header section
  105. and put your own zzsyn somewhere.  You can then pop up a MessageBox or
  106. print the error to some output window.
  107.  
  108.