Checking Nonterminal in CanMatchCallback

SyntaxEditor for WPF Forum

Posted 10 years ago by Kevin Fournier
Version: 14.1.0602
Avatar

I have an ambiguity in my language in which two nonterminals start with @identifier. Unfortunately, that terminal is within yet another nonterminal that is rather complex. Here are the ambiguous nonterminals alond with the ambiguous "varOrMethod" nonterminal.

assignmentStatement.Production = varOrMethod + postfixExpression.Optional() + (@plus | @minus | @colon).Optiona() + @equalsign + expression + endOfStatement;

methodStatement.Production = varOrMethod + endOfStatement;

varOrMethod.Production = @identifier + (@openparenthesis + expressionList.Optional() + @closeparenthesis).Optional();

To clarify these rules, the language I am parsing allows for the following:

// This is a method
A(1)
 
// This is a variable
A(1) = 2

How to I check for a valid varOrMethod nonterminal within the CanMatchCallback delegate so I can then check for an endOfStatement immediately following? In other words, I'm not sure how to manually validate a production rule.

Comments (2)

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

Hi Kevin,

That could be tough.  You could use a CanMatchCallback at the point of ambiguity, such as on AssignmentStatement (assuming that appears higher in a calling alternation than MethodStatement does). But then you'd have to run through all the tokens that could be valid, which wouldn't be easy.

It might be better if you make a new AssignmentOrMethodStatement.  Basically have it be the AssignmentStatement production but make the portion after VarOrMethod and through to the EndOfStatement be Optional().  Have it always produce an assignment.  Then wrap the tree constructor with a custom tree constructor class where if it detects that no assignment was made, convert the resulting AST over to a MethodStatement AST node instead.  That would be a lot easier to do.


Actipro Software Support

Posted 10 years ago by Kevin Fournier
Avatar

That is an excellent suggestion. I will pursue that avenue. Thank you.

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

Add Comment

Please log in to a validated account to post comments.