Please use the alphabetical Index, Contents List, and your browsers search feature to find what you need.

This page generated at Thu Mar 4 08:28:29 PST 1999. This page is a sample piece of documentation generated from smalltalk.syntax.mth . For more information see the documentation on Dr. Botting's MATHS project.


    .Title XBNF of Little Smalltalk Version 3.0 Based on samples and Appendix 2 of Timothy Budd's "A Little Smalltalk" QA76.6 B835 1987 Addison-Wesley ISBN 0-201-10698-1

    [ smalltalk.glossary ] [ smalltalk.methods.syntax ] [ glossary.objects ] [ math.syntax ] [ comp.text.ASCII.html ]

    Interactive User Input

    The user supplies a series of commands to the interpreter each terminated by an end_of_line character and containing a special kind of expression:
  1. user_input::=#command CTRL_D.
  2. command::= cascaded_expression EOLN.

    Smalltalk 3 prints the result of each command.

  3. user_output::=prompt #(output_of_command value_of_command EOLN prompt).

    Example commands

    1. 1 + 2
    2. Number viewMethod: #+
    3. 5 factorial
    4. #(1 2 3) size
    5. 'cat' at: 3 put: p; at: 2 put: u

    Summary of Semantics

    The interpreter reads a line of user input and parses it according to the syntax. The syntax determines where to start evaluating the expression - with the simplest subexpression. The syntax also determines, for expression two parts: an object, and a message that it will be sent. The interpretter works by sending a message to an object and the getting a value back. This is also an object which can either be sent more messages, or can become part of a message that is sent to another object (as determined by the syntax) or even both.

    When a message is sent to an object the object's class determines what to do with it. Each class has a dictionary of message templates against which the incoming message is matched. If there is no matching template then the message is sent to the classes superclass (parent), and so on. When a matching message is found, then the arguments in the incoming message are associated with the arguments in the template and a piece of Smalltalk code called the method for that message is interpretted. In this code there are local variables, global (shared) variables, and the arguments from the message - and all of these are assigned to objects. The variable 'self' is object that got the message as well. The method is already compiled into a special machine independent "byte code". This is a series of compiled expressions that are obeyed one step at a time. Each expression is interpretted by the rules in this paragraph.

    Ultimately a value comes out of the expression and this is printed. Meanwhile the methods that have been obeyed may have produced output and may also have changed the environment in which they exist, and themselves in complicated ways.

    .Details /u/faculty/cs320/smalltalk.semantics

    Expressions and Objects

    The Basic Smalltalk expressions are organised in a hierarchy:
    keyword expression'cat', 'dog' at: 2 put: $o
    binary expression1 + 5 factorial
    unary expression 5 factorial
    simple expression 5
    The higher ones contain the lower ones inside themselves. The lower ones are evaluated first. A simple expression can be a complex expression in parentheses.

  4. keyword_expression::= binary_expression keyword_message.
  5. keyword_message::=#(keyword binary_expression). So
  6. |-keyword_expression = binary_expression #(keyword binary_expression).

    Notice that each keyword_expression has a keyword_message, and that the message is made up of keywords and actual parameters (expressions). If you delete these actual arguments and concatenate the keywords you get the keyword_selector of the message that Smalltalk uses to identifier that message uniquely.

  7. binary_expression::= unary_expression #binary_message.
  8. binary_message::=binary_selector unary_expression.
  9. |-binary_expression= unary_expression #(binary_selector unary_expression).

  10. unary_expression::= primary #unary_selector.

  11. primary::= variable | literal | block | primitive | "(" cascaded_expression ")".

  12. cascaded_expression::= keyword_expression #( ";" continuation).

  13. continuation::= N(message).

  14. message::= (unary_selector | binary_message | keyword_message).

    Notice that a unary selector and a keyword differ solely in the colon at the end of the latter. In Smalltalk we often use a unary method (foo) to extract data from an object and the associated keword message (foo:) to place data in the object.


  15. variable::=(lower_case_letter #(letter|digit)) & (globalName | local_name).

    The local_names depend on the context. In a method there will be variables used in the method, plus arguments passed in the message, plus variables associated with each instance of a class. In a block the local variables are those in the block's context plus the inputs to the block.

  16. local_name::=instance_variables_of_object_s_class | variables_of_current_method| arguments_passed_to_object_in_message | inputs_to_the_block.

    The tradition is to use English words like this:

  17. traditional_variable::= first_word #later_word,
  18. first_word::=N(lower_case_letter) & English,
  19. later_word::=(upper_case_letter #lower_case_letter ) & English.

    Control Structures

    Uniquely Smalltalk does not have a special syntax for control structures. Instead the class of Boolean objects ({true, false}) and the class of Blocks ( see later ) together have messages that have methods that work like iterative and selective structures.

  20. loop::structure=See the methods whileTrue: do: .... [ smalltalk.methods.html ]
  21. selection::structure=See methods ifTrue: ifFalse: ifTrue:ifFalse: and: ifFalse:ifTrue: .... [ smalltalk.methods.html ]

    Representations of Objects in Expressions


    Expressions contain literal representations of objects:
    #+Symbol(for a message)
    #(1 2 'cat')Array
    <120 3>Primitive command
    [ :x | x squared ]Block

  22. literal::= number | symbol | string | character_constant | bytearray | array_constant.

  23. number::= (sign|) unsigned_number,
  24. sign::= "+" | "-" ,
  25. unsigned_number::=unsigned_fraction ("e" sign unsigned_integer|),
  26. unsigned_fraction::=unsigned_integer("." unsigned_integer|),
  27. unsigned_integer::= N(digit).

  28. string::=q #(char~q | q q) q where q:="'".

  29. character_constant::="$" char.

  30. symbol::="#" N(char). Names and messages have associated symbols.

  31. byte_array::= "#" "[" N(unsigned_integer) "]".

  32. array_constant::="#" array.
  33. array::="(" #( number | symbol | string | character_constant | array) ")".

  34. primitive::="<" ( variable | primitive unsigned_integer ) # primary ">".

  35. block::= simple_block | unary_block | binary_block | ternary_block,
  36. simple_block::="[" statements "]".
  37. unary_block::="[" input_var "|" statements "]".
  38. binary_block::="[" input_var input_var "|" statements "]".
  39. ternary_block::="[" input_var input_var input_var "|" statements "]".
  40. input_var::= ":" variable.

    . . . . . . . . . ( end of section Representations of Objects in Expressions) <<Contents | Index>>

    Method (for handling a message)

  41. method::= method_pattern (temporary_variables|) statements.

  42. method_pattern::=unary_selector | binary_selector argument_variable | N(keyword argument_variable).

    Notice that each method has a unique symbol associated with it made up by concatenating all the selectors and prefixing a hash_mark("#").


  43. statements::= #( expression #("." expression) ).
  44. expression::= ("^" |) #(variable "<-") cascaded_expression.


  45. unary_selector::="sqrt" | "squared" | "respondsTo" | "addSubClass" | ... .
  46. unary_selector::=lower_case_letter identifier.

  47. binary_selector::="+"|"-"|"/"|"*"|"**"|... .
  48. binary_selector::= b | b b where b:= char~(letter | digit | parentheses | brackets | semicolon | period).

  49. keyword_selector::="max:" | "between:and:" | "and:" |... .
  50. keyword_selector::=N(keyword).
  51. keyword::="respondsTo:" | "max:" | "between:" | "to:" |...,
  52. keyword::=lower_case_letter identifier ":".

    Syntax of Loaded Files

    This syntax is empirical and speculative...

  53. loadable_input::=#(loadable_method | loadable_class | loadable_comment) EOF.
  54. loadable_class::= "Class" name superclass #instance_variable EOLN | "Class" previously_declared_class EOLN method EOLN "|" EOLN,

  55. loadable_method::= "Methods" class something EOLN method #( "|" EOLN method ) "]" EOLN.
  56. something::="'"initialization"'" | "'" all "'" |... ,

  57. loadable_comment::= "*" comment EOLN,

    Class Description (Not input!)

    This is an ideal class description. When a file is loaded a different but similar syntax is used (see loadable_input above).

  58. class_description::=class_heading (instance_variables|) protocol.

  59. class_heading::= "Class" class_name (colon superclass_name|).
  60. class_name::= upper_case_letter identifier.

  61. instance_variables::= "|" #instance_variable "|".

  62. protocol::= "[" method #( "|" method ) "]".

Formulae and Definitions in Alphabetical Order