A compiler construction project that parses arithmetic expressions and displays them as ASCII-art syntax trees. Built with Lex (Flex) and Yacc (Bison), featuring expression evaluation and elegant tree visualization.
This project implements a complete arithmetic expression parser that builds Abstract Syntax Trees (AST) and displays them in a visually appealing ASCII-art format. It serves as an educational tool for understanding compiler construction principles, particularly lexical analysis, parsing, and syntax tree generation.
- Lexical Analysis - Token recognition using Flex
- Syntax Parsing - Grammar implementation with Bison
- AST Construction - Dynamic tree building during parsing
- ASCII-art Visualization - Beautiful tree display with connecting lines
- Expression Evaluation - Calculate results while traversing the tree
- Error Recovery - Graceful handling of syntax errors
- Extended Operators - Support for exponentiation and unary negation
-
scanner.l- Lexical analyzer (Flex)- Token recognition and classification
- Whitespace and newline handling
- Error reporting for invalid characters
-
parser.y- Syntax analyzer (Bison)- Grammar rules definition
- Operator precedence and associativity
- AST construction during parsing
-
tree.h/tree.c- AST implementation- Node structures for different expression types
- Tree manipulation functions
- Visualization and evaluation algorithms
| Operator | Description | Precedence | Associativity |
|---|---|---|---|
+ |
Addition | 1 | Left |
- |
Subtraction | 1 | Left |
* |
Multiplication | 2 | Left |
/ |
Division | 2 | Left |
^ |
Exponentiation | 3 | Right |
- |
Unary negation | 4 | Right |
() |
Parentheses | - | - |
- GCC compiler
- Flex >= 2.5
- Bison >= 3.0
- Make utility
- Linux/Unix/macOS environment
- Clone the repository:
git clone https://github.com/yourusername/pretty-printer-expressions.git
cd pretty-printer-expressions- Install dependencies (Ubuntu/Debian):
sudo apt-get update
sudo apt-get install build-essential flex bison- Build the project:
make# Method 1: Using make
make run
# Method 2: Direct execution
./pretty_printerโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Pretty Printer Wyraลผeล Arytmetycznych โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
> 2+3*4
===== ANALIZA WYRAลปENIA =====
๐ Drzewo skลadniowe:
โโโ [+]
โโโ 2
โโโ [*]
โโโ 3
โโโ 4
๐งฎ Obliczony wynik: 14
=============================
> (2+3)*4
๐ Drzewo skลadniowe:
โโโ [*]
โโโ [+]
โ โโโ 2
โ โโโ 3
โโโ 4
๐งฎ Obliczony wynik: 20
> 2^3^2
๐ Drzewo skลadniowe:
โโโ [^]
โโโ 2
โโโ [^]
โโโ 3
โโโ 2
๐งฎ Obliczony wynik: 512
> -5+10
๐ Drzewo skลadniowe:
โโโ [+]
โโโ [-]
โ โโโ 5
โโโ 10
๐งฎ Obliczony wynik: 5
expr:
NUMBER
| expr '+' expr
| expr '-' expr
| expr '*' expr
| expr '/' expr
| expr '^' expr
| '-' expr %prec UNARY
| '(' expr ')'
;typedef struct Node {
NodeType type;
union {
int number;
struct {
char op;
struct Node *left;
struct Node *right;
} op_details;
struct {
char op;
struct Node *operand;
} unary_details;
};
} Node;The tree is displayed using a recursive algorithm that:
- Prints the current node with appropriate prefix
- Uses "โโโ" for non-tail children and "โโโ" for tail children
- Adjusts the prefix for subtrees to create vertical lines
- Maintains proper alignment across all levels
Run the built-in test suite:
make testThis executes predefined test cases:
- Basic arithmetic:
2+3*4 - Parentheses:
(2+3)*4 - Exponentiation:
2^3+1 - Negation:
-5+10
- Time Complexity: O(n) for parsing, where n is the number of tokens
- Space Complexity: O(h) for the AST, where h is the height of the expression tree
- Memory Management: Automatic cleanup after each expression evaluation
The project includes a comprehensive Makefile with the following targets:
| Command | Description |
|---|---|
make |
Build the project |
make run |
Build and run |
make test |
Run test cases |
make clean |
Remove build artifacts |
make help |
Show available commands |
The parser includes robust error handling:
- Lexical Errors: Unknown characters are reported with line numbers
- Syntax Errors: Invalid expressions trigger error recovery
- Runtime Errors: Division by zero is detected during evaluation
- Memory Errors: Allocation failures are handled gracefully
This project demonstrates key compiler construction concepts:
- Lexical Analysis - Pattern matching and tokenization
- Syntax Analysis - Context-free grammar implementation
- Semantic Analysis - Type checking (implicit in evaluation)
- AST Generation - Tree data structure construction
- Tree Traversal - Recursive algorithms for display and evaluation
Student ID: s24825
Course: Compiler Construction / Formal Languages and Translation Techniques