473,406 Members | 2,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Implementing a restricted C langage (Lex-Yacc-C++)

Hello !

I have a special need implementing a lexical parser. I have a C
restricted grammar, in which I accept such operations as (3<3.5) or
(s<"string"). My problem is that I meet some conception problem. I
would like Yacc to simply operate $$=$1+$3 on my following Node
objects. I tryed to overload operators so that NodeInt n1 < NodeLong n2
automatically get the right values with getValue() function but C++
spec forbids template virtual methods.
Could you please advise a design or a track to unlock me please ?
Thanks in advance,

Steve

class Node {
protected:
int type;

public:
Node();
~Node();
virtual int comparison(Node* n2) = 0;
int operator < (Node& n2); // call comparison(n2)

template <typename T>
virtual T getValue();
};

class NodeString : public Node {
private:
char* s;

public:
NodeString(void* p);
~NodeString();
int comparison(Node* n2); /* may operate
* this->getValue() < n2.getValue()
* strcmp(this->getValue(), n2.getValue())
* strcmp(this->getValue(),
* itoa(n2.getValue()))
*/
char* getValue();
};
class NodeLong : public Node {
private:
long l;

public:
NodeLong(void* p);
~NodeLong();
int comparison(Node* n2);
double getValue();
};

Oct 18 '06 #1
1 1870
On 18 Oct 2006 06:29:01 -0700 in comp.lang.c++, "steve"
<mo**********@gmail.comwrote,
virtual int comparison(Node* n2) = 0;
int operator < (Node& n2); // call comparison(n2)

template <typename T>
virtual T getValue();
Even if the template were accepted, it would not solve your problem.
The template would just give you the equivalent of writing two
member functions,
virtual int getValue();
virtual string getValue();
but that won't go because you cannot overload on return type. The
same issue viewed from the other side is that when you get down to
///
int comparison(Node* n2); /* may operate
* this->getValue() < n2.getValue()
* strcmp(this->getValue(), n2.getValue())
* strcmp(this->getValue(),
* itoa(n2.getValue()))
*/
/// then the compiler still could not choose which getvalue() to
call or which of those comparisons to apply.

The complication is that you need to choose method based on the
dynamic type of _both_ left and right operands. Look up "double
dispatch". BTW, you forgot the
strcmp(itoa(this->getvalue), n2.getvalue())
case, so there really are four involved.

I suppose you have only the two derived classes and that it will
never become many derived classes, so then the solution is not too
complicated.

Replace comparison with two functions:
virtual bool comp_string(NodeString* n2);
virtual bool comp_long(NodeLong* n2);

Make operator< virtual also. Then,
int NodeString::operator < (Node& n2) {
return n2.comp_string(*this);
}
and
int NodeLong::operator < (Node& n2) {
return n2.comp_long(*this);
}

Finally,
bool NodeString::comp_string(NodeString* n2) {
return strcmp(n2->s, s);
}
bool NodeString::comp_long(NodeLong* n2) {
return strcmp(itoa(n2->l), s);
}
bool NodeLong::comp_string(NodeString* n2) {
return strcmp(n2->s, itoa(l));
}
bool NodeLong::comp_long(NodeLong* n2)
return n2->l < l;
}

Notice that the n1 and n2 sides get switched during the call to
comp_*(). Fixup the bool return to what you really wanted. Replace
n2->member with getLong() and getString() as you prefer. Change all
those pointers to const references! Fix any bugs I left for you.

Do not listen to any proposal using typeid() or dynamic_cast<>().
That's the cowards way out!

Oct 18 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

30
by: Sean R. Lynch | last post by:
I've been playing around with Zope's RestrictedPython, and I think I'm on the way to making the modifications necessary to create a capabilities-based restricted execution system. The idea is to...
13
by: Rolf Magnus | last post by:
Hi, I would like to embed a python interpreter within a program, but since that program would be able to automatically download scripts from the internet, I'd like to run those in a restricted...
354
by: Montrose... | last post by:
After working in c# for a year, the only conclusion I can come to is that I wish I knew c. All I need is Linux, the gnu c compiler and I can do anything. Web services are just open sockets...
19
by: chris | last post by:
Hello, I've recently been trying to understand the various structures supplied by c++, and the one I find most confusing is deque. One quick question about this. It seems most implementations...
15
by: Bernard | last post by:
Hi All, I am not sure if I should be asking this question on clc or clc++. Let me try on both. I hope that this is not too trivial for the brilliant minds over here. I know that OOP questions...
70
by: Rajan | last post by:
Hi, I am trying to simulate a memcpy like this void* mem_cpy(void* dest, void* src, int bytes) { dest = malloc(bytes); } Now if I want to copy the bytes from src to dest, how do I copy these...
21
by: iapain | last post by:
I'm developing a webIDE for python and I've 2 questions regarding it. 1. How can i disable some of the modules without deleting. e.g I wish to disable "os" module. 2. How can i force user code...
6
by: Charleees | last post by:
Hi all, I have a Login Page whrere all Functionalities such as validations are Done in Client Side... I have to Implement Remember My Mail Id and PAss Word Functionality also in Client side.....
3
by: Paul Rudin | last post by:
I'm occasionally seeing tracebacks like this: Traceback (most recent call last): File "logging/__init__.py", line 744, in emit File "logging/__init__.py", line 630, in format File...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.