javapretty-printbnfc

bnfc pretty printer java error


I have this BNFC file describing a configuration file made of many sections where each section has a name between [] followed by a list of simple declaration

comment "#";
rulse Boolean ::= "True" | "False";
Conf. Config ::= [Section];  //a config is a list of sections
terminator Section "";

Sec. Section ::= "[" NomeSec "]" [Decl]; //A section is made of a name and a list of declarations
terminator Decl ";";

NomeSez. NomeSec ::= Ident;

Dec. Decl ::= VarN "=" Type;

VarName. VarN ::= Ident;

Int.    Type::=Integer;
Char.   Type::=Char;
String. Type::=String;
Float.  Type::=Double;
Bool.   Type::=Boolean;

Example:

[Section1]
Var1 = 3;
Var2 = "test";
#ignored comment

[SectionA]
var4 = True;

with an undefined number of sections and declarations.

I ran the command bnfc -m -java <filename> from the shell and everything goes ok a part from the pretty printer. When its time to compile the prettyprinter.java tons of errors are generated. For example:

ES5/PrettyPrinter.java:10: error: reference to String is ambiguos 
private static final String _L_PAREN = new String("("); both class
ES5.Absyn.String  and class java.lang.String in java.lang match

All the errors are of this type. I'm wondering , i just built the grammar , i failed the grammar or the BNFC failed? Thanks


Solution

  • Because BNFC creates java classes for each category and label, if use names that are in java.lang it creates ambiguities (e.g. String, Boolean...).

    It works with the following renames (I also added an explicit entry point):

    entrypoints Config;
    
    comment "#";
    rules MyBoolean ::= "True" | "False";
    Conf. Config ::= [Section];  -- a config is a list of sections
    terminator Section "";
    
    Sec. Section ::= "[" NomeSec "]" [Decl]; -- A section is made of a name and a list of declarations
    terminator Decl ";";
    
    NomeSez. NomeSec ::= Ident;
    
    Dec. Decl ::= VarN "=" Type;
    
    VarName. VarN ::= Ident;
    
    TInt.    Type::=Integer;
    TChar.   Type::=Char;
    TString. Type::=String;
    TFloat.  Type::=Double;
    TBool.   Type::=MyBoolean;