[antlr-interest] C++ Map not usable, always SEGFAULT
Jim Idle
jimi at temporal-wave.com
Fri Oct 15 10:25:22 PDT 2010
1) Do not embed code in the grammar as you will end up with an unreadable
mess (see many other posts about this);
2) Don't use $text unless what you are doing is trivial, but remember that
the $text items are released once you free the parser (see many other posts
about this);
3) Don't use 'literals', use tokens defined in the lexer (see many other
posts about this);
4) However, that is not your problem, download and run valgrind, which will
tell you what you are doing wrong - this is a programming issue, not an
antlr issue;
http://antlr.markmail.org
http://valgrind.org/
You might be better just putting this code in a main.c program and taking
antlr out of the equation. When you can just set the list you will see what
you are doing wrong.
Jim
> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of Christian Benjamin Ries
> Sent: Friday, October 15, 2010 2:05 AM
> To: antlr-interest at antlr.org
> Subject: [antlr-interest] C++ Map not usable, always SEGFAULT
>
> Hello, I'm a little bit frustated.
>
> I'm trying to use a C++ Map with ANTLR, but I get a segfault always.
>
> The source is pasted below, any suggestions?
>
> The output is always:
> --
> cr at ....:~/dsl/output$ ./dsl ../install1.host_dsl
> NetIP: 127.0.0.1
> hlist size: 0
> Segmentation fault
> --
>
> 1. Snippet - class Host with Map:
> ---- 1. START
> class Host {
> private:
> Network network;
> Distribution distribution;
> public:
> Host();
> Host(const Host& host);
> ~Host();
> void setNetwork(Network network) { this->network = network; }
> Network & getNetwork() { return this->network; }
> void setDistribution(Distribution distri) { this->distribution =
distri; }
> Distribution & getDistribution() { return this->distribution; } };
struct
> HostMapCompare {
> bool operator()( const char* s1, const char* s2 ) const {
> return strcmp( s1, s2 ) < 0;
> }
> };
> typedef std::map<const char*, Host, HostMapCompare> HostList; typedef
> std::map<const char*, Host, HostMapCompare>::iterator HostListIterator;
> ---- 1. END
>
> ---- 2. START
> cluster :
> host*
> ;
> ---- 2. END
>
> ---- 3. START
> host
> scope Symbols;
> @init {
> $Symbols::name->custom = new std::string();
> $Symbols::name->freeCustom = freeName;
> }
> : 'Host' n=STRING {
> $Symbols::name->custom = $n.text->chars;
> }
> OPEN_BRACE
> host_values[$Symbols::name]
> CLOSE_BRACE
> ;
>
> host_values[pANTLR3_COMMON_TOKEN hostname]
> @init {
> std::string hostid;
> Network network;
> }
> : {
> hostid = (char*)hostname->custom;
> hostid = hostid.substr(1,hostid.length()-2);
> }
> (
> 'distribution' ':' DISTRIBUTION END
> | 'netip' ':' a=IP {network.setNetIp((char*)$a.text->chars);} END
> | 'netgw' ':' b=IP {network.setNetGw((char*)$b.text->chars);} END
> | 'netns' ':' c=IP {network.setNetNs((char*)$c.text->chars);} END
> | 'netnm' ':' d=IP {network.setNetNm((char*)$d.text->chars);} END
> | 'nethn' ':' STRING
{network.setNetHn((char*)$STRING.text->chars);}
> END
> )*
> 'services' ':' (ID|service) ((',' ID)|service)* END {
> VisualGrid::Host h;
> h.setNetwork(network);
> std::cout << "NetIP: " << h.getNetwork().getNetIp() <<
std::endl;
> std::cout << "hlist size: " << hlist.size() << std::endl;
> hlist[hostid.c_str()] = h; // COULD NOT SET?!?!?!?!?
> std::cout << "hlist size: " << hlist.size() << std::endl;
> }
> ;
> ---- 3. END
>
> --
> ==========================================================
> ==================================
> Dipl.-Ing. (FH) Christian Benjamin Ries
> University of Applied Sciences Bielefeld Department of Engineering
Sciences
> and Mathematics CMSE - Computational Materials Science& Engineering
>
> Wilhelm-Bertelsmann-Str. 10
> D-33602 Bielefeld
> Office: 202 (WBS II)
> Phone: +49 521 106-71222
> Fax: +49 521 106-71241
> http://www.christianbenjaminries.de
> ==========================================================
> ==================================
>
>
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-
> email-address
More information about the antlr-interest
mailing list