by lispm · 9y ago · 0 comments
Lisp has a two-level syntax. Remember: syntax decribes the structure of valid programs in a language.
First level is the syntax of s-expressions and its data types: numbers, symbols, strings, conses, lists, characters, vectors, ...
The second level is the syntax of the programming language Lisp:
* function calls
* built-in special operators: IF, LET, FUNCTION, PROGN, ... together with lambda expresssions
* macros: macros can provide arbitrarily complex syntax. Examples: DEFUN, DEFCLASS, DEFMETHOD, DEFMACRO, HANDLER-CASE, LOOP, DOTIMES, ...
Could we say that concetanative languages like forth have no syntax. It seems to me there is no AST in those languages, just a stack.
I'm not formally trained in CS, so that's my own individual opinnion.
LET is an example. LET provides bindings for variables:
LET
a list of bindings
a sequence of declarations
a sequence of expressions
(LET ((a 1)
(b 2)
c)
(declare (integer a))
(declare (type (integer 0 100) b)
(setq c (+ a b))
(expt c 2))
The EBNF syntax for LET is: let ({var | (var [init-form])}*)
declaration*
form*
DECLARATION and FORM have more syntax.If Lisp sees a LET form which does not use above syntax, then it is a syntax error. A compiler or interpreter will tell you that. The compiler will detect that at compile time.
The following forms are all errors:
(let a 10 (+ a 10))
(let (a 10) (+ a 10)) ; 10 is not an identifier
(LET (((A) 10)) (+ a 10)) ; (a) is not an identifier
(let ((a 10)) (+ a 10) (declare (integer a))) ; declaration at wrong place
(let ((a = 10)) (a + 10)) ; Lisp has no infix operators...1. Lisp is written preorder; Forth is written postorder.
2. The number of children a node in your Lisp AST has is marked explicitly, by parentheses, whereas the number of children a node in your Forth AST has is defined externally.
There is no difference between a Lisp-style
(+ (* 3 5 8) (+ 4 1))
and a Forth-style 3 5 8 * * 4 1 + +
except that the * in Forth-style can't take a variable number of arguments. But if you added parens to Forth, it could: ((3 5 8 *) (4 1 +) +)> the number of children a node in your Forth AST has is defined externally
and that's basically why.
Here's a 1000 node AST:
foo
/ \
Where are the rest of the nodes? Oh, they are defined externally. 3 5 8 3 N* 4 1 + +
The equivalent of (+ (* 3 5 8) 4 1) could be 3 5 8 3 N* 4 1 3 N+