parser segfault: tips/tricks?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

parser segfault: tips/tricks?

Littlefield, Tyler
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hello all:
I'm getting a segfault when I run this. It looks like the debug
prints, but when I debug it I just get an endless loop of backtrace.
If anyone can help point me in the right direction, I'd appreciate it.
I'd also appreciate, if possible any tips/tricks for cleaning up this
grammar.

Thanks!

//code here:
/***
*I-EBNF parser
*
*This defines a grammar for BNF.
*/

//Speeds up compilation times.
//This is a relatively small grammar, this is useful.
#define BOOST_SPIRIT_NO_PREDEFINED_TERMINALS
#define BOOST_SPIRIT_QI_DEBUG

#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/fusion/adapted.hpp>
#include <boost/fusion/support.hpp>
#include <vector>
#include <string>
#include <iostream>

namespace Parser
{

namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;

enum class RHSType
{
    Terminal, Identifier
};
struct RHS
{
    RHSType type;
    std::string value;
};
struct Rule
{
    std::string identifier; //lhs
    std::vector<RHS> rhs;
};
}

//expose our structs to fusion:
BOOST_FUSION_ADAPT_STRUCT(
    Parser::RHS,
    (Parser::RHSType, type)
    (std::string, value)
)
BOOST_FUSION_ADAPT_STRUCT(
    Parser::Rule,
    (std::string, identifier)
    (std::vector<Parser::RHS>, rhs)
)

namespace Parser
{
typedef std::vector<Rule> RuleList;

//our grammar definition
template <typename Iterator>
struct Grammar: qi::grammar<Iterator, std::list<Rule>, ascii::space_type>
{
    Grammar(): Grammar::base_type(rules)
    {
        qi::char_type char_;

        letter = char_("a-zA-Z");
        digit = char_('0', '9');
        symbol = char_('[') | ']' | '[' | ']' | '(' | ')' | '<' | '>'
| '\'' | '\"' | '=' | '|' | '.' | ',' | ';';
        character = letter | digit | symbol | '_';
        identifier = letter >> *(letter | digit | '_');
        terminal = (char_('\'') >> character >> *character >>
char_('\'')) | (char_('\"') >> character >> *character >> char_('\"'));
        lhs = identifier;
        rhs = terminal | identifier | char_('[') >> rhs >> char_(']')
| char_('{') >> rhs >> char_('}') | char_('(') >> rhs >> char_(')') |
rhs >> char_('|') >> rhs | rhs >> char_(',') >> rhs;
        rule = identifier >> char_('=') >> rhs;
        rules = rule >> *rule;
    }

private:
    qi::rule<Iterator, char(), ascii::space_type> letter, digit,
symbol, character;
    qi::rule<Iterator, std::string(), ascii::space_type> identifier,
lhs, terminal;
    qi::rule<Iterator, RHS, ascii::space_type> rhs;
    qi::rule<Iterator, Rule, ascii::space_type> rule;
    qi::rule<Iterator, std::list<Rule>, ascii::space_type> rules;
};

}

int main()
{
    Parser::Grammar<std::string::const_iterator> parser;
    boost::spirit::ascii::space_type space;
    std::string input;
    std::vector<std::string> output;
    bool result;

    while (std::getline(std::cin, input))
        {
            if (input.empty())
                {
                    break;
                }
            std::string::const_iterator it, itEnd;
            it = input.begin();
            itEnd = input.end();
            result = phrase_parse(it, itEnd, parser, space, output);
            if (result && it == itEnd)
                {
                    std::cout << "success" << std::endl;
                }
        }

    return 0;
}

Thanks,
- --
Take care,
Ty
twitter: @sorressean
web:http://tysdomain.com
pubkey: http://tysdomain.com/files/pubkey.asc
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2

iQEcBAEBAgAGBQJWBdyVAAoJEAdP60+BYxejFg0H/1uTQ76BNO6/wz19b/i70815
UVSnqJCHX9BIiaNaVcSKuEFII5n3VgSeR7s9CXQI1lnZDk8RwpJG4geY6kTvaiXG
Nsn+qEhSMShiu7AHYgSwqVhTYHCFTAgfoF5Mpa4YLo3e47u3SBr9xPep81b67HWS
t17PVzvylY2oq25WRZq6TLSWaKaYJ+nx2l1d1AOFfdVLGsK8D2XK5i4XfzVwx+4A
qi0GactHnUT+FyyAh40dst/1B0OsO0YN513mv3r+IawMmL1+HMWASNhY0pSauwer
aSIgqXyHu/N8gt53sm65Y1kKdp0nVGFxIXhuMArNbIyGPekVcs5/HBlYWQUQMqA=
=ifrq
-----END PGP SIGNATURE-----

------------------------------------------------------------------------------
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general
Reply | Threaded
Open this post in threaded view
|

Re: parser segfault: tips/tricks?

sehe
On 09/26/2015 01:45 AM, Littlefield, Tyler wrote:
> Hello all:
> I'm getting a segfault when I run this. It looks like the debug
> prints, but when I debug it I just get an endless loop of backtrace.
> If anyone can help point me in the right direction, I'd appreciate it.
> I'd also appreciate, if possible any tips/tricks for cleaning up this
> grammar.
>
> Thanks!
I started answering but it got quite extensive.

