0

I am trying to create a very simple parser for an if-else statement with conditional expressions using java

The parse syntax :

stmnt ::= matched | unmatched
matched ::= if ( expr ) then matched else matched | others
unmatched ::= if ( expr ) then  stmnt  | if ( expr  ) then matched else unmatched
exp ::= factor exp’
exp’ ::=  factor | > factor | <> factor | == factor  | >= factor  | <= factor  | 
factor  ::= id | num | ! id

I have been looking around the net at different types of parsing algorithms, all of them seeming very abstract and complex.

Are there any suggestions for good algorithm for this .

  • [This](https://stackoverflow.com/a/1932664/2970947) should get you started. – Elliott Frisch Jul 29 '18 at 02:45
  • Do a Google search for `lex and yacc for Java`. – selbie Jul 29 '18 at 02:49
  • @selbie - Lex and Yacc are not Java tools. If you are going to suggest to Google, you should at least suggest terms that will give good results; "parser generator Java" – Stephen C Jul 29 '18 at 03:02
  • If you'd rather hand-code a parser, look into the [**recursive descent**](https://en.wikipedia.org/wiki/Recursive_descent_parser) technique. For a tiny grammar like this it's not a bad choice. But if you ever need to do a parser for an entire language, you'll want to use a parser generator. – Kevin Anderson Jul 29 '18 at 03:12
  • @StephenC - hence the predicate "for java". – selbie Jul 29 '18 at 03:20
  • I know. I saw that. It is *still* a poor recommendation. – Stephen C Jul 29 '18 at 03:28

0 Answers0