[antlr-interest] Using semantic predicates with antlr 3 and python runtime
Dan Bailey
dannybe at gmail.com
Thu Nov 29 11:21:44 PST 2007
Hi,
I am writing a parser for mel (a language based on tcl) and am having a lot
of problems getting the intricacies of the language to parse.
One of the main problems I am having is through using the semantic
predicates, both to make the grammar easier to read and to make a few of the
statements conditional on variables.
Firstly, if someone could explain the difference between semantic predicates
and gated semantic predicates that would be useful?
One of the errors I have come across is when passing variables around as
arguments, antlr does not pass the variables into the syntactic predicates
it generates for some reason (the synpred1() type functions). However, I
have found that I can edit this file by hand and add in the variables to the
arguments manually, but clearly this is laborious and time-consuming.
Here is a simple example of some mel code which I have had to resort to
semantic predicates in order to try and get round:
(1) function;
(2) function -flag;
(3) function("arg");
(4) function(3+4);
(5) function (3+4) -flag;
(6) $a = `function`;
(7) $a = `function(function2)`;
(8) $a = `function -flag`;
(9) function(`function2 -flag`);
All of the above commands are completed valid, which demonstrates the
variety in the language in function calls alone.
This is the solution I have come up with in antlr grammar:
method[ticks]:
{ ticks == "ticksallowed" }? => '`' methodcall["ticksnotallowed"]
'`' |
methodcall[ticks]
;
methodcall[ticks]:
methodname '(' expression[ticks] ')' (methodargs[ticks])* |
methodname '(' expression[ticks] (',' expression[ticks])+ ')' |
methodname methodargs[ticks] (methodargs[ticks])* |
methodname ('('')')?
;
methodargs[ticks]: expression[ticks];
expression[ticks]: flag | number | STRING | method[ticks];
I have tried to simplify this to demonstrate the problem. In (6) for
example, in order to match the second backtick to an ending tick (rather
than the start of a new backticked method call which is clearly not
allowed), I pass around a variable to halt the parser from trying to execute
the backtick strand if it is already inside a backtick.
I have had many problems trying to get this to work though. Am I going about
this in the correct manor, or can someone suggest a better approach to this?
Thanks,
Dan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20071129/06287959/attachment.html
More information about the antlr-interest
mailing list