3
\$\begingroup\$

I wrote a Perl 6 grammar to parse a C++ function. My final goal would be to parse an entire header. The aim is not to correct C++ syntax errors, but to parse valid C++.

Do you have some advice or improvements?

#!/usr/bin/env perl6
grammar FUNCTION {
 token TOP { [ <attr> \s+ ]? <type> [ \s* <type_mod> ]? 
 (\s+) <fname> (\s*) "("
 (\s*) [<parameter> [ "," (\s*) <parameter> ]* ]? (\s*) 
 ')'(\s*) ';' 
 }
 token name { \w+ }
 token namespace { [ "::" ]? [ <name> "::" ]* }
 token attr { <name> }
 token type { <namespace>? <name> }
 token type_mod { [ \*|\& ]+ }
 token fname { <name> }
 token variable { <name> }
 token parameter { <type> [\s* <type_mod> ]? \s+ <variable> }
}
my $str = "const ::one::std::string ** ma1n( int&& i, two::std::string va1e_ );";
my $parsed = FUNCTION.parse($str);
say $parsed;
rolfl
98.1k17 gold badges219 silver badges419 bronze badges
asked Nov 21, 2018 at 19:22
\$\endgroup\$
0

1 Answer 1

4
\$\begingroup\$

I'd expect to see many more tests of any program that addresses a problem as gnarly as parsing a C++ declaration.

Choosing a couple I've recently had cause to write (on Stack Overflow), I would immediately add

Neither of these succeeded when I tried them.

answered Nov 21, 2018 at 22:58
\$\endgroup\$
1
  • 1
    \$\begingroup\$ I've no way to test it right there but I believe it also lacks noexcept support \$\endgroup\$ Commented Nov 22, 2018 at 14:28

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.