views:

42

answers:

1

I am wondering where is the borderline between grammar specification and semantic analysis. What is better: to use a detailed grammar description or leave the details for semantic phase? For example: imagine an OO-language like C# with enum type, that can "derive" from a primitive type

enum X : int { a = 1 }

Now, should the correctness (in this case: primitive or non-primitive type) of base-type of enum be a subject of grammar check or semantic analysis?

+1  A: 

This is an extremely broad question. There are so many other considerations such that it would be very difficult to simply say that one tactic is better than another in all situations. It depends on your purpose and the definition of the rest of the language. If other parts of the language are inherently syntactically ambiguous (and must be disambiguated using semantic information), then it's apparent that it's necessary to perform tasks as a step of semantic analysis.

That said, I am a fan of catching all errors as early as possible. If your language and target permits you to parse a completely unambiguous semantic representation of your input without requiring any semantic analysis to reject invalid programs, then I believe that is going to be the best approach by far.

Basically, you are definitely balancing your ability to reject erroneous programs as early as possible with the complexity of your grammar and parser and duplication of effort between the grammar description and the semantic analysis phase. I don't believe it's ever possible to say that doing things in one way is better than another.

Gian