Parsing a string, Grammar file.
Posted
by defn
on Stack Overflow
See other posts from Stack Overflow
or by defn
Published on 2010-03-09T03:35:06Z
Indexed on
2010/03/09
3:51 UTC
Read the original article
Hit count: 429
How would I separate the below string into its parts. What I need to separate is each < Word > including the angle brackets from the rest of the string. So in the below case I would end up with several strings 1. "I have to break up with you because " 2. "< reason >" (without the spaces) 3. " . But Let's still " 4. "< disclaimer >" 5. " ."
I have to break up with you because <reason> . But let's still <disclaimer> .
below is what I currently have (its ugly...)
boolean complete = false;
int begin = 0;
int end = 0;
while (complete == false) {
if (s.charAt(end) == '<'){
stack.add(new Terminal(s.substring(begin, end)));
begin = end;
} else if (s.charAt(end) == '>') {
stack.add(new NonTerminal(s.substring(begin, end)));
begin = end;
end++;
} else if (end == s.length()){
if (isTerminal(getSubstring(s, begin, end))){
stack.add(new Terminal(s.substring(begin, end)));
} else {
stack.add(new NonTerminal(s.substring(begin, end)));
}
complete = true;
}
end++;
© Stack Overflow or respective owner