473,747 Members | 2,849 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.getValu e()))
*/
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 1883
On 18 Oct 2006 06:29:01 -0700 in comp.lang.c++, "steve"
<mo**********@g mail.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.getValu e()))
*/
/// 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(thi s->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(Nod eString* n2);
virtual bool comp_long(NodeL ong* n2);

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

Finally,
bool NodeString::com p_string(NodeSt ring* n2) {
return strcmp(n2->s, s);
}
bool NodeString::com p_long(NodeLong * n2) {
return strcmp(itoa(n2->l), s);
}
bool NodeLong::comp_ string(NodeStri ng* 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
2569
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 strip out any part of RestrictedPython that's not necessary for doing capabilities and do all security using just capabilities. The basic idea behind capabilities is that you don't give any piece of code you don't trust a reference to something...
13
4028
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 environment, which basically means that I want to allow only a specific set of modules to be used by the scripts, so that it wouldn't be possible for them to remove files from the hard drive, kill processes or do other nasty stuff. Is there any...
354
15897
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 hooked up to interfaces. The Gtk is more than enough gui.
19
4376
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 of deque are quite complex. Why couldn't I implement deque with a couple of vectors V,W where the deque is the element of V in reverse order followed by W? This would appear to me to satisfy all the conditions, and be significantly simpler. Am I...
15
426
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 have been asked on clc before so it is probably OK. I am a newbie to C++. BS 3rd edition states: % The throw transfers control to a handler for exceptions .... %
70
11654
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 bytes. I am stuck here.
21
1716
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 to access only his particular folder, I dont want to create uses in unix, e.g fp = open(PATH, 'w') # If this PATH is defined then use can access files else he cant .. is there is any way?
6
6054
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.. When Remember PAssWord Check Box is Checked.....
3
2641
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 "logging/__init__.py", line 421, in format RuntimeError: instance.__dict__ not accessible in restricted mode I'm not sure what it means. Googling leads me to <http://docs.python.org/lib/restricted.htmlbut that says that those
0
8979
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8818
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9354
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9223
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8233
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6790
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6067
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4588
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2771
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.