Well... I don't think your grammar is ambiguous. It's not doing what you think it should, but that is another matter...
Let's see... you're defining two token types in your lexical grammar, SchemaName
and TableName
. Are these the exact same regular expression pattern or just similar, i.e. not exactly the same but with significant overlap?
One thing that seems clear is that the string "dbo.test" matches both ShemaName
and TableName
but it will be taken to be one or the other based on which pattern occurs first. This is the basic rule. The first match wins. (Actually, it's the longer match wins, then if they are the same length, the first match, the one that occurs earlier wins.)
So, anyway, the string "dbo.test" will always be matched as TableName
and never as SchemaName
. So the syntactic rule of DROP SCHEMA <SchemaName>
will never work because you will have <TableName>
instead of <SchemaName>
. And that is what is going on.
I guess one question is whether you really need two token types SchemaName
and TableName
. If you just had one type, TableOrSchemaName
or maybe just Name
, then that would work surely.
On the other hand, if, for whatever reason, you want to have those two token types, you could have:
<DROP> <SCHEMA> DEACTIVATE_TOKENS TABLE_NAME (<SchemaName>)
And since you deactivate the TABLE_NAME
token type, the string "dbo.test" will now be matched as a SCHEMA_NAME
token type.
That is another possibility.