c_cpp 一种解析调用pantry解析的新方法,可用于解析非嵌套语言。
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 一种解析调用pantry解析的新方法,可用于解析非嵌套语言。相关的知识,希望对你有一定的参考价值。
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
/*Pantry Parsing*/
//implements pantry parsing, a parsing which works for any order of operators, prefix, infix or postfix.
#define ADV_WS(chPtr) while(isspace(*chPtr)) chPtr++
#define IS_NUM_START(chPtr) (isdigit(*chPtr) || *chPtr == '-' && isdigit(chPtr[1]))
//initalizer for parse result struct
#define INIT_OPEVENT(opeve) do { \
opeve.first = 0; \
opeve.second = 0; \
opeve.op = 0; \
} while (0)
//quickly determines if chPtr is at operator
#define PRS_OPERATOR(chPtr, opVar) switch(*chPtr) { \
case '+': \
opVar = Operator_Add; \
chPtr++; \
break; \
case '-': \
if(chPtr[1] == '>') { \
opVar = Operator_Arrow; \
chPtr += 2; \
} \
else { \
opVar = Operator_Sub; \
chPtr++; \
} \
break; \
case '*': \
opVar = Operator_Mul; \
chPtr++; \
break; \
default: \
opVar = Operator_Null; \
chPtr++; \
}
//main overhead parsing macro
// Must use or logical so not done till all 3 are done
#define PRS_EVENT(opeve ,chPtr) while(!opeve.first || !opeve.second || !opeve.op) { \
ADV_WS(chPtr); \
if(isdigit(*chPtr)) { \
if(!opeve.first) opeve.first = strtol(chPtr, &chPtr, 10);\
else if(!opeve.second) opeve.second = strtol(chPtr, &chPtr, 10);\
} \
if(!opeve.op) PRS_OPERATOR(chPtr, opeve.op);\
}
struct Reader
{
char* begin;
char* end;
};
typedef struct Reader Reader;
enum Operator
{
Operator_Null,
Operator_Add,
Operator_Sub,
Operator_Mul,
Operator_Arrow //more than 1 char
};
typedef enum Operator Operator;
//struct to hold pantry parse results
struct OpEvent
{
long first;
long second;
Operator op;
};
typedef struct OpEvent OpEvent;
int main(int argc, char const *argv[])
{
OpEvent tester;
INIT_OPEVENT(tester);
char* code = " 33333 -> 55 88 -> *";
PRS_EVENT(tester, code);
printf(" code is '%s', op is %d, first is %ld, second is %ld\n", code, tester.op, tester.first, tester.second);
以上是关于c_cpp 一种解析调用pantry解析的新方法,可用于解析非嵌套语言。的主要内容,如果未能解决你的问题,请参考以下文章
颤振 - 在 null 上调用了方法“[]”(解析 json)
c_cpp NMEA格式解析
c_cpp 上行报文解析
c_cpp MFC解析命令行
ajax异步传输之深入解析
c_cpp OpenCV构建信息解析器