Re: Why are ambiguous grammars usually a bad idea? Why are languages usually defined and implemented with ambiguous grammars?

Kaz Kylheku <480-992-1380@kylheku.com>
Thu, 30 Dec 2021 18:14:50 -0000 (UTC)

          From comp.compilers

Related articles
| List of all articles for this month |
From: Kaz Kylheku <480-992-1380@kylheku.com>
Newsgroups: comp.compilers
Date: Thu, 30 Dec 2021 18:14:50 -0000 (UTC)
Organization: A noiseless patient Spider
References: 21-12-003 21-12-017 21-12-022
Injection-Info: gal.iecc.com; posting-host="news.iecc.com:2001:470:1f07:1126:0:676f:7373:6970"; logging-data="56449"; mail-complaints-to="abuse@iecc.com"
Keywords: parse, design
Posted-Date: 30 Dec 2021 13:56:13 EST

On 2021-12-30, gah4 <gah4@u.washington.edu> wrote:
> On Wednesday, December 29, 2021 at 2:28:34 PM UTC-8, Kaz Kylheku wrote:
>
> (snip)
>
>> I also believe there is one more element at play: mathematics. People
>> study mathematics in school, and those who go on to do programming tend
>> to be ones who were more exposed to it or paid more attention.
>
> This reminds me of learning associativity of exponentiation (**)
> in Fortran IV (I believe it isn't in the Fortran 66 standard) before I
> learned it in algebra class. I suspect that there are others I learned
> from programming before learning them in math class


In Common Lisp, the expt function is strictly binary, so it eliminates
the question of associativity. Some basic arithmetic functions are
n-ary, like (+ a b c d e f), where that is documented (and readily
understood) that in cases where it matters, it is left-to-right
reduction.


In TXR Lisp, I made expt n-ary, so you can write


    (expt x y z w)


But! The associativity is right-to-left, making that equivalent to:


    (expt x (expt y (expt z w)))


This is for two reasons. One is math: (expt x y z w) defined this
way follows:


          w
        z
      y
    x


secondly, it is more useful, becuase the left-to-right interpretation
is:


  ((( y) z) w)
  (((x ) ) )


and that is just


  (expt x (* y z w))


which is easy enough to write if that's what you want! It's not much
more verbiage than the (expt x y z w) you may have wanted. Regardless of
the number of operands, it's just an extra set of parentheses and a *
operator.


Whereas if you want (expt x (expt y (expt z w)) and (expt x y z w)
doesn't give it to you, that *is* a lot of verbiage, whose nesting grows
with each additional argument.


The associativity rule that saves the most verbiage is the better one,
even if it is opposite to many other arithmetic functions.


>> People who are programmers actually had a first contact with formal
>> syntax in mathematics.
>
>> The conflation between syntax and semantics may ultimately come from
>> that place. Mathematicians design their notations deliberately, in such
>> ways that when they manipulate symbols, while observing certain rules,
>> they are actually preserving semantics. The notation directly enables
>> semantically meaningful manipulation, as a tool of thought.
>
> I suspect that people learn some things in the first programming language
> that they learn, and then expect it to be the same in others. When it isn't,
> people get surprised or confused.
>
> When I started with unix, I learned csh programming, and mostly
> avoided sh (and successors). One reason for that is, as well as I
> knew at the time, differences in the syntax and semantics of them.
> [Fortran has always had ** exponentiation, starting with the original
> version in 1956. It always bound tighter than +-*/ but wasn't
> associative, A**B**C not allowed, -John]


When I started programming from nothing, I saw BASIC examples in a
book which was doing things like:


    10 X = 2
    20 X = X + 1


The only language with formulas that I was coming from was math.
(Though I was only in grade 6, I know how to solve systems of linear
equations from school, because I had recently come to Canada from
Slovakia.)


So, I thought, what? How can X be equal to X + 1; you cannot solve
this absurdity!


From then I knew that the people who program computers to understand
symbols are free thinkers who make them mean anything they want.


--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal


Post a followup to this message

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