I agree with demerphq's argument about programming languages generally not being context-free. The loose structure and syntax are all roughly context-free, but the example of requiring variables to be predefined before their use shows that any CFG wouldn't work. There is a difference between syntactically well-formed inputs to the parser/compiler and programs that actually run/compile.

Now I have a more general question for the theoriticians: If a programming language were indeed a CFL, would it have the power to be as expressive as a Turing machine? In other words, can a programming language that is represented completely via a CFG be able to express all computable programs?

This dilemma is a little beyond my expertise in the area. At first glance seems as though the specification for all possible Turing machines could not be expressed with a CFG -- there must be all-but-infinitely-many states, and the transition function must be well-defined. This doesn't seem possible in a CFG, due to the same type of problem with predefining variables, as demerphq correctly points out above. I'm actually proctoring a test this morning in our Theory of Computing class, so I will ask the prof what he thinks and get back to you on it ;)

Anyway, great question dystrophy, and enjoy taking Theory of Computation. Every programmer can use a good dose of theory. Just remember to be nice to your theory TA's, OK? ;)

Update: Well, the good professor seems to recall a Princeton research project that was a compiler that would compile and execute any input at all, including garbage. So there's a CFG-definable program specification which was Turing-expressible. He also thought the set of all Algol 66 or 68 programs might have been context-free. So it would seem that the set of all programs of a language need not be context-sensitive.

blokhead


In reply to Re: original definition vs final language by blokhead
in thread original definition vs final language by dystrophy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.