translated right?

closed account (Dy7SLyTq)
so im learning the basics of compiler design from this site: http://compilers.iecc.com/crenshaw/tutor1.txt
and it gave me this code (which can be found at the bottom) (i dont know what language it is. it says 68000 Assembly but it didnt look like any flavor of assembly ive ever seen or the 68000 examples ive googled) and since i dont know how to write in it i decided to use c++. so anyways, this is my first attempt. did i translate it correctly? (note im not asking for advice on how to do it better. that will come later with the finished product)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <iostream>
#include <string>
#include <cstdlib>
#include <cctypes>

using std::cout;
using std::cerr;
uisng std::endl;
using std::string;

const char TAB = '\t';
char Look = '';

void Init();
void GetChar();
void Error(string&);
void Abort(string&)
void Expected(string&);
void Match(char);
char GetName();
char GetNum();
void Emit(string&);
void EmitLn(string&);

int main(int argc, char *argv[])
{
	Init();
}

void Init()
{
	GetChar();
}

void GetChar();
{
	Look = getchar();
}

void Error(string &ErrMsg)
{
	cerr<<"Error: "<< ErrMsg;
}

void Abort(string &ErrMsg);
{
	Error(ErrMsg);
	exit(0);
}

void Expected(string &ErrMsg)
{
	Abort(ErrMsg + "Expected");
}

void Match(char Current)
{
	if(Look == Current)
		GetChar();

	else
		Expected("\"" + Current + "\"");
}

char GetName()
{
	if(!isalpha(Look))
		Expected("Name");

	GetChar();
	return toupper(Look);
}

char GetNum()
{
	if(!isdigit(Look))
		Expected("Integer");

	GetChar();
	return Look;
}

void Emit(string &Line)
{
	cout<< TAB << Line;
}

void EmitLn(string &Line)
{
	Emit(Line);
	cout<< endl;
}


edit: the i couldnt figure out what the := operator did so i assumed it meant return, because the only other thing i could think of was call itself, but that would become infinite recursion

edit 2: the second page said it was "turbo pascal" which im still not familiar with, but just fyi
Last edited on
It's Turbo Pascal, not 68000. http://en.wikipedia.org/wiki/Turbo_Pascal

I think what he's saying is that you'll eventually convert this into 68k. You'll probably do this later in the tutorials. I'm sure there'll be a code generator section.
Last edited on
closed account (Dy7SLyTq)
yeah i misunderstood. i looked up a turbo pascal tut afterwords and figured it all out. := isnt return (didnt make much sense but it was the only thing i could think of). its like gos assignment operator. it infers type based on value
Ah, I see.

This takes me back. :-)

I had a compiler writing module at university. My first fling with both compilers and command line Unix. Good times.
closed account (Dy7SLyTq)
could you help me with an error i got?

$ g++ test.cpp -o test
test.cpp: In function ‘void Match(char)’:
test.cpp:65:40: error: invalid conversion from ‘int’ to ‘const char*’ [-fpermissive]
In file included from /usr/lib/gcc/i686-pc-cygwin/4.7.3/include/c++/string:55:0,
from /usr/lib/gcc/i686-pc-cygwin/4.7.3/include/c++/bits/locale_classes.h:42,
from /usr/lib/gcc/i686-pc-cygwin/4.7.3/include/c++/bits/ios_base.h:43,
from /usr/lib/gcc/i686-pc-cygwin/4.7.3/include/c++/ios:43,
from /usr/lib/gcc/i686-pc-cygwin/4.7.3/include/c++/ostream:40,
from /usr/lib/gcc/i686-pc-cygwin/4.7.3/include/c++/iostream:40,
from test.cpp:1:
/usr/lib/gcc/i686-pc-cygwin/4.7.3/include/c++/bits/basic_string.tcc:214:5: error: initializing argument 1 of ‘std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’ [-fpermissive]
test.cpp: In function ‘void Expression()’:
test.cpp:99:31: error: invalid operands of types ‘const char*’ and ‘const char [4]’ to binary ‘operator+’


the functions in question are:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
void Match(char Current)
{
	if(Look == Current)
		GetChar();

	else
		Expected(string('\"' + Current + '\"'));
}


