简体   繁体   English

代表CFG生产

[英]representing CFG productions

I'm working on a program that needs to decide if a string "(example + another) * other" belongs to a certain grammar. 我正在开发一个程序,该程序需要确定字符串“((示例+另一个)*其他”)是否属于某个语法。

    Start = Expr endline
    Expr  = Term Expr2
    Expr2 = + Term Expr2 | - Term Expr2 | e
    Term  = Factor Term2
    Term2 = * Factor Term2 | / Factor Term2 | e
    Factor= id | ( Expr ) | num 

For example I'm trying to implement something similar to above grammar in Java. 例如,我试图在Java中实现类似于上述语法的内容。 So far I have switch statements with recusion but I feel this is not the way to do it. 到目前为止,我对switch语句怀有沉思,但我觉得这不是做到这一点的方法。 Are there simpler ways of representing productions? 有更简单的方式代表作品吗? Any tips would be appreciated. 任何提示将不胜感激。 Thanks 谢谢

Switch statements with recursion is a lot closer to what you need than you might think. 带有递归的Switch语句比您想像的要近得多。

See my answer on how to build a recursive descent parser . 请参阅我关于如何构建递归下降解析器的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM