[antlr-interest] Pre parse keyworded function calls into canonical function calls

Ben Corne ben.corne at gmail.com
Fri Aug 12 06:22:21 PDT 2011


Hello ANTLR users

I'm wondering if it's possible to do two translation passes using ANTLR for
writing a parser for a language in which both keyworded function
calls/definitions as canonical function calls are possible.
Appart from the syntax, both calls are semantically equal to eachother.

example:
is: 1 + 2 equal: 3;
is:equal:(1+2, 3);

The reason why I want to translate the keyworded function calls into
canonical is to separate language-specific code to convert keyworded calls
to their canonical representation from the grammar, to make porting to a
different backend easier. Also, introducing the keyworded style to my
grammar introduces some ambiguities that are inherrent to unbounded keyword
expressions.

What I'd like to know is wether the folowing is a good idea or does it sound
like I'm not seeing a more obvious approach?
The idea to achieve this is having 2 parser passes:

1) Translate keyworded occurences from the original input to their canonical
representations (using the rule: always try to form the longest possible
keyword chain)
prog
  : (KEYWORD) => keyword prog*
  | ~KEYWORD prog*
  ;
keyword
  : (kws+=singlekeyword)+
  ;
single_keyword
  : key=KEYWORD arg=(~KEYWORD*)
  ;

KEYWORD
  : ( 'A'..'Z' | 'a'..'z' ) ':'
  ;

do some magic to rewrite foo: x bar: y into foo:bar:(x,y);

2) Run the regular language grammar over the modified input from the first
pass.

Kind Regards
Ben Corne.


More information about the antlr-interest mailing list