Skip to content

Commit c487f88

Browse files
committed
First commit
0 parents  commit c487f88

25 files changed

+3252
-0
lines changed

CMPS 104A.zip

131 KB
Binary file not shown.

Makefile

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
COMPILECPP = g++ -std=gnu++17 -g -O0 -Wall -Wextra
2+
FLEX = flex --outfile=${CLGEN}
3+
4+
BISON = bison --defines=${HYGEN} \
5+
--output=${CYGEN}
6+
7+
EXECBIN = oc
8+
SOURCES = astree.cpp lyutils.cpp \
9+
string_set.cpp auxlib.cpp \
10+
symtable.cpp attr_bitset.cpp \
11+
oiler.cpp main.cpp
12+
ALLSOURCES = ${SOURCES} ${CLGEN} ${CYGEN}
13+
HEADERS = astree.h lyutils.h string_set.h auxlib.h \
14+
symtable.h attr_bitset.h oiler.h
15+
LSOURCES = scanner.l
16+
CLGEN = yylex.cpp
17+
YSOURCES = parser.y
18+
HYGEN = yyparse.h
19+
CYGEN = yyparse.cpp
20+
21+
OBJECTS = ${ALLSOURCES:.cpp=.o}
22+
MAKEFILE = Makefile
23+
24+
all : ${EXECBIN}
25+
26+
27+
${EXECBIN} : ${CYGEN} ${HYGEN} ${OBJECTS}
28+
${COMPILECPP} -o ${EXECBIN} ${OBJECTS}
29+
30+
%.o : %.cpp
31+
#checksource ${SOURCES}
32+
${COMPILECPP} -c $<
33+
34+
yylex.o : yylex.cpp
35+
g++ -std=gnu++14 -g -O0 -c $<
36+
37+
${CLGEN} : ${LSOURCES}
38+
${FLEX} ${LSOURCES}
39+
40+
${CYGEN} ${HYGEN} : ${YSOURCES}
41+
${BISON} ${YSOURCES}
42+
43+
clean :
44+
rm -f ${CLGEN} ${CYGEN} ${HYGEN} ${DEPFILE} \
45+
*.output *.o *.out *.err *.str *.tok *.ast *.sym \
46+
*.oil
47+
48+
spotless :
49+
${MAKE} clean; rm -f ${EXECBIN}
50+
51+
ci :
52+

README

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
This program is a quarter long project in which my partner and i wrote a 3 code language called "oc"
2+
through a combination of C++, Flex, and Bison. The included files were written using pair programming
3+
containing code from the both of us. The Makefile was not written by us and was provided by the professor
4+
the code written by us are contained in the following files
5+
6+
astree.h
7+
astree.c
8+
parser.y
9+
scanner.l
10+
string_set.h
11+
string_set.c
12+
symtable.h
13+
symtable.c
14+
oiler.h
15+
oiler.c
16+
17+
all other files were provided by the professor.
18+
included in the zip is a very very brief writeup of the project
19+
i submitted for my senior portfolio
20+
21+
22+
23+
24+
25+
26+
27+
28+
This program was completed using pair programming.
29+
Partner: Nelson Yeap ([email protected])
30+
Partner: Chenxing Ji ([email protected])
31+
32+
We acknowledge that each partner in a programming pair should
33+
drive roughly 50% of the time the pair is working together, and
34+
at most 25% of an individual's effort for an assignment should
35+
be spent working alone. Any work done by a solitary programmer
36+
must be reviewed by the partner. The object is to work
37+
together, learning from each other, not to divide the work into
38+
two pieces with each partner working on a different piece.
39+
40+
Nelson Yeap spent 0 hours working alone.
41+
Chenxing Ji spent 0 hours working alone.
42+
We spent 50 hours working together.
43+
Nelson Yeap spent 25 hours driving.
44+
Chenxing Ji spent 25 hours driving.
45+
46+
Please grade the work submitted by [email protected]
47+
and not the work submitted by [email protected].
48+

asg1-stringset.pdf

79.1 KB
Binary file not shown.

