Re: Variable Declaration syntax?

torbenm@app-4.diku.dk (=?iso-8859-1?q?Torben_=C6gidius_Mogensen?=)
8 Apr 2006 16:04:35 -0400

          From comp.compilers

Related articles
Variable Declaration syntax? nvn_ravi@sify.com (Ravi) (2006-04-03)
Re: Variable Declaration syntax? cfc@shell01.TheWorld.com (Chris F Clark) (2006-04-08)
Re: Variable Declaration syntax? torbenm@app-4.diku.dk (2006-04-08)
| List of all articles for this month |

From: torbenm@app-4.diku.dk (=?iso-8859-1?q?Torben_=C6gidius_Mogensen?=)
Newsgroups: comp.compilers
Date: 8 Apr 2006 16:04:35 -0400
Organization: Department of Computer Science, University of Copenhagen
References: 06-04-014
Keywords: design, syntax
Posted-Date: 08 Apr 2006 16:04:35 EDT

"Ravi" <nvn_ravi@sify.com> writes:


> I like to known why in most of the programming languages has a constran
> like the first character of the variable declaration should not be any
> special character,numbers,etc.
>
> [The short answer is that if you allow arbitrary punctuation in
> names, you can't tell the names from the punctuation. -John]


To elaborate on John's comment, the problem is to distinguish names
from predefined symbols like keywords, operators and delimiting
symbols. But just like most languages allow variable names to contain
keywords (so you, for example, can have variables called "lift" or
"down", even though they contain "if" and "do", respectively), some
languages allow names to contain punctuation symbols and use rules
like those for variables to disambiguate.


The main problem now becomes deciding when a symbol ends and another
begins. A simple solution is to set aside a number of characters
that are not allowed in names and require any two symbols (names,
keywords or other) to be separated by these symbols.


For example, in Scheme (a LISP dialect), you can use (almost) any
characters except whitespace and parentheses in names, so valid names
include &q, **~ and so on. This is workable because Scheme uses
parenthesis to delimit syntactic constructs, so it doesn't need infix
operator symbols or keywords that are parsed in a special way.


But this quickly breaks down if you have a more traditional syntax, as
such a rule would require you to add spaces between many symbols that
you would normally just write together, e.g., "2+3" would be a single
symbol so you would have to write "2 + 3" to add 2 and 3. Haskell
(another functional language) uses a refinement of the Scheme idea:
Characters are divided into two subsets: The alphanumeric and anything
else (except whitespace, parentheses, quotes and a few other). A
symbol can be composed of any characters within one of these subsets,
and you only need whitespace to separate two symbols if they are from
the same category. Hence, "2+3" works as you would expect, but
"x+++y" consists of exactly three symbols, as there is no rule (like
in C) that forces a split of "+++" into "++" and "+". This works
reasonably well in practice. Additionally, you can assign operator
precedences to non-alphanumeric symbols.


                Torben


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.