ANTLR, how to convert BNF, EBNF data to ANTLR?

I need to generate a CSV data parser. Somehow I managed to write BNF, EBNF for CSV data, but I donโ€™t know how to convert this to 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 gives an error and does not accept brackets. I'm not an ANTLR expert, can anyone help?

+4
source share
1 answer

hi, I need to generate a CSV data parser ...

In most languages โ€‹โ€‹that I know, a decent third-party CSV parser already exists. So, chances are you are reinventing the wheel.

For example, in EBNF, we iterate over [{entry entry} newline] newline

The equivalent in ANTLR will 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 the rules with parentheses (subheads are what they are called):

 'a' 'b'+ 

matches: ab , abb , abbb , ... , and:

 ('a' 'b')+ 

matches: ab , abab , ababab , ...

+6
source

Source: https://habr.com/ru/post/1315435/


All Articles