PL/0 to C compiler to teach basic compiler construction from a practical, hands-on perspective.

Overview

pl0c

pl0c is a compiler for the PL/0 language. It reads in PL/0 source code and outputs equivalent C source code.

It was written to be the subject of a series of blog posts on writing a compiler from a hands-on practical perspective for the beginner.

Building

Just run make. Then (optionally) make install.

If you are on a platform that does not include strtonum(3) in its libc, remove -DHAVE_STRTONUM from CFLAGS. In this case, you'll have to put strtonum.c in the same directory as pl0c. This will be improved.

Version

The current version is 1.0.0.

You can find a source tarball in the Releases tab.

Issues and Pull Requests

Issues and Pull Requests are accepted at any time.

If your Pull Request changes the compiler in any way, I ask that you remember to update the bootstrap compiler using make genbootstrap and including the updated bootstrap compiler as part of the Pull Request.

You might also like...
A project uses for beginners, who wants to learn basic Cpp.

Learning Basic Cpp The basic project for who wants to learn Cpp. Notes: All the files are coded using Microsoft Visual Studio 2019. If you want to cod

A Compiler Writing Journey

In this Github repository, I'm documenting my journey to write a self-compiling compiler for a subset of the C language. I'm also writing out the details so that, if you want to follow along, there will be an explanation of what I did, why, and with some references back to the theory of compilers.

a header-only crossplatform type-safe dynamic compiler generator based on C++ 17.
a header-only crossplatform type-safe dynamic compiler generator based on C++ 17.

Mu Compiler Generator MuCompilerGenerator(MuCplGen) a Header-Only dynamic compiler generator based on C++ 17. Why MuCplGen? header-only cross-platform

Colang - Programming language and compiler —WORK IN PROGRESS—

Co programming language Building Initial setup: ./init.sh will install the following into deps/: ckit build tool and rbase library ckit-jemalloc memor

🌼 Homework of Computer Systems: A Programmer's Perspective (3rd Edition) and Autolab solutions of CMU 15-513: Intro to Computer Systems
🌼 Homework of Computer Systems: A Programmer's Perspective (3rd Edition) and Autolab solutions of CMU 15-513: Intro to Computer Systems

Exercisebook of Computer Systems: A Programmer's Perspective, 3/E (CS:APP3e) CS:APP3e is written by Randal E. Bryant and David R. O'Hallaron, Carnegie

A guide for C Programming from the perspective of a Pulchowk student (IOE).

C-Programming-Guide This README contains a list of resources that will be required as you get used to programming in C.

OpenPose: Real-time multi-person keypoint detection library for body, face, hands, and foot estimation
OpenPose: Real-time multi-person keypoint detection library for body, face, hands, and foot estimation

Build Type Linux MacOS Windows Build Status OpenPose has represented the first real-time multi-person system to jointly detect human body, hand, facia

Teach the C programming language using a collection of super beginner friendly tutorials and challenges.
Teach the C programming language using a collection of super beginner friendly tutorials and challenges.

TeachMeCLikeIm5 You are welcome to contribute to this repo. See the CONTRIBUTING.md for more info 📜 About this repo 📜 A collection of super beginner

Code repository for SC21 hands-on activities

Hello Numerical World NOTE: This repo was imported from here In this repo is a very simple implementation of an application for solving the one dimens

Hands-On example code for Sensor Fusion and Autonomous Driving Stack based on Autoware
Hands-On example code for Sensor Fusion and Autonomous Driving Stack based on Autoware

Autoware "Hands-On" Stanford Lecture AA274 / Graz University of Technology M. Schratter, J. Zubaca, K. Mautner-Lassnig, T. Renzler, M. Kirchengast, S.

Teach you how to code an OS by yourself.
Teach you how to code an OS by yourself.

前言 操作系统,作为人类当前创造出来的最复杂的系统,其蕴含了大量精巧的设计。完全理解操作系统,应该是每一个程序员都想做的事情,但是每当我们想要去学习操作系统,了解其奥秘时,都会被里面 晦涩难懂的设计和一些闻所未闻的名词劝退。即使没有被劝退,非常痛苦的看完一本晦涩难懂的操作系统的书籍后,也会发现自己好

