Function getText() doesn't work properly

42 views
Skip to first unread message

Karolina

unread,
Jul 20, 2015, 8:02:54 AM7/20/15
to antlr-di...@googlegroups.com
Hello,
I have the following grammar file for ANTLR 4.5.1:

grammar Daty;
@lexer::members
{
private String day;
private String month;
   
private String tail(String str, Integer len) {
   
return str.substring(str.length() - len, str.length());
}
private void after_DD() {
   
System.out.println(getText());
    day
= tail(getText(), 2);
}
private void after_JUNE() {
    month
= "06";
}
}

start
: d+;

d
: (D1);
D1
: JUNE{after_JUNE();}' 'DD{after_DD();}', ';

WS
: .+? -> skip;
fragment DD
: '01'|'02'|'03'|'04'|'05'|'06'|'07'|'08'|'09'|'10'|
             
'11'|'12'|'13'|'14'|'15'|'16'|'17'|'18'|'19'|'20'|
             
'21'|'22'|'23'|'24'|'25'|'26'|'27'|'28'|'29'|'30'|
             
'31';
fragment JUNE
:  'czerwiec'|'czerwca';

And I'm trying to parse the following string:

czerwiec 22,  czerwca 22,

In function after_DD() the getText() returns:

czerwiec 22
czerwca
22,


Why function works properly the first time, and the second no longer?
Thanks,
K.

Jim Idle

unread,
Jul 20, 2015, 8:28:33 AM7/20/15
to antlr-di...@googlegroups.com
Because you have hard coded a space after the comma. You should probably not code the spaces and comma in to the token. 

Jim
--
You received this message because you are subscribed to the Google Groups "antlr-discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to antlr-discussi...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Karolina

unread,
Jul 20, 2015, 8:58:37 AM7/20/15
to antlr-di...@googlegroups.com
Thank you for your response.
But what if I have (with no space and comma):
D1 : JUNE{after_JUNE();}DD{after_DD();}'txt';

And I'm trying to parse the following string:
 czerwiec22txt czerwca22txt



And the getText() returns:
czerwiec22
czerwca22t


K.

Jim Idle

unread,
Jul 21, 2015, 1:13:00 AM7/21/15
to antlr-di...@googlegroups.com
Well, your rule isn't going to match, you are probably getting lexer errors? Are you using the interpreter or compiling and running it?

parse: JUNE DD  EOF;

JUNE: ... ;
DD: ... ;
WS: (' ' | '\t')+ -> skip() ;
FLUFF: . -> skip();

Don't try to code structure in the lexer basically.

Jim


On Mon, Jul 20, 2015 at 8:58 PM, Karolina <kk.s...@gmail.com> wrote:
Thank you for your response.
But what if I have (with no space and comma):
D1 : JUNE{after_JUNE();}DD{after_DD();}'txt';

And the getText() returns:
czerwiec22
czerwca22t


K.

--

Karolina

unread,
Jul 21, 2015, 5:34:59 AM7/21/15
to antlr-di...@googlegroups.com
I'm not getting lexer errors and my rule is going to match.
I changed some code:
private void before(){
day=month="";
}
private
void after(){
    setText
(day + '-' + month);
}

start
: d+;
d
: (D1) {System.out.println(_ctx.getText());} ;
D1
: {before();}JUNE{after_JUNE();}DD{after_DD();}'txt'{after();};


And I'm getting:
22-06
2t-06

In both cases, the rule has been matched. The second time I got the wrong result.

K.
Reply all
Reply to author
Forward
0 new messages