[antlr-interest] lexer semantic predicate

Philippe Frankson Philippe.Frankson at Frsglobal.com
Thu Feb 16 15:28:47 PST 2012


My syntax was not correct for semantic predicate (forgot the '=>', see
below the correction) but still one remaining problem.

NAME		: ({!inTSDName && !inWCName}?=> ALPHA (ALPHA | INT)*) 
		| ({inTSDName}?=> TSDFIRST (TSDNEXT)*) 
		| ({inWCName}?=> WCNAMEFIRST (WCNAMENEXT)*);

The token NAME is returned for each of the following lexer rules :

EMPTY		: E M P T Y;
IF		: I F;
KEY		: K E Y;
SEQNUM	: S E Q U E N C E N U M B E R;
SUM		: S U M;
UPPER		: U P P E R;
LOWER		: L O W E R;
PROPER	: P R O P E R;
ROUND		: R O U N D;
FIXED		: F I X E D;
PAGENUMBER	: P A G E N U M B E R;

As those rules are defined before the NAME rule, I would expect the
appropriate token is returned for each of those.

I don't understand what I'm doing wrong.

Thank you for any help
Philippe
 

-----Original Message-----
From: antlr-interest-bounces at antlr.org
[mailto:antlr-interest-bounces at antlr.org] On Behalf Of Philippe Frankson
Sent: 16 February 2012 12:31
To: antlr-interest at antlr.org
Subject: [antlr-interest] lexer semantic predicate

Hi,

I'm using ANTLR 3.4
Here attached, the grammar file (light version).
I would like the lexer to return the same token for 3 specific cases:
{NAME1}<<NAME2>>[NAME3|NAME3]...
So, when I am inside '{ }', I have a work collection name. When I am
inside '<< >>', I have a report name (called TSD name), otherwise I have
another name.
The possible characters for each of those is different so I wanted to
use the semantic predicate to deal with it.
Here is the lexer rule that is supposed to return the token 'NAME':

NAME		: ({!inTSDName && !inWCName}? ALPHA (ALPHA | INT)*) |
({inTSDName}? TSDFIRST (TSDNEXT)*) | ({inWCName}? WCNAMEFIRST
(WCNAMENEXT)*);

And to initialize the variables I have the following lexer rules:
SST @init {inTSDName = true;}		
		: '<<';
		
LLT @init {inTSDName = false;}
		: '>>';
		
OCB @init {inWCName = true;}
		: '{';

CCB @init {inWCName = false;}
		: '}';
 

If I test the grammar with the following rule:
sum({mywc}<<myreport>>[row1,row2..row10|col1,col2,col3]) > 0.5 + .2

Question 1.
I notice that the first returned token is NAME and not SUM like expected
despite the fact that SUM is defined before. Why so ? How to fix that
problem ?

Question 2.
While debugging , it looks like it enters in an infinite loop. I suspect
it is related to the 'NAME' rule. Can you help me solving that issue ?

Thanks a lot.
Philippe


-----Original Message-----
From: antlr-interest-bounces at antlr.org
[mailto:antlr-interest-bounces at antlr.org] On Behalf Of Jim Idle
Sent: 08 February 2012 19:54
To: antlr-interest at antlr.org
Subject: Re: [antlr-interest] newbi question : operator + any

Did you see my explanation of why you must use:

ANYTHING : . ;

And not use + or * with the . ?

Jim

> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest- 
> bounces at antlr.org] On Behalf Of Marco Laponder
> Sent: Tuesday, February 07, 2012 10:58 PM
> To: antlr-interest at antlr.org
> Subject: Re: [antlr-interest] newbi question : operator + any
>
> Lets explain what I am trying to accomplish, maybe you can provide me 
> the most impressive grammars :-)
>
>
>
> I am trying to parse a filter string which filter on a column of a 
> table, so I need to know the operator. In my example I stated the '>'
> but of course I would like to expand it to '<','>=' etc and alter on 
> even add parenthesis, 'and' and 'or' etc. But I would like to start 
> simple as I am new at ANTLR.
>
>
>
> Your sample does the trick, however looks hard to maintain, e.g., when

> I add the '<' to COMPARE I also need to add it to ANYTHING. (Well it 
> seems still more maintainable than a single regular expression no one 
> understands to do the trick ;-) )
>
>
>
> Kind regards,
> Marco
>
>
>
> ________________________________
>
> Van: Marco Laponder
> Verzonden: dinsdag 7 februari 2012 8:45
> Aan: 'Gokulakannan Somasundaram'
> CC: antlr-interest at antlr.org
> Onderwerp: RE: [antlr-interest] newbi question : operator + any
>
>
>
> This still gives me the same error, any other ideas ? Of is antlr just

> not appropriate for this ?
>
>
>
> Kind regards,
>
> Marco
>
>
>
> ________________________________
>
> Van: Gokulakannan Somasundaram [mailto:gokul007 at gmail.com]
> Verzonden: maandag 6 februari 2012 16:56
> Aan: Marco Laponder
> CC: antlr-interest at antlr.org
> Onderwerp: Re: [antlr-interest] newbi question : operator + any
>
>
>
> COMPARE : '>';
>
> ANYTHING : (~'>')(.)*;
>
> testExpression : COMPARE ANYTHING;
>
>
>
>
>
> Try this.
>
>
>
> Gokul.
>
> On Mon, Feb 6, 2012 at 8:30 PM, Marco Laponder 
> <marco.laponder at kewill.com> wrote:
>
> Hi All,
>
>
>
> I am trying to setup a very simple grammar, ik should be able to
handle
>
>
>
> "> thiscouldbeanything"
>
>
>
> So I tried:
>
>
>
> COMPARE : '>';
>
> ANYTHING : (.)+;
>
> testExpression : COMPARE ANYTHING;
>
>
>
> but when generating java resources it gave me the error:
>
>
>
> "The following alternatives can never be matched: 1"
>
>
>
> The problems seems to be the ANYTHING, so how should I define a
grammar
> to be able to handle this ?
>
>
>
> Kind regards,
>
> Marco
>
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe:
> http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>
>
>
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-
> email-address

List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe:
http://www.antlr.org/mailman/options/antlr-interest/your-email-address


More information about the antlr-interest mailing list