by chimeracoder · 13y ago · 0 comments
We actually had this discussion on #go-nuts a few weeks ago - part of the confusion stems from the fact that most language compilers enforce requirements that aren't covered by the parser. In other words, a parser oftentimes accepts (fails to reject) invalid strings.
Easy examples of requirements that violate context-freedom are the requirement that variables be declared before use, or the inability to divide an integer literal by 0. (The latter is a compiler error in Go[0] - I'm not sure about C++, but I know the former is a requirement in both languages).
Finally, we should make a distinction - even if the set of valid programs could be described perfectly by a context-free grammar (and therefore implemented by a pushdown automaton), chances are the actual parser for the language's compiler doesn't implement the full set of rules, which again reinforces the point that most parsers are biased in their outcome (biased in favor of accepting potentially invalid strings).
> If you look up the definition of context-free languages, it will basically tell you that all grammar rules must have left-hand sides that consist of exactly one non-terminal symbol
That's not exactly true - one can rewrite (normalize) a context-free grammar into this form, but CFGs are often written in an "unnormalized" form which has multiple nonterminal symbols, because it tends to be clearer to read in many cases.
[0] As demonstrated here, the compiler will actually reject the division-by-zero at compile-time: http://play.golang.org/p/3gwDeaoiM2
For example, in a context free language, if "A : B;" is a variable declaration, A is always a type and B is always a variable name. In the expression "C = C + 1", C would always be a variable name even if C has not been declared. In this case the error will be detected in the next compilation phase but not during grammar checking.
In the same ways all type checking are done after grammar checking.
For example :
int i = "iyi";
is a valid line for C++ grammar but invalid for the type checker.The main ppoint is that for some languages those two kinds of checks can not be easily separated. The top answer to the linked question demonstrates that for C++. It occurs that in C++ typen<1>() can be either parsed as a template istantiation or not-parsed (i.e. producing a syntax error) as (typen < 1)>().
The syntactical correctness of this expression depends on the result of evaluation of a fine piece of template metaprogramming - there must be more than one grammar checking phase, each one dependent on the evaluation of the previous one. This gives a good hint on why C++ compile times are sometimes so huge.
But when I looked into it just now, the introduction to the language spec [1] says "The grammar is compact and regular ...". But how can the grammar be regular? According to Wikipedia "regular grammars" describe languages that can be expressed using regular expressions. [2][3] Wouldn't matching parenthesis and braces require at least a context free language instead of regular?
[1] http://golang.org/ref/spec#Introduction
It's a matter of semantics. (doubly so)
These errors that you describe during compilation are defined by what? Not by the syntax (at least not in r5rs Scheme [1]) but instead by the semantics. Conflating the two would, of course, result in the impression of non-context-free language syntaxes because if we require the syntax to also be the semantics of the language then the syntax must be able to evaluate the program i.e. the syntax must describe a turing machine.
I think the confusion is compilation. Compilation is a series of evaluation steps taken before complete evaluation.
All the errors (as they exist in Scheme) that you describe are DEFINED by the semantics of r5rs Scheme. For example, the semantics of variable reference are:
E[[I]] = λρκ . hold (lookup ρ I) (single(λ . = undefined → wrong “undefined variable”, send κ)) [r5rs, Section 7.2.3]
Which basically says, if the variable has been bound, then use its value to continue the evaluation. If it is unbound, then stop evaluating and signal the error "undefined variable".
====
"That's not exactly true - one can rewrite (normalize) a context-free grammar into this form, but CFGs are often written in an "unnormalized" form which has multiple nonterminal symbols, because it tends to be clearer to read in many cases."
I think you misunderstand what the OP is stating. OP refers to the left hand sides:
E -> E + E
| E * E
Certainly the left hand side in a CFG normally has a single non-terminal. I suppose you could write a grammar with the left hand sides containing multiple non-terminal symbols and then, if it turns out the grammar you wrote is in fact context-free, you could re-write it in the above form, but does anyone actually do this?[1] http://www.schemers.org/Documents/Standards/R5RS/HTML/r5rs-Z...
It's usually just a warning, and runtime error. It's actually used in a few cases to attempt to kill a process.
Apple's abort(), for example, uses it: http://www.opensource.apple.com/source/Libc/Libc-262/stdlib/...
Which Lisp you have in mind? Surely not Common Lisp, which has Turing-complete syntax.
By 'closest', I simply meant that these deviations are easy to analyze or account for in an informal sense - for example, by forbidding/exempting user-defined macros. (Naturally, they cannot be precisely accounted for without executing the program or solving the halting problem, since the definition of the macro may itself require executing arbitrary code).
The deviations of languages like C++ and Java, for example, are hairier to try and enumerate, even informally (imprecisely) - I remember doing this as an exercise for my compilers class in school.