Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

35 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ParsingCompiler

VŠB-TUO — School project · Programming Languages and Compilers (PJP)

Java ANTLR4

A complete compiler and interpreter for a custom statically-typed programming language, built with ANTLR4. The pipeline covers four stages: parsing, type checking, stack-based code generation, and interpretation.

Successful compilation   Error handling

Pipeline

Stage Description
Parser ANTLR4 lexer + parser, reports all syntax errors
Type checker Semantic analysis, validates type compatibility
Code generator Produces 26-instruction stack-based bytecode
Interpreter Executes the generated bytecode

Language Specification

Types and literals

Type Literal Default
int sequence of digits 0
float digits with . 0.0
bool true / false false
string "text" ""

Variables must be declared before use. Repeated declaration is an error. int values automatically promote to float where needed — there is no reverse conversion.

Statements

  • type var1, var2; — variable declaration
  • expression; — expression statement (assignment has side effects)
  • read var1, var2; — read values from stdin
  • write expr1, expr2; — write values to stdout, newline after last
  • { ... } — block
  • if (cond) stmt [else stmt] — conditional
  • while (cond) stmt — loop
  • // — single-line comment

Operators (lowest to highest precedence)

Operator Signature
= x × x → x (right-assoc)
|| B × B → B
&& B × B → B
== != x × x → B
< > x × x → B (numeric)
+ - . arithmetic / string concat
* / % I × I → I or F × F → F
! B → B
unary - I → I or F → F

Instruction set (stack-based VM)

Instruction Description
push T x Push value x of type T (I/F/S/B)
pop Discard top of stack
load id Push variable value
save id Pop and store into variable
add sub mul div mod Binary arithmetic
uminus Unary minus
concat String concatenation
and or not Boolean logic
gt lt eq Comparisons
itof Convert int to float
label n Mark jump target
jmp n Unconditional jump
fjmp n Jump if top of stack is false
print n Pop and print n values
read T Read value of type T from stdin

Requirements

  • Java 21+
  • Maven 3.8.1+ (ANTLR 4.9.2 managed automatically)

Setup

  1. Clone the repository:

    git clone https://github.com/Firestone82/ParsingCompiler.git
    cd ParsingCompiler
  2. Build:

    mvn clean package
  3. Run a program:

    java -jar target/ParsingCompiler-*.jar path/to/program.lang

Sample programs and error examples are in the src/main/antlr4/Project/ directory.

License

This project was created as a school assignment at VŠB-TUO.

About

VŠB-TUO — School project: compiler and interpreter for a custom language built with ANTLR4 (PJP)

Topics

Resources

Stars

Watchers

Forks

Used by

Contributors

Languages