How can I determine the grammar of an INI file using BNFC?

http://www.cs.chalmers.se/Cs/Research/Language-technology/BNFC/

how should I write my labeled BNF to get BNFC to create an INI parser for me?

So far, I only got o__O!

entrypoints File ; comment "#" ; token ID ( letter | digit | ["-_'"] )+ ; Ini. File ::= [Section] ; Sect. Section ::= "[" ID "]" [Statement] ; Bind. Statement ::= ID "=" ID ; separator Statement "\n" ; terminator Section "" ; 

 [name] #x = 10 y = 20 

 Parse Successful! [Abstract Syntax] Ini [Sect (ID "name") [Bind (ID "y") (ID "20")]] [Linearized tree] [name]y = 20 

 [name] x = 10 #y = 20 

 Parse Successful! [Abstract Syntax] Ini [Sect (ID "name") [Bind (ID "x") (ID "10")]] [Linearized tree] [name]x = 10 

o__O I'm stuck ...

+6
parsing haskell context-free-grammar bnf ebnf
source share
1 answer

I asked one of the BNFC developers and quoted his answer here:

Space characters, such as newlines, are not supported in tokens because BNFC has a hard conductor such as lexer "space". The idea is that spaces cannot carry meaning into “well-behaved” languages. One of these limitations is what made BNFC so simple ... but you have to solve it using a preprocessor, for example. parsing input line by line.


Such as:

 entrypoints File ; comment "#" ; token ID ( letter | digit | ["-_'"] )+ ; Ini. File ::= [Section] ; Sect. Section ::= "[" ID "]" [Statement] ; Bind. Statement ::= ID "=" ID ; separator Statement "//" ; terminator Section "//" ; 

Reading:

 [name] x = 10 y = 20 

Preprocess:

 [name]// x = 10// y = 20// 

Analyze:

 Ini [Sect (ID "name") [Bind (ID "x") (ID "10"), Bind (ID "y") (ID "20")]] 

Transform:

  ↓ ↓ Ini [Sect (ID "name") [Bind (ID "x") (ID "0"), Bind (ID "y") (ID "0")]] 

Record:

 [name]// x = 0// y = 0// 

post processing:

 [name] x = 0 y = 0 

(not tested, don't know if it works, just to give an idea!)

+5
source share

All Articles