Using ANTLR4 in C# for building Expression trees

1,061 views
Skip to first unread message

Александр Кучеренко

unread,
Mar 21, 2014, 8:41:06 AM3/21/14
to
Hello everybody.

I'm trying to build Expression tree using ANTLR visitor in C#. ANTLR is used for parsing simple logical string like 'str1 OR str2'. From that string I want to get C# Expression<Func<String,bool>> so I can test if passed string matches conditions defined in logical string.
But after I call visit method on visitor as the result I get null, but in debug I can see that all my methods work as they should.
Could you please find problem in my code.
File "example usage.cs" contains sketch  of that I'm trying to achieve.
File "LogicalLangTreeVisitor.cs" contains my implementation of visitor.
File "LogicalLang.g4" contains my grammar.


LogicalLang.g4
LogicalLangTreeVisitor.cs
example usage.cs

Sam Harwell

unread,
Mar 21, 2014, 5:26:43 PM3/21/14
to antlr-di...@googlegroups.com

Your example requires that expr be following by a newline character. In your sample input, no newline character is given, so the parse tree contains error nodes.

 

Also note that you didn’t provide an implementation for VisitProg or VisitStat in your visitor.

 

The default behavior for the visitor, in combination with error nodes appearing in your parse tree, is producing the null return value you are observing.

 

Sam

--
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.

Александр Кучеренко

unread,
Mar 24, 2014, 1:40:06 AM3/24/14
to antlr-di...@googlegroups.com
Thanks for your comments. I have fixed my problem by adding new clause in stat rule for expr with no newline.


суббота, 22 марта 2014 г., 1:26:43 UTC+4 пользователь Sam Harwell написал:
Reply all
Reply to author
Forward
0 new messages