need some expert help here!
can anyone help me to complete this code?
Code:
typedef enum {
NUMBER,
BOOL,
ERROR} token_type;
typedef struct {
token_type type;
union {
int int_val;
int bool_val;
char* error_message;
};
} token;
typedef struct {
const char* data;
int len;
} substring;
static substring trim(substring str) {
substring result;
int i;
result.len=0;
for(i=0;i<str.len;i++) {
if(str.data[i]!=' ') {
result.data = str.data+i;
result.len = str.len-i;
break;
}
}
for(i=result.len-1;i>=0;i--) {
if(result.data[i]!=' ') {
result.len = i+1;
break;
}
}
return result;
}
/**
* Return -1 if matching bracket isn't found
*/
int matching_braket(substring str, int open_pos) {
int depth = 0;
int i;
for(i=open_pos;i<str.len;i++) {
if(str.data[i]=='(') depth++;
if(str.data[i]==')') depth--;
if(depth==0) return i;
if(depth<0) return -1;
}
return -1;
}
token fold(substring str) {
str = trim(str);
if(str.data[0]=='(') {
int right_pos = matching_bracket(str,0);
if(right_pos==str.len-1) {
// it's likely BOOL_EXPR ::= '(' BOOL_EXPR ')' rule
substring subtask;
subtask.data = str.data+1;
subtask.len = str.len-2;
return token_fold(subtask);
}
// ...
}
// ... and so on
}__________________
the output screen is something like this :-
Prompt: (1 = = 1) && (2 != 1) || (2 <= 3)
Result: true
Prompt: 1 > 2 || 2 >= 2 || 3 > 2
Result: true
Prompt: (2 > 3 && 5 = = 2) || (2 > 3)
Result: false
Prompt: (1 > 2) > (2 = = 2)
Result: Syntax error: nonnegative number expected as operand for >
Prompt: 1 && 2 || 3
Result: Syntax error: Boolean expression expected as operand for &&
Result: Syntax error: Boolean expression expected as operand for ||
Prompt: ( 2 > 3 && 4 < 3
Result: Syntax error: ) missing