-
Apologies for possibly naive question. Thanks a lot for the help I am trying to write a grammar in which there are multiple options, any of which can be matched (only one per input string), but it's not clear to me how to do so and still capture the parts that I want to capture. Here's some example code: struct name
: plus< alpha >
{};
struct epithet
: plus< alpha >
{};
// various possible combinations
struct just_a_name
: must< name >
{};
struct genus_epithet
: must< name, space, epithet >
{};
struct genus_epithet_infra
: must< name, space, epithet, space, name >
{};
struct grammar
: star<
sor<
just_a_name,
genus_epithet,
genus_epithet_infra
>,
eof
> {};
template<>
struct action< name >
{
template< typename Input >
static void apply( const Input& in, std::string& v, const std::string& )
{
v = in.string();
}
};
template<>
struct action< epithet >
{
template< typename Input >
static void apply( const Input& in, const std::string&, std::string& v )
{
v = in.string();
}
}; In the actions, I want to grab the |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
The order within the If you try to match the longer expressions first by using
Although even that won't work because This might be because your example is an artificial example? Your real grammar is probably different, in any case your problem is the way the grammar works. |
Beta Was this translation helpful? Give feedback.
-
Thanks! Okay, here's the real code: https://github.com/ropenscilabs/pegax/blob/master/src/sci_name.h#L16-L158 I have these different patterns https://github.com/ropenscilabs/pegax/blob/master/src/sci_name.h#L38-L62 and i'm going to add many more. In my grammar any of them can be a possible match depending on the input string - and I'm hoping to have a cleaner looking grammar statement rather than one huge grammar statement with all possible patterns. Is there a way to use a grammar like https://github.com/ropenscilabs/pegax/blob/master/src/sci_name.h#L64-L77 but still be able to pull out individual parts when a match is found, e.g., |
Beta Was this translation helpful? Give feedback.
-
You real grammar is even worse than the example grammar you posted earlier, sorry. It can not possibly work. Think about it: Those (plus other) alternatives are inside of a Plus for the PEGTL any combination of |
Beta Was this translation helpful? Give feedback.
You real grammar is even worse than the example grammar you posted earlier, sorry. It can not possibly work. Think about it:
softSpace
isstar<space>
which means the separator could be nothing. Andname
,epithet
andinfraname
are allplus<alpha>
, separated bysoftSpace
. Looking atjust_a_name
,genus_epithet
andgenus_epithet_infra
, there is no way to reasonably parseABC
as an input. Could be anything of those three.Those (plus other) alternatives are inside of a
star<sor<...>>
. How would you imaging the inputfoobarbazblablubblaberhalloholagaga
to match to those rules? It could befoobarbaz
matchesjust_a_name
three times, once withfoo
, once withbar
and once withbaz
. Or is it meant t…