struct inheritance: full listing

N

Neill

For a course on "Compiler Design" we were giving the listing from below, I
have to write:
1) constuctors & destructors (easy!)
2) a function int Stm::maxargs() that tells the maximum number of arguments
of any print statement within any subexpression of a given statement. For
example, maxargs(prog) is 2. Is that even possible???

-----------------------------
#include <iostream>
#include <string>
using namespace std;

struct Stm_ {};
struct Exp_ {};
struct ExpList_ {};

typedef Stm_* Stm;
typedef Exp_* Exp;
typedef ExpList_* ExpList;

struct AssignStm : public Stm_
{
string id;
Exp exp;
AssignStm(string i, Exp e);
};

struct PrintStm : public Stm_
{
ExpList exps;
PrintStm(ExpList es);
};

struct CompoundStm : public Stm_
{
Stm stm1, stm2;
CompoundStm(Stm s1, Stm s2);
};

struct IdExp : public Exp_
{
string id;
IdExp(string i);
};

struct NumExp : public Exp_
{
int num;
NumExp(int n);
};

typedef enum {Plus, Minus, Times, Div} Binop;

struct OpExp : public Exp_
{
Exp left, right;
Binop oper;
OpExp(Exp l, Binop op, Exp r);
};

struct EseqExp : public Exp_
{
Stm stm;
Exp exp;
EseqExp(Stm s, Exp e);
};

struct PairExpList : public ExpList_
{
Exp head;
ExpList tail;
PairExpList(Exp h, ExpList t);
};

struct LastExpList : public ExpList_
{
Exp last;
LastExpList(Exp l);
};

int main()
{
Stm prog =
new CompoundStm(
new AssignStm(
"a",
new OpExp(new NumExp(5), Plus, new NumExp(3))),
new CompoundStm(
new AssignStm(
"b",
new EseqExp(
new PrintStm(
new PairExpList(
new IdExp("a"),
new LastExpList(
new OpExp(new IdExp("a"), Minus, new NumExp(1))))),
new OpExp(new NumExp(10), Times, new IdExp("a")))),
new PrintStm(new LastExpList(new IdExp("b")))));

return 0;
}
 
N

Neill

Neill said:
For a course on "Compiler Design" we were giving the listing from below, I
have to write:
1) constuctors & destructors (easy!)
2) a function int Stm::maxargs() that tells the maximum number of
arguments of any print statement within any subexpression of a given
statement. For example, maxargs(prog) is 2. Is that even possible???

CORRECTION: prog->maxargs() is 2.
 
I

Ian Collins

Neill said:
For a course on "Compiler Design" we were giving the listing from below, I
have to write:
1) constuctors & destructors (easy!)
2) a function int Stm::maxargs() that tells the maximum number of arguments
of any print statement within any subexpression of a given statement. For
example, maxargs(prog) is 2. Is that even possible???

-----------------------------
#include <iostream>
#include <string>
using namespace std;

struct Stm_ {};
struct Exp_ {};
struct ExpList_ {};

typedef Stm_* Stm;
typedef Exp_* Exp;
typedef ExpList_* ExpList;

Why have you kept those awful typedefs?
 
N

Neill

Ian Collins said:
Why have you kept those awful typedefs?

I know they are ugly, but they stressed on saying *not* to remove anything
from the giving listing :-( So I'm stuck, I only can write a function int
Stm_::maxargs() and even then I cannot decide which inherited struct did
call it using "typeid"...
 
K

Kai-Uwe Bux

Neill said:
For a course on "Compiler Design" we were giving the listing from below, I
have to write:
1) constuctors & destructors (easy!)
2) a function int Stm::maxargs() that tells the maximum number of
arguments of any print statement within any subexpression of a given
statement. For example, maxargs(prog) is 2. Is that even possible???

Yes. Add virtual functions.
-----------------------------
#include <iostream>
#include <string>
using namespace std;

struct Stm_ {

virtual int maxargs ( void ) = 0;
};
struct Exp_ {

virtual int maxargs ( void ) = 0;
};
struct ExpList_ {

virtual int maxargs ( void ) = 0;
virtual int size ( void ) = 0;


In each of the derived classes, provide the right implementation. Since the
number of arguments to a PrintStm could depend on the length of the list of
arguments, you will also need the size() member for ExpList_.

typedef Stm_* Stm;
typedef Exp_* Exp;
typedef ExpList_* ExpList;

struct AssignStm : public Stm_
{
string id;
Exp exp;
AssignStm(string i, Exp e);
};

struct PrintStm : public Stm_
{
ExpList exps;
PrintStm(ExpList es);
};

struct CompoundStm : public Stm_
{
Stm stm1, stm2;
CompoundStm(Stm s1, Stm s2);
};

struct IdExp : public Exp_
{
string id;
IdExp(string i);
};

struct NumExp : public Exp_
{
int num;
NumExp(int n);
};

typedef enum {Plus, Minus, Times, Div} Binop;

struct OpExp : public Exp_
{
Exp left, right;
Binop oper;
OpExp(Exp l, Binop op, Exp r);
};

struct EseqExp : public Exp_
{
Stm stm;
Exp exp;
EseqExp(Stm s, Exp e);
};

struct PairExpList : public ExpList_
{
Exp head;
ExpList tail;
PairExpList(Exp h, ExpList t);
};

struct LastExpList : public ExpList_
{
Exp last;
LastExpList(Exp l);
};

int main()
{
Stm prog =
new CompoundStm(
new AssignStm(
"a",
new OpExp(new NumExp(5), Plus, new NumExp(3))),
new CompoundStm(
new AssignStm(
"b",
new EseqExp(
new PrintStm(
new PairExpList(
new IdExp("a"),
new LastExpList(
new OpExp(new IdExp("a"), Minus, new NumExp(1))))),
new OpExp(new NumExp(10), Times, new IdExp("a")))),
new PrintStm(new LastExpList(new IdExp("b")))));

return 0;
}


Best

Kai-Uwe Bux
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,266
Messages
2,571,078
Members
48,772
Latest member
Backspace Studios

Latest Threads

Top