[antlr-interest] Advice on effective expr parsing

Remi Koutcherawy remi.koutcherawy at wanadoo.fr
Wed Aug 28 13:12:26 PDT 2002


Hi

> Yes, using syntax predicate this can be handled as [...]
> But I afraid this is NOT effective way.
> Because expr in general case can be VERY LONG and COMPLEX.

You can also try to evaluate the syntactic predicate with a special rule designed only
to distinguish between the two alternatives :

row_expr
     :    (LPAREN is_row_list_element) =>
           LPAREN row_list_element ( COMMA row_list_element )* RPAREN
    |    row_list_element
     ;

// To illustrate I consider an row_list_element to be anything followed by a COMMA
// May not be adequate for your grammar
is_row_list_element
     : (~(RPAREN | LPAREN | COMMA))+ COMMA
     | LPAREN is_row_list_element RPAREN;

> So I wonder does exists any "secret" "cool" trick to resolve this problem in
> row_expr ?

You may have a look at http://www.webgain.com/products/java_cc/lookahead.html
5. SYNTACTIC LOOKAHEAD

Here is a short extract  (the syntax is different but not too far):

void TypeDeclaration() :{}
 {
   LOOKAHEAD(ClassDeclaration())
   ClassDeclaration()
 |
   InterfaceDeclaration()
 }

The problem with the above syntactic LOOKAHEAD specification is that
the LOOKAHEAD calculation takes too much time and does a lot of
unnecessary checking.  In this case, the LOOKAHEAD calculation can
stop as soon as the token "class" is encountered, but the
specification forces the calculation to continue until the end of the
class declaration has been reached - which is rather time consuming.
This problem can be solved by placing a shorter expansion to try out
in the syntactic LOOKAHEAD specification as in the following example:

 void TypeDeclaration() :
 {}
 {
   LOOKAHEAD( ( "abstract" | "final" | "public" )* "class" )
   ClassDeclaration()
 |
   InterfaceDeclaration()
 }

Hope this help
Rémi Koutchérawy

P.S.
I first used Antlr then switched to JavaCC, waiting for Antlr 2.8.
JavaCC is easier especially for the lexer which is LR (no need to left factor)





 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 



More information about the antlr-interest mailing list