Skip to content

BeastOfShadow/ac-dc-compiler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

24 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🧠 MiniLang AC/DC Compiler

A compiler for two toy languages: AC (an imperative C-like language) and DC (a stack-based postfix language).

Java Platform License Status Made with ❀️


AC/DC Compiler is a two-part compiler project developed for educational purposes. It supports two minimalistic languages:

  • AC: An imperative toy language with expressions, assignments, control flow, and I/O.
  • DC: A stack-based postfix language inspired by Unix’s dc, using reverse Polish notation. To see the documentation, click this link.

πŸš€ Features

  • πŸ” Lexical Analysis with custom tokenizer
  • 🧾 Parser with recursive descent for grammar rules
  • 🧠 Abstract Syntax Tree (AST) generation
  • πŸ› οΈ Semantic checks for identifiers and types
  • πŸ”„ Support for combined assignment operators (+=, -=, etc.)
  • πŸ–¨ Print statement support
  • ❌ Custom exception handling for syntactic and lexical errors
  • πŸ§ͺ JUnit testing for core components

🌀 Supported Languages

Language Description
AC C-like imperative language with variables, operators, control flow
DC Postfix (stack-based) expression language (like Unix dc)

πŸ“„ AC Language Overview

✍️ Syntax (AC)

x = 3 + 4;
y = x * 2;
print y;

🎯 Features (AC)

  • Variables and assignments
  • Arithmetic expressions: +, -, *, /
  • Print statements: print x;
  • Semi-colon terminated statements

πŸ“„ DC Language Overview

✍️ Syntax (DC)

3 4 + p

🎯 Features (DC)

  • Postfix notation (RPN)
  • Stack operations: push, pop, print
  • Arithmetic operations: +, -, *, /
  • p = print top of the stack
  • n = pop and discard top

βœ… Example:

5 1 2 + 4 * + 3 - p

Interpreted as:

  • Push 5
  • Push 1, 2 β†’ 1 2 + β†’ 3
  • 3 4 * β†’ 12
  • 5 12 + β†’ 17
  • 17 3 - β†’ 14
  • p β†’ prints 14

To check if the input is valid, you can use the dc command in your terminal:

echo "5 1 2 + 4 * + 3 - p" | dc

for exmple.

To see the documentation, click this link.


Tokens and Patterns

Token Pattern Class
INT [0-9]+ Constant/Litteral
FLOAT [0-9]+.([0-9]{0,5}) Constant/Literal
ID [a-z][a-z0-9]* Identificator
TYINT int Key Word
TYFLOAT float Key Word
PRINT print Key Word
OP_ASSIGN += | -= | *= | /= Operators
ASSIGN = Operator
PLUS + Operator
MINUS - Operator
TIMES * Operator
DIVIDE / Operator
SEMI ; Delimiter
EOF (char) -1 End Input

Character to ignore: ' ', '\n', '\t', '\r'.

Token format example:

<TYINT,r:1><ID,r:1,tempa><SEMI,r:1>
<ID,r:2,tempa><ASSIGN,r:2><INT,r:2,5><SEMI,r:2>
<TYFLOAT,r:3><ID,r:3,tempb><ASSIGN,r:3><ID,r:3,tempa><DIVIDE,r:3>
<FLOAT,r:3,3.2><SEMI,r:2>
<ID,r:4,tempb><OP_ASSIGN,r:4,+=><INT,r:4,7><SEMI,r:4>
<PRINT,r:5><ID,r:5,tempb><SEMI,r:5><EOF,r:5>

πŸ“˜ Predictive Parsing Table (Grammar)

This table outlines the predictive parsing rules for the AC language grammar.

Num. LHS RHS Predict Set
0 Prg DSs $ { TYFLOAT, TYINT, ID, PRINT, EOF }
1 DSs Dcl DSs { TYFLOAT, TYINT }
2 DSs Stm DSs { ID, PRINT }
3 DSs Ο΅ { EOF }
4 Dcl Ty id DclP { TYFLOAT, TYINT }
5 DclP ; { SEMI }
6 DclP = Exp ; { ASSIGN }
7 Stm id opAss Exp ; { ID }
8 Stm print id ; { PRINT }
9 Exp Tr ExpP { ID, FLOAT, INT }
10 ExpP + Tr ExpP { PLUS }
11 ExpP - Tr ExpP { MINUS }
12 ExpP Ο΅ { SEMI }
13 Tr Val TrP { ID, FLOAT, INT }
14 TrP * Val TrP { TIMES }
15 TrP / Val TrP { DIVIDE }
16 TrP Ο΅ { MINUS, PLUS, SEMI }
17 Ty float { TYFLOAT }
18 Ty int { TYINT }
19 Val intVal { INT }
20 Val floatVal { FLOAT }
21 Val id { ID }
22 Op = { ASSIGN }
23 Op opAss { OP_ASSIGN }

To improve its readability:

Prg  β†’ DSs $

DSs  β†’ Dcl DSs | Stm DSs | Ξ΅

Dcl  β†’ Ty id DclP

DclP β†’ ; | = Exp ;

Stm  β†’ id opAss Exp ; | print id ;

Exp  β†’ Tr ExpP

ExpP β†’ + Tr ExpP | - Tr ExpP | Ξ΅

Tr   β†’ Val TrP

TrP  β†’ * Val TrP | / Val TrP | Ξ΅

Ty   β†’ float | int

Val  β†’ intVal | floatVal | id

Op   β†’ = | opAss

πŸ§‘β€πŸ’» Tech Stack

  • Language: Java 17
  • Build Tool: Maven
  • Testing: JUnit 5
  • IDE Recommended: IntelliJ IDEA / VS Code with Java plugins

πŸ“ Project Structure

minilang-compiler/
β”‚
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ ast/              # AST node definitions
β”‚   β”œβ”€β”€ eception/         # Custom exception
β”‚   β”œβ”€β”€ parser/           # Parser and grammar implementation
β”‚   β”œβ”€β”€ scanner/          # Tokenizer and token types
β”‚   β”œβ”€β”€ symbolTable/      # Type checking, symbol table
β”‚   β”œβ”€β”€ test/             # Unit tests
β”‚   β”œβ”€β”€ token/            # Token structure and type enums
β”‚   β”œβ”€β”€ visitor/          # Visitor pattern for AST traversal and interpretation
β”‚   └── Main              # Main where you can test the program
β”‚
└── README.md               

βš™οΈ Getting Started

Follow these steps to run the project locally.

πŸ“₯ 1. Clone the repository

https://github.com/BeastOfShadow/FLT.git
cd FLT

πŸ”§ 2. Build the project

mvn clean install

πŸš€ 3. Run the compiler

Run Main.java to test our program!


πŸ§ͺ Example Input AC

x = 5;
x += 2;
print x;

Will produce a valid AST and simulate print output: 7.


πŸ§ͺ Example Input DC

5 1 2 + 4 * + 3 - p

Interpreted as:

  • Push 5
  • Push 1, 2 β†’ 1 2 + β†’ 3
  • 3 4 * β†’ 12
  • 5 12 + β†’ 17
  • 17 3 - β†’ 14
  • p β†’ prints 14

🧠 Developer Notes

  • The parser uses a recursive descent strategy.
  • Statements are matched using a predictive lookahead with peekToken().
  • AST nodes like NodeAssign, NodePrint, and NodeBinOp are used to represent program structure.
  • Compound operators like += are transformed internally to binary operations within assignments.

πŸ™ Credits

This compiler was developed as an academic project by:


πŸ“„ License

This project is licensed under the Apache License 2.0.

About

Mini compiler for AC and DC

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages