이 난 나무에 부울 식을 구문 분석 쓴 마지막 Objective- C 코드 도움을 주셔서 감사합니다 그래서 :
가가의
A AND B OR C AND NOT(B)
과 같은 식을 필요가 형태 :
:
A.B + C./b
이 우선 순위가 구문 분석 괄호와 함께 작동 이 이름으로 묵시적으로 방법이 할
@interface TreeNode : NSObject{
NSMutableArray *children;
TreeNode *parent;
NSString *value;
}
+(TreeNode *)newTreeNodeWithValue:(NSString *)Value;
-(void)addChild:(TreeNode *)child;
다음의 TreeNode 개체 속성과 메소드를 가지고
-(TreeNode *)ParseStringIntoTree:(NSString *)InputString{ //input string to parse
//returns root-node of tree
TreeNode *first=[[TreeNode alloc] init];
TreeNode *current=first;
NSString *workingString = [NSString stringWithString:InputString];
if (([workingString characterAtIndex:0]=='(') && ([workingString characterAtIndex:workingString.length-1]==')')) {
NSRange boop={1,workingString.length-2};
workingString=[workingString substringWithRange:boop];
}
int brackCount=0;
bool plussesLeft=FALSE;
for (int pos=0; pos<workingString.length; pos++) {
char currentC=[workingString characterAtIndex:pos];
//1
if (currentC=='(') {
brackCount++;
}
//2
if (currentC==')') {
brackCount--;
}
if (currentC=='+' && brackCount==0){
plussesLeft=TRUE;
}
}
//############ PARSE plus signs with BRACKETS
brackCount=0;
int prevPlusPos=-1;
if (plussesLeft) {
for (int pos=0; pos<workingString.length; pos++) {
char currentC=[workingString characterAtIndex:pos];
//1
if (currentC=='(') {
brackCount++;
}
//2
if (currentC==')') {
brackCount--;
}
//3
if (currentC=='+'&&brackCount==0) {
NSRange boop={prevPlusPos+1, pos-prevPlusPos-1};
NSString *toParse=[workingString substringWithRange:boop];
TreeNode *child;
if(toParse.length>1){child=[self ParseStringIntoTree:toParse];}
else{child=[TreeNode newTreeNodeWithValue:toParse];}
[current addChild:child];
[current setValue:@"+"];
prevPlusPos=pos;
}
//4
if (pos==workingString.length-1 &&brackCount==0 && prevPlusPos!=-1) {
NSRange boop={prevPlusPos+1, pos-prevPlusPos};
NSString *toParse=[workingString substringWithRange:boop];
TreeNode *child;
if(toParse.length>1){child=[self ParseStringIntoTree:toParse];}
else{child=[TreeNode newTreeNodeWithValue:toParse];};
[current addChild:child];
[current setValue:@"+"];
}
}
}
//############ finish PARSE plus signs with BRACKETS
BOOL dotsLeft=FALSE;
for (int pos=0; pos<workingString.length; pos++) {
char currentC=[workingString characterAtIndex:pos];
//1
if (currentC=='(') {
brackCount++;
}
//2
if (currentC==')') {
brackCount--;
}
if (currentC=='.' && brackCount==0){
dotsLeft=TRUE;
}
}
int prevDotPos=-1;
if (!plussesLeft && dotsLeft) {
for (int pos=0; pos<workingString.length; pos++) {
char currentC=[workingString characterAtIndex:pos];
//1
if (currentC=='(') {
brackCount++;
}
//2
if (currentC==')') {
brackCount--;
}
//3
if (currentC=='.' && brackCount==0 && prevPlusPos==-1) {
NSRange boop={prevDotPos+1, pos-prevDotPos-1};
NSString *toParse=[workingString substringWithRange:boop];
TreeNode *child;
if(toParse.length>1){child=[self ParseStringIntoTree:toParse];}
else{child=[TreeNode newTreeNodeWithValue:toParse];}
[current addChild:child];
[current setValue:@"."];
prevDotPos=pos;
}
//4
if (pos==workingString.length-1 &&brackCount==0 && prevDotPos!=-1) {
NSRange boop={prevDotPos+1, pos-prevDotPos};
NSString *toParse=[workingString substringWithRange:boop];
TreeNode *child;
if(toParse.length>1){child=[self ParseStringIntoTree:toParse];}
else{child=[TreeNode newTreeNodeWithValue:toParse];};
[current addChild:child];
[current setValue:@"."];
}
}
//left with current being the
}
if (!plussesLeft && !dotsLeft) {
if ([workingString characterAtIndex:0]=='/') {
TreeNode *child=[self ParseStringIntoTree:[workingString substringFromIndex:1]];
[current addChild:child];
[current setValue:@"/"];
}
if (workingString.length==1) {
[current setValue:workingString];
}
}
return first;
}
. 이것은 나중에 다른 사람을 도울 수 있습니다. 파서를 부울 대수 또는 특히 다른 파서의 기초로 사용하는 것이 좋습니다.
+1 문자열이 올바른 형식이라는 것을 보증 할 수 있다면 RD 파서를 작성하는 것이 대부분 사소할 수 있습니다. –
그 덕분에, 위키 피 디아 항목을 살펴 봤지만 파서를 더 정확하게 작성하는 방법, 팁에 대해 여전히 헷갈 렸습니다. – Tawfiqh
@Tawfiqh : 파서를 생성하는 파서 생성기를 사용하십시오. 나는 단순하기 때문에 [레몬] (http://www.hwaci.com/sw/lemon/lemon.html)을 제안 할 것이다. – georg