简体   繁体   中英

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. So far I have switch statements with recusion but I feel this is not the way to do it. 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.

See my answer on how to build a recursive descent parser .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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