astree.cpp

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
// $Id: astree.cpp,v 1.9 2017-10-04 15:59:50-07 - - $
2+
// Chenxing Ji
3+
// cji13
4+
// Nelson Yeap
5+
// nyeap
6+
7+
#include <assert.h>
8+
#include <inttypes.h>
9+
#include <stdarg.h>
10+
#include <stdio.h>
11+
#include <stdlib.h>
12+
#include <string.h>
13+
//#include "attr_bitset.h"
14+
#include "astree.h"
15+
#include "string_set.h"
16+
#include "lyutils.h"
17+
#include "symtable.h"
18+
19+
astree::astree (int symbol_, const location& lloc_, const char* info) {
20+
symbol = symbol_;
21+
lloc = lloc_;
22+
lexinfo = string_set::intern (info);
23+
// vector defaults to empty -- no children
24+
}
25+
26+
astree::~astree() {
27+
while (not children.empty()) {
28+
astree* child = children.back();
29+
children.pop_back();
30+
delete child;
31+
}
32+
if (yydebug) {
33+
fprintf (stderr, "Deleting astree (");
34+
astree::dump (stderr, this);
35+
fprintf (stderr, ")\n");
36+
}
37+
}
38+
39+
astree* astree::adopt (astree* child1, astree* child2) {
40+
if (child1 != nullptr) children.push_back (child1);
41+
if (child2 != nullptr) children.push_back (child2);
42+
return this;
43+
}
44+
45+
astree* astree::adopt_sym (astree* child, int symbol_) {
46+
symbol = symbol_;
47+
return adopt (child);
48+
}
49+
50+
void astree::dump_node (FILE* outfile) {
51+
char *tok_name = strdup(parser::get_tname(symbol));
52+
if (strstr(tok_name, "TOK_") == tok_name)
53+
tok_name += 4;
54+
//printf("%s\n", to_string_array(attribute).c_str());
55+
//fprintf(outfile,"{%zu}", block_count);
56+
fprintf(outfile, "%s \"%s\" (%zu.%zu.%zu) {%zu} ",
57+
tok_name, lexinfo->c_str(),lloc.filenr, lloc.linenr,
58+
lloc.offset, block_num);
59+
if(attribute[static_cast<unsigned>(attr::STRUCT)]){
60+
//fprintf(outfile,"struct ");
61+
fprintf(outfile, "struct \"%s\" ", struct_name->c_str());
62+
}
63+
fprintf(outfile, "%s", to_string_array(attribute).c_str());
64+
//fprintf(
65+
if(decl_lloc.linenr == 0){
66+
fprintf(outfile,"\n");
67+
}else{
68+
fprintf(outfile, " (%zd.%zd.%zd)\n", decl_lloc.filenr,
69+
decl_lloc.linenr, decl_lloc.offset);
70+
}
71+
}
72+
73+
void astree::dump_tree (FILE* outfile, int depth) {
74+
for(int i = 0; i < depth; i++){
75+
if(depth == 0)
76+
break;
77+
else{
78+
fprintf(outfile, "%s", "| ");
79+
}
80+
}
81+
dump_node (outfile);
82+
//fprintf (outfile, "\n");
83+
for (astree* child: children) child->dump_tree (outfile, depth + 1);
84+
fflush (nullptr);
85+
}
86+
87+
void astree::dump (FILE* outfile, astree* tree) {
88+
if (tree == nullptr) fprintf (outfile, "nullptr");
89+
else tree->dump_tree (outfile, 0);
90+
}
91+
92+
93+
void astree::print (FILE* outfile, astree* tree, int depth) {
94+
fprintf (outfile, "; %*s", depth * 3, "");
95+
fprintf (outfile, "%s \"%s\" (%zd.%zd.%zd) {%zu} %s",
96+
parser::get_tname (tree->symbol), tree->lexinfo->c_str(),
97+
tree->lloc.filenr, tree->lloc.linenr, tree->lloc.offset,
98+
tree->block_num, to_string_array(tree->attribute).c_str());
99+
if(tree->decl_lloc.linenr == 0){
100+
//printf("Not a declared ident yet\n");
101+
fprintf(outfile, "\n");
102+
}else{
103+
//printf("Existed symbol\n");
104+
fprintf(outfile, " (%zd.%zd.%zd)\n", tree->decl_lloc.filenr, \
105+
tree->decl_lloc.linenr, tree->decl_lloc.offset);
106+
}
107+
for (astree* child: tree->children) {
108+
astree::print (outfile, child, depth + 1);
109+
}
110+
}
111+
112+
void destroy (astree* tree1, astree* tree2) {
113+
if (tree1 != nullptr) {
114+
while(tree1->children.size() != 0){
115+
tree1->children.pop_back();
116+
destroy(tree1);
117+
}
118+
delete tree1;
119+
}
120+
if (tree2 != nullptr) {
121+
while(tree2->children.size() != 0){
122+
tree2->children.pop_back();
123+
destroy(tree2);
124+
}
125+
delete tree2;
126+
}
127+
}
128+
129+
void errllocprintf (const location& lloc, const char* format,
130+
const char* arg) {
131+
static char buffer[0x1000];
132+
assert (sizeof buffer > strlen (format) + strlen (arg));
133+
snprintf (buffer, sizeof buffer, format, arg);
134+
errprintf ("%s:%zd.%zd: %s",
135+
lexer::filename (lloc.filenr), lloc.linenr, lloc.offset,
136+
buffer);
137+
}
138+
139+

