Auto completion feature for a custom language defining in Antlr4

336 views
Skip to first unread message

Tharindu Munasinghe

unread,
Jul 6, 2015, 5:34:08 AM7/6/15
to antlr-di...@googlegroups.com
I am writing a web based code editor for custom query language (similar to SQL) . The grammar is written in Antlr4 API so I am using Antlr4-JavaScript run time for the parsing purposes. 

I tried to use getExpectedTokensWithinCurrentRule() to extract the next possible tokens types (by re parsing the input while user changing the code) . But while the user is typing the code it would not be parsed successfully without errors. So that method didn't helped me to get the information while user is typing the code.

Once I debug the code I realized that , 

parser._interp.atn.getExpectedTokens(STATE_NUMBER,null).toString(parser.literalNames,parser.symbolicNames)

would return the list of possible tokens for given state_number and context. I have tested this code segment with manually entering the state_numbers mentioned in generated parser's rule functions and this thing worked really well . So I tried to keep track the State Number just before the error is generated and suggest the tokens based on that. 

But as I noticed the adaptivePredict()  throw exception before it get reached to the most recent state number  . In other terms the top level parsing rules throw exceptions before it reaches to the mostly matched low level parser rules. So my second approach also not working properly.

Can some one help me to figure out a method with Antlr4 to get the possible token list for most recent context(While the input is invalid) ? . Or else any other strategy to implement auto completion , is highly appreciated.

Thanks.  
 

Terence Parr

unread,
Jul 16, 2015, 3:40:19 PM7/16/15
to antlr-di...@googlegroups.com
The set of configurations that come back from the error tell you what the states are that are possible. Each configuration has a parser stack and the current state in the ATN. sorry I don't have time to be more specific.

You should also look at the new stuff I added in 4.5.1 that uses the interpreter to get all possible parse trees for lookahead from the current position. I believe that will get you further.

Ter

--
You received this message because you are subscribed to the Google Groups "antlr-discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to antlr-discussi...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Dictation in use. Please excuse homophones, malapropisms, and nonsense. 

Tharindu Munasinghe

unread,
Jul 28, 2015, 8:17:36 AM7/28/15
to antlr-di...@googlegroups.com, pa...@cs.usfca.edu
Sorry for the delay in response . Thank you very much Terence for the
information.. . Yes , getting all possible parse trees for lookahead
from the current position solves most of the problems . I have tested
them in JavaScript run-time but it seems to me like these new
features are only accessible in Java run time , isn't it? . It would
be great if these are implemented in JS run-time also , so that for
sure it can be used in my scenario.

What are the configurations you have mentioned above?. It would be
great If you can share some references about them ( is it in the
Antlr4 reference guide ? )
Tharindu Munasinghe.
Undergraduate ,Department of Computer Science and Engineering
University of Moratuwa.
Contact no. +94770460887
> You received this message because you are subscribed to a topic in the
> Google Groups "antlr-discussion" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/antlr-discussion/0nPpUiIi0qg/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to

Terence Parr

unread,
Jul 29, 2015, 5:34:08 PM7/29/15
to Tharindu Munasinghe, antlr-di...@googlegroups.com
The configurations are ATN configurations that indicate, among other things, the state in the ATN where the parser reached.
Ter

Woeths Plemagus

unread,
Sep 22, 2016, 2:05:26 AM9/22/16
to antlr-discussion, munasingh...@gmail.com
Could you elaborate on "new stuff I added in 4.5.1 that uses the interpreter to get all possible parse trees for lookahead from the current position." I'm fairly new to antlr 4 and this seems pretty interesting. I've tried looking in the docs to find where such a function on the interpreter but wasn't able to find it.

Is it an analogue to getExpectedTokens on _interp.atn ?
Reply all
Reply to author
Forward
0 new messages