lexical grammar

A

aegis

let's say we have the following declaration:

volatile int (*func)(int a);

We start with a declaration (A.2.2)
that expands to declaration-specifiers?
We find the right form:
type-qualifier declaration-specifiers opt?
after volatile is expanded, we look for
type specifier:
type-specifier declaration-specifiers opt

So after that is expanded we have:
(*func)(int a);

this counts as a init-declarator?
If so, then we expand it as a declarator
since it is not being initialized?

Then that brings us to the rule:
pointer opt direct-declarator
for (*func)(int a);

No pointer encountered yet, so we continue
expanding:

direct-declarator:
identifier
( declarator )
direct-declarator [ type-qualifier-list opt assignment-expression opt ]
direct-declarator [ static type-qualifier-list opt
assignment-expression ]
direct-declarator [ type-qualifier-list static assignment-expression ]
direct-declarator [ type-qualifier-list opt *]
direct-declarator ( parameter-type-list )
direct-declarator ( identifier-list opt )

We encounter ( declarator )
and reapply this rule?

If so, then the rule pointer opt direct-declarator
for declarator works here and we expand
the declarator part which qualifies as an
identifier. But that leaves us with:
(int a);

Which I wonder, where is the rule that should
be applied here that follows from our expansion?

Somewhere in the rules of direct declarator
we should branch to a rule that solves a parameter
list. But because our construction was (*func),
we should ignore the identifier in parens after
resolving that it was an identifier from
the direct declarator rule, right?

This is where I don't see how we branch to
a rule that resolves the parameter list.

I don't think it would be the following
rule:
direct-declarator ( parameter-type-list )
unless we consider the expanded identifier
as a direct declarator?

Also, chris torek, if you are reading this,
what data structure would you recommend for
representing this? I read an older
article by you that describes this process
as being in factored form. How do you recommend
to factor it all programmatically?
 
R

Richard Bos

aegis said:
Then that brings us to the rule:
pointer opt direct-declarator
for (*func)(int a);

You are (AFAICT!) OK up to here, but...
No pointer encountered yet, so we continue
expanding:

direct-declarator:
identifier
( declarator )
direct-declarator [ type-qualifier-list opt assignment-expression opt ]
direct-declarator [ static type-qualifier-list opt
assignment-expression ]
direct-declarator [ type-qualifier-list static assignment-expression ]
direct-declarator [ type-qualifier-list opt *]
direct-declarator ( parameter-type-list )
direct-declarator ( identifier-list opt )

We encounter ( declarator )
and reapply this rule?

....if so, we run into problems (as you see), so we take the next option
that applies, which is

direct-declarator ( parameter-type-list )

The first part (the direct-declarator) matches (*func), which you've
already done; the second part - ( parameter-type-list ) - matches the
(int a).
This is where I don't see how we branch to
a rule that resolves the parameter list.

I don't think it would be the following
rule:
direct-declarator ( parameter-type-list )
unless we consider the expanded identifier
as a direct declarator?

And you've just proved that it is, so all is fine, surely?
Also, chris torek, if you are reading this,
what data structure would you recommend for
representing this?

A tree, most likely.

Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top