user-0 提问: 超长算法表达式的解析

回复:
user: user-1
content: 引用 10 楼 dingxz105090 的回复:[quote=引用 8 楼 my_live_123 的回复:] LZ是想语法解析呢，还是想得到最后结果呢？ 当然是要得到最后的结果，但是要得到结果就得解析语法先，不然这只是文本串，怎么能直接加到【int a = 】的后面呢   你这代码是YACC 还是 ANTLR 还是 TCC 啊[/quote] 放到int a=后为了正确语法分析，构成一个完整的定义和初始化语句。如果不要int，那么必须放到函数体内。C是结构化语言，函数外除了初始化赋值外，不能有表达式。一般编译器会给出"data definition has no type or storage class"警告。放到函数内，必须有个简单的函数体，因此归约量会增加不少。  没有使用工具，自己写的LR语法分析程序

user: user-0
content: 引用 8 楼 my_live_123 的回复:LZ是想语法解析呢，还是想得到最后结果呢？ 当然是要得到最后的结果，但是要得到结果就得解析语法先，不然这只是文本串，怎么能直接加到【int a = 】的后面呢   你这代码是YACC 还是 ANTLR 还是 TCC 啊

user: user-1
content: LZ是想语法解析呢，还是想得到最后结果呢？

user: user-2
content: 引用 6 楼 dingxz105090 的回复:[quote=引用 5 楼 zhao4zhong1 的回复:] 参考TCC 老赵你这不按常理出牌啊，你不是应该来一句“仅供参考”然后贴一大段代码的吗？  不过我这边也有点眉目了，可以做出来了[/quote] 我预言，未来某一天就算你自认为做出来了，再过不了多长时间，你还得去参考TCC。

