Posted 11 years ago by Ian Davis
Version: 12.2.0572
Avatar

Is there a way to access / compute the follow set for a grammar node in the EBNF support that you have in the SE parser framework?

We have some cases where it would be very useful to compute a follow set (or a guess at one) but we don't know how without doing it by hand...

Also, is there a way to control the formatting of the EBNF printout (or to rewrite it easily?)

Thanks.

Comments (3)

Answer - Posted 11 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Ian,

Assuming you mean how do you get the list of tokens that can begin a grammar node, the object returned by EbnfTerm.MatchCondition has a GetFirstSet method you can call to get that info.

Each IGrammarNode has a Children collection that you can iterate through and recurse into.


Actipro Software Support

Posted 11 years ago by Kelly Leahy - Software Architect, Milliman
Avatar

What we're looking for was what is termed a "follow set" in LL(k) parsers.  I assume from your answer that actipro's grammars don't have any easy way to get this, so we'll have to use the computation of follow set.

To compute a follow set for a production, you find all occurrences of the production in the grammar, and you look at the first sets of all non-optional terms that appear after these occurrences.  If there are optional terms, you have to include everything up to the first non-optional term as well.

The follow set is the best way to give a good error message for what you're expecting when a error occurs after matching a given node.  However, I suppose this can also be done via a first set for the next node - it just becomes problematic when you want to show a good error for a node based on its context.

I'll try to come up with an example of what we're trying to do and see if you can give us a better alternative.

Kelly

Kelly Leahy Software Architect Milliman, USA

Posted 11 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Kelly,

I believe our MatchCondition.GetFirstSet method does effectively return what you are describing.  It should contain the list of terminals based on grammar compilation that can possibly start the term.

On a side note, we have a nice way already of providing error messages for terms.  If you look at our SimpleGrammar in Getting Started 4d, you can see its use where we set ErrorAlias both for terminals and a couple non-terminals.  The documentation describes it a bit more but if one is set for a non-terminal then a nice error message will get displayed.  So we typically set the error alias on things like Expression or Statement in a language.  That way whenever some form of expression fails to match, the user gets an "Expression expected." error.


Actipro Software Support

The latest build of this product (v24.1.1) was released 1 month ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.