A guide that teach you build a custom version of Chrome / Electron on macOS / Windows / Linux that supports hardware / software HEVC decoding.

enable-chromium-hevc-hardware-decoding A guide that teach you build a custom version of Chrome / Electron on macOS / Windows / Linux that supports har

Practical assignments for the XDU compiler course: The interpreter of function drawing language.

drawing-lang-interpreter Practical assignments for the XDU compiler course: The toy interpreter of function drawing language, written by XDU Zhang Yi(

libsais is a library for linear time suffix array and burrows wheeler transform construction based on induced sorting algorithm.

libsais libsais is a library for fast (see Benchmarks below) linear time suffix array and Burrows-Wheeler transform construction based on induced sort

Recast is state of the art navigation mesh construction toolset for games. A virtual network Differential GNSS server-client project using Precise Point Positioning (PPP). Global coverage. Without physical base station construction needed. An open-source virtual base station approach.
A virtual network Differential GNSS server-client project using Precise Point Positioning (PPP). Global coverage. Without physical base station construction needed. An open-source virtual base station approach.

Virtual-Network-DGNSS-Project This project is the software implementation for a publicly available, open-source, client/server VN-DGNSS implementation

Bau Bau is a DIY 4 legged quadruped robot inspired for construction robotics course.

Bau-Bau-Robot Bau Bau is a DIY 4 legged quadruped robot inspired for construction robotics course. In this course, we are looking forward to solve a p

Signed - a 3D modeling and construction language based on Lua and SDFs. Signed will be available for macOS and iOS and is heavily optimized for Metal.
Signed - a 3D modeling and construction language based on Lua and SDFs. Signed will be available for macOS and iOS and is heavily optimized for Metal.

Signed - A 3D modeling language Abstract Signed is a Lua based 3D modeling language, it provides a unique way to create high quality 3D content for yo

Practical mutation testing tool for C and C++

Mull Mull is a tool for Mutation Testing based on LLVM/Clang with a strong focus on C and C++ languages. For installation and usage please refer to th

Comments
  • I/O - Simple output of integers - proof of concept.

    I/O - Simple output of integers - proof of concept.

    I see you have a TODO referencing the need for simple I/O, but since I was curious I figured I'd have a stab at something basic myself.

    I google'd "pl/0 examples" and found this wikipedia page:

    • https://en.wikipedia.org/wiki/PL/0

    That contains a simple "dump primes" sample, which contains a call to write to output a number. Adding support for that was trivial, albeit simple because I assumed only integer output. With that in place:

    $ cat primes.pl0 
    const max = 100;
    var arg, ret;
    
    procedure isprime;
    var i;
    begin
    	ret := 1;
    	i := 2;
    	while i < arg do
    	begin
    		if ( arg / i * i ) = arg then
    		begin
    			ret := 0;
    			i := arg
    		end;
    		i := i + 1
    	end
    end;
    
    procedure primes;
    begin
    	arg := 2;
    	while arg < max do
    	begin
    		call isprime;
    		if  ret = 1  then write arg;
    		arg := arg + 1
    	end
    end;
    
    call primes
    .
    

    Then compiling and running:

    frodo ~/pl0c $ ./pl0c primes.pl0  > p.c
    frodo ~/pl0c $ gcc p.c
    frodo ~/pl0c $ ./a.out 
    2
    3
    5
    7
    11
    13
    17
    19
    23
    29
    31
    37
    41
    43
    47
    53
    59
    61
    67
    71
    73
    79
    83
    89
    97
    

    The change is trivial:

    frodo ~/pl0c $ git diff *.c
    diff --git a/pl0c.c b/pl0c.c
    index f416730..7044009 100644
    --- a/pl0c.c
    +++ b/pl0c.c
    @@ -33,6 +33,7 @@
     #define TOK_VAR                'V'
     #define TOK_PROCEDURE  'P'
     #define TOK_CALL       'c'
    +#define TOK_WRITE       'w'
     #define TOK_BEGIN      'B'
     #define TOK_END                'E'
     #define TOK_IF         'i'
    @@ -184,6 +185,8 @@ ident(void)
                    return TOK_PROCEDURE;
            else if (!strcmp(token, "call"))
                    return TOK_CALL;
    +       else if (!strcmp(token, "write"))
    +               return TOK_WRITE;
            else if (!strcmp(token, "begin"))
                    return TOK_BEGIN;
            else if (!strcmp(token, "end"))
    @@ -679,6 +682,12 @@ statement(void)
                            cg_call();
                    expect(TOK_IDENT);
                    break;
    +        case TOK_WRITE:
    +            expect(TOK_WRITE);
    +            if ( type == TOK_IDENT)
    +                aout("printf(\"%%d\\n\",%s);",token);
    +            expect(TOK_IDENT);
    +            break;
            case TOK_BEGIN:
                    cg_symbol();
                    expect(TOK_BEGIN);
    @@ -827,6 +836,8 @@ main(int argc, char *argv[])
            readin(argv[1]);
            startp = raw;
     
    +        aout("#include <stdio.h>\n");
    +
            initsymtab();
     
    
    

    I explicitly submitted this as a diff rather than a pull-request because no doubt if you were to do this you'd do it properly, and support input too. Right now I don't need that to satisfy my curiosity :)

    opened by skx 2
