[antlr-interest] Logic like ~ for parsing

Kirby Bohling kirby.bohling at gmail.com
Mon Mar 8 08:02:55 PST 2010


Replies inline.

On Mon, Mar 8, 2010 at 9:46 AM, Jim Idle <jimi at temporal-wave.com> wrote:
> Take a look at this:
>
> http://www.antlr.org/wiki/display/ANTLR3/Custom+Syntax+Error+Recovery
>

I'll have to read that closely.

> And note that you can implement your own kinds of predicates using input.mark() and release etc.
>

I'm not having a lot of luck with that, I'll have to boil down a
minimal version and try again, and post a grammar and sample input if
I still can't make heads or tails of it.  I had a version that did the
right thing, except, it'd didn't consume any input, so it'd just loop
forever.  I couldn't figure out how to move the predicate higher up.
Maybe, I'll have to use a dynamic scope, return value or something to
push the value up or down the stack to have it become a predicate
higher up in the tree.

> However it looks like you might have your logic the wrong way around in that you want to use the predicates to select the production, not select the fact that it isn't the correct production:
>
> foo
> : (nda)=>nda
> | (ndb)=>ndb
> | DANGLIN_TOKEN
> ...
>

You'd think that, but alas, there is a rule rewrite that puts the
DANGLING_TOKEN in the correct subtree (I didn't show that in the
previous example, and realized everybody would suggest that I try
exactly what you said after I clicked send).  It's wiki markup, and
I'd like to have it be part of the "verbose output" sub-tree, not the
"markup to translate" subtree.  I suppose I could have 2-3 sections of
"verbose output" tree next to each other.  Maybe I can coalesce them
later?

So if I had (^V T1 T2) (^V T3) (^V T4 T5), is there a way in a tree
parse to turn that into (^V T1 T2 T3 T4 T5)?  I'd like to do that
inside of ANTLR if I can (parser, or tree walker).  I've been working
pretty hard to keep the amount of language specifics out of my
grammar, I'd like it to be easy to port over to other languages if at
all possible.

Thanks again,
Kirby


> But if you look through the ways you can resync the input in error recovery, you may get better mileage.
>
> Jim
>
>> -----Original Message-----
>> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
>> bounces at antlr.org] On Behalf Of Kirby Bohling
>> Sent: Sunday, March 07, 2010 10:45 PM
>> To: ANTR Interest
>> Subject: [antlr-interest] Logic like ~ for parsing
>>
>> All,
>>
>>    I have a grammar that is pretty far along.  But I really need a
>> rule like this:
>>
>> dangling_match:
>>     (non_dangling_a | non_dangling_b) => // Failure to match, get back
>> up to the foo level somehow.
>>     | DANGLING_TOKEN
>> ;
>>
>> foo:
>>    (non_dangling_a | non_dangling_b |
>> options_including_dangling_match)+
>> ;
>>
>> options_including_dangling_match:
>>    (option_1 | option_2| option_3| option_4| dangling_match)+;
>>
>> So non_dangling_a, non_dangling_b, and dangling match all start with
>> the token DANGLING_MATCH.  I'd really like non_dangling_match to on
>> cases where I have a dangling token.  As a concrete example, in C, I
>> only want the dangling option to match if I have a stray '{', but if
>> the '{' looks like it's part of well formed statement, I don't want
>> dangling_match to match and consume that input.  I'd like to arrange
>> for the system to get back up to the "foo" rule, and have the input
>> consumed there.
>>
>> If it were a lexer, I think I'd write the rule this way:
>>
>> dangling_match:
>>     { input.LA(1) == DANGLING_TOKEN }?
>>     (~(non_dangling_a|non_dangling_b)) => DANGLING_TOKEN
>> ;
>>
>> I can't seem to find a way to accomplish this in the parser.  I've
>> tried marking and resetting the stream, and using dynamic scopes.  The
>> problem is that everything I've found that compiles will kick me into
>> an infinite loop.  If it looks like a non_dangling case (the input is
>> well formed), it won't consume the input, and never leave the
>> "options_including_dangling_match" rule to get back to the "foo" rule
>> which would consume the non_dangling input.
>>
>> I've tried poking around in the FAQ, but I didn't see anything obvious.
>>
>> Thanks in advance,
>>    Kirby
>>
>> List: http://www.antlr.org/mailman/listinfo/antlr-interest
>> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-
>> email-address
>
>
>
>
> 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