Watch me while I livestream answering your question(s) here:
https://www.livecoding.tv/sehe/

I've cross-posted the question to
http://stackoverflow.com/q/32801103/85371 because I feel it has better
facilities to present an answer.

Cheers,
Seth

------------------------------------------------------------------------------
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general
Reply | Threaded
Open this post in threaded view
|

Re: parser segfault: tips/tricks?

sehe
On 09/26/2015 09:30 PM, Seth wrote:

> On 09/26/2015 01:45 AM, Littlefield, Tyler wrote:
>> Hello all:
>> I'm getting a segfault when I run this. It looks like the debug
>> prints, but when I debug it I just get an endless loop of backtrace.
>> If anyone can help point me in the right direction, I'd appreciate it.
>> I'd also appreciate, if possible any tips/tricks for cleaning up this
>> grammar.
>>
>> Thanks!
> I started answering but it got quite extensive.
>
> Watch me while I livestream answering your question(s) here:
> https://www.livecoding.tv/sehe/
>
> I've cross-posted the question to
> http://stackoverflow.com/q/32801103/85371 because I feel it has better
> facilities to present an answer.
>
It is done!

I hope you were at least able to enjoy the background music there. Also,
had a bit of fun hearing me grapple with Spirit intricacies.

I've attached both the resulting working sample code and the
StackOverflow post (as HTML)

Let me know whether this was helpful to you :)

Seth



------------------------------------------------------------------------------

_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general

draft.md.html (23K) Download Attachment
test.cpp (4K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: parser segfault: tips/tricks?

Littlefield, Tyler
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 9/26/2015 5:06 PM, Seth wrote:

> On 09/26/2015 09:30 PM, Seth wrote:
>> On 09/26/2015 01:45 AM, Littlefield, Tyler wrote:
>>> Hello all: I'm getting a segfault when I run this. It looks
>>> like the debug prints, but when I debug it I just get an
>>> endless loop of backtrace. If anyone can help point me in the
>>> right direction, I'd appreciate it. I'd also appreciate, if
>>> possible any tips/tricks for cleaning up this grammar.
>>>
>>> Thanks!
>> I started answering but it got quite extensive.
>>
>> Watch me while I livestream answering your question(s) here:
>> https://www.livecoding.tv/sehe/
>>
>> I've cross-posted the question to
>> http://stackoverflow.com/q/32801103/85371 because I feel it has
>> better facilities to present an answer.
>>
> It is done!
>
> I hope you were at least able to enjoy the background music there.
> Also, had a bit of fun hearing me grapple with Spirit intricacies.
>
> I've attached both the resulting working sample code and the
> StackOverflow post (as HTML)
>
> Let me know whether this was helpful to you :)
>
> Seth
>
>


Thanks a lot for the SO explaination of why you did what you did, I
really appreciate it. I'm looking through this now. Thanks again and
sorry you went through so much effort to record the video.

Thanks,
- --
Take care,
Ty
twitter: @sorressean
web:http://tysdomain.com
pubkey: http://tysdomain.com/files/pubkey.asc
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2

iQEcBAEBAgAGBQJWBxfKAAoJEAdP60+BYxej1RUH/2nFYfNRa7XG95LRhuqc0VZp
k7MDYKbg+pXFcwoBNscfVXJTtowxdObgTke6uPdGpmyn2OtsmJ+zuAXNq09tZ4h1
LxRNY6wv8RcGG6VojpWcDsnM/d0uE3w6tN5Sq/tD/RNilV0AtBM62oa47j1fjXGA
/AxhdKYNAgDBaKqgkwWz+hpfIuurUyYImUvN1cqDFnLZc/BnjecLsgIVNWC/3sbm
akukkK/8MfGKEHa2b00CI/eVW514b0M+JGbgvkIyFWpCTQeJgc/pf4EtiKcoxCeb
AvA+fBfyujlnDIYJ+NaT28eJuVY0JOJOJESDWtesbS8MXWO1RalWlJpHXPS7AH0=
=K+RA
-----END PGP SIGNATURE-----

------------------------------------------------------------------------------
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general
Reply | Threaded
Open this post in threaded view
|

Re: parser segfault: tips/tricks?

sehe
On 09/27/2015 12:10 AM, Littlefield, Tyler wrote:
>  Thanks again and
> sorry you went through so much effort to record the video.
No problem at all. It's just something I decided to do for answering
stackoverflow questions.

It effectively doesn't take more time at all - in fact it's just doing
the same as I would do to just answer the question but

 - with a social aspect
 - and showing more details

The work flows (reading the error messages and solving the issues with
the grammar incrementally e.g.) may help people more than just
presenting the end-result with some cut-and-dried summary of what was done.

The answer is actually on StackOverflow so it could reach other people
with an interest in Spirit (we had ~15 concurrent viewers last night, as
it is).

Cheers,
Seth

------------------------------------------------------------------------------
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general