Come posso definire una grammatica del file INI usando il BNFC?
-
22-07-2019 - |
Domanda
http://www.cs.chalmers.se/Cs / / Lingua-Technology Research / BNFC /
come devo scrivere il mio BNF etichettato per fare in modo che BNFC generi un parser INI per me?
Finora sono arrivato 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 Sono bloccato ...
Soluzione
Ho chiesto a uno degli sviluppatori BNFC e cito la sua risposta qui:
I caratteri spaziali come le nuove linee sono non ben supportato nei token, perché BNFC ha un tipo di lexer cablato & Quot; spazio " ;. L'idea è che gli spazi non possono portare significato in "ben educato" le lingue. Una di quelle restrizioni che ha reso BNFC così semplice ... ma dovresti riuscire a risolverlo usando un preprocessore, ad es. analizzare la riga di input per linea.
Come ad esempio:
entrypoints File ;
comment "#" ;
token ID ( letter | digit | ["-_'"] )+ ;
Ini. File ::= [Section] ;
Sect. Section ::= "[" ID "]" [Statement] ;
Bind. Statement ::= ID "=" ID ;
separator Statement "//" ;
terminator Section "//" ;
Leggi:
[name]
x = 10
y = 20
pre-elaborazione:
[name]//
x = 10//
y = 20//
Parse:
Ini [Sect (ID "name") [Bind (ID "x") (ID "10"), Bind (ID "y") (ID "20")]]
Trasforma:
↓ ↓
Ini [Sect (ID "name") [Bind (ID "x") (ID "0"), Bind (ID "y") (ID "0")]]
Scrivi:
[name]//
x = 0//
y = 0//
Postprocessa:
[name]
x = 0
y = 0
(non selezionato, non so se funziona, solo per dare un'idea !!)