Mini Pascal Compiler

Environment: VC6

I had to make a project last month for the Compiler Techniques course at the university where I study.  This project was to make a simple Pascal compiler for an imaginary virtual machine.  I also work for a software company so I had to make the project in my free time after university and work, so in some places the code is made in a rush (you know what I mean).  Anyway, this program is a nice compiler sample, and for people who are interested, might me a good source of ideas.

Mini Pascal Compiler

The program is a multi document/view based application.  It supports 3 types of files:

  • *.rez, a token result file that is generated after parsing the source code
  • *.asm, an assembler file containing virtual machine code, that is generated by the compiler together with the binary file
  • *.pas, Pascal source files
For the asm and pas documents, I used the syntax coloring editor code from Adrei Stcherbachenko, that I downloaded from CodeGuru.

This sample was made using Visual C++ v 6.0, and contains 2 projects: the compiler and the virtual code interpreter.

General Structure

I used the following class hierarchy:

   CObject -> CStringTokenizer -> CPascalLexAnalyzer -> CPascalCompiler

I tried to modularize the compiling process so that the reusability of the classes would be higher.  For example, the CStringTokenizer class can be used for tokenizing strings and command line parameters and can be used to make a C Lexical analyzer, etc.  The CPascalLexAnalyzer is used to tokenize the Pascal source, and the CPascalCompiler is responsible for making syntactical and semantically verifications and for the actual compilation.

Downloads

Download source - 169 Kb


Comments

  • Pascal Compiler Problem :(

    Posted by Legacy on 06/25/2003 12:00am

    Originally posted by: victor

    vmpascal Could not create output directory 'e:\zscpascal'.
    
    

    c:\Documents and Settings\Administrator\Desktop\PascCompilers\zscpascal\StringTokenizer.cpp(37): error C2593: 'operator +=' is ambiguous

    CStringTokenizer::CStringTokenizer(CString& string)
    {
    m_bPushedBack = FALSE;
    m_bEolIsSignificant = FALSE;
    m_bSlSlComments = TRUE;
    m_bSlStComments = TRUE;
    m_bPascalComments = TRUE;
    m_iLineNo = 1; // the first line
    m_iChar = 0;
    m_peekc = ' ';
    m_sString = string;
    >>>> m_sString += -1;

    i tried to run on c++.net on win 2000 just in case. i really want to look this compiler since i writing my own now.

    Reply
  • Wonderful Work!

    Posted by Legacy on 05/31/2002 12:00am

    Originally posted by: Peter

    I would be grateful if you could provide me the principle description of the codes. It is difficult for me to research source codes without guider documents. I would appreciate it if you could contact me as: petersia@hotmail.com. Thanks!

    Reply
  • Good job

    Posted by Legacy on 03/05/2002 12:00am

    Originally posted by: Sanjoy chowdhury

    Good job. Thanks

    Reply
  • Extensions?

    Posted by Legacy on 03/19/2001 12:00am

    Originally posted by: Sia, Kelvin

    Do you have a simple C lexical parser?
    Do you have a version of the source in CBuilder?

    Reply
  • What is Result?

    Posted by Legacy on 09/16/2000 12:00am

    Originally posted by: Taegoon

    I want compile result
    what is result?

    ~.pas -> compile -> ~.exe
    where are execute file?

    Reply
  • Output Directory

    Posted by Legacy on 05/30/2000 12:00am

    Originally posted by: John A. Donaldson

    How do I override the default output directory? Presently
    it is set to E:\

    Reply
  • How to do?

    Posted by Legacy on 05/28/2000 12:00am

    Originally posted by: Daniel Anderson

     I would like to compile and to test the code of the program ZCPASCAL in Visual C++, though I don't know as proceeding for not knowing the environment for such procedure.  
    
    It would be very grateful if it was able to me to help.
    I advance gratefulness,

    Respectfully,
    Daniel Anderson

    danander@infraero.gov.br and scandisker@hotmail.with

    Reply
  • Nice Editor

    Posted by Legacy on 10/27/1999 12:00am

    Originally posted by: Ernest L.

    The editor is nice!
    It helps me with my design.
    Thanks and keep your good work.
    Ernest L.

    Reply
  • Good - 1 error in the zipped source

    Posted by Legacy on 08/26/1999 12:00am

    Originally posted by: rinix

    Really good
    I'm doing an universal editor like UltraEdit, but with improved capacities with command line compilers (all languages)

    But after downloading the sources, I've compiled them, and there were a little error on the TypeInfo struct

    the assignment operator must return a value and there's no return statment in the code, so I've placed the statment

    TypeInfo & operator = (TypeInfo &info)
    {
    m_nClass = info.m_nClass;
    m_nType = info.m_nType;
    m_nIndMin = info.m_nIndMin;
    m_nIndMax = info.m_nIndMax;
    m_nSize = info.m_nSize;
    m_VarList.RemoveAll( );
    m_VarList.AddTail( &info.m_VarList );

    // my added code
    return *this;
    }

    Gook luck in continuing

    regards, rinix

    Reply
  • Good job!

    Posted by Legacy on 08/12/1999 12:00am

    Originally posted by: webdog

    Thank you and your good job

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: September 10, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild". This loop of continuous delivery and continuous feedback is …

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds