Skip to content

Commit 110491c

Browse files
initial commit
0 parents  commit 110491c

26 files changed

+1374
-0
lines changed

.gitignore

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Prerequisites
2+
*.d
3+
4+
# Compiled Object files
5+
*.slo
6+
*.lo
7+
*.o
8+
*.obj
9+
10+
# Precompiled Headers
11+
*.gch
12+
*.pch
13+
14+
# Compiled Dynamic libraries
15+
*.so
16+
*.dylib
17+
*.dll
18+
19+
# Fortran module files
20+
*.mod
21+
*.smod
22+
23+
# Compiled Static libraries
24+
*.lai
25+
*.la
26+
*.a
27+
*.lib
28+
29+
# Executables
30+
*.exe
31+
*.out
32+
*.app
33+
34+
*.pdf

compiler/CodeGenVisitor.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include "CodeGenVisitor.h"
2+
3+
antlrcpp::Any CodeGenVisitor::visitProg(ifccParser::ProgContext *ctx)
4+
{
5+
std::cout<< ".globl main\n" ;
6+
std::cout<< " main: \n" ;
7+
8+
this->visit( ctx->return_stmt() );
9+
10+
std::cout << " ret\n";
11+
12+
return 0;
13+
}
14+
15+
16+
antlrcpp::Any CodeGenVisitor::visitReturn_stmt(ifccParser::Return_stmtContext *ctx)
17+
{
18+
int retval = stoi(ctx->CONST()->getText());
19+
20+
std::cout << " movl $"<<retval<<", %eax\n" ;
21+
22+
return 0;
23+
}

compiler/CodeGenVisitor.h

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#pragma once
2+
3+
4+
#include "antlr4-runtime.h"
5+
#include "generated/ifccBaseVisitor.h"
6+
7+
8+
class CodeGenVisitor : public ifccBaseVisitor {
9+
public:
10+
virtual antlrcpp::Any visitProg(ifccParser::ProgContext *ctx) override ;
11+
virtual antlrcpp::Any visitReturn_stmt(ifccParser::Return_stmtContext *ctx) override;
12+
};
13+

compiler/Makefile

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# config.mk contains the paths to antlr4 etc.
2+
# Each student should have a config.mk corresponding to her system.
3+
# Examples are ubuntu.mk, DI.mk, fedora.mk
4+
# Then config.mk should be in the .gitignore of your project
5+
include config.mk
6+
7+
CC=g++
8+
CCFLAGS=-g -c -std=c++17 -I$(ANTLRINC) -Wno-attributes # -Wno-defaulted-function-deleted -Wno-unknown-warning-option
9+
LDFLAGS=-g
10+
11+
default: all
12+
all: ifcc
13+
14+
##########################################
15+
# link together all pieces of our compiler
16+
OBJECTS=build/ifccBaseVisitor.o \
17+
build/ifccLexer.o \
18+
build/ifccVisitor.o \
19+
build/ifccParser.o \
20+
build/main.o \
21+
build/CodeGenVisitor.o
22+
23+
ifcc: $(OBJECTS)
24+
@mkdir -p build
25+
$(CC) $(LDFLAGS) build/*.o $(ANTLRLIB) -o ifcc
26+
27+
##########################################
28+
# compile our hand-writen C++ code: main(), CodeGenVisitor, etc.
29+
build/%.o: %.cpp generated/ifccParser.cpp
30+
@mkdir -p build
31+
$(CC) $(CCFLAGS) -MMD -o $@ $<
32+
33+
##########################################
34+
# compile all the antlr-generated C++
35+
build/%.o: generated/%.cpp
36+
@mkdir -p build
37+
$(CC) $(CCFLAGS) -MMD -o $@ $<
38+
39+
# automagic dependency management: `gcc -MMD` generates all the .d files for us
40+
-include build/*.d
41+
build/%.d:
42+
43+
##########################################
44+
# generate the C++ implementation of our Lexer/Parser/Visitor
45+
generated/ifccLexer.cpp: generated/ifccParser.cpp
46+
generated/ifccVisitor.cpp: generated/ifccParser.cpp
47+
generated/ifccBaseVisitor.cpp: generated/ifccParser.cpp
48+
generated/ifccParser.cpp: ifcc.g4
49+
@mkdir -p generated
50+
$(ANTLR) -visitor -no-listener -Dlanguage=Cpp -o generated ifcc.g4
51+
52+
# prevent automatic cleanup of "intermediate" files like ifccLexer.cpp etc
53+
.PRECIOUS: generated/ifcc%.cpp
54+
55+
##########################################
56+
# view the parse tree in a graphical window
57+
58+
# Usage: `make gui FILE=path/to/your/file.c`
59+
FILE ?= ../tests/testfiles/1_return42.c
60+
61+
gui:
62+
@mkdir -p generated build
63+
$(ANTLR) -Dlanguage=Java -o generated ifcc.g4
64+
javac -cp $(ANTLRJAR) -d build generated/*.java
65+
java -cp $(ANTLRJAR):build org.antlr.v4.gui.TestRig ifcc axiom -gui $(FILE)
66+
67+
##########################################
68+
# delete all machine-generated files
69+
clean:
70+
rm -rf build generated
71+
rm -f ifcc

compiler/README.md

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# C compiler with antlr4/c++
2+
3+
## Instructions
4+
5+
This minimal example shows how to build a basic C compiler using
6+
Antlr4 for C++. The only file the compiler can deal with is:
7+
8+
```
9+
int main() {
10+
return N;
11+
}
12+
```
13+
where `N` is a positive integer constant.
14+
15+
## Source Files
16+
- `ifcc.g4` contains the grammar in antlr4 format
17+
- `main.cpp` contains the C++ code to call the antlr4-generated parser
18+
on the file name provided in the command line.
19+
- `CodeGenVisitor.h` produces the actual assembly-language output
20+
21+
## Prerequisites
22+
Before building your compiler, you should install antlr using your
23+
distribution's package manager, or by running the provided shell
24+
script: `install-antlr.sh`.
25+
26+
## Compilation scripts
27+
- `Makefile` contains the actual build logic. Please read this file
28+
and ask questions on parts you do not understand.
29+
The Makefile includes a .mk file that defines several variables
30+
(ANTLR, ANTLRJAR, ANTLRINC and ANTLRLIB) indicating the location
31+
of various parts of the Antlr suite.
32+
You can (should) change those values to suit your installation, either
33+
by editing the Makefile and/or adding another .mk file.
34+

compiler/config.mk

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# the ANTLRJAR part below is copied from /usr/bin/antlr4
2+
ANTLRJAR=/usr/share/java/stringtemplate4.jar:/usr/share/java/antlr4.jar:/usr/share/java/antlr4-runtime.jar:/usr/share/java/antlr3-runtime.jar/:/usr/share/java/treelayout.jar
3+
ANTLRINC=/usr/include/antlr4-runtime
4+
ANTLRLIB=/usr/lib/x86_64-linux-gnu/libantlr4-runtime.a
5+
ANTLR=antlr4

compiler/generated/ifcc.interp

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
token literal names:
2+
null
3+
'int'
4+
'main'
5+
'('
6+
')'
7+
'{'
8+
'}'
9+
';'
10+
'return'
11+
null
12+
null
13+
null
14+
null
15+
16+
token symbolic names:
17+
null
18+
null
19+
null
20+
null
21+
null
22+
null
23+
null
24+
null
25+
RETURN
26+
CONST
27+
COMMENT
28+
DIRECTIVE
29+
WS
30+
31+
rule names:
32+
axiom
33+
prog
34+
return_stmt
35+
36+
37+
atn:
38+
[3, 24715, 42794, 33075, 47597, 16764, 15335, 30598, 22884, 3, 14, 24, 4, 2, 9, 2, 4, 3, 9, 3, 4, 4, 9, 4, 3, 2, 3, 2, 3, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 2, 2, 5, 2, 4, 6, 2, 2, 2, 20, 2, 8, 3, 2, 2, 2, 4, 11, 3, 2, 2, 2, 6, 19, 3, 2, 2, 2, 8, 9, 5, 4, 3, 2, 9, 10, 7, 2, 2, 3, 10, 3, 3, 2, 2, 2, 11, 12, 7, 3, 2, 2, 12, 13, 7, 4, 2, 2, 13, 14, 7, 5, 2, 2, 14, 15, 7, 6, 2, 2, 15, 16, 7, 7, 2, 2, 16, 17, 5, 6, 4, 2, 17, 18, 7, 8, 2, 2, 18, 5, 3, 2, 2, 2, 19, 20, 7, 10, 2, 2, 20, 21, 7, 11, 2, 2, 21, 22, 7, 9, 2, 2, 22, 7, 3, 2, 2, 2, 2]

compiler/generated/ifcc.tokens

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
T__0=1
2+
T__1=2
3+
T__2=3
4+
T__3=4
5+
T__4=5
6+
T__5=6
7+
T__6=7
8+
RETURN=8
9+
CONST=9
10+
COMMENT=10
11+
DIRECTIVE=11
12+
WS=12
13+
'int'=1
14+
'main'=2
15+
'('=3
16+
')'=4
17+
'{'=5
18+
'}'=6
19+
';'=7
20+
'return'=8
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
// Generated from ifcc.g4 by ANTLR 4.7.2
3+
4+
5+
#include "ifccBaseVisitor.h"
6+
7+

compiler/generated/ifccBaseVisitor.h

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
// Generated from ifcc.g4 by ANTLR 4.7.2
3+
4+
#pragma once
5+
6+
7+
#include "antlr4-runtime.h"
8+
#include "ifccVisitor.h"
9+
10+
11+
/**
12+
* This class provides an empty implementation of ifccVisitor, which can be
13+
* extended to create a visitor which only needs to handle a subset of the available methods.
14+
*/
15+
class ifccBaseVisitor : public ifccVisitor {
16+
public:
17+
18+
virtual antlrcpp::Any visitAxiom(ifccParser::AxiomContext *ctx) override {
19+
return visitChildren(ctx);
20+
}
21+
22+
virtual antlrcpp::Any visitProg(ifccParser::ProgContext *ctx) override {
23+
return visitChildren(ctx);
24+
}
25+
26+
virtual antlrcpp::Any visitReturn_stmt(ifccParser::Return_stmtContext *ctx) override {
27+
return visitChildren(ctx);
28+
}
29+
30+
31+
};
32+

0 commit comments

Comments
 (0)