[Skip Navigation] [CSUSB] / [CNS] / [Comp Sci Dept] / [R J Botting] / [Samples] / lisp.syntax
[Index] [Contents] [Source Text] [About] [Notation] [Copyright] [Comment/Contact] [Search ]
Tue Sep 18 15:26:07 PDT 2007

Contents


    Syntax of LISP

      Lexical Elements of LISP

      Many of the undefined terms in this document like cond, car, cdr, and so on are lexemes: [ lisp.lexemes.html ] [ comp.text.ASCII.html ]
    1. dot::= See http://csci.csusb.edu/dick/samples/lisp.lexemes.html#dot

      Syntax of LISP

      The two fundamental pieces of LISP syntax are the idea of a list of things and the dotted pair:
    2. For any set of strings X, list_of(X)::="(" # X ")",
    3. For any set of strings X, dotted_pair(X)::="(" X dot X ")".

      A dotted pair is the external image of a node in an internal data structure with two components called "car" and "cdr" for historical reasons. Each component in the pair points at an atomic item or another list.

      Each list is coded internally as dotted pairs:

       	(A) = (A.NIL).
       	(A B) = (A.(B.NIL)).
       	(A B C ) = (A.(B.(C.NIL)) ).
       	(A B C D ... Y Z) = (A.(B.(C.(.D ... (Y. (Z. NIL)) ... ).

      After the lexical analysis the interpreter parses input as a list_structure before evaluating it. A list_structure is empty, or a dotted_pair, or an atom, or any number of lists inside a pair of parenthesis. We often call a list structure a list for short. But always be careful to distinguishing 'a list of numbers' from a 'list structure of numbers'

    4. list_structure::=atom | dotted_pair(list_structure) | list_of(list_structure).

      Note. In old LISP, commas were optional separators in lists... but not in XLISP and Scheme. Note. The separating spaces, tabs, etc in a list are all sieved out by the lexical scan. This scan also removes comment lines that start with a semicolon [ comment in lisp.lexemes ]

    5. atom::= [ atom in lisp.lexemes ]

      Syntax of S_expressions

        Note: Syntax assumes that a lexical analysis process has already divided the input into lexemes and deleted any extra whitespace.

        Constants

      1. constant::= nil | true | number | string | quoted_list,
      2. word::= [ word in lisp.lexemes ]
      3. string::= [ string in lisp.lexemes ]

      4. quoted_list::="(" "QUOTE" list_structure ")" | single_quote list_structure, -- The second form is an abbreviation for the first.
      5. quoted_list::="( QUOTE ( A B C ) )" | " '( A B C )" | ....

        Expressions

      6. expression::=S_expression | macro_call.

      7. S_expression::=Syntax_expression.
      8. S_expression::= constant | "(" function #expression ")" | variable. -- The function determines the correct number of expressions.
      9. function::=car | cdr | cons | ...,
      10. function::= word & (predefined_function | created_by_define ).

      11. macro_call::="(" macro_name #list_structure "), -- In a macro the arguments are not automatically evaluated and can be anything at all.
      12. macro_name::=cond | define | lambda |...,
      13. macro_name::=word & (predefined_macro | created_by_defmacro).

      14. macro::=lambda_form | conditional | definition |...,
      15. lambda_form::= "(" lambda list_of(word) expression ")".
      16. conditional::= "(" cond #( "(" expression #expression ")" )")".
      17. definition::="(" define "(" function arguments ")" #expression ")" | "(" defun function list_of(word) #expression ")".

      18. program::=Not in pure LISP!,
      19. program::="(" progn #expression ")". program:semantics::=The n expressions are evaluated in sequence and the last value (the n'th) is printed.

      20. block::="(" let "(" #local_variable")" $expression ")".
      21. local_variable::= "(" atom expression ")".
      22. block::semantics=each local variable is bound to the value of the expression next to it and then the expressions after the local variables are executed.

      23. arguments::= #word.

      24. variable::=word & (global | local).

        'global' variables are created by commands like 'setq' by the user. 'Local' variables are created in lambda_forms, definitions, LETs, PROGs,.... and deleted at the end of the expression or form.

      . . . . . . . . . ( end of section Expressions) <<Contents | End>>

    . . . . . . . . . ( end of section Syntax) <<Contents | End>>

End