LISP: Input And Output


Published on

LISP: Input And Output

Published in: Technology
1 Like
  • Be the first to comment

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

LISP: Input And Output

  1. 1. Lisp Input and Output<br />
  2. 2. overview<br />Read function.<br />Parsing of numbers and symbols<br />Macro characters<br />Input functions<br />Output functions<br />Querying the user<br />
  3. 3. Printed representation<br />Lisp presents a representation of most objects in printed representation which is used for input/output purposes.<br />Functions such as print takes a lisp object and send the characters of its printed representation to a stream.<br />The collection of routines that does this is called (Lisp)printer.<br />
  4. 4. What read function accepts<br />Lisp reader accepts characters, interpret them as a printed representation of the lisp object, and construct and return such an object.<br />The reader can also be used as a lexical analyzer for a more general user-written parser.<br />The reader is organized as a recursive descent parser.<br />
  5. 5. The reader operates by reading a character from the input stream and treating it in one of the three ways:<br /><ul><li>Whitespace characters are served as separators but otherwise ignored.
  6. 6. Constituent and escape characters are accumulated to make a token, which is then interpreted as number or symbol.
  7. 7. Macro characters trigger the invocation of the functions that can perform arbitrary parsing actions, including recursive invocation of the reader.</li></ul>Every character that appears in the input stream must be one of the following types:<br />Illegal, whitespace, constituents ( [,],{,},?,and !)<br />single escape, multiple escape or macro.<br />
  8. 8. Parsing of numbers and symbols<br />When an extended token is read, it is interpreted as a number or symbol.<br />Whitespace, macro or escape character will always be treated as alphabetic within an extended token.<br />
  9. 9. Lisp syntax for numbers<br />Number::=integer|ratio|floating-point number<br />Integer::=[sign] {digit}+ [decimal-point]<br />Ratio::= [sign] {digit}+ / {digit}+<br />Floating-point-number::= [sign] {digit}* decimal-point {digit}+ [exponent]| [sign] {digit}+ [decimal-number {digit}*] exponent<br />Sign::= +|-<br />Decimal-point::= .<br />Digit::= 0|1|2|3|4|5|6|7|8|9<br />Exponent::= exponent-marker [sign] {digit}+<br />Exponent-marker::= e|s|f|d|l|E|S|F|D|L<br />
  10. 10. Token must satisfy the following requirements:<br />It consists entirely of digits, signs (+ or -), ratio markers (/), decimal points(.), extension characters(^ or _), and number markers.<br />It consists at least one digit.<br />It begins with a digit, sign, decimal point, or extension character.<br />It does not end with a sign.<br />Ex: 1b5000, 777777q, 1.7J, ^-43^, etc.<br />
  11. 11. symbols<br />The following are always treated as symbols:<br />*read-base*<br />The value of read base controls the interpretation of tokens by read as being integers or ratios.<br />Its value is the radix in which the integers or ratios are to be read.<br />The value must be any integer from 2 to 36,and it is normally 10.<br /> for ex if the *read-base* is set as 16(hexadecimal radix), variables with names such as a,b,f , bad and face will be treated by the reader as numbers.<br />/ /5 + 1+ 1-<br /> foo+ - ^ ^/-<br />
  12. 12. *read-supress*if this value is set as nil, the Lisp reader operates normally, when it is not null the most interesting operation of the reader are suppressed.<br />*read-eval* its default value is always t, if *read-eval* is false. Reader macro signals an error.<br />
  13. 13. Macro characters<br />When a macro character is encountered, then the function associated with that macro is invoked and may produce an object to be returned.<br />Macro characters are normally defined as follows:<br /> The left parenthesis initiates reading a pair of list. The function read is called recursively to read successive objects until a right parenthesis is found to be next in the input stream. A list of objects read are returned.<br />
  14. 14. ‘ single-quote provides an abbreviation to make it easier to put constants in the programs.<br />; is used to write comments.<br />“ double-quote character represents the printed representation of a string.<br />A back-quote is followed by a template, a picture of a data structure to be built.<br /># is dispatching macro-character. It reads an optional digit string and then one more character, and uses that character to select a function to run as a macro-character function.<br />
  15. 15. Standard dispatching macro character syntax <br />#x reads in a character object that represents a character x.<br /># ame1 reads in as the character object whose name is name1. The name1 should have the syntax of the symbol.<br /># ’ is an abbreviation for (function foo)<br />A series of representations of objects enclosed by #( and )is read as a simple vector of those objects.<br />If unsigned decimal integer appears between the # and (, it specifies explicitly the length of the vector.<br />
  16. 16. #:foo requires foo to have a syntax of an unqualified symbol name.<br />It denotes an uninterened symbol whose name is foo.<br />Every time an uninterended symbol is created when this symbol is encountered.<br />#,foo is read as the object resulting from the evaluation of the lisp object represented by foo, which may be the printed representation of any lisp object.<br />#B reads rational in binary(radix 2) ex:#B110113<br />#o reads rational in octal(radix 8) ex: #o777511<br />#X reads rational in hexadecimal(radix 16) ex: #xFoo3840<br />
  17. 17. #nA constructs an n-dimensional array<br />Ex: #2A ((0 1 5) (foo 2(hot dog))) represents a 2 cross 3 matrix: 0 1 5<br /> f00 2 (hot dog)<br />#s (name slot1 value1 slot2 value2….) denotes a structure.<br />#+ syntax provides a read time conditionality facility; the syntax is #+feature form<br />#- form is equivalent to #+(not feature) form.<br />If feature is true, then this syntax represents a lisp object whose printed representation is form.<br />If the feature is false, then this syntax is effectively white space.<br />
  18. 18. The following names are standards across all implementations:<br />Newline, space<br />Rubout  delete character<br />Page the form-feed or page-separator character<br />Tab the tabulate character <br />Return carriage return character<br />Linefeed the line-feed character<br />
  19. 19. Read table<br />Read table is the data structure used to control the reader.<br />It has information about the syntax of each character.<br />*read-table* is the current read table<br />To program the reader for a different syntax, begin with the copy of the standard common lisp read table and then customize the individual characters within the copy.<br />Copy-readtable &optional from-readtable to read-table<br />
  20. 20. readtable case readtable is a function used to control the readers interpretation of the case.<br />It provides access to a slot in the read table.<br />The possible values of the slots are:<br />:upcase replaceable characters are converted to upper case<br />:downcase replaceable characters are converted into lower case<br />:preserve the cases of all characters remain unchanged<br />:invert all replicable characters of same case are converted to opposite case<br />
  21. 21. Input functions<br />Characters in inputs take optional arguments called:<br /><ul><li>Input-streamarguments is the argument from which to obtain the input from.
  22. 22. eof-error-p argument controls if the input is from a file, and end of file is reached.
  23. 23. If eof-error-p is true , an error will be signaled at the end of the file. If it is false, then no error is signaled, instead it returns eof-value.
  24. 24. if end-of-file is encountered and eof-error-p argument is not nil, the kind of error that is signaled may depend on the value of recursive-p.</li></li></ul><li>read &optional input-stream eof-error-p eof-value recursive-p<br />reads in the printed representation of the lisp object from the input stream, builds a corresponding lisp object, and returns the object.<br />read-delimited-list char &optional input-stream recursive-p<br />this reads objects from the stream until the next character after an object representation is a char.<br />read-line &optional input-stream eof-error-p eof-value recursive-p<br />read-line a line of text terminated by a new-line.<br />It returns the line as a character string. read-char &optional input-stream eof-error-p eof-value recursive-p<br /> read-char input one character from the input stream and returns it as character object.<br />
  25. 25. Unread-char character &optional input-stream<br /> unread-char puts the character onto the front of the input-stream.<br /> listen &optional input-stream<br /> The predicate listen is true if there is a character immediately available from the input stream, and false if not.<br /> clear-input &optional input-stream<br /><ul><li>Clears any buffered input associated with the input stream. </li></ul>Read-byte binary-input-stream &optional eof-error-p eof-value<br /> read-byte tread one byte from the binary input stream and returns it in the form of an integer.<br />
  26. 26. Output functions that operate on stream of functions<br />These functions take an optional argument output-stream(defaults to variable *standard-output*) on where to send the output.<br /> write object &key :stream :escape :radix :base :circle :pretty :level :length :case :gensym :array: readable: right-margin :miser-width :lines and :pprint-dispatch<br />the printed representation of the object is written to the output stream specified by :stream, which defaults to the value *standard-output*<br /> The other keyword arguments specify values used to control the generation of the printed representation. <br />
  27. 27. Prin1 object &optional output-stream<br />prin1 outputs the printed representation of object to output-stream.<br />Print object &optional output-stream<br />printed representation of the object is preceded by newline and followed by a space.<br />PPrin1 object &optional output-stream<br />trailing space is omitted and the object is printed with the *print-pretty* flag non-nil to produce “pretty” output.<br />Princ object &optional output-stream<br /> Is similar to prin1 except that the output has no escape characters.<br />
  28. 28. Write-char character &optional output-stream<br />outputs the character to the output stream and returns the character<br />Write-string string &optional output-stream &key :start :end<br />Write-line string &optional output-stream &key :start :end<br />writes the character of the specified substring to the output-stream.<br />The :start and :end delimit a substring of string in usual manner.<br /> write-lined does the same thing but then outputs a new-line afterwards.<br />
  29. 29. finish-output &optional output-stream<br /> Attempts to ensure that all the output sent to the output stream has reached the destination, only then returns nil.<br />force-output &optional output-stream<br />initiates the emptying of any internal buffers and returns nil without waiting for completion.<br />clear-output &optional output-stream<br /><ul><li>Attempts to abort any outstanding poutput operation in progress in order to allow as less output as possible.</li></ul>Output to binary streams:<br />Write-byte integer binary-output-stream<br /> write-byte returns one byte, the value of the integer.<br />
  30. 30. Formatted output<br />The function format is used for producing nicely formatted text.<br /><ul><li>format destination control-string &rest arguments</li></ul>Format outputs the characters of control string<br />A format directive consists of a tilde(~) , optional prefix parameters separated by commas, optional colon( : ) and ( @ ) sign modifiers, and a single character indicating what kind of directive this is.<br />
  31. 31. ~D, An arg, should be an integer is printed in decimal radix.<br />Few examples for format function:<br />(format nil “foo”) “foo”<br />(setq x 5)<br />(format nil “The answer is ~D.”, x)”The answer is 5.”<br />(format nil “The answer is ~3D.”, x) “The answer is 5.”<br />
  32. 32. Querying the user<br />The following functions provide a convenient interface for asking the questions of the user.<br />y-or-n-p &optional format-string &rest arguments<br />This predicate is used for asking the user a question whose answer is either yes or no.<br />It types a message if supplied.<br />All input and output is performed using the stream in the global variable *query-io*<br />
  33. 33. Ex:<br />(y-or-n-p “Produce Listing file?”) <br />opens a pop-up asking “Produce Listing file?”<br />with two options “Yes” and ‘no”. Returns T on<br />yes else returns NIL.<br />(y-or-n-p “Cannot connect to network host ~S. Retry?” host)<br />Y-or-n-p must be used when the user waiting to be questioned before preceding further.<br />