1

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

Wooble
  • 87,717
  • 12
  • 108
  • 131
GbGbGbGbEB
  • 56
  • 1
  • 6
  • I suggest you read up on parsers. Here's a few keywords for you: LR-parsing, LL-parsing, first-sets, recursive descent :-) – aioobe Mar 10 '12 at 22:04

1 Answers1

0

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.

Community
  • 1
  • 1
Ira Baxter
  • 93,541
  • 22
  • 172
  • 341