[antlr-interest] Define "function" as numerical and alphanumerical expression

David-Sarah Hopwood david-sarah at jacaranda.org
Wed Nov 4 21:43:28 PST 2009


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

David-Sarah Hopwood wrote:
>    There are a couple of more complicated ambiguities in this grammar
>    surrounding argument lists. [...]

Actually <arguments> specifies the condition of an 'if' expression;
the name misled me slightly.

Anyway, I see now what underlying problem is causing these ambiguities
(and also why the numerical_expression seems wrong). The grammar is
trying to maintain a type distinction between numeric expressions, and
boolean expressions. But in doing so, it doesn't allow the conventional
use of parenthesis around any subexpression.

If the language does in fact allow boolean expressions to be
parenthesized, e.g. as in

  NOT ((1+2) < 3 AND (NOT 4 < 5))

then that's not compatible with maintaining the numeric/boolean type
distinction in the grammar (because what is after a '(' can be an
expression of either boolean or numeric type).

My advice would be to use a conventional operator precedence grammar,
as below (now that I've spent the time to fix the grammar I might as
well give you this part of it), and check types after parsing.


grammar Example;

start                        : expression EOF ;

expression                   : conditional_expression
                             | logical_expression ;

conditional_expression       : IF expression THEN logical_expression
                                 (ELSE expression)? ;

logical_expression           : not_expression (logical_op not_expression)* ;

not_expression               : NOT* relational_expression ;

relational_expression        : value_expression
                                 ( relational_op value_expression
                                 | IN in_clause ) ;

value_expression             : numerical_expression
                             | alphanumerical_expression
                             | function_expression
                             | data_field;

numerical_expression         : primary_expression
                                 (numOp primary_expression)* ;

primary_expression           : NumericLiteral
                             | LPAREN expression RPAREN ;

in_clause                    : LPAREN literal (COMMA literal)* RPAREN
                             | FILE OBJNAME ;

literal                      : StringLiteral | NumericLiteral ;

logical_op                   : OR
                             | AND ;

relational_op                : relOp
                             | aRelOp
                             | nRelOp expression TO ;

function_expression          : OBJNAME LPAREN function_arguments RPAREN ;

function_arguments           : expression (COLON expression)* ;

- --
David-Sarah Hopwood  ⚥  http://davidsarah.livejournal.com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.12 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iF4EAREIAAYFAkryZfoACgkQWUc8YzyzqAfAngD/bANqb0/k9sIWDXeTgID4qAi5
8YtdKjn2VbEYsxW40z8A/i8gD0f6gk0YSrG2iUOOLJQFx6M+8u2TjYq4XJhYGmgV
=+SIX
-----END PGP SIGNATURE-----


More information about the antlr-interest mailing list