I am creating a lexical from scratch and I am getting into the part of matching (")[\\w]+(").
I have this regular expression ^(\")[\\w]+(\")$, but it won't catch the string.
SSCCE:
Map<String, String> lexicalMap = new HashMap<>();
// add all regex to `lexicalMap` via `lexicalMap.put([regex], [tokentype])`
// Tokenize the string format of the syntax to `List<String> tokens`
// List<String> tokens contains ["string", "data", "=", "test"] on the syntax: string data = "test"
for(String element : tokens) {
for(String regex : lexicalMap.keySet()) {
if(element.matches(regex))
System.out.print(lexicalMap.get(regex) + " ");
}
}
System.out.println();
REGEXs:
identifier = ^[\\w]+$
operator = ^(\\=)$
string = ^(\")[\\w]+(\")$ // THE PROBLEM
keyword = ^(string)$
Here is the case input/ouput I am following:
INPUT:
"test"
""
test
string data = "test"
OUTPUT:
string
string
identifier
keyword identifier operator string
UPDATED: 02/22/2013
- Added SSCCE segment.
["string", "data", "=", "\"test\""]``. Note how I stored"test"`\"data\"is not a string. If it would have been like -"\"data\"", then that is a string. And that would be stored like -\"\\\"data\\\"\". You first need to be sure of what all kinds of input you are getting?keywordis also a valid identifier. So, it will match two regexes inMap. And I would say, you should not match a keyword with regex. Since keywords in Java are fixed, so better to have aSetof all those keywords, and match against that set.