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 ;
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.
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.
ANTLR (ANother Tool for Language Recognition) is a powerful parser generator for reading, processing, executing, or translating structured text or binary files.
To create a tree like this:
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+)
;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With