Компилятор

C++ Bison LLVM

О проекте

Учебный проект. Компилятор, написанный для некого I языка. Разрабатывался в рамках курса: Compilers Construction.

Language characteristics:
- Imperative, structured language
- Program = sequence of declarations (variables, types, subprograms)
- Execution starts from an explicitly invoked subprogram
- Scopes: entities visible from declaration to block end

AST: Node Hierarchy
ASTNode (base class)
⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣├── Expression
⁣ ⁣ ⁣ ⁣ ⁣ ⁣ │⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ├── IntegerLiteral, RealLiteral, BooleanLiteral, StringLiteral
⁣ ⁣ ⁣ ⁣ ⁣ ⁣ │⁣⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ├── Identifier
⁣ ⁣ ⁣ ⁣ ⁣ ⁣ │⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ├── BinaryOp, UnaryOp
⁣ ⁣ ⁣ ⁣ ⁣ ⁣ │⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ├── ArrayAccess, FieldAccess
⁣ ⁣ ⁣ ⁣ ⁣ ⁣ │⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ └── RoutineCallExpr
⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ├── Statement
⁣ ⁣ ⁣ ⁣ ⁣ ⁣ │⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ├── VarDecl, TypeDecl, RoutineDecl
⁣ ⁣ ⁣ ⁣ ⁣ ⁣ │⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ├── Assignment, IfStatement, WhileLoop, ForLoop
⁣ ⁣ ⁣ ⁣ ⁣ ⁣ │⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ├── PrintStatement, ReadStatement
⁣ ⁣ ⁣ ⁣ ⁣ ⁣ │⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ └── ReturnStatement, BreakStatement, ContinueStatement
⁣ ⁣ ⁣ ⁣ ⁣ ⁣ └── Type
⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ├── PrimitiveType (integer, real, boolean, string)
⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ├── ArrayType, RecordType
⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ ⁣ └── UserDefinedType

Code example:


type Point is record
var x : integer
var y : integer
end

var p : Point
p.x := 3
p.y := 4

routine AddPoints(a : Point; b : Point) : Point is
var result : Point
result.x := a.x + b.x
result.y := a.y + b.y
return result
end

p := AddPoints(p, p)
print p.x
print p.y