I've recently undertaken the task of writing a stack-based programming language. Before I started designing my language however, I thought it'd be a good idea to read and experiment with existing stack-based languages.
This brings me to the topic of this post. I was reading through the Wikipedia article on Forth, a stack-based language which uses postfix style expressions. In the article, I saw the following statement:
Forth's flexibility makes a static BNF grammar inappropriate, and it does not have a monolithic compiler. Extending the compiler only requires writing a new word, instead of modifying a grammar and changing the underlying implementation.
From my understanding, in Forth lingo, the term "word" seems to be basically synonymous with "subroutine". Given this, the above statement seems strange. Why exactly would the ability to create new functions in Forth make a formal grammar for Forth inappropriate? Why would you need to re-write the grammar for each new subroutine you define? How does writing a new word in the environment constitute extending the compiler? The above statement seems akin to saying that a formal grammar is inappropriate for Python because you can define new functions.
In fact, I decided to attempt to write a BNF style grammar for a simple subset of Forth below:
program ::= stmt+ stmt ::= func | expr func ::= ':' expr+ ';' expr ::= INTEGER | word word ::= ('+' | '-' | '*' | '/' )
The above grammar would seem to cover a valid subset of Forth statements, and doesn't seem that hard to extended to cover all valid statements in the Forth language. Furthermore, if a compiler's parser implements the above grammar, I fail to see how the compiler would ever be extended. The compiler will simply add any new words to it's environment. Only the environment is changed. It almost seems as if the above excerpt from Wikipedia is conflating the underlining code that composes the compiler (which does not change) with the compiler's environment (which does change).
In summary, why would Forth's ablitiy to define new words (subroutines) make is inappropriate for a written grammar?