You get the error because 'SELECT' will match everything in the input
till EOF greedily (because of .+) and obviously WS cannot match
anything. When you use .+, you have to turn off greedy. Try something
like:
TEXT : SELECT ( options {greedy=false;} : . )* FROM
This should work:
grammar Test;
r : TEXT
{
System.out.println( $TEXT.text );
}
;
TEXT : SELECT ( options {greedy=false;} : . )* FROM
{
String str =$text;
setText( str.substring(6, str.length()-4) );
}
;
fragment
SELECT : 'select'
;
fragment
FROM : 'from'
;
Cheers, Indhu
陈凯成 wrote:
Hi!
I hava to intercept a _expression_ between two string, and then send to
another application.
like a Java regex, for example:
//
-------------------------------------------------------------------------
// Java Code Begin
//
-------------------------------------------------------------------------
String regex = "select\\s+((.)+)\\s+from";
Pattern p = Pattern.complie(regex);
Matcher m = p.matcher("select stu.id as 'Student ID', stu.name as
'Student Name' from");
String select = null;
if(m.find()){
select = m.group(1);
}
System.out.println(select);// print [stu.id as 'Student ID', stu.name
as 'Student Name']
send(select);// send to another application.
//
-------------------------------------------------------------------------
// Java Code End
//
-------------------------------------------------------------------------
I try to write a sample grammar file, but it dosen't works.
//
-------------------------------------------------------------------------
// Grammar File Begin
//
-------------------------------------------------------------------------
start : 'select' WS SELECT WS 'from';
SELECT : (.)+ {
System.out.println(getText());
send(getText());
};
WS : ( '\t' | ' ' | '\r' | '\n'| '\u000C' )+;
//
-------------------------------------------------------------------------
// Grammar File End
//
-------------------------------------------------------------------------
I get a error message as follow when I generate code:
//
-------------------------------------------------------------------------
// Error Message Begin
//
-------------------------------------------------------------------------
[23:31:49] error(208): Sql.g:28:1: The following token definitions can
never be matched because prior tokens match the same input: WS
//
-------------------------------------------------------------------------
// Message Begin
//
-------------------------------------------------------------------------
Thanks a lot!
---------------------------------
THE
POWER
OF
JAVA
把MSN装进手机,更多聊天乐趣等你挖掘! 立刻下载!
List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "il-antlr-interest" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/il-antlr-interest?hl=en
-~----------~----~----~----~------~----~------~--~---
|
List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe:
http://www.antlr.org/mailman/options/antlr-interest/your-email-address