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

Jim Idle jimi at temporal-wave.com
Wed Nov 4 08:41:15 PST 2009


It looks like you are trying to produce a grammar from some kind of normative spec and have made the classic mistake of trying to include context in the expression of the grammar. The problem is that such specs are not written for writing parsers with but more of an explanation of how things go together in general.

 

So, you don't want rules like alphanumerical_expression and so on, you just want expression, and you must make your expression rule left factored and organized for LL() parsers, not just typed in like below. You can steal the expression rule tree from any of the sample grammars (choose one that might be close already), such as java or C.

 

Basically though, you are not going to get very far starting from the grammar below. What is it you are trying to parse?

 

Jim

 

 

From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-bounces at antlr.org] On Behalf Of Thomas Dill
Sent: Wednesday, November 04, 2009 8:33 AM
To: antlr-interest at antlr.org
Subject: [antlr-interest] Define "function" as numerical and alphanumerical expression

 

Hi!

 

I need help for the following problem:

 

I think the best way to describe it, is to show you what I tried. This produces a bunch of errors. 

 

expression                     :    conditional_expression | numerical_expression | alphanumerical_expression | function | data_field ;

conditional_expression         :    'IF' arguments 'THEN' value ('ELSE' (conditional_expression | value))? ;

arguments                      :    argument  (('OR' | 'AND') argument)* |

'(' arguments ')' ;

Argument                      :    value relational_operator value |

                                    (NOT? value IN) ('(' (SingleStringLiteral | NumericLiteral) (COMMA (SingleStringLiteral | NumericLiteral))* ')' | FILE OBJNAME)  ;

relational_operator            :    RelOp | alphanumerical_relational_op | numerical_relational_op ;

alphanumerical_relational_op   :    ARelOp ;

numerical_relational_op        :    NRelOp numerical_expression  'TO' ;

value                          :    numerical_expression  | alphanumerical_expression | function | data_field;

function                       :    FUNCTION function_argument ')';

function_argument              :    (numerical_expression  | alphanumerical_expression | function | data_field) (COLON numerical_expression  | alphanumerical_expression | function | data_field)*;

alphanumerical_expression      :    (SingleStringLiteral) (chrOp (SingleStringLiteral))+;

numerical_expression           :    (numExprA) (NumOp (numExprA))+ |

'('numerical_expression  ')' ;

numExprA                       :    NumericLiteral; 

data_field                     :    PREOP? PREFIX? OBJNAME

 

PREOP                          :     ('AVE'|'MAX'|'MIN'|'FST'|'TOT'|'CNT'|'SUM'|'ALL'|'ST'|'CT'|'ASQ'|'PCT'|'RPCT'|'SEG'|'DST')'.';     

PREFIX                         :    OBJNAME '.';    

FUNCTION                       :    OBJNAME '(';

OBJNAME                        :    ('A'..'Z') ('A'..'Z'|'0'..'9'|'_')*;

NumOp                          :    '**' | '*' | '+' | '-' | '/' | 'AND' | 'OR';     

ChrOp                          :    '|' | '||' ;

RelOp                         :     'EQ'|'IS'|'ISNOT'|'ISFROM'|'EXCEEDS'|'NE'|'GE'|'GT'|'LT'|'LE'|'IS MISSING'|'ISNOT MISSING'  ;

NRelOp                         :    'FROM'|'ISFROM'|'NOTFROM';

ARelOp                         :    'CONTAINS'|'OMITS'|'INCLUDES'|'EXCLUDES'|'NOT LIKE'|'LIKE';

fragment StringLiteral         :    '"' ~('"')* '"' | SingleStringLiteral ;

SingleStringLiteral            :    '\'' ~('\'')* '\''   ;

NumericLiteral                 :    DecimalLiteral | IntegerLiteral ;

fragment IntegerLiteral        :    DecimalDigit+;

fragment DecimalLiteral        :    DecimalDigit+ '.' DecimalDigit* | '.' DecimalDigit+ ;

fragment DecimalDigit          :    ('0'..'9') ;

 

I really don't know how to describe the problem but I urgently need some help.

 

Best regards,

Tom



-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20091104/fda2b64c/attachment.html 


More information about the antlr-interest mailing list