3

I want to use Java to parse a very simple grammar, for example:

/*comments*/
"aaa" = "bbb"

That's all. I want all tokens (comment, string, equals).

Is there any Java library that can handle this?

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
disorderdev
  • 1,458
  • 1
  • 14
  • 28
  • 1
    I didn't get it. Are you looking for something like ANTLR? http://www.antlr.org/ – zengr Nov 09 '11 at 06:06
  • @zengr I believe so. Please follow this: http://stackoverflow.com/questions/278480/antlr-tutorials and this http://jnb.ociweb.com/jnb/jnbJun2008.htm – Kowser Nov 09 '11 at 07:26
  • Maybe OP only wants the tokens and not an actual parser for the implied simple grammar? – Ira Baxter Nov 09 '11 at 09:47
  • ANTLR might be a little too heavy for such a simple thing. but if you all recommend ANTLR, I'll try it. thanks. – disorderdev Nov 09 '11 at 18:05

3 Answers3

3

You gonna have to write the base grammar that recognize everyone of these tokens and then generate the parser (lexical and syntaxic) with a tool like SableCC or JavaCC (They both produce Java classes). Then you'll have a parser that can parse your language.

I hope it is what you meant by parse a [...] grammar.

talnicolas
  • 13,885
  • 7
  • 36
  • 56
1

for really basic needs you can use either java.util.StringTokenizer or java.io.StreamTokenizer.

Dmitry B.
  • 9,107
  • 3
  • 43
  • 64
0

You can code a recursive descent parser pretty easily for a simple language. See Is there an alternative for flex/bison that is usable on 8-bit embedded systems?

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