Releases(pl0c-1.0.2)
Owner
Brian Callahan
OpenBSD developer / Prof., ITWS@RPI
Brian Callahan
A guide for C Programming from the perspective of a Pulchowk student (IOE).

C-Programming-Guide This README contains a list of resources that will be required as you get used to programming in C.

IT Club, Pulchowk 26 Jul 25, 2022
Teach the C programming language using a collection of super beginner friendly tutorials and challenges.

TeachMeCLikeIm5 You are welcome to contribute to this repo. See the CONTRIBUTING.md for more info ?? About this repo ?? A collection of super beginner

inspirezonetech 11 Nov 4, 2022
Practical exercises for the "Advanced C++" course in 2021/22

Advanced C++ Programming - Practical Part This repository contains practical exercises to accompany the lecture. Based on Material by Alex Hirsch. Gra

null 11 Dec 19, 2022
A BASIC Compiler and IDE for Amiga Computers

AQB: A BASIC Compiler and IDE for Amiga Computers About Project Scope Requirements Installation Benchmark Results Source Code Command Reference Refere

Guenter Bartsch 59 Dec 30, 2022
Basic eBPF examples in Golang using libbpfgo

libbpfgo-beginners Basic eBPF examples in Golang using libbpfgo. Accompanying slides from my talk at GOTOpia 2021 called Beginner's Guide to eBPF Prog

Liz Rice 166 Dec 28, 2022
This repository is a summary of the basic knowledge of recruiting job seekers and beginners in the direction of C/C++ technology, including language, program library, data structure, algorithm, system, network, link loading library, interview experience, recruitment, recommendation, etc.

?? C/C++ 技术面试基础知识总结,包括语言、程序库、数据结构、算法、系统、网络、链接装载库等知识及面试经验、招聘、内推等信息。This repository is a summary of the basic knowledge of recruiting job seekers and beginners in the direction of C/C++ technology, including language, program library, data structure, algorithm, system, network, link loading library, interview experience, recruitment, recommendation, etc.

huihut 27k Dec 31, 2022
Basic Fortnite Internal Cheat Source

Basic Fortnite Internal Cheat Source That's a basic fortnite cheat internal source for pasters and for people that just want to learn from it. This is a trash code that will be improved when I have time and when I want.

Android1337 13 Jun 13, 2022
C++ Programs from Basic to Advanced lavel.

✨️ C++ Programs (Basic to Advanced Level) ??️ ??️ Local Environment Setup If you are still willing to set up your environment for C++, you need to hav

Atul Tripathi 1 Oct 23, 2021
✨️ C Programs (Basic to Advanced Level - Chapter wise) 🚀️

✨️ C Programs (Basic to Advanced Level - Chapter wise) ??️ ??️ Tutorial Table Days Topics Day 1 Chapter 1: Variables, Constants, and Keywords Day 2 Ch

Atul Tripathi 1 Oct 22, 2021
Learn basic elements in C++ and learn CMake

learn-cpp-cmake Learn basic elements in C++ and learn CMake This repo has code from several sources. If you think we have violated any copyright law o

mafiaboy009 6 Mar 1, 2022