problem with declaration grammar?

Ellery Newcomer ellery-newcomer at utulsa.edu
Fri Feb 20 19:12:23 PST 2009


Jerry Quinn wrote:
> Ellery Newcomer Wrote:
> 
>> jerry quinn wrote:
>>> Ellery Newcomer Wrote:
>>>>> Maybe I'm missing something.  The grammar shown in http://www.digitalmars.com/d/2.0/declaration.html has the following rules:
>>>>>
>>>>> BasicType2:
>>>>>         *
>>>>>         [ ]
>>>>>         [ Expression ]
>>>>>         [ Expression .. Expression ]
>>>>>         [ Type ]
>>>>>         delegate Parameters FunctionAttributesopt
>>>>>         function Parameters FunctionAttributesopt
>>>>>
>>>>> Declarator:
>>>>>         BasicType2 Declarator DeclaratorSuffixesopt
>>>>>         BasicType2 Identifier DeclaratorSuffixesopt
>>>>>
>>>>> With this definition, I don't see how you can get Declarator->Identifier.
>>>>>
>>>>> Jerry
>>>>>
>>>> You are correct. BasicType2 can match nothing. It should also be able to 
>>>> match what it does above multiple times.
>>> As I'm looking at this further, there seems to be more issues.  In particular, I don't think the grammar can parse:
>>>
>>> int (*x)(char);
>>>
>>> as specified.  Doing so gives (my best attempt)
>>>
>>> Decl -> BasicType Declarators ;
>>> BasicType -> int
>>> Declarators -> DeclaratorInitializer
>>> DeclaratorInitializer -> Declarator
>>> Declarator -> BasicType2 Identifier DeclaratorSuffixes
>>> BasicType2 -> NULL (assuming that the grammar should be revised like this)
>>> Identifier -> BAD PARSE
>>>
>>>
>> yeah, if you haven't figured out by now, the grammar is a bunch of hooey.
>>
>> I spent like a month building an ANTLR grammar based on the above, and 
>> then realized much of it was garbage.
>>
>> Then I spent two months going through the source code and rewriting most 
>> of the rules. Just got done with it a week or two ago :) That was all 
>> version 1, but it looks the same, so if memory serves the above rules 
>> should look something like this:
>>
>> BasicType2_x:
>> 	*
>> 	[ ]
>> 	[ Expression ]
>> 	[ Expression .. Expression ]
>> 	[ Type ]
>> 	delegate Parameters FunctionAttributesopt
>> 	function Paramters FunctionAttributesopt
>> BasicType2:
>> 	BasicType2_x
>> 	BasicType2 BasicType2_x
>> 	epsilon
>> Declarator:
>> 	BasicType2 Identifier DeclaratorSuffixesopt
>> 	BasicType2 ( Declarator ) DeclaratorSuffixesopt
>>
>> Apologies for any BNF misuse
> 
> Cool.  Do you feel like posting the whole thing somewhere?  

knock yourself out http://www.personal.utulsa.edu/~ellery-newcomer/antlrd.g

D version 1, ANTLR version 2, note it isn't really complete yet; I 
haven't gotten around to AST construction.

Oh, and don't look at the lexer part. It's ugly as sin.
> 
> As an aside comment, it might be better from a grammar perspective to make usage of BasicType2 optional, rather than have the epsilon in the BasicType2 rule itself.  Then every rule would consume at least 1 token, and _opt is the only expansion shortcut needed.
> 
> In the form show, you can simplify the BasicType2 rule to
> 
> BasicType2:
>   BasicType2 BasicType2_x
>   epsilon
> 

doh. I knew that.



More information about the Digitalmars-d mailing list