astree.h

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// $Id: astree.h,v 1.7 2016-10-06 16:13:39-07 - - $
2+
// Chenxing Ji
3+
// cji13
4+
// Nelson Yeap
5+
// nyeap
6+
7+
#ifndef __ASTREE_H__
8+
#define __ASTREE_H__
9+
10+
#include <string>
11+
#include <vector>
12+
//#include <bitset>
13+
//#include "auxlib.h"
14+
//#include "symtable.h"
15+
#include <unordered_map>
16+
#include "attr_bitset.h"
17+
using namespace std;
18+
19+
struct location {
20+
size_t filenr;
21+
size_t linenr;
22+
size_t offset;
23+
};
24+
25+
struct astree {
26+
27+
// Fields.
28+
int symbol; // token code
29+
location lloc; // source location
30+
location decl_lloc; // symbol declared location
31+
const string* lexinfo; // pointer to lexical information
32+
vector<astree*> children; // children of this n-way node
33+
attr_bitset attribute;
34+
size_t block_num;
35+
size_t block_count;
36+
const string* struct_name; //parent struct name
37+
//symbol_table* struct_table;
38+
39+
// Functions.
40+
astree (int symbol, const location&, const char* lexinfo);
41+
~astree();
42+
astree* adopt (astree* child1, astree* child2 = nullptr);
43+
astree* adopt_sym (astree* child, int symbol);
44+
void dump_node (FILE*);
45+
void dump_tree (FILE*, int depth = 0);
46+
static void dump (FILE* outfile, astree* tree);
47+
static void print (FILE* outfile, astree* tree, int depth = 0);
48+
};
49+
50+
void destroy (astree* tree1, astree* tree2 = nullptr);
51+
52+
void errllocprintf (const location&, const char* format, const char*);
53+
54+
#endif
55+

attr_bitset.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <unordered_map>
2+
#include <bitset>
3+
#include <string>
4+
#include <iostream>
5+
#include "attr_bitset.h"
6+
using namespace std;
7+
const string attr_to_string (int i) {
8+
9+
static const unordered_map<int,string> hash {
10+
{0 , "void " },
11+
{1 , "int " },
12+
{2 , "null " },
13+
{3 , "string " },
14+
{4 , "" },
15+
{5 , "array " },
16+
{6 , "function " },
17+
{7 , "variable " },
18+
{8 , "field " },
19+
{9 , "typeid " },
20+
{10 , "lval " },
21+
{11 , "local " },
22+
{12 , "param " },
23+
{13 , "const " },
24+
{14 , "vreg " },
25+
{15 , "vaddr " },
26+
{16 , "char " },
27+
{17 , "bitset_size "}
28+
};
29+
auto str = hash.find (i);
30+
if (str == hash.end()) throw invalid_argument (__PRETTY_FUNCTION__);
31+
return str->second;
32+
//return store;
33+
}
34+
const string to_string_array(attr_bitset attributes){
35+
string store;
36+
//string temp = attributes.to_string();
37+
for (size_t i = 0; i < attributes.size(); ++i)
38+
{
39+
if(attributes[i]){
40+
store.append(attr_to_string(i));
41+
}
42+
}
43+
return store;
44+
}

attr_bitset.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#ifndef __ATTR_BITSET_H__
2+
#define __ATTR_BITSET_H__
3+
4+
#include <string>
5+
#include <vector>
6+
#include <bitset>
7+
#include <string>
8+
#include <iostream>
9+
#include <unordered_map>
10+
using namespace std;
11+
12+
enum class attr{
13+
VOID, INT, NULLX, STRING, STRUCT, ARRAY, FUNCTION, VARIABLE,
14+
FIELD, TYPEID, LVAL, LOCAL, PARAM, CONST, VREG, VADDR, CHAR,
15+
BITSET_SIZE,
16+
};
17+
18+
using attr_bitset = bitset<unsigned(attr::BITSET_SIZE)>;
19+
const string attr_to_string (int i);
20+
const string to_string_array(attr_bitset attributes);
21+
#endif

0 commit comments

Comments
 (0)