void Expected(string ErrMsg)
{
	Abort(ErrMsg + "Expected");
}

void EmitLn(string &Line)
{
	Emit(Line);
	cout<< endl;
}

void Expression()
{
	EmitLn("MOVE #" + string(GetNum()) + ",DO");
}
Sure.

You're trying to pass a reference of a literal.
closed account (Dy7SLyTq)
what about for match?
The std::string constructor wants a const char*, and you're trying to pass the sum of two ints and one char.
Last edited on
closed account (Dy7SLyTq)
the compiler says that, but how is '\"' an int?
closed account (Dy7SLyTq)
never mind guys. i had prototypes of the functions that still had type string&. i solved it
the compiler says that, but how is '\"' an int?


It isn't. but '\"' + '\"' is. Btw, you don't need the escaped character here: '"' works fine.

http://stackoverflow.com/questions/4814668/addition-of-two-chars-produces-int
closed account (Dy7SLyTq)
good to know. thanks. i fixed it though.
closed account (Dy7SLyTq)
one last question:
im getting strange output with my code. what am i doing wrong?

$ ./test
1 + 3
MOVE #1,Do
MOVE D0,D1

e //<-- that was because it asked for input again and i needed to break it
MOVE #3,Do
ADD D1,D0


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <iostream>
#include <string>
#include <cstdlib>
#include <cctype>

using std::cout;
using std::cerr;
using std::endl;
using std::cin;
using std::string;

const char TAB = '\t';
char Look = 0;

void Init();
void GetChar();
void Error(string&);
void Abort(string);
void Expected(string);
void Match(char);
char GetName();
char GetNum();
void Emit(string&);
void EmitLn(string);
void Term();
void Expression();
void Add();
void Subtract();

int main()
{
	Init();
	Expression();
}

void Init()
{
	GetChar();
}

void GetChar()
{
	cin>> Look;
}

void Error(string &ErrMsg)
{
	cerr<<"Error: "<< ErrMsg;
}

void Abort(string ErrMsg)
{
	Error(ErrMsg);
	exit(0);
}

void Expected(string ErrMsg)
{
	Abort(ErrMsg + "Expected");
}

void Match(char Current)
{
	if(Look == Current)
		GetChar();

	else
	{
		string ErrMsg = "\"";
		ErrMsg += Current;
		ErrMsg += "\"";
		Expected(ErrMsg);
	}
}

char GetName()
{
	if(!isalpha(Look))
		Expected("Name");

	return toupper(Look);
}

char GetNum()
{
	if(!isdigit(Look))
		Expected("Integer");

	char Num = Look;
	GetChar();
	return Num;
}

void Emit(string &Line)
{
	cout<< TAB << Line;
}

void EmitLn(string Line)
{
	Emit(Line);
	cout<< endl;
}

void Term()
{
	string Expr = "MOVE #";
	Expr += GetNum();
	Expr += ",Do";
	EmitLn(Expr);
}

void Expression()
{
	Term();
	EmitLn("MOVE D0,D1");
 
	switch(Look)
	{
		case '+':
			Add();
			break;

		case '-':
			Subtract();
			break;

		default:
			Expected("Addop");
	}
}

void Add()
{
	Match('+');
	Term();
	EmitLn("ADD D1,D0");
}

void Subtract()
{
	Match('-');
	Term();
	EmitLn("SUB D1,D0");
}
Which bit is strange?

The '1 + 3' and the 'e' look odd. The rest are valid 68k instructions (apart from having a Do instead of a D0).
closed account (Dy7SLyTq)
yes but i shouldnt have to write the e for input. and the 1+3 was my input to be translated to the assembly instructions
Oh right.

Then that's correct then.
closed account (Dy7SLyTq)
yeah but why is it asking for more input after the 1+3?
Sometimes the amount of posts you've got surprises me.
Do you ever try to debug an executable? o.O
closed account (Dy7SLyTq)
you shouldnt base anything off post count. i post a lot in lounge. im living proof that high post count has nothing to do with skill. however, i did try but i couldnt figure it out. i gave it up though, because i figured i could either continuing translating pascal code writing shell programs, or write my own full bodied compiler based off the stroustrop calculator, which gives me more freedom and flexibility.
Topic archived. No new replies allowed.