[antlr-interest] How to get a list of all valid options for the next token?

Niemeijer, R.A. r.a.niemeijer at tue.nl
Wed Aug 13 00:12:40 PDT 2008


Hello Brent,

 

It seems my question wasn't clear enough. I have no intention of having
a dynamic grammar. What I meant by disabling puzzle pieces is this: on
the left side of the screen there is a list of all the possible puzzle
pieces, a library if you will. From there the user drags pieces to the
right side of the screen to form sequences of puzzle pieces. What I want
to do is disable the pieces in the library. An alternative way of
looking at it is seeing it as Intellisense/code completion. While the
user is typing (dragging pieces), I want to display a popup list of all
the possible words (pieces) that can come at the current position in the
sentence. The grammar itself is completely fixed, just like the grammar
of programming languages that allow code completion.

 

From: Foust [mailto:javafoust at gmail.com] 
Sent: dinsdag 12 augustus 2008 22:14
To: Niemeijer, R.A.
Subject: RE: [antlr-interest] How to get a list of all valid options for
the next token?

 

> I want to disable all the pieces that cannot come at that point in the
sentence.

> I want to disable all the pieces that cannot come at that point in the
sentence.

 

My guess is that this will be pretty difficult to implement using even
an augmented context-free grammar. Usually, you would parse any valid
sentence (a list of puzzle pieces, in your case), then do the semantic
analysis as a later pass (iterate over the actual puzzle pieces entered
and check each one for validity). 

 

Multiple passes should make the code easier to write (as well as
maintain). And error reporting should be more intuitive. It's pretty
difficult to enforce all semantic meaning at the parse phase and still
output meaningful error messages.

 

If you really want to experiment with changing the grammar dynamically,
though, you could generate an AST in your parser for any valid sentence,
and then use semantic predicates in your tree grammar to turn on and off
certain rules. But I think you'll find that just adding validity checks
in your tree grammar actions will be the most straightforward, providing
all the advantages of a multi-pass interpreter.

 

Brent

 

From: antlr-interest-bounces at antlr.org
[mailto:antlr-interest-bounces at antlr.org] On Behalf Of Niemeijer, R.A.
Sent: Tuesday, August 12, 2008 2:57 AM
To: antlr-interest at antlr.org
Subject: [antlr-interest] How to get a list of all valid options for the
next token?

 

Hello everyone,

 

I'm making a program where users construct short sentences by combining
puzzle pieces. This series of puzzle pieces is then converted to text
and parsed by Antlr. Currently the user has access to all the different
pieces at any point in the sentence, even if the grammar would not be
able to parse this. I want to disable all the pieces that cannot come at
that point in the sentence.

My idea was to do this with the help of the exceptions the parser
generates. For example, when it throws a MismatchedTokenException I can
find the expected token through the Expecting property of the exception.
Unfortunately this only works when there's only one possibility. When
there are multiple it instead throws a NoViableAltException or a
MismatchedSetException. I haven't been able to find a list of valid
following tokens in their properties.

 

Can anyone tell me if there is any way of determining the list of valid
tokens after the current token in the sentence?

 

Thanks in advance.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080813/411a2dee/attachment.html 


More information about the antlr-interest mailing list