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: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • Due to internal controls and regulations, the amount of long term archival data is increasing every year. Since magnetic tape does not need to be periodically operated or connected to a power source, there will be no data loss because of performance degradation due to the drive actuator. Read this white paper to learn about a series of tests that determined magnetic tape is a reliable long-term storage solution for up to 30 years.

Most Popular Programming Stories

More for Developers

RSS Feeds