problem with declaration grammar?

Sergey Gromov snake.scaly at gmail.com
Thu Feb 19 04:39:30 PST 2009


Thu, 19 Feb 2009 01:30:36 -0500, jerry quinn wrote:

> Christopher Wright Wrote:
> 
>> jerry quinn wrote:
>>> Hi there,
>>> 
>>> I'm not sure if I'm missing something, but I'm having trouble seeing that a simple declaration will parse correctly with the D grammar.
>>> 
>>> If we take a declaration statment like:
>>> 
>>> int x = 3;
>>> 
>>> we have (my best guess):
>>> 
>>> DeclarationStatement -> Declaration
>>> Declaration -> Decl
>>> Decl -> BasicType Declarators ;
>>> BasicType -> int
>>> Declarators -> DeclaratorInitializer
>>> DeclaratorInitializer -> Declarator = Initializer
>>> Declarator -> BasicType2 Identifier
>>> BasicType2 -> ????
>>> 
>>> I'm thinking that BasicType2 is optional here, rather than required as the grammar shows.  Is that correct?
>>> 
>>> Thanks
>>> Jerry
>> 
>> . Declaration -> Decl
>> . Decl -> BasicType Declarators
>> . BasicType -> "int"
>> . Declarators -> DeclaratorInitializer
>> . DeclaratorInitializer -> Declarator "=" Initializer
>> We agree up to here.
>> 
>> . Declarator -> Identifier
>> Here, you don't need BasicType2, and if you use it, you recurse, so 
>> using the rule Declarator -> BasicType2 Declarator here is useless.
> 
> What you describe sounds like what I'd expect.
> 
> 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

The grammar works the other way around:

int x = 3 ;

int -> BasicType(int)
// this is either Decl or Type, need more tokens, expect Declarators,
// Declarator, or Declarator2
-----
x -> Identifier(x)
// either DeclaratorInitializer (Declarators), Declarator,
// IdentifierList (not expecting), StructMemberInitializer (not
// expecting), or PrimaryExpression (not expecting)
// therefore expecting '=' or DeclaratorSuffixes
-----
= -> = // token
// Identifier(x) = -> definitely DeclaratorInitializer, expecting
// Initializer, that is , either void, AssignExpression,
// ArrayInitializer, or StructInitializer
-----
3 -> IntegerLiteral(3) -> PrimaryExpression(3)
// may be some sort of other expression, need more tokens
-----
; -> ; // token
// not a part of an expression
-----
PrimaryExpression(3) -> AssignExpression(3)
Identifier(x) = AssignExpression(3) -> DeclaratorInitializer(x=3)
// DeclaratorInitializer(x=3) is Declarators, expect ',' but actually
// have ';' next, so
DeclaratorInitializer(x=3) -> Declarators(x=3)
-----
BasicType(int) DeclaratorInitializer(x=3) ; -> Decl(int x=3;)

Finita la comedia.



More information about the Digitalmars-d mailing list