plcc is a compiler for a small PL/0-derived teaching language, compiling
source programs down to LLVM IR text
(.ll). The frontend is a flex/bison lexer-parser that builds an AST; each
AST node knows how to emit its own LLVM IR.
The language (see programs/*.pl0 for examples) supports:
constandvardeclarations, plus fixed-sizearraydeclarations (var f 5;declares an array of 5 elements).procedureandfunctiondeclarations (with parameters and, for functions, areturn).- Control flow:
if/then/else,while/do,for/upto/downto,break. - I/O:
read(x),write(x),writeline(x). - Expressions with the usual arithmetic/comparison operators and an
oddpredicate.
Example (programs/array_conditions.pl0):
const x = 5, array a = [1,2,3];
var q,z,y,d 3, f 5;
begin
f[1] := 5;
f[2] := 3;
if f[2] > f[1] then
writeline(f[2])
else
writeline(f[1]);
read(q);
...
end.
grammar/tokens.l flex lexer
grammar/parser.y bison grammar; builds the AST and drives codegen
│
▼ (generated into src/generate/ at build time)
src/*.cpp, src/include/*.hpp
ExprAST, CondAST, StmtAST, DeclAST hierarchies (src/include/*.hpp)
Each node implements generate()/generateCode(), emitting LLVM IR
text directly to the output file (ProgramAST::writeToFile()).
src/include/header.hpp— umbrella include, pulls in the AST headers in dependency order (ExprAST→CondAST→StmtAST→DeclAST).src/{Expr,Cond,Stmt,Decl}AST.cpp— AST node implementations.grammar/parser.y— grammar, semantic actions that build the AST, and themain()driver.
Requires g++ (C++11), flex, and bison.
make # builds ./plcc
make debug # builds with bison's --debug --verbose -k (see Makefile DEBUG_FLAGS)
make clean # removes build/, src/generate/, and the plcc binaryGenerated lexer/parser sources land in src/generate/; object files land in
build/.
./plcc <input.pl0> <output-prefix> [opt-level]- Parses
<input.pl0>and writes LLVM IR to<output-prefix>.ll. - If
<opt-level>is given (e.g.-O2), also invokes LLVM'sopton the generated IR and writes<output-prefix>_optimized.ll.
./plcc programs/simple.pl0 simple
lli simple.ll # run the IR directly, or `llc` it to native codesymbolTableTest/ is a standalone symbol-table smoke test
(symbolTableTest/main.cpp), separate from the plcc build.
llvm_tests/ holds small hand-written C/LLVM-IR reference snippets
(array, main, scan) used while developing codegen for arrays and
read/scanf, not generated by plcc itself.