[antlr-interest] how to match any char between two string?
Kevin Chen
bestgembler at hotmail.com
Sun May 17 19:13:51 PDT 2009
Thanks for you reply!
In that case, it work. But it isn't enough.
I consider a grammar:
select [ any character (an expr of an another app) ] from [ any character (an expr of an another app) ] //required
where [ any character (an expr of an another app) ] //optional element
order by [ any character (an expr of an another app) ] //optional element
group by [ any character (an expr of an another app) ] //optional element
limit [ any character (an expr of an another app) ] //optional element
option [key1 = value1 [,key2 = value2 ...] ] //optional element, if any, require more then one key-value-pair
for example:
1.
SELECT *, INTERVAL(posted,NOW()-7*86400,NOW()-86400) AS timeseg,
AVG(price) AS avgprice, COUNT(DISTINCT storeid), @ID, @weight
from one , TWO, three
where MATCH( '"test doc"/3'| '@@title hello' )
OPTION one = 1,
two = 2
2.
SELECT *, INTERVAL(posted,NOW()-7*86400,NOW()-86400) AS timeseg,
AVG(price) AS avgprice, COUNT(DISTINCT storeid), @ID, @weight
from one , TWO, three
where MATCH( '"test doc"/3'| '@@title hello' )
order by timeseg DESC, @weight DESC
group by @count
limit 100 + @count
OPTION three = 3,
four = 4
I don't want to analyze those expressions,
'cause there's an another application will analyze those expressions.
The point is I don't know how to get the "any character"s...
Thanks a lot ! :-)
THE
POWER
OF
JAVA
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
_________________________________________________________________
Windows Live™: Keep your life in sync. Check it out!
http://windowslive.com/explore?ocid=TXT_TAGLM_WL_t1_allup_explore_012009
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20090518/dfcee194/attachment.html
More information about the antlr-interest
mailing list