tags:

views:

230

answers:

1

I have to generate parser of CSV data. Somehow I managed to write BNF, EBNF for CSV data but I don't know how to convert this into an ANTLR grammar (which is a parser generator). For example, in EBNF we write:

[{header entry}newline]newline

but when I write this in ANTLR to generate a parser, it's giving an error and not taking brackets. I am not expert in ANTLR can anyone help?

+1  A: 

hi , i have to generate parser of CSV data ...

In most languages I know, there already exists a decent 3rd party CSV parser. So, chances are that you're reinventing the wheel.

For Example in EBNF we wrire [{header entry}newline]newline

The equivalent in ANTLR would look like this:

((header entry)* newline)? newline

In other words:

                 | (E)BNF | ANTLR
-----------------+--------+------
'a' zero or once | [a]    | a?
'a' zero or more | {a}    | a*
'a' once or more | a {a}  | a+

Note that you can group rules by using parenthesis (sub-rules is what they're called):

'a' 'b'+

matches: ab, abb, abbb, ..., while:

('a' 'b')+

matches: ab, abab, ababab, ...

Bart Kiers
thanks this will really help me while converting EBNF into ANTLR
Hammad
@Hammad, you're welcome.
Bart Kiers
hi bart, where do we use {} brackets in ANTLR . For Examplefield : ( f=QUOTED | f=UNQUOTED | // nothing ) { $line::fields.add(($f == null) ? "" : $f.text); } ;
Hammad
@Hammad, you use `{ ... }` in your rules to embed code. If you have a specific question about it, I recommend you create a new question instead of asking it in the comments here. Good luck!
Bart Kiers