Test Driven Parser

SyntaxEditor for WPF Forum

Posted 11 years ago by Ian Davis
Version: 12.2.0571
Avatar

Is there any guidance, patterns, or advice for creating a language parser/grammar, test first? The tree constructor API for the grammar seems very focused on creating the entire grammar in a single method (shared context) which makes testing very difficult.

The other option I see is to write a top down parser by hand, which should allow full testing.

Thanks

Comments (4)

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

Hi Ian,

I'm sorry we don't really have any guidance for that.  However the good thing is that any of the non-terminals can easily become a root for the grammar.  So if you set up all your non-terminals as fields (or properties) on the grammar class instead of variables, you could make tests for each one by setting the grammar's Root property to the NonTerminal being tested, and then execute the parsing like normal and see what the non-terminal production's AST results were.


Actipro Software Support

Posted 11 years ago by Ian Davis
Avatar

I had started to follow down that line to see what I can do. I changed the grammar ctor to this for construction:

        public MyGrammar(NonTerminal root = null)
            : base("MyLanguage")
        {
            Root = root ?? Create();
        }

This lets me change the non-terminal root for testing, or a default construction of the full grammar. I then made all of my terminals fields in the class (and productions which are only composed of terminals).

I can't do the grammar construction outside of the grammar class since the utility methods (Ast, AstFrom, etc) leverage internal, obfuscated, classes which I could otherwise use to isolate the production creation in other classes.

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

Hi Ian,

Those Ast, AstFrom methods are all public static methods on Grammar.  So you should be able to use them externally.


Actipro Software Support

Posted 11 years ago by Ian Davis
Avatar

Thanks, somehow I missed that.

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.