#1
  1. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,782
    Rep Power
    4301

    Critiques wanted for my C++ code


    Hi all,
    I decided to brush up on my C++ skills and do a refresher on C++ 14 in particular. Therefore, I wrote a quick n' dirty PL/0 interpreter using clang++ as the compiler. You can get it from here:

    https://bitbucket.org/scorpions4ever/pl0_interpreter/

    The code is in two directories, parser_only/ and compiler/ (which has the parser + compiler + interpreter). You can run 'make' in either directory and I've included a test program which you can run using:
    ./pl0 tests/validprogram.pl0

    You can also build the testlexer and testparser by running "make testlexer" or "make testparser" under the parser_only/ directory.

    Would love to hear some feedback from you guys about the code.

    PL/0 is a tiny language originally described by Niklaus Wirth in "Algorithms + Data Structures = Programs". A little googling should get you some notes about the structure of the language, which helps understand the parsing code.

    Thanks in advance!
    Last edited by Scorpions4ever; January 15th, 2017 at 04:24 AM.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2014
    Location
    India
    Posts
    200
    Rep Power
    4
    I've read about PL/I but never heard of this language. Looking up on Wikipedia, it seems that the language referred here is not a subset of PL/I but rather a simpler version of Pascal introduced in the book, Algorithms + Data Structures = Programs (Niklaus Wirth, 1976).

    Unfortunately I've never had a background in Pascal (looks like I took up programming much later to learn it) but I think the syntax is pretty simple to learn for anyone (esp. someone with a programming background).

    Nevertheless, I cloned the git repo and it easily compiled with g++ 6.2.1 (by changing the compiler in the Makefile) on debian-testing (amd64) with only a single warning. I might take a look at the code when I get time; Overall it looks like a fun language to build a parser for but I need to read up on it before saying anything.

    Code:
    $ git clone https://bitbucket.org/scorpions4ever/pl0_interpreter.git
    Cloning into 'pl0_interpreter'...
    remote: Counting objects: 56, done.
    remote: Compressing objects: 100% (55/55), done.
    remote: Total 56 (delta 16), reused 0 (delta 0)
    Unpacking objects: 100% (56/56), done.
    $ cd pl0_interpreter/compiler
    $ vi Makefile
    $ make
    g++ -c -O2 -g -Wall -std=c++14   -o main.o main.cpp
    g++ -c -O2 -g -Wall -std=c++14   -o symbols.o symbols.cpp
    g++ -c -O2 -g -Wall -std=c++14   -o keywords.o keywords.cpp
    g++ -c -O2 -g -Wall -std=c++14   -o mnemonics.o mnemonics.cpp
    g++ -c -O2 -g -Wall -std=c++14   -o scanner.o scanner.cpp
    g++ -c -O2 -g -Wall -std=c++14   -o lexer.o lexer.cpp
    g++ -c -O2 -g -Wall -std=c++14   -o parser.o parser.cpp
    g++ -c -O2 -g -Wall -std=c++14   -o symtable.o symtable.cpp
    symtable.cpp: In member function ‘SymtableEntry SymTable::get(long int)’:
    symtable.cpp:36:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         if (position < table.size()) {
             ~~~~~~~~~^~~~~~~~~~~~~~
    g++ -c -O2 -g -Wall -std=c++14   -o instruction.o instruction.cpp
    g++ -c -O2 -g -Wall -std=c++14   -o codelist.o codelist.cpp
    g++ -c -O2 -g -Wall -std=c++14   -o interpreter.o interpreter.cpp
    g++ -O2 -g -Wall -std=c++14    -o pl0 main.o symbols.o keywords.o mnemonics.o scanner.o lexer.o parser.o symtable.o instruction.o codelist.o interpreter.o
    $ ./pl0
    Usage: ./testlexer filename.pl0
    $ ./pl0 tests/validprogram.pl0
    ...
  4. #3
  5. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,782
    Rep Power
    4301
    Hi hexman, thanks for looking at the code. Interesting about that warning, I didn't see it in the two compilers that I tested. The warning is correct though, I'll fix it.

    A few notes about PL/0 and my implementation of it. In Wirth's book, he seems to have some kind of non-ASCII keyboard, because he has operators for <= and >= as a single symbol (≤ and ≥). I could have written my lexer to read <= or =>, but I opted to use a single symbol as in his book, so I used [ and ] instead for ≤ and ≥
    The full list of operators are:
    Code:
    =               equals
    #               not equals
    <               less than
    [               less than or equals
    >               greater than
    ]               greater than or equals
    Also, what wasn't really documented in Wirth's book (but was implemented) is that the interpreter prints out every time it writes to a memory location. So if it processes these two statements:
    Code:
    x := 23;
    y := x + x + 10;
    Then it will print out:
    Code:
    23
    56
    i.e. it will print every time it saves a value to the variable on the left hand side of the expression. It is very easy to implement a PRINT keyword and make it so that it only prints when this keyword is encountered, instead of printing every time it evaluates an assignment expression. I actually put that in yesterday. I'm thinking of putting in a few more enhancements to the original PL/0 language as well.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo

IMN logo majestic logo threadwatch logo seochat tools logo