简体   繁体   中英

Antlr AST tree construction

I'm trying to create an AST using ANTLR tree grammar.

Given a syntax like following:

rule    : head ':-' litlist (';' ':-' litlist)* DOT_END

I want to create a tree like following

 ^(RULES ^(head litlist)+)

That is, I want (head litlist) to be repeated as many as the number of litlist in the syntax. I tried something like above, but I'm getting an error like this:

Syntax error:required (...)+ loop did not match anything at input EXPR where EXPR is another term in the grammar.

Basically I want something like following:

a : type ID (',' ID)* ';' -> ^(type ID)+;

which is described in Tree constructon .

But my syntax has multiple tokens inside repeated clause, which seems to be a problem.

What is the right way to do this?

Have you tried another level of indirection? Such as:

rule    : clauses DOT_END ->  ^(RULES clauses)
clauses : head ':-' litlist (';' ':-' litlist)* -> ^(head litlist)+

Maybe the syntax is not proper yet.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM