Partial AST by fail parsing

103 views
Skip to first unread message

Phuc Luoi

unread,
Jul 29, 2012, 12:25:02 PM7/29/12
to sab...@googlegroups.com
Hi Etienne,

can I get the (parial) AST when the parser detects a grammar error in
the input? For example

grammar := sttm *
sttm := expr ';'

and the imput is

expr_1 ; expr_2 ; expr_3

then can I get the (parsed) AST ist somehow like

grammar
    + --- sttm
    |       +---- expr_1
    |       +----- ';'
    + ---- sttm
    |       +-----expr_2
    |       +----- ';'
    +----- sttm
    |       +-----expr3


Even the input is wrong. It will be very useful to user to find errors in input.

Thanks

Hong Phuc 

Etienne Gagnon

unread,
Jul 30, 2012, 8:49:46 PM7/30/12
to sab...@googlegroups.com
Hi Hong Phuc,

This is a very interesting idea.

There is a potentially infinite number of incomplete ASTs that can match the parse stack. I guess that SableCC would have to pick one arbitrarily (I see no point in being picky about an incomplete AST).

Given the arbitrary nature of an incomplete AST, its utility is restricted to superficial syntactic applications (e.g. indenting incomplete files). It would be an error to use such an AST for doing meaningful semantic work. As a consequence, I think that the arbitrary incomplete AST should only be available upon request, not by default.

I'm adding this feature to the SableCC 4 todo list. (One more thing to do!)

Thanks for the proposition,

Etienne
Etienne Gagnon, Ph.D.
http://sablecc.org
Le 12-07-29 12:25, Phuc Luoi a écrit :
can I get the (parial) AST when the parser detects a grammar error in
the input? For example
[...]

Phuc Luoi

unread,
Jul 31, 2012, 12:16:50 PM7/31/12
to sab...@googlegroups.com
Hi Etienne,


> I'm adding this feature to the SableCC 4 todo list. (One more thing to do!)

Thank you very much. It will help the user to find the grammar error,

Hong Phuc

--
-- You received this message because you are subscribed to the SableCC group. To post to this group, send email to sab...@googlegroups.com. To unsubscribe from this group, send email to sablecc+u...@googlegroups.com. For more options, visit this group at https://groups.google.com/d/forum/sablecc?hl=en
 
 

Miguel Angel Garcia de Dios

unread,
Apr 9, 2014, 2:31:07 PM4/9/14
to sab...@googlegroups.com
Hi Etienne,

I am implementing auto-completion for my language, that has been implemented using SableCC. I need to get an AST from a possibly incomplete expression in my language, in order to analyze certain parts and obtain the desired information for the auto-completion.
Is currently any way of parse an expression and get the AST just before the parser exception? For example, the grammar of my language would parse "3 + 5", but would not parse "3 +". In the latter case, I would need the AST just before the error: "3 +". I need to get the AST of partial expressions in order to get the desired information, since working with strings seems to be impossible.

Thank you very much.

Etienne Gagnon

unread,
Apr 10, 2014, 7:59:00 AM4/10/14
to sab...@googlegroups.com
Hi Miguel,

You could hack the src/org/sablecc/sablecc/parser.txt file to add the current parsing stack content into ParserException objects. (You just need to unpack the sablecc.jar file, modify the file, and repack it). Something like:
public class ParserException extends Exception
{
    private Token token;
    // ADDED: list of nodes on the parsing stack
    private List<Node> stack;

    public ParserException(Token token, ListIterator<Object> stack, String  message)
    {
        super(message);
        this.token = token;

        // ADDED:save current stack content
        this.stack = new LinkedList<Node>();
        for(Object o : stack) {
          this.stack.add((Node) o);
        }
    }

    public Token getToken()
    {
        return this.token;
    }

    // ADDED: accessor
    public List<Node> getStack() {
      return this.stack;
    }
}

This will give you the forest (set of trees) of the prefix leading to the parser exception.

Be aware that using this information is tricky; there could be many possible interpretations for a given prefix. That's why I do not include this in the official release.

Have fun!


Etienne
Etienne Gagnon, Ph.D.
http://sablecc.org
On 2014-04-09 14:31, Miguel Angel Garcia de Dios wrote:
Reply all
Reply to author
Forward
0 new messages