[antlr-interest] Using semantic predicates with antlr 3 and pythonruntime

Harald M. Müller harald_m_mueller at gmx.de
Fri Nov 30 14:48:45 PST 2007


Just one idea: Instead of distinguishing based on a semantic predicate,
duplicate your grammar (nt_... is "no-tick-...."):
 
-------------
 
method
    : methodcall
    | '`' nt_methodcall '`'
    ;
 
methodcall
    :
    |   methodname '(' expression ')' (expression)*
    |   methodname '(' expression (',' expression)+ ')' | 
    |   methodname (expression)+ |
    |   methodname ('('')')? 
    ;
 
expression
    : simpleexpression 
    | method
    ;
 
nt_methodcall
    :
    |   methodname '(' nt_expression ')' (nt_expression)*
    |   methodname '(' nt_expression (',' nt_expression)+ ')' | 
    |   methodname (nt_expression)+ |
    |   methodname ('('')')? 
    ;
 
nt_expression
    : simpleexpression 
    | nt_method
    ;
 
simpleexpression: flag | number | STRING ;
 
-------------
 
(I have removed your methodargs; and changed the methodsargs (methodargs)*
to (methodsargs)+ - that way, the grammar above is not so much longer than
yours ;-) - but you might have removed certain parts which were in those
separate rules/branches).
 
Of course, this is code duplication. But it certainly makes debugging and
adapting to that tiny difference of tick or not tick easier.
I must say that I like this "design-time nailing down" of a grammar - it is
akin to compile-time type safety which often (especially with generics) now
helps me to make a design correct the first time it compiles!
 
Regards
Harald M.
 



  _____  

From: antlr-interest-bounces at antlr.org
[mailto:antlr-interest-bounces at antlr.org] On Behalf Of Dan Bailey
Sent: Thursday, November 29, 2007 8:22 PM
To: antlr-interest at antlr.org
Subject: [antlr-interest] Using semantic predicates with antlr 3 and
pythonruntime


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/20071130/0bbc10be/attachment.html 


More information about the antlr-interest mailing list