Skip to content
poyrazogluyigitPublic

About

PLCC - Compiler for a custom extension of PL0 (developed as part of CMPE425 - Compiler Design)

Resources

Stars

1 star

Watchers

1 watching

Forks

Latest commit

 

History

32 Commits

Folders and files

Repository files navigation

plcc

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.

Language

The language (see programs/*.pl0 for examples) supports:

  • const and var declarations, plus fixed-size array declarations (var f 5; declares an array of 5 elements).
  • procedure and function declarations (with parameters and, for functions, a return).
  • 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 odd predicate.

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.

Architecture

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 the main() driver.

Building

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 binary

Generated lexer/parser sources land in src/generate/; object files land in build/.

Usage

./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's opt on 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 code

Tests

symbolTableTest/ 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.

About

PLCC - Compiler for a custom extension of PL0 (developed as part of CMPE425 - Compiler Design)

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages