* Syntax question: new with a constrained subtype indication
@ 2005-04-25 15:44 James Alan Farrell
2005-04-25 16:07 ` Alex R. Mosteo
` (3 more replies)
0 siblings, 4 replies; 13+ messages in thread
From: James Alan Farrell @ 2005-04-25 15:44 UTC (permalink / raw)
Hi all,
I'm working on software to analyze Ada programs using ASIS. As such I
need to make sure I handle all cases in the syntax (and I'm finding that
in past assignments I've used only a small portion of the whole language!)
I'm currently working on a piece of code to analyze allocator
expressions (ARM 4.8). The entire relevant syntax is:
4.8:
allocator ::=
new subtype_indication | new qualified_expression
3.2.2:
subtype_indication ::= subtype_mark [constraint]
3.2.2:
subtype_mark ::= subtype_name
3.2.2:
constraint ::= scalar_constraint | composite_constraint
3.2.2:
scalar_constraint ::=
range_constraint | digits_constraint | delta_constraint
3.5
range_constraint ::= range range
3.5:
range ::= range_attribute_reference
| simple_expression .. simple_expression
From this, it appears I should be able to do something like:
package body Mod1 is
procedure Proc1 is
type Vector is array (Integer range <>) of Float;
type VectorPtr is access Vector;
V : VectorPtr;
begin
V := new Vector range 1 .. 10;
end Proc1;
end mod1;
Using gnat 3.15p, I get this error:
mod1.adb:15:23: incorrect constraint for this kind of type
gnatmake: "mod1.adb" compilation error
I suspect this is a type issue, but let me know if there is something
incorrect in the syntax.
The thing is, I've read 4.8 of the *annotated* ARM and I don't see
anything to say I cannot do this. So what am I missing? Is there
something, other than that document, that spells out the allowed types
of constraint?
Thank you,
James Alan Farrell
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: Syntax question: new with a constrained subtype indication
2005-04-25 15:44 Syntax question: new with a constrained subtype indication James Alan Farrell
@ 2005-04-25 16:07 ` Alex R. Mosteo
2005-04-25 16:34 ` James Alan Farrell
2005-04-25 16:18 ` Adrien Plisson
` (2 subsequent siblings)
3 siblings, 1 reply; 13+ messages in thread
From: Alex R. Mosteo @ 2005-04-25 16:07 UTC (permalink / raw)
James Alan Farrell wrote:
> Hi all,
> I'm working on software to analyze Ada programs using ASIS. As such I
> need to make sure I handle all cases in the syntax (and I'm finding that
> in past assignments I've used only a small portion of the whole language!)
>
> I'm currently working on a piece of code to analyze allocator
> expressions (ARM 4.8). The entire relevant syntax is:
>
> 4.8:
> allocator ::=
> new subtype_indication | new qualified_expression
>
> 3.2.2:
> subtype_indication ::= subtype_mark [constraint]
>
> 3.2.2:
> subtype_mark ::= subtype_name
>
> 3.2.2:
> constraint ::= scalar_constraint | composite_constraint
>
> 3.2.2:
> scalar_constraint ::=
> range_constraint | digits_constraint | delta_constraint
>
> 3.5
> range_constraint ::= range range
>
> 3.5:
> range ::= range_attribute_reference
> | simple_expression .. simple_expression
>
> From this, it appears I should be able to do something like:
>
> package body Mod1 is
>
> procedure Proc1 is
>
> type Vector is array (Integer range <>) of Float;
> type VectorPtr is access Vector;
>
> V : VectorPtr;
>
> begin
> V := new Vector range 1 .. 10;
What I've done quite frequently is
V := new Vector (1 .. 10);
> end Proc1;
>
> end mod1;
>
> Using gnat 3.15p, I get this error:
>
> mod1.adb:15:23: incorrect constraint for this kind of type
> gnatmake: "mod1.adb" compilation error
>
> I suspect this is a type issue, but let me know if there is something
> incorrect in the syntax.
>
> The thing is, I've read 4.8 of the *annotated* ARM and I don't see
> anything to say I cannot do this. So what am I missing? Is there
> something, other than that document, that spells out the allowed types
> of constraint?
>
> Thank you,
> James Alan Farrell
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: Syntax question: new with a constrained subtype indication
2005-04-25 15:44 Syntax question: new with a constrained subtype indication James Alan Farrell
2005-04-25 16:07 ` Alex R. Mosteo
@ 2005-04-25 16:18 ` Adrien Plisson
2005-04-25 16:37 ` James Alan Farrell
2005-04-25 16:34 ` Martin Krischik
2005-04-25 17:02 ` Martin Dowie
3 siblings, 1 reply; 13+ messages in thread
From: Adrien Plisson @ 2005-04-25 16:18 UTC (permalink / raw)
James Alan Farrell wrote:
> V := new Vector range 1 .. 10;
Vector is a composite type, not a scalar type, so i don't think you
can apply a scalar_constraint to this type. (but you are right, the
grammar does not forbid it explicitly).
have you looked at 3.2.2 and subsequent chapters to see what is said
about constraints ? hint: often things are not forbidden in the
grammar but additional constraints can be found by reading the whole text.
--
rien
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: Syntax question: new with a constrained subtype indication
2005-04-25 16:07 ` Alex R. Mosteo
@ 2005-04-25 16:34 ` James Alan Farrell
2005-04-25 18:52 ` Martin Krischik
0 siblings, 1 reply; 13+ messages in thread
From: James Alan Farrell @ 2005-04-25 16:34 UTC (permalink / raw)
>> V := new Vector range 1 .. 10;
>
>
> What I've done quite frequently is
>
> V := new Vector (1 .. 10);
>
Yes, but the problem is I need to account for the WHOLE EBNF, not just
what I would do. (ie, I need to account for ANYTHING my customers MIGHT do)
Thanks
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: Syntax question: new with a constrained subtype indication
2005-04-25 15:44 Syntax question: new with a constrained subtype indication James Alan Farrell
2005-04-25 16:07 ` Alex R. Mosteo
2005-04-25 16:18 ` Adrien Plisson
@ 2005-04-25 16:34 ` Martin Krischik
2005-04-25 19:03 ` James Alan Farrell
2005-04-25 17:02 ` Martin Dowie
3 siblings, 1 reply; 13+ messages in thread
From: Martin Krischik @ 2005-04-25 16:34 UTC (permalink / raw)
James Alan Farrell wrote:
> Hi all,
> I'm working on software to analyze Ada programs using ASIS. As such I
> need to make sure I handle all cases in the syntax (and I'm finding that
> in past assignments I've used only a small portion of the whole language!)
>
> I'm currently working on a piece of code to analyze allocator
> expressions (ARM 4.8). The entire relevant syntax is:
>
> 4.8:
> allocator ::=
> new subtype_indication | new qualified_expression
>
> 3.2.2:
> subtype_indication ::= subtype_mark [constraint]
>
> 3.2.2:
> subtype_mark ::= subtype_name
>
> 3.2.2:
> constraint ::= scalar_constraint | composite_constraint
>
> 3.2.2:
> scalar_constraint ::=
> range_constraint | digits_constraint | delta_constraint
>
> 3.5
> range_constraint ::= range range
>
> 3.5:
> range ::= range_attribute_reference
> | simple_expression .. simple_expression
>
> From this, it appears I should be able to do something like:
>
> package body Mod1 is
>
> procedure Proc1 is
>
> type Vector is array (Integer range <>) of Float;
> type VectorPtr is access Vector;
>
> V : VectorPtr;
>
> begin
> V := new Vector range 1 .. 10;
V := new Vector (Integer range 1 .. 10);
or shorter:
V := new Vector (1 .. 10);
You mixed the syntax for integer types:
http://en.wikibooks.org/wiki/Programming:Ada:Types:range
with the one for arrays
http://en.wikibooks.org/wiki/Programming:Ada:Types:array
The later need the ( and ).
> end Proc1;
>
> end mod1;
>
> Using gnat 3.15p, I get this error:
>
> mod1.adb:15:23: incorrect constraint for this kind of type
> gnatmake: "mod1.adb" compilation error
>
> I suspect this is a type issue, but let me know if there is something
> incorrect in the syntax.
>
> The thing is, I've read 4.8 of the *annotated* ARM and I don't see
> anything to say I cannot do this. So what am I missing? Is there
> something, other than that document, that spells out the allowed types
> of constraint?
Your idea was right - just a syntax problem.
Have fun
Martin
--
mailto://krischik@users.sourceforge.net
Ada programming at: http://ada.krischik.com
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: Syntax question: new with a constrained subtype indication
2005-04-25 16:18 ` Adrien Plisson
@ 2005-04-25 16:37 ` James Alan Farrell
2005-04-25 17:16 ` Adrien Plisson
0 siblings, 1 reply; 13+ messages in thread
From: James Alan Farrell @ 2005-04-25 16:37 UTC (permalink / raw)
Adrien Plisson wrote:
> James Alan Farrell wrote:
>
>> V := new Vector range 1 .. 10;
>
>
> Vector is a composite type, not a scalar type, so i don't think you can
> apply a scalar_constraint to this type. (but you are right, the grammar
> does not forbid it explicitly).
>
Actually I just noticed about the scalar vs composit. So is there any
time when using a scalar constraint is reasonable/allowed with the new
operator? I will take your advice and read 3.2.2. I hope I wont have
to read the *WHOLE* text to answer this one question -- it is too large
and I get lost and confused ;)
> have you looked at 3.2.2 and subsequent chapters to see what is said
> about constraints ? hint: often things are not forbidden in the grammar
> but additional constraints can be found by reading the whole text.
>
Thank you (and Alex) for your rapid replies.
James
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: Syntax question: new with a constrained subtype indication
2005-04-25 15:44 Syntax question: new with a constrained subtype indication James Alan Farrell
` (2 preceding siblings ...)
2005-04-25 16:34 ` Martin Krischik
@ 2005-04-25 17:02 ` Martin Dowie
2005-04-25 17:22 ` Adrien Plisson
3 siblings, 1 reply; 13+ messages in thread
From: Martin Dowie @ 2005-04-25 17:02 UTC (permalink / raw)
James Alan Farrell wrote:
> The thing is, I've read 4.8 of the *annotated* ARM and I don't see
> anything to say I cannot do this. So what am I missing? Is there
> something, other than that document, that spells out the allowed types
> of constraint?
What about 4.8(4):
"An initialized allocator is an allocator with a qualified_expression. An
uninitialized allocator is one with a subtype_indication. In the
subtype_indication of an uninitialized allocator, a constraint is
permitted
only if the subtype_mark denotes an unconstrained composite subtype;
if there is no constraint, then the subtype_mark shall denote a definite
subtype."
Cheers
-- Martin
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: Syntax question: new with a constrained subtype indication
2005-04-25 16:37 ` James Alan Farrell
@ 2005-04-25 17:16 ` Adrien Plisson
0 siblings, 0 replies; 13+ messages in thread
From: Adrien Plisson @ 2005-04-25 17:16 UTC (permalink / raw)
James Alan Farrell wrote:
> I hope I wont have
> to read the *WHOLE* text to answer this one question -- it is too large
> and I get lost and confused ;)
well, you will have to...
i found those two, defined in sections noted "legality rule":
ARM95 3.6.1 §5
"An index_constraint shall appear only in a subtype_indication whose
subtype_mark denotes either an unconstrained array subtype, or an
unconstrained access subtype whose designated subtype is an
unconstrained array subtype; in either case, the index_constraint
shall provide a discrete_range for each index of the array type."
ARM95 3.7.1 §7
"A discriminant_constraint is only allowed in a subtype_indication
whose subtype_mark denotes either an unconstrained discriminated
subtype, or an unconstrained access subtype whose designated subtype
is an unconstrained discriminated subtype."
there is a similar paragraph for Fixed-point type (ARM95 3.5.9 §11),
but strangely, i can't find one for Integer types, nor for real types...
--
rien
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: Syntax question: new with a constrained subtype indication
2005-04-25 17:02 ` Martin Dowie
@ 2005-04-25 17:22 ` Adrien Plisson
2005-04-25 23:28 ` Randy Brukardt
0 siblings, 1 reply; 13+ messages in thread
From: Adrien Plisson @ 2005-04-25 17:22 UTC (permalink / raw)
Martin Dowie wrote:
> What about 4.8(4):
>
> "An initialized allocator is an allocator with a qualified_expression. An
> uninitialized allocator is one with a subtype_indication. In the
> subtype_indication of an uninitialized allocator, a constraint is
> permitted
> only if the subtype_mark denotes an unconstrained composite subtype;
> if there is no constraint, then the subtype_mark shall denote a definite
> subtype."
but this one does not forbid to write a scalar_constraint when the
subtype_mark is a composite type...
--
rien
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: Syntax question: new with a constrained subtype indication
2005-04-25 16:34 ` James Alan Farrell
@ 2005-04-25 18:52 ` Martin Krischik
2005-04-26 14:17 ` James Alan Farrell
0 siblings, 1 reply; 13+ messages in thread
From: Martin Krischik @ 2005-04-25 18:52 UTC (permalink / raw)
James Alan Farrell wrote:
>>> V := new Vector range 1 .. 10;
>>
>>
>> What I've done quite frequently is
>>
>> V := new Vector (1 .. 10);
>>
>
> Yes, but the problem is I need to account for the WHOLE EBNF, not just
> what I would do. (ie, I need to account for ANYTHING my customers MIGHT
> do)
How is he going to do it when the compiler won't compile it?
Martin
--
mailto://krischik@users.sourceforge.net
Ada programming at: http://ada.krischik.com
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: Syntax question: new with a constrained subtype indication
2005-04-25 16:34 ` Martin Krischik
@ 2005-04-25 19:03 ` James Alan Farrell
0 siblings, 0 replies; 13+ messages in thread
From: James Alan Farrell @ 2005-04-25 19:03 UTC (permalink / raw)
Martin Krischik wrote:
Yes, I see it now. Thanks.
> James Alan Farrell wrote:
>
>
>>Hi all,
>>I'm working on software to analyze Ada programs using ASIS. As such I
>>need to make sure I handle all cases in the syntax (and I'm finding that
>>in past assignments I've used only a small portion of the whole language!)
>>
>>I'm currently working on a piece of code to analyze allocator
>>expressions (ARM 4.8). The entire relevant syntax is:
>>
>> 4.8:
>> allocator ::=
>> new subtype_indication | new qualified_expression
>>
>> 3.2.2:
>> subtype_indication ::= subtype_mark [constraint]
>>
>> 3.2.2:
>> subtype_mark ::= subtype_name
>>
>> 3.2.2:
>> constraint ::= scalar_constraint | composite_constraint
>>
>> 3.2.2:
>> scalar_constraint ::=
>> range_constraint | digits_constraint | delta_constraint
>>
>> 3.5
>> range_constraint ::= range range
>>
>> 3.5:
>> range ::= range_attribute_reference
>> | simple_expression .. simple_expression
>>
>> From this, it appears I should be able to do something like:
>>
>>package body Mod1 is
>>
>> procedure Proc1 is
>>
>> type Vector is array (Integer range <>) of Float;
>> type VectorPtr is access Vector;
>>
>> V : VectorPtr;
>>
>> begin
>> V := new Vector range 1 .. 10;
>
>
> V := new Vector (Integer range 1 .. 10);
>
> or shorter:
>
> V := new Vector (1 .. 10);
>
> You mixed the syntax for integer types:
>
> http://en.wikibooks.org/wiki/Programming:Ada:Types:range
>
> with the one for arrays
>
> http://en.wikibooks.org/wiki/Programming:Ada:Types:array
>
> The later need the ( and ).
>
>
>> end Proc1;
>>
>>end mod1;
>>
>>Using gnat 3.15p, I get this error:
>>
>>mod1.adb:15:23: incorrect constraint for this kind of type
>>gnatmake: "mod1.adb" compilation error
>>
>>I suspect this is a type issue, but let me know if there is something
>>incorrect in the syntax.
>>
>>The thing is, I've read 4.8 of the *annotated* ARM and I don't see
>>anything to say I cannot do this. So what am I missing? Is there
>>something, other than that document, that spells out the allowed types
>>of constraint?
>
>
> Your idea was right - just a syntax problem.
>
> Have fun
>
> Martin
>
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: Syntax question: new with a constrained subtype indication
2005-04-25 17:22 ` Adrien Plisson
@ 2005-04-25 23:28 ` Randy Brukardt
0 siblings, 0 replies; 13+ messages in thread
From: Randy Brukardt @ 2005-04-25 23:28 UTC (permalink / raw)
"Adrien Plisson" <aplisson-news@stochastique.net> wrote in message
news:426d275e$0$1403$ba620e4c@news.skynet.be...
> Martin Dowie wrote:
> > What about 4.8(4):
> >
> > "An initialized allocator is an allocator with a qualified_expression.
An
> > uninitialized allocator is one with a subtype_indication. In the
> > subtype_indication of an uninitialized allocator, a constraint is
> > permitted
> > only if the subtype_mark denotes an unconstrained composite subtype;
> > if there is no constraint, then the subtype_mark shall denote a
definite
> > subtype."
>
> but this one does not forbid to write a scalar_constraint when the
> subtype_mark is a composite type...
Looking at the RM, I think that the rules for range_constraint cover this
(but not very clearly). The bounds of a the range have to have the type of
the subtype_mark - 3.5(5). Thus
Vector range 1 .. 10;
is illegal, because "1" and "10" aren't Vectors. What is not certain to me
is why
Vector range V1 .. V2;
is illegal. (It's nonsense, of course, but ignoring that...) 3.5(4) says
that a range specifies some subset of scalar values, but there don't seem to
be any rules that back this up (at least, I couldn't find any) - and 3.5(4)
is written in the conversational style of introductory text. I would have
expected a legality rule ("the bounds of a range shall have a scalar type"
or something like that) to reinforce this.
I guess I'll have to rely on Dewar's rule (the Standard never says anything
that requires nonsense) here. Still, 3.5(4) makes the intent clear, so it
doesn't seem worth fixing.
As previously noted, the constraint in an allocator is only permitted for
composite types, and a range must be scalar, so clearly you can't have a
range_constraint in an allocator.
Randy.
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: Syntax question: new with a constrained subtype indication
2005-04-25 18:52 ` Martin Krischik
@ 2005-04-26 14:17 ` James Alan Farrell
0 siblings, 0 replies; 13+ messages in thread
From: James Alan Farrell @ 2005-04-26 14:17 UTC (permalink / raw)
Martin Krischik wrote:
> James Alan Farrell wrote:
>
>
>>>> V := new Vector range 1 .. 10;
>>>
>>>
>>>What I've done quite frequently is
>>>
>>>V := new Vector (1 .. 10);
>>>
>>
>>Yes, but the problem is I need to account for the WHOLE EBNF, not just
>>what I would do. (ie, I need to account for ANYTHING my customers MIGHT
>>do)
>
>
> How is he going to do it when the compiler won't compile it?
>
> Martin
You mean as a result of syntax errors? We only need to worry about
syntactically correct programs, but we still need to worry about any
syntactically correct program.
So the question was, why was the compiler not compiling it? Is it some
caveat in the gnat compiler, and other compilers would handle it?
And the answer to the question (as we've found) is that words in 3.2.2
show that this sort of constraint is inappropriate for an array. Ie, it
is incorrect, not because of something in the EBNF, but because of
someting outside of the EBNF.
BTW, it SHOULD be that all Ada compilers compile the same stuff in the
same way. But compilation is a subtle process. We always run our
software through ACATS to ensure we can handle anything the user might
throw our way. And yet, in the past we have come up with examples that
crash the program. Or to put it another way, the example contained
something ACATS did not. Therefore there are things certified compilers
are not tested for.
(sorry don't have an example handy. Once that problem was corrected we
moved on and I don't recall what it was)
J.
^ permalink raw reply [flat|nested] 13+ messages in thread
end of thread, other threads:[~2005-04-26 14:17 UTC | newest]
Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-04-25 15:44 Syntax question: new with a constrained subtype indication James Alan Farrell
2005-04-25 16:07 ` Alex R. Mosteo
2005-04-25 16:34 ` James Alan Farrell
2005-04-25 18:52 ` Martin Krischik
2005-04-26 14:17 ` James Alan Farrell
2005-04-25 16:18 ` Adrien Plisson
2005-04-25 16:37 ` James Alan Farrell
2005-04-25 17:16 ` Adrien Plisson
2005-04-25 16:34 ` Martin Krischik
2005-04-25 19:03 ` James Alan Farrell
2005-04-25 17:02 ` Martin Dowie
2005-04-25 17:22 ` Adrien Plisson
2005-04-25 23:28 ` Randy Brukardt
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox