Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ANTLR grammar for defining/calling multi-parameter functions

I have a grammar that I'd like to include multi-parameter functions in (like f(x,y)). I'm using AST output with my own tree parser. Right now my parameter list production is

paramdefs: (ID COMMA)* ID ;

This works fine, but the AST output for

z(x,y)=expression

is

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

(i.e. it's very flat).

The FUNC CommonTree's children, in the general case, are {function name, parameter, comma, parameter, defined expression}, for any number of parameters. I'd like the parameter list to be a single child and not have commas (this would make it easier to walk the tree).

Ideally, this is what the tree would look like:

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

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

Relevant associated areas of the grammar:

funcdef: ID  '(' paramdefs ')' '=' expr -> ^(FUNC ID paramdefs expr) ;

paramdefs: (ID COMMA)* ID ;
like image 630
Cajunluke Avatar asked Jul 11 '11 00:07

Cajunluke


People also ask

What grammar does ANTLR use?

ANTLR 2 accepts three types of grammar specifications -- parsers, lexers, and tree-parsers (also called tree-walkers). Because ANTLR 2 uses LL(k) analysis for all three grammar variants, the grammar specifications are similar, and the generated lexers and parsers behave similarly.

What is ANTLR Lexer?

A lexer (often called a scanner) breaks up an input stream of characters into vocabulary symbols for a parser, which applies a grammatical structure to that symbol stream.

What does ANTLR generate?

ANTLR (ANother Tool for Language Recognition) is a powerful parser generator for reading, processing, executing, or translating structured text or binary files.


1 Answers

To create a tree like this:

enter image description here

for the input z(x,y)=expr, do the following:

grammar ...

...

tokens {
  FUNC;
  PARAMS;
}

...

funcdef
  :  ID  '(' paramdefs ')' '=' expr   -> ^(FUNC ID paramdefs expr) 
  ;

paramdefs
  :  (ID COMMA)* ID                   -> ^(PARAMS ID+)
  ;
like image 156
Bart Kiers Avatar answered Sep 23 '22 03:09

Bart Kiers