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

Thomas Dill thomas.dill at teleport.ch
Wed Nov 4 23:51:57 PST 2009


Hi Jim and David-Sarah Hopwood,

 

Thanks for your replies. 

 

@Jim: I'm trying to parse a WebFOCUS report to get its meta data and
automatically make a documentation of the report. That's why I have to get
as much of the information from an expression as possible.  

 

@David-Sarah Hopwood: I first have to study your answers, but I think they
are going to be very helpful. I forgot to write in my question, that the
ANTLR code I sent was just an extract and "simplified" (or not. :o) ). There
are rules for all the missing tokens and there are lexer rules for all
quoted tokens. Thank you as well for the "How To Ask Questions The Smart
Way" link.  Not read it yet, but I'm going to.

 

Best regards,

Tom

 

From: antlr-interest-bounces at antlr.org
[mailto:antlr-interest-bounces at antlr.org] On Behalf Of Jim Idle
Sent: Mittwoch, 4. November 2009 17:41
To: antlr-interest at antlr.org
Subject: Re: [antlr-interest] Define "function" as numerical and
alphanumerical expression

 

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/20091105/b9b4ea5d/attachment.html 


More information about the antlr-interest mailing list