user: user-1
content: int a = (((53078923 == 53086475) && ((53088651 == 53176715) || (53088651 == 53189771)) && (Date >= 201203) && (Date <= 201302)) || ((53078923 == 53086475) && (53088651 == 45417463051) && (Date <= 201302)) || ((53078923 == 53086475) && ((53088651 == 53226763) || (53088651 == 53144075) || (53088651 == 53205003) || (53088651 == 53170187) || (53088651 == 53117963)) && (Date >= 201109) && (Date <= 201206)) || ((53078923 == 53086475) && ((53088651 == 53161483) || (53088651 == 53272459)) && (Date >= 201203) && (Date <= 201206)));   语法归约过程   int  a  //归约的当前标识和下一个标识 type-specifier //归约栈信息，int 归约到type-specifier declaration-specifiers ////归约栈信息，type-specifier归约到declaration-specifiers a  =  //归约的当前标识和下一个标识，以下类似 declaration-specifiers direct-declarator //a->direct-declarator,以后雷同，只看栈顶归约信息即可 declaration-specifiers declarator  =  (   (  (   (  (   (  53078923   53078923  ==   declaration-specifiers declarator = ( ( ( primary-expression  declaration-specifiers declarator = ( ( ( postfix-expression  declaration-specifiers declarator = ( ( ( unary-expression  declaration-specifiers declarator = ( ( ( cast-expression  declaration-specifiers declarator = ( ( ( multiplicative-expression  declaration-specifiers declarator = ( ( ( additive-expression  declaration-specifiers declarator = ( ( ( shift-expression  declaration-specifiers declarator = ( ( ( relational-expression  declaration-specifiers declarator = ( ( ( equality-expression  ==  53086475   53086475  )   declaration-specifiers declarator = ( ( ( equality-expression == primary-expression  declaration-specifiers declarator = ( ( ( equality-expression == postfix-expression  declaration-specifiers declarator = ( ( ( equality-expression == unary-expression  declaration-specifiers declarator = ( ( ( equality-expression == cast-expression  declaration-specifiers declarator = ( ( ( equality-expression == multiplicative-expression  declaration-specifiers declarator = ( ( ( equality-expression == additive-expression  declaration-specifiers declarator = ( ( ( equality-expression == shift-expression  declaration-specifiers declarator = ( ( ( equality-expression == relational-expression  declaration-specifiers declarator = ( ( ( equality-expression  declaration-specifiers declarator = ( ( ( AND-expression  declaration-specifiers declarator = ( ( ( exclusive-OR-expression  declaration-specifiers declarator = ( ( ( inclusive-OR-expression  declaration-specifiers declarator = ( ( ( logical-AND-expression  declaration-specifiers declarator = ( ( ( logical-OR-expression  declaration-specifiers declarator = ( ( ( conditional-expression  declaration-specifiers declarator = ( ( ( assignment-expression  declaration-specifiers declarator = ( ( ( expression  )  &&   declaration-specifiers declarator = ( ( primary-expression  declaration-specifiers declarator = ( ( postfix-expression  declaration-specifiers declarator = ( ( unary-expression  declaration-specifiers declarator = ( ( cast-expression  declaration-specifiers declarator = ( ( multiplicative-expression  declaration-specifiers declarator = ( ( additive-expression  declaration-specifiers declarator = ( ( shift-expression  declaration-specifiers declarator = ( ( relational-expression  declaration-specifiers declarator = ( ( equality-expression  declaration-specifiers declarator = ( ( AND-expression  declaration-specifiers declarator = ( ( exclusive-OR-expression  declaration-specifiers declarator = ( ( inclusive-OR-expression  declaration-specifiers declarator = ( ( logical-AND-expression  &&  (   (  (   (  53088651   53088651  ==   declaration-specifiers declarator = ( ( logical-AND-expression && ( ( primary-expression  declaration-specifiers declarator = ( ( logical-AND-expression && ( ( postfix-expression  declaration-specifiers declarator = ( ( logical-AND-expression && ( ( unary-expression  declaration-specifiers declarator = ( ( logical-AND-expression && ( ( cast-expression  declaration-specifiers declarator = ( ( logical-AND-expression && ( ( multiplicative-expression  declaration-specifiers declarator = ( ( logical-AND-expression && ( ( additive-expression  declaration-specifiers declarator = ( ( logical-AND-expression && ( ( shift-expression  declaration-specifiers declarator = ( ( logical-AND-expression && ( ( relational-expression  declaration-specifiers declarator = ( ( logical-AND-expression && ( ( equality-expression  ==  53176715   53176715  )   declaration-specifiers declarator = ( ( logical-AND-expression && ( ( equality-expression == primary-expression  declaration-specifiers declarator = ( ( logical-AND-expression && ( ( equality-expression == postfix-expression  declaration-specifiers declarator = ( ( logical-AND-expression && ( ( equality-expression == unary-expression  declaration-specifiers declarator = ( ( logical-AND-expression && ( ( equality-expression == cast-expression  declaration-specifiers declarator = ( ( logical-AND-expression && ( ( equality-expression == multiplicative-expression  declaration-specifiers declarator = ( ( logical-AND-expression && ( ( equality-expression == additive-expression  declaration-specifiers declarator = ( ( logical-AND-expression && ( ( equality-expression == shift-expression  declaration-specifiers declarator = ( ( logical-AND-expression && ( ( equality-expression == relational-expression  declaration-specifiers declarator = ( ( logical-AND-expression && ( ( equality-expression  declaration-specifiers declarator = ( ( logical-AND-expression && ( ( AND-expression  declaration-specifiers declarator = ( ( logical-AND-expression && ( ( exclusive-OR-expression  declaration-specifiers declarator = ( ( logical-AND-expression && ( ( inclusive-OR-expression  declaration-specifiers declarator = ( ( logical-AND-expression && ( ( logical-AND-expression  declaration-specifiers declarator = ( ( logical-AND-expression && ( ( logical-OR-expression  declaration-specifiers declarator = ( ( logical-AND-expression && ( ( conditional-expression  declaration-specifiers declarator = ( ( logical-AND-expression && ( ( assignment-expression  declaration-specifiers declarator = ( ( logical-AND-expression && ( ( expression  )  ||   declaration-specifiers declarator = ( ( logical-AND-expression && ( primary-expression  declaration-specifiers declarator = ( ( logical-AND-expression && ( postfix-expression  declaration-specifiers declarator = ( ( logical-AND-expression && ( unary-expression  declaration-specifiers declarator = ( ( logical-AND-expression && ( cast-expression  declaration-specifiers declarator = ( ( logical-AND-expression && ( multiplicative-expression  declaration-specifiers declarator = ( ( logical-AND-expression && ( additive-expression  declaration-specifiers declarator = ( ( logical-AND-expression && ( shift-expression  declaration-specifiers declarator = ( ( logical-AND-expression && ( relational-expression  declaration-specifiers declarator = ( ( logical-AND-expression && ( equality-expression  declaration-specifiers declarator = ( ( logical-AND-expression && ( AND-expression  declaration-specifiers declarator = ( ( logical-AND-expression && ( exclusive-OR-expression  declaration-specifiers declarator = ( ( logical-AND-expression && ( inclusive-OR-expression  declaration-specifiers declarator = ( ( logical-AND-expression && ( logical-AND-expression  declaration-specifiers declarator = ( ( logical-AND-expression && ( logical-OR-expression   此处省略一千多行 这句代码归约出1111行信息（好吉利的数字），由于回复字符不能超过10000个字符串，因此就无法全贴了。LZ想要，可以给个邮箱

user: user-3
content: 何不试试ANTLR， 语法规则与EB解析类但是提供的类更加友好。

user: user-0
content: 引用 1 楼 user-4 的回复:自己 yacc 写个呗, 又不麻烦.略高端，不会玩YACC，有时间学习一下。

user: user-0
content: 引用 5 楼 zhao4zhong1 的回复:参考TCC 老赵你这不按常理出牌啊，你不是应该来一句“仅供参考”然后贴一大段代码的吗？  不过我这边也有点眉目了，可以做出来了

user: user-2
content: 参考TCC

user: user-0
content: 引用 3 楼 lincolnandlinda 的回复:何不试试ANTLR， 语法规则与EB解析类但是提供的类更加友好。 为何都如此高端，有没有C/C++的方式实现的

user: user-4
content: 自己 yacc 写个呗, 又不麻烦.