I want to select the word "String"
from the line "String helloString String Stringhello helloStringhello"
.
Here should selected the 2 words "
String
"(first and the middle)"
String
" in "helloString
" or "Stringhello
" or "helloStringhello
" shouldn't be selected.
This is my RE:
<YYINITIAL> (String) {return new Token(TokenType.String,yytext());}
But it select any word "String".
My Jlex code:
import java.io.*;
enum TokenType {Type_String,Identifier}
class Token{
String text;
TokenType type;
Token(TokenType type,String text)
{
this.text=text;
this.type=type;
}
public String toString()
{
return String.format("[%s,%s]",type,text);
}
}
%%
%class Lexer
%public
%function getNextToken
%type Token
%{
public static void main(String[] args) throws IOException {
FileReader r = new FileReader("in.txt");
Lexer l = new Lexer(r);
Token tok;
while((tok=l.getNextToken())!=null){
System.out.println(tok);
}
r.close();
}
%}
%line
%char
SPACE=[\r\t\n\f\ ]
ALPHA=[a-zA-Z]
DIGIT=[0-9]
ID=({ALPHA}|_)({ALPHA}|{DIGIT}|_)*
%%
<YYINITIAL> {ID} {return new Token(TokenType.Identifier,yytext());}
<YYINITIAL> (String) {return new Token(TokenType.Type_String,yytext());}
<YYINITIAL> {SPACE}* {}
<YYINITIAL> . {System.out.println("error - "+yytext());}