ANTLR for defining / calling multi-parameter functions

I have a grammar in which I would like to include multi-parameter functions (e.g. f(x,y) ). I use AST output with my own tree parser. Production parameter list right now

 paramdefs: (ID COMMA)* ID ; 

This works fine, but AST output for

 z(x,y)=expression 

is an

 (FUNC (z)(x)(,)(y)(expression)) 

(that is, it is very flat).

FUNC CommonTree children are generally { function name , parameter , comma , parameter , defined expression } for any number of parameters. I would like the parameter list to be an only child and not have commas (this would make it easier to move around the tree).

Ideally, this would look like a tree:

 (FUNC (z)((x)(y))(expression)) 

(note the absence of a comma element and the grouping of x and y .

Relevant related areas of grammar:

 funcdef: ID '(' paramdefs ')' '=' expr -> ^(FUNC ID paramdefs expr) ; paramdefs: (ID COMMA)* ID ; 
+6
source share
1 answer

To create a tree like this:

enter image description here

to enter z(x,y)=expr , follow these steps:

 grammar ... ... tokens { FUNC; PARAMS; } ... funcdef : ID '(' paramdefs ')' '=' expr -> ^(FUNC ID paramdefs expr) ; paramdefs : (ID COMMA)* ID -> ^(PARAMS ID+) ; 
+8
source

Source: https://habr.com/ru/post/892453/


All Articles