-
Just faced a problem with grammar. I think I'm doing something wrong but don't know how to fix it.
This was mapped to PEGTL as: struct userinfo : seq< sor< user, telephone_subscriber >, opt< seq< one<':'>, password > >, one<'@'> > {};
struct SIP_URI : seq< sip_proto, opt< userinfo >, hostport, uri_parameters, opt< headers > > {}; The problem is that it detects Can you please tell me what am I doing wrong and how to fix it? |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments
-
How do you know that it matched |
Beta Was this translation helpful? Give feedback.
-
Sure, I have action for |
Beta Was this translation helpful? Give feedback.
-
Probably what happens is that it is attempted to match There are several ways to fix this. The easiest is to use look-ahead with Another is to let the rules like In addition you can merge common prefixes in the grammar and branch later, so instead of The look-ahead is the easiest but you parse some parts twice, the merging/rewriting is the most work and can change the grammar to the point of not being very recognisable, however it yields the most efficient results. |
Beta Was this translation helpful? Give feedback.
-
Thanks a lot! Now I've used temporary context to capture stored parts and commit them later at once. Code in actions actually become a bit simpler IMHO. Could you please elaborate your comment on using |
Beta Was this translation helpful? Give feedback.
-
When using |
Beta Was this translation helpful? Give feedback.
Probably what happens is that it is attempted to match
userinfo
, which successfully matchesuser
to (the first part of) the host name, but because of the missing "@" the fulluserinfo
does not match, and then everything backtracks, but by then the action foruser
has already been called.There are several ways to fix this.
The easiest is to use look-ahead with
at
to make sure that it is in fact auserinfo
before parsing again with actions enabled.Another is to let the rules like
user
store the data in a temporary location, and only commit it to their final destination once the overarching rule successfully matches.In addition you can merge common prefixes in the grammar and branch later…