Creating an ANTLR 4.2 ParseTreePattern in C#

413 views
Skip to first unread message

Onur

unread,
Apr 1, 2014, 6:58:16 AM4/1/14
to antlr-di...@googlegroups.com


I tried to find out how the tree pattern matching works in C# by modifying the code found here:
https://github.com/antlr/antlr4/blob/master/tool/test/org/antlr/v4/test/TestParseTreeMatcher.java#L387 

Although I can parse the expression, I can't create the ParseTreePattern.

Since this topic is quite new, I can't find any tutorial like stuff. Maybe someone can give me a hint...


This is the grammar file: 


	grammar X6;
 
	@parser::members
	{
		protected const int EOF = Eof;
	}
 
	@lexer::members
	{
		protected const int EOF = Eof;
		protected const int HIDDEN = Hidden;
	}
 
	/*
	 * Parser Rules
	 */
 
 
	s   : expr ';'
		;
	exprexpr '.' IDexpr '*' exprexpr '=' exprIDINT
		;
	ID : [a-z]+ ;
	INT : [0-9]+ ;
	WS : [ \r\n\t]+ -> skip ;



This is the code I use:

                string input = "3*4*5";
 
                var tokenStream = new CommonTokenStream(new X6Lexer(new Antlr4.Runtime.AntlrInputStream(input)));
 
                var parser = new X6Parser(input: tokenStream)
                    {
                        BuildParseTree = true,
                    };
 
                var s = parser.expr(); // <-- works fine
 
                string pattern = "<expr> * <expr> * <expr>";
 
                var parseTreePattern = parser.CompileParseTreePattern(pattern, X6Parser.RULE_s); // <-- throws the exception mentioned below



The code executes fine until the last line which throws an Antlr4.Runtime.InputMismatchException.

Offending token: {[@5,-1:-1='EOF',<-1>,0:0]}
Stack trace:
   at Antlr4.Runtime.Tree.Pattern.ParseTreePatternMatcher.Compile(String pattern, Int32 patternRuleIndex)
   at Antlr4.Runtime.Parser.CompileParseTreePattern(String pattern, Int32 patternRuleIndex, Lexer lexer)
   at Antlr4.Runtime.Parser.CompileParseTreePattern(String pattern, Int32 patternRuleIndex)
   at STParser.STParserTest.test() in myTestFile.cs last line 



I'm using Visual Studio 2010 and this is the NuGet Package file if the version matters:

<packages>
  <package id="Antlr4" version="4.2.1-alpha001" targetFramework="net40" />
  <package id="Antlr4.Runtime" version="4.2.1-alpha001" targetFramework="net40" />
</packages>

Sam Harwell

unread,
Apr 1, 2014, 7:25:39 AM4/1/14
to antlr-di...@googlegroups.com

Hi Onur,

 

You told the parse tree pattern to start at rule s, but that rule requires a semicolon at the end of the expression. You should update the call to CompileParseTreePattern to use RULE_expr instead of RULE_s.

 

I highly recommend you also update your rule s to include an explicit EOF at the end, and update your input to include the semicolon and call parser.s() instead of parser.expr() to parse the input.

 

Also, starting with the 4.2.0 release, BuildParseTree defaults to true and you no longer need to include the @parser::members or @lexer::members blocks in your C# grammars.

 

The following Gist shows working sample code for this feature:

https://gist.github.com/sharwell/9912132

 

Thanks,

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.

Reply all
Reply to author
Forward
0 new messages