[GRASS5] r.mapcalc problem

[5.3 CVS/HEAD from a couple of weeks ago]

I'm trying to use r.mapcalc from the command line, with a map name which
contains a dash ("-"). I'm getting this error:

GRASS5.3> r.mapcalc \"a-b\"=1
invalid map: a-b
syntax error, unexpected '=', expecting $end
Parse error

The "invalid map" above doesn't exist yet, if I make something called
'a-b', the invalid map error disappears but the syntax/parse errors
remain.

does anyone know where I'm going wrong, or is this a r.mapcalc bug?

If I understand correctly, quoted map names containing operators should
be ok on both sides of the equation..

?

thanks,
Hamish

Hamish wrote:

I'm trying to use r.mapcalc from the command line, with a map name which
contains a dash ("-"). I'm getting this error:

GRASS5.3> r.mapcalc \"a-b\"=1
invalid map: a-b
syntax error, unexpected '=', expecting $end
Parse error

The "invalid map" above doesn't exist yet, if I make something called
'a-b', the invalid map error disappears but the syntax/parse errors
remain.

does anyone know where I'm going wrong, or is this a r.mapcalc bug?

It's a mapcalc limitation. The documentation isn't clear on whether
this is meant to work.

If I understand correctly, quoted map names containing operators should
be ok on both sides of the equation..

I don't know whether they "should" be OK, but they aren't.

Currently, the LHS of an assignment has to be a NAME or VARNAME, both
of which are defined by the regexp:

    [^ ^#@,"'\000-\037()\[\]+\-*/%><!=&|?:;]+

[The difference is that a VARNAME has already occurred as the LHS of a
previous assignment, whereas a NAME hasn't.]

The syntax for map names where they occur as values (i.e. other than
on the LHS of an assignment) is more general:

  map : STRING
      | NAME
      | NAME '@' NAME { $$ = composite($1,$3); }
      ;

[STRING is an arbitrary quoted string.]

The attached patch will allow quoted strings to occur on the LHS of an
assigmnent (at least from the perspective of the parser; there may be
other factors which I've overlooked).

More generally, I think that we should reconsider the behaviour of
assignment. Currently, the semantics are far from clear.

At a minimum, we should probably refactor the grammar, so that
top-level assignments (i.e. map creation) are handled separately to
variable assignments.

--
Glynn Clements <glynn.clements@virgin.net>

(attachments)

mapcalc-grammar.diff (324 Bytes)

Glynn Clements wrote:

More generally, I think that we should reconsider the behaviour of
assignment. Currently, the semantics are far from clear.

At a minimum, we should probably refactor the grammar, so that
top-level assignments (i.e. map creation) are handled separately to
variable assignments.

I've now done that, and made a couple of other minor alterations. The
consequences of the change are:

1. You can now use quoted names on the LHS of an assignment.

2. You can now reference qualified (map@mapset) maps where one of the
two parts happens to be a variable name without having to use quotes.

3. You can now access functions which have the same name as a
variable.

4. A top-level statement which isn't an assignment now generates a
parse error rather than an internal error.

5. You can no longer create a map with the name of an existing
variable. A corollary of this is that you can no longer create a map
twice.

Points 1-4 shouldn't be contentious, as they should only affect inputs
which would previously resulted in an error.

Point 5 is based on the assumption that explicit failure is usually
preferable to a silent wrong answer. It eliminates the risk of:

  r.mapcalc "a = ... ; $foo = ... ; b = a"

silently producing bad data for b where $foo happens to be "a".

Behaviour which is unchanged includes:

1. Map definitions also create a variable which can be read in
subsequent expressions.

2. Variables can be "overwritten" (more accurately: reassigned).

A substantial number of scripts rely upon these two points (primarily
point 1, although at least shade.rel.sh relies upon point 2).

The situation still isn't ideal, IMHO, but I'm not sure what else can
be done while maintaining compatibility with the original r.mapcalc
(and with existing scripts). Although, it might be possible to
restrict variable assignments (not counting map definitions) to within
eval(); all of the cases which I've seen fit into that pattern.

--
Glynn Clements <glynn.clements@virgin.net>