CSharpGrammar and namespace requirement

Jul 15, 2008 at 8:49 AM
This is a fantastic library!

In CSharpGrammar.cs, in the rule for compilation_unit, it ends with namespace_declarations_opt, which means that the file has to contain a namespace, which isn't true of C#. Should that be namespace_member_declarations instead?
Jul 15, 2008 at 1:11 PM
Edited Jul 15, 2008 at 1:40 PM


danielearwicker wrote:
This is a fantastic library!

In CSharpGrammar.cs, in the rule for compilation_unit, it ends with namespace_declarations_opt, which means that the file has to contain a namespace, which isn't true of C#. Should that be namespace_member_declarations instead?


the "_opt" suffix shows that it's optional. look at its rule:
  namespace_declarations_opt.Rule = MakeStarRule(namespace_declarations_opt, null, namespace_declaration);
MakeStarRule(listNonTerminal, delimiter, listMember) will produce a rule that allows listMember appear zero or more times.

(unrelated) I think there's a problem in CSharpGrammar's rules for property. C# 3.0 has this new feature called automatically implemented properties, where all you have to write is something like:
public SomeType SomeProperty { get; set; }
With the following rule, the code above won't parse:
accessor_declaration.Rule = attributes_opt + accessor_modifier_opt + accessor_name + block;
because the rule demands a block following "get" or "set".
Even before C# 3.0, properties could be virtual, and with this rule those virtual properties will fail to parse too.
Jul 15, 2008 at 2:44 PM


rednaxela wrote:


danielearwicker wrote:
This is a fantastic library!

In CSharpGrammar.cs, in the rule for compilation_unit, it ends with namespace_declarations_opt, which means that the file has to contain a namespace, which isn't true of C#. Should that be namespace_member_declarations instead?


the "_opt" suffix shows that it's optional. look at its rule:
  namespace_declarations_opt.Rule = MakeStarRule(namespace_declarations_opt, null, namespace_declaration);
MakeStarRule(listNonTerminal, delimiter, listMember) will produce a rule that allows listMember appear zero or more times.


The problem is that CSharpGrammar accepts this as a comp. unit:

namespace X  { class Y { } }

But won't accept this (which is valid C#):

class Y { }

So I'm guessing that the top level of a comp. unit is like the inside of a namespace (the global namespace).
Jul 15, 2008 at 4:21 PM
Edited Jul 15, 2008 at 4:31 PM


danielearwicker wrote:
The problem is that CSharpGrammar accepts this as a comp. unit:

namespace X  { class Y { } }

But won't accept this (which is valid C#):

class Y { }

So I'm guessing that the top level of a comp. unit is like the inside of a namespace (the global namespace).


Ah, I get the point.
Yep, that's a flaw in the grammar file. I noticed that too, but it was quite some time ago, and I just forgot about it.

In the spec, it goes like:

compilation-unit:
extern-alias-directives<SUB>opt</SUB>   using-directives<SUB>opt</SUB>  global-attributes<SUB>opt</SUB>
              namespace-member-declarations<SUB>opt</SUB>

namespace-member-declarations:
namespace-member-declaration
namespace-member-declarations namespace-member-declaration

namespace-member-declaration:
namespace-declaration
type-declaration

Compare that to the grammar file:
      compilation_unit.Rule = extern_alias_directives_opt + using_directives_opt + attributes_opt + namespace_declarations_opt;
      namespace_declaration.Rule = "namespace" + qualified_identifier + namespace_body + semi_opt;
      namespace_member_declaration.Rule = namespace_declaration | type_declaration;
      namespace_member_declarations.Rule = MakePlusRule(namespace_member_declarations, null, namespace_member_declaration);


So, I think the solution would be modifying namespace_declarations_opt.Rule to have namespace_member_declarations as the third argument to the call to MakeStarRule(), and then renaming namespace_declarations_opt to namespace_member_declarations_opt.

Well, that introduces a few more S-R conflicts, though.

Coordinator
Jul 15, 2008 at 5:36 PM

Hi

Thanks for the comments - these are all valid points. Remember that this grammar is just a sample. Care to make it real? You're welcome to do it!

Roman