home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Applications / Python 1.3 / source code / Doc / libparser.tex < prev    next >
LaTeX Document  |  1995-12-17  |  10.6 KB  |  [TEXT/R*ch]

open in: MacOS 8.1     |     Win98     |     DOS

browse contents    |     view JSON data     |     view as text


This file was processed as: LaTeX Document (document/latex).

ConfidenceProgramDetectionMatch TypeSupport
100% dexvert LaTeX Document (document/latex) magic Supported
1% dexvert Text File (text/txt) fallback Supported
100% file LaTeX document, ASCII text, with CR line terminators default
100% checkBytes Printable ASCII default
100% perlTextCheck Likely Text (Perl) default
100% siegfried fmt/281 LaTeX (Subdocument) default
100% detectItEasy Format: plain text[CR] default (weak)


id metadata
keyvalue
macFileType[TEXT]
macFileCreator[R*ch]



hex view
+--------+-------------------------+-------------------------+--------+--------+
|00000000| 25 20 6c 69 62 70 61 72 | 73 65 72 2e 74 65 78 0d |% libpar|ser.tex.|
|00000010| 25 0d 25 20 49 6e 74 72 | 6f 64 75 63 74 6f 72 79 |%.% Intr|oductory|
|00000020| 20 64 6f 63 75 6d 65 6e | 74 61 74 69 6f 6e 20 66 | documen|tation f|
|00000030| 6f 72 20 74 68 65 20 6e | 65 77 20 70 61 72 73 65 |or the n|ew parse|
|00000040| 72 20 62 75 69 6c 74 2d | 69 6e 20 6d 6f 64 75 6c |r built-|in modul|
|00000050| 65 2e 0d 25 0d 25 20 43 | 6f 70 79 72 69 67 68 74 |e..%.% C|opyright|
|00000060| 20 31 39 39 35 20 56 69 | 72 67 69 6e 69 61 20 50 | 1995 Vi|rginia P|
|00000070| 6f 6c 79 74 65 63 68 6e | 69 63 20 49 6e 73 74 69 |olytechn|ic Insti|
|00000080| 74 75 74 65 20 61 6e 64 | 20 53 74 61 74 65 20 55 |tute and| State U|
|00000090| 6e 69 76 65 72 73 69 74 | 79 0d 25 20 61 6e 64 20 |niversit|y.% and |
|000000a0| 46 72 65 64 20 4c 2e 20 | 44 72 61 6b 65 2c 20 4a |Fred L. |Drake, J|
|000000b0| 72 2e 20 20 54 68 69 73 | 20 63 6f 70 79 72 69 67 |r. This| copyrig|
|000000c0| 68 74 20 6e 6f 74 69 63 | 65 20 6d 75 73 74 20 62 |ht notic|e must b|
|000000d0| 65 20 64 69 73 74 72 69 | 62 75 74 65 64 20 6f 6e |e distri|buted on|
|000000e0| 0d 25 20 61 6c 6c 20 63 | 6f 70 69 65 73 2c 20 62 |.% all c|opies, b|
|000000f0| 75 74 20 74 68 69 73 20 | 64 6f 63 75 6d 65 6e 74 |ut this |document|
|00000100| 20 6f 74 68 65 72 77 69 | 73 65 20 6d 61 79 20 62 | otherwi|se may b|
|00000110| 65 20 64 69 73 74 72 69 | 62 75 74 65 64 20 61 73 |e distri|buted as|
|00000120| 20 70 61 72 74 0d 25 20 | 6f 66 20 74 68 65 20 50 | part.% |of the P|
|00000130| 79 74 68 6f 6e 20 64 69 | 73 74 72 69 62 75 74 69 |ython di|stributi|
|00000140| 6f 6e 2e 20 20 4e 6f 20 | 66 65 65 20 6d 61 79 20 |on. No |fee may |
|00000150| 62 65 20 63 68 61 72 67 | 65 64 20 66 6f 72 20 74 |be charg|ed for t|
|00000160| 68 69 73 20 64 6f 63 75 | 6d 65 6e 74 0d 25 20 69 |his docu|ment.% i|
|00000170| 6e 20 61 6e 79 20 72 65 | 70 72 65 73 65 6e 74 61 |n any re|presenta|
|00000180| 74 69 6f 6e 2c 20 65 69 | 74 68 65 72 20 6f 6e 20 |tion, ei|ther on |
|00000190| 70 61 70 65 72 20 6f 72 | 20 65 6c 65 63 74 72 6f |paper or| electro|
|000001a0| 6e 69 63 61 6c 6c 79 2e | 20 20 54 68 69 73 0d 25 |nically.| This.%|
|000001b0| 20 72 65 73 74 72 69 63 | 74 69 6f 6e 20 64 6f 65 | restric|tion doe|
|000001c0| 73 20 6e 6f 74 20 61 66 | 66 65 63 74 20 6f 74 68 |s not af|fect oth|
|000001d0| 65 72 20 65 6c 65 6d 65 | 6e 74 73 20 69 6e 20 61 |er eleme|nts in a|
|000001e0| 20 64 69 73 74 72 69 62 | 75 74 65 64 20 70 61 63 | distrib|uted pac|
|000001f0| 6b 61 67 65 0d 25 20 69 | 6e 20 61 6e 79 20 77 61 |kage.% i|n any wa|
|00000200| 79 2e 0d 25 0d 0d 5c 73 | 65 63 74 69 6f 6e 7b 42 |y..%..\s|ection{B|
|00000210| 75 69 6c 74 2d 69 6e 20 | 4d 6f 64 75 6c 65 20 5c |uilt-in |Module \|
|00000220| 73 65 63 74 63 6f 64 65 | 7b 70 61 72 73 65 72 7d |sectcode|{parser}|
|00000230| 7d 0d 5c 62 69 6d 6f 64 | 69 6e 64 65 78 7b 70 61 |}.\bimod|index{pa|
|00000240| 72 73 65 72 7d 0d 0d 0d | 25 20 3d 3d 3d 3d 20 32 |rser}...|% ==== 2|
|00000250| 2e 20 3d 3d 3d 3d 0d 25 | 20 47 69 76 65 20 61 20 |. ====.%| Give a |
|00000260| 73 68 6f 72 74 20 6f 76 | 65 72 76 69 65 77 20 6f |short ov|erview o|
|00000270| 66 20 77 68 61 74 20 74 | 68 65 20 6d 6f 64 75 6c |f what t|he modul|
|00000280| 65 20 64 6f 65 73 2e 0d | 25 20 49 66 20 69 74 20 |e does..|% If it |
|00000290| 69 73 20 70 6c 61 74 66 | 6f 72 6d 20 73 70 65 63 |is platf|orm spec|
|000002a0| 69 66 69 63 2c 20 6d 65 | 6e 74 69 6f 6e 20 74 68 |ific, me|ntion th|
|000002b0| 69 73 2e 0d 25 20 4d 65 | 6e 74 69 6f 6e 20 6f 74 |is..% Me|ntion ot|
|000002c0| 68 65 72 20 69 6d 70 6f | 72 74 61 6e 74 20 72 65 |her impo|rtant re|
|000002d0| 73 74 72 69 63 74 69 6f | 6e 73 20 6f 72 20 67 65 |strictio|ns or ge|
|000002e0| 6e 65 72 61 6c 20 6f 70 | 65 72 61 74 69 6e 67 20 |neral op|erating |
|000002f0| 70 72 69 6e 63 69 70 6c | 65 73 2e 0d 0d 54 68 65 |principl|es...The|
|00000300| 20 5c 63 6f 64 65 7b 70 | 61 72 73 65 72 7d 20 6d | \code{p|arser} m|
|00000310| 6f 64 75 6c 65 20 70 72 | 6f 76 69 64 65 73 20 61 |odule pr|ovides a|
|00000320| 6e 20 69 6e 74 65 72 66 | 61 63 65 20 74 6f 20 50 |n interf|ace to P|
|00000330| 79 74 68 6f 6e 27 73 20 | 69 6e 74 65 72 6e 61 6c |ython's |internal|
|00000340| 0d 70 61 72 73 65 72 20 | 61 6e 64 20 62 79 74 65 |.parser |and byte|
|00000350| 2d 63 6f 64 65 20 63 6f | 6d 70 69 6c 65 72 2e 20 |-code co|mpiler. |
|00000360| 20 54 68 65 20 70 72 69 | 6d 61 72 79 20 70 75 72 | The pri|mary pur|
|00000370| 70 6f 73 65 20 66 6f 72 | 20 74 68 69 73 20 69 6e |pose for| this in|
|00000380| 74 65 72 66 61 63 65 0d | 69 73 20 74 6f 20 61 6c |terface.|is to al|
|00000390| 6c 6f 77 20 50 79 74 68 | 6f 6e 20 63 6f 64 65 20 |low Pyth|on code |
|000003a0| 74 6f 20 65 64 69 74 20 | 74 68 65 20 70 61 72 73 |to edit |the pars|
|000003b0| 65 20 74 72 65 65 20 6f | 66 20 61 20 50 79 74 68 |e tree o|f a Pyth|
|000003c0| 6f 6e 20 65 78 70 72 65 | 73 73 69 6f 6e 0d 61 6e |on expre|ssion.an|
|000003d0| 64 20 63 72 65 61 74 65 | 20 65 78 65 63 75 74 61 |d create| executa|
|000003e0| 62 6c 65 20 63 6f 64 65 | 20 66 72 6f 6d 20 74 68 |ble code| from th|
|000003f0| 69 73 2e 20 20 54 68 69 | 73 20 63 61 6e 20 62 65 |is. Thi|s can be|
|00000400| 20 62 65 74 74 65 72 20 | 74 68 61 6e 20 74 72 79 | better |than try|
|00000410| 69 6e 67 0d 74 6f 20 70 | 61 72 73 65 20 61 6e 64 |ing.to p|arse and|
|00000420| 20 6d 6f 64 69 66 79 20 | 61 6e 20 61 72 62 69 74 | modify |an arbit|
|00000430| 72 61 72 79 20 50 79 74 | 68 6f 6e 20 63 6f 64 65 |rary Pyt|hon code|
|00000440| 20 66 72 61 67 6d 65 6e | 74 20 61 73 20 61 20 73 | fragmen|t as a s|
|00000450| 74 72 69 6e 67 2c 20 61 | 6e 64 0d 65 6e 73 75 72 |tring, a|nd.ensur|
|00000460| 65 73 20 74 68 61 74 20 | 70 61 72 73 69 6e 67 20 |es that |parsing |
|00000470| 69 73 20 70 65 72 66 6f | 72 6d 65 64 20 69 6e 20 |is perfo|rmed in |
|00000480| 61 20 6d 61 6e 6e 65 72 | 20 69 64 65 6e 74 69 63 |a manner| identic|
|00000490| 61 6c 20 74 6f 20 74 68 | 65 20 63 6f 64 65 0d 66 |al to th|e code.f|
|000004a0| 6f 72 6d 69 6e 67 20 74 | 68 65 20 61 70 70 6c 69 |orming t|he appli|
|000004b0| 63 61 74 69 6f 6e 2e 20 | 20 49 74 27 73 20 61 6c |cation. | It's al|
|000004c0| 73 6f 20 66 61 73 74 65 | 72 2e 0d 0d 54 68 65 72 |so faste|r...Ther|
|000004d0| 65 20 61 72 65 20 61 20 | 66 65 77 20 74 68 69 6e |e are a |few thin|
|000004e0| 67 73 20 74 6f 20 6e 6f | 74 65 20 61 62 6f 75 74 |gs to no|te about|
|000004f0| 20 74 68 69 73 20 6d 6f | 64 75 6c 65 20 77 68 69 | this mo|dule whi|
|00000500| 63 68 20 61 72 65 20 69 | 6d 70 6f 72 74 61 6e 74 |ch are i|mportant|
|00000510| 0d 74 6f 20 6d 61 6b 69 | 6e 67 20 75 73 65 20 6f |.to maki|ng use o|
|00000520| 66 20 74 68 65 20 64 61 | 74 61 20 73 74 72 75 63 |f the da|ta struc|
|00000530| 74 75 72 65 73 20 63 72 | 65 61 74 65 64 2e 20 20 |tures cr|eated. |
|00000540| 54 68 69 73 20 69 73 20 | 6e 6f 74 20 61 20 74 75 |This is |not a tu|
|00000550| 74 6f 72 69 61 6c 0d 6f | 6e 20 65 64 69 74 69 6e |torial.o|n editin|
|00000560| 67 20 74 68 65 20 70 61 | 72 73 65 20 74 72 65 65 |g the pa|rse tree|
|00000570| 73 20 66 6f 72 20 50 79 | 74 68 6f 6e 20 63 6f 64 |s for Py|thon cod|
|00000580| 65 2e 0d 0d 4d 6f 73 74 | 20 69 6d 70 6f 72 74 61 |e...Most| importa|
|00000590| 6e 74 6c 79 2c 20 61 20 | 67 6f 6f 64 20 75 6e 64 |ntly, a |good und|
|000005a0| 65 72 73 74 61 6e 64 69 | 6e 67 20 6f 66 20 74 68 |erstandi|ng of th|
|000005b0| 65 20 50 79 74 68 6f 6e | 20 67 72 61 6d 6d 61 72 |e Python| grammar|
|000005c0| 20 70 72 6f 63 65 73 73 | 65 64 0d 62 79 20 74 68 | process|ed.by th|
|000005d0| 65 20 69 6e 74 65 72 6e | 61 6c 20 70 61 72 73 65 |e intern|al parse|
|000005e0| 72 20 69 73 20 72 65 71 | 75 69 72 65 64 2e 20 20 |r is req|uired. |
|000005f0| 46 6f 72 20 66 75 6c 6c | 20 69 6e 66 6f 72 6d 61 |For full| informa|
|00000600| 74 69 6f 6e 20 6f 6e 20 | 74 68 65 0d 6c 61 6e 67 |tion on |the.lang|
|00000610| 75 61 67 65 20 73 79 6e | 74 61 78 2c 20 72 65 66 |uage syn|tax, ref|
|00000620| 65 72 20 74 6f 20 74 68 | 65 20 4c 61 6e 67 75 61 |er to th|e Langua|
|00000630| 67 65 20 52 65 66 65 72 | 65 6e 63 65 2e 20 20 54 |ge Refer|ence. T|
|00000640| 68 65 20 70 61 72 73 65 | 72 20 69 74 73 65 6c 66 |he parse|r itself|
|00000650| 0d 69 73 20 63 72 65 61 | 74 65 64 20 66 72 6f 6d |.is crea|ted from|
|00000660| 20 61 20 67 72 61 6d 6d | 61 72 20 73 70 65 63 69 | a gramm|ar speci|
|00000670| 66 69 63 61 74 69 6f 6e | 20 64 65 66 69 6e 65 64 |fication| defined|
|00000680| 20 69 6e 20 74 68 65 20 | 66 69 6c 65 0d 5c 63 6f | in the |file.\co|
|00000690| 64 65 7b 47 72 61 6d 6d | 61 72 2f 47 72 61 6d 6d |de{Gramm|ar/Gramm|
|000006a0| 61 72 7d 20 69 6e 20 74 | 68 65 20 73 74 61 6e 64 |ar} in t|he stand|
|000006b0| 61 72 64 20 50 79 74 68 | 6f 6e 20 64 69 73 74 72 |ard Pyth|on distr|
|000006c0| 69 62 75 74 69 6f 6e 2e | 20 20 54 68 65 20 70 61 |ibution.| The pa|
|000006d0| 72 73 65 0d 74 72 65 65 | 73 20 73 74 6f 72 65 64 |rse.tree|s stored|
|000006e0| 20 69 6e 20 74 68 65 20 | 60 60 41 53 54 20 6f 62 | in the |``AST ob|
|000006f0| 6a 65 63 74 73 27 27 20 | 63 72 65 61 74 65 64 20 |jects'' |created |
|00000700| 62 79 20 74 68 69 73 20 | 6d 6f 64 75 6c 65 20 61 |by this |module a|
|00000710| 72 65 20 74 68 65 0d 61 | 63 74 75 61 6c 20 6f 75 |re the.a|ctual ou|
|00000720| 74 70 75 74 20 66 72 6f | 6d 20 74 68 65 20 69 6e |tput fro|m the in|
|00000730| 74 65 72 6e 61 6c 20 70 | 61 72 73 65 72 20 77 68 |ternal p|arser wh|
|00000740| 65 6e 20 63 72 65 61 74 | 65 64 20 62 79 20 74 68 |en creat|ed by th|
|00000750| 65 0d 5c 63 6f 64 65 7b | 65 78 70 72 28 29 7d 20 |e.\code{|expr()} |
|00000760| 6f 72 20 5c 63 6f 64 65 | 7b 73 75 69 74 65 28 29 |or \code|{suite()|
|00000770| 7d 20 66 75 6e 63 74 69 | 6f 6e 73 2c 20 64 65 73 |} functi|ons, des|
|00000780| 63 72 69 62 65 64 20 62 | 65 6c 6f 77 2e 20 20 54 |cribed b|elow. T|
|00000790| 68 65 20 41 53 54 0d 6f | 62 6a 65 63 74 73 20 63 |he AST.o|bjects c|
|000007a0| 72 65 61 74 65 64 20 62 | 79 20 5c 63 6f 64 65 7b |reated b|y \code{|
|000007b0| 74 75 70 6c 65 32 61 73 | 74 28 29 7d 20 66 61 69 |tuple2as|t()} fai|
|000007c0| 74 68 66 75 6c 6c 79 20 | 73 69 6d 75 6c 61 74 65 |thfully |simulate|
|000007d0| 20 74 68 6f 73 65 0d 73 | 74 72 75 63 74 75 72 65 | those.s|tructure|
|000007e0| 73 2e 0d 0d 45 61 63 68 | 20 65 6c 65 6d 65 6e 74 |s...Each| element|
|000007f0| 20 6f 66 20 74 68 65 20 | 74 75 70 6c 65 73 20 72 | of the |tuples r|
|00000800| 65 74 75 72 6e 65 64 20 | 62 79 20 5c 63 6f 64 65 |eturned |by \code|
|00000810| 7b 61 73 74 32 74 75 70 | 6c 65 28 29 7d 20 68 61 |{ast2tup|le()} ha|
|00000820| 73 20 61 20 73 69 6d 70 | 6c 65 0d 66 6f 72 6d 2e |s a simp|le.form.|
|00000830| 20 20 54 75 70 6c 65 73 | 20 72 65 70 72 65 73 65 | Tuples| represe|
|00000840| 6e 74 69 6e 67 20 6e 6f | 6e 2d 74 65 72 6d 69 6e |nting no|n-termin|
|00000850| 61 6c 20 65 6c 65 6d 65 | 6e 74 73 20 69 6e 20 74 |al eleme|nts in t|
|00000860| 68 65 20 67 72 61 6d 6d | 61 72 20 61 6c 77 61 79 |he gramm|ar alway|
|00000870| 73 0d 68 61 76 65 20 61 | 20 6c 65 6e 67 74 68 20 |s.have a| length |
|00000880| 67 72 65 61 74 65 72 20 | 74 68 61 6e 20 6f 6e 65 |greater |than one|
|00000890| 2e 20 20 54 68 65 20 66 | 69 72 73 74 20 65 6c 65 |. The f|irst ele|
|000008a0| 6d 65 6e 74 20 69 73 20 | 61 6e 20 69 6e 74 65 67 |ment is |an integ|
|000008b0| 65 72 20 77 68 69 63 68 | 0d 69 64 65 6e 74 69 66 |er which|.identif|
|000008c0| 69 65 73 20 61 20 70 72 | 6f 64 75 63 74 69 6f 6e |ies a pr|oduction|
|000008d0| 20 69 6e 20 74 68 65 20 | 67 72 61 6d 6d 61 72 2e | in the |grammar.|
|000008e0| 20 20 54 68 65 73 65 20 | 69 6e 74 65 67 65 72 73 | These |integers|
|000008f0| 20 61 72 65 20 67 69 76 | 65 6e 0d 73 79 6d 62 6f | are giv|en.symbo|
|00000900| 6c 69 63 20 6e 61 6d 65 | 73 20 69 6e 20 74 68 65 |lic name|s in the|
|00000910| 20 43 20 68 65 61 64 65 | 72 20 66 69 6c 65 20 5c | C heade|r file \|
|00000920| 63 6f 64 65 7b 49 6e 63 | 6c 75 64 65 2f 67 72 61 |code{Inc|lude/gra|
|00000930| 6d 69 6e 69 74 2e 68 7d | 20 61 6e 64 20 74 68 65 |minit.h}| and the|
|00000940| 0d 50 79 74 68 6f 6e 20 | 6d 6f 64 75 6c 65 20 5c |.Python |module \|
|00000950| 63 6f 64 65 7b 4c 69 62 | 2f 73 79 6d 62 6f 6c 2e |code{Lib|/symbol.|
|00000960| 70 79 7d 2e 20 20 45 61 | 63 68 20 61 64 64 69 74 |py}. Ea|ch addit|
|00000970| 69 6f 6e 61 6c 20 65 6c | 65 6d 65 6e 74 20 6f 66 |ional el|ement of|
|00000980| 20 74 68 65 0d 74 75 70 | 6c 65 20 72 65 70 72 65 | the.tup|le repre|
|00000990| 73 65 6e 74 73 20 61 20 | 63 6f 6d 70 6f 6e 65 6e |sents a |componen|
|000009a0| 74 20 6f 66 20 74 68 65 | 20 70 72 6f 64 75 63 74 |t of the| product|
|000009b0| 69 6f 6e 20 61 73 20 72 | 65 63 6f 67 6e 69 7a 65 |ion as r|ecognize|
|000009c0| 64 20 69 6e 20 74 68 65 | 0d 69 6e 70 75 74 20 73 |d in the|.input s|
|000009d0| 74 72 69 6e 67 3a 20 74 | 68 65 73 65 20 61 72 65 |tring: t|hese are|
|000009e0| 20 61 6c 77 61 79 73 20 | 74 75 70 6c 65 73 20 77 | always |tuples w|
|000009f0| 68 69 63 68 20 68 61 76 | 65 20 74 68 65 20 73 61 |hich hav|e the sa|
|00000a00| 6d 65 20 66 6f 72 6d 20 | 61 73 20 74 68 65 0d 70 |me form |as the.p|
|00000a10| 61 72 65 6e 74 2e 20 20 | 41 6e 20 69 6d 70 6f 72 |arent. |An impor|
|00000a20| 74 61 6e 74 20 61 73 70 | 65 63 74 20 6f 66 20 74 |tant asp|ect of t|
|00000a30| 68 69 73 20 73 74 72 75 | 63 74 75 72 65 20 77 68 |his stru|cture wh|
|00000a40| 69 63 68 20 73 68 6f 75 | 6c 64 20 62 65 20 6e 6f |ich shou|ld be no|
|00000a50| 74 65 64 0d 69 73 20 74 | 68 61 74 20 6b 65 79 77 |ted.is t|hat keyw|
|00000a60| 6f 72 64 73 20 75 73 65 | 64 20 74 6f 20 69 64 65 |ords use|d to ide|
|00000a70| 6e 74 69 66 79 20 74 68 | 65 20 70 61 72 65 6e 74 |ntify th|e parent|
|00000a80| 20 6e 6f 64 65 20 74 79 | 70 65 2c 20 73 75 63 68 | node ty|pe, such|
|00000a90| 20 61 73 20 74 68 65 0d | 6b 65 79 77 6f 72 64 20 | as the.|keyword |
|00000aa0| 5c 63 6f 64 65 7b 69 66 | 7d 20 69 6e 20 61 6e 20 |\code{if|} in an |
|00000ab0| 5c 65 6d 70 68 7b 69 66 | 5c 5f 73 74 6d 74 7d 2c |\emph{if|\_stmt},|
|00000ac0| 20 61 72 65 20 69 6e 63 | 6c 75 64 65 64 20 69 6e | are inc|luded in|
|00000ad0| 20 74 68 65 20 6e 6f 64 | 65 20 74 72 65 65 0d 77 | the nod|e tree.w|
|00000ae0| 69 74 68 6f 75 74 20 61 | 6e 79 20 73 70 65 63 69 |ithout a|ny speci|
|00000af0| 61 6c 20 74 72 65 61 74 | 6d 65 6e 74 2e 20 20 46 |al treat|ment. F|
|00000b00| 6f 72 20 65 78 61 6d 70 | 6c 65 2c 20 74 68 65 20 |or examp|le, the |
|00000b10| 5c 63 6f 64 65 7b 69 66 | 7d 20 6b 65 79 77 6f 72 |\code{if|} keywor|
|00000b20| 64 20 69 73 0d 72 65 70 | 72 65 73 65 6e 74 65 64 |d is.rep|resented|
|00000b30| 20 62 79 20 74 68 65 20 | 74 75 70 6c 65 20 5c 63 | by the |tuple \c|
|00000b40| 6f 64 65 7b 28 31 2c 20 | 27 69 66 27 29 7d 2c 20 |ode{(1, |'if')}, |
|00000b50| 77 68 65 72 65 20 5c 63 | 6f 64 65 7b 31 7d 20 69 |where \c|ode{1} i|
|00000b60| 73 20 74 68 65 0d 6e 75 | 6d 65 72 69 63 20 76 61 |s the.nu|meric va|
|00000b70| 6c 75 65 20 61 73 73 6f | 63 69 61 74 65 64 20 77 |lue asso|ciated w|
|00000b80| 69 74 68 20 61 6c 6c 20 | 5c 63 6f 64 65 7b 4e 41 |ith all |\code{NA|
|00000b90| 4d 45 7d 20 65 6c 65 6d | 65 6e 74 73 2c 20 69 6e |ME} elem|ents, in|
|00000ba0| 63 6c 75 64 69 6e 67 0d | 76 61 72 69 61 62 6c 65 |cluding.|variable|
|00000bb0| 20 61 6e 64 20 66 75 6e | 63 74 69 6f 6e 20 6e 61 | and fun|ction na|
|00000bc0| 6d 65 73 20 64 65 66 69 | 6e 65 64 20 62 79 20 74 |mes defi|ned by t|
|00000bd0| 68 65 20 75 73 65 72 2e | 0d 0d 54 65 72 6d 69 6e |he user.|..Termin|
|00000be0| 61 6c 20 65 6c 65 6d 65 | 6e 74 73 20 61 72 65 20 |al eleme|nts are |
|00000bf0| 72 65 70 72 65 73 65 6e | 74 65 64 20 69 6e 20 6d |represen|ted in m|
|00000c00| 75 63 68 20 74 68 65 20 | 73 61 6d 65 20 77 61 79 |uch the |same way|
|00000c10| 2c 20 62 75 74 20 77 69 | 74 68 6f 75 74 0d 61 6e |, but wi|thout.an|
|00000c20| 79 20 63 68 69 6c 64 20 | 65 6c 65 6d 65 6e 74 73 |y child |elements|
|00000c30| 20 61 6e 64 20 74 68 65 | 20 61 64 64 69 74 69 6f | and the| additio|
|00000c40| 6e 20 6f 66 20 74 68 65 | 20 73 6f 75 72 63 65 20 |n of the| source |
|00000c50| 74 65 78 74 20 77 68 69 | 63 68 20 77 61 73 0d 69 |text whi|ch was.i|
|00000c60| 64 65 6e 74 69 66 69 65 | 64 2e 20 20 54 68 65 20 |dentifie|d. The |
|00000c70| 65 78 61 6d 70 6c 65 20 | 6f 66 20 74 68 65 20 5c |example |of the \|
|00000c80| 63 6f 64 65 7b 69 66 7d | 20 6b 65 79 77 6f 72 64 |code{if}| keyword|
|00000c90| 20 61 62 6f 76 65 20 69 | 73 0d 72 65 70 72 65 73 | above i|s.repres|
|00000ca0| 65 6e 74 61 74 69 76 65 | 2e 20 20 54 68 65 20 76 |entative|. The v|
|00000cb0| 61 72 69 6f 75 73 20 74 | 79 70 65 73 20 6f 66 20 |arious t|ypes of |
|00000cc0| 74 65 72 6d 69 6e 61 6c | 20 73 79 6d 62 6f 6c 73 |terminal| symbols|
|00000cd0| 20 61 72 65 20 64 65 66 | 69 6e 65 64 20 69 6e 0d | are def|ined in.|
|00000ce0| 74 68 65 20 43 20 68 65 | 61 64 65 72 20 66 69 6c |the C he|ader fil|
|00000cf0| 65 20 5c 63 6f 64 65 7b | 49 6e 63 6c 75 64 65 2f |e \code{|Include/|
|00000d00| 74 6f 6b 65 6e 2e 68 7d | 20 61 6e 64 20 74 68 65 |token.h}| and the|
|00000d10| 20 50 79 74 68 6f 6e 20 | 6d 6f 64 75 6c 65 0d 5c | Python |module.\|
|00000d20| 63 6f 64 65 7b 4c 69 62 | 2f 74 6f 6b 65 6e 2e 70 |code{Lib|/token.p|
|00000d30| 79 7d 2e 0d 0d 54 68 65 | 20 41 53 54 20 6f 62 6a |y}...The| AST obj|
|00000d40| 65 63 74 73 20 61 72 65 | 20 6e 6f 74 20 61 63 74 |ects are| not act|
|00000d50| 75 61 6c 6c 79 20 72 65 | 71 75 69 72 65 64 20 74 |ually re|quired t|
|00000d60| 6f 20 73 75 70 70 6f 72 | 74 20 74 68 65 20 66 75 |o suppor|t the fu|
|00000d70| 6e 63 74 69 6f 6e 61 6c | 69 74 79 0d 6f 66 20 74 |nctional|ity.of t|
|00000d80| 68 69 73 20 6d 6f 64 75 | 6c 65 2c 20 62 75 74 20 |his modu|le, but |
|00000d90| 61 72 65 20 70 72 6f 76 | 69 64 65 64 20 66 6f 72 |are prov|ided for|
|00000da0| 20 74 68 72 65 65 20 70 | 75 72 70 6f 73 65 73 3a | three p|urposes:|
|00000db0| 20 74 6f 20 61 6c 6c 6f | 77 20 61 6e 0d 61 70 70 | to allo|w an.app|
|00000dc0| 6c 69 63 61 74 69 6f 6e | 20 74 6f 20 61 6d 6f 72 |lication| to amor|
|00000dd0| 74 69 7a 65 20 74 68 65 | 20 63 6f 73 74 20 6f 66 |tize the| cost of|
|00000de0| 20 70 72 6f 63 65 73 73 | 69 6e 67 20 63 6f 6d 70 | process|ing comp|
|00000df0| 6c 65 78 20 70 61 72 73 | 65 20 74 72 65 65 73 2c |lex pars|e trees,|
|00000e00| 20 74 6f 0d 70 72 6f 76 | 69 64 65 20 61 20 70 61 | to.prov|ide a pa|
|00000e10| 72 73 65 20 74 72 65 65 | 20 72 65 70 72 65 73 65 |rse tree| represe|
|00000e20| 6e 74 61 74 69 6f 6e 20 | 77 68 69 63 68 20 63 6f |ntation |which co|
|00000e30| 6e 73 65 72 76 65 73 20 | 6d 65 6d 6f 72 79 20 73 |nserves |memory s|
|00000e40| 70 61 63 65 20 77 68 65 | 6e 0d 63 6f 6d 70 61 72 |pace whe|n.compar|
|00000e50| 65 64 20 74 6f 20 74 68 | 65 20 50 79 74 68 6f 6e |ed to th|e Python|
|00000e60| 20 74 75 70 6c 65 20 72 | 65 70 72 65 73 65 6e 74 | tuple r|epresent|
|00000e70| 61 74 69 6f 6e 2c 20 61 | 6e 64 20 74 6f 20 65 61 |ation, a|nd to ea|
|00000e80| 73 65 20 74 68 65 20 63 | 72 65 61 74 69 6f 6e 0d |se the c|reation.|
|00000e90| 6f 66 20 61 64 64 69 74 | 69 6f 6e 61 6c 20 6d 6f |of addit|ional mo|
|00000ea0| 64 75 6c 65 73 20 69 6e | 20 43 20 77 68 69 63 68 |dules in| C which|
|00000eb0| 20 6d 61 6e 69 70 75 6c | 61 74 65 20 70 61 72 73 | manipul|ate pars|
|00000ec0| 65 20 74 72 65 65 73 2e | 20 20 41 20 73 69 6d 70 |e trees.| A simp|
|00000ed0| 6c 65 0d 60 60 77 72 61 | 70 70 65 72 27 27 20 6d |le.``wra|pper'' m|
|00000ee0| 6f 64 75 6c 65 20 6d 61 | 79 20 62 65 20 63 72 65 |odule ma|y be cre|
|00000ef0| 61 74 65 64 20 69 6e 20 | 50 79 74 68 6f 6e 20 69 |ated in |Python i|
|00000f00| 66 20 64 65 73 69 72 65 | 64 20 74 6f 20 68 69 64 |f desire|d to hid|
|00000f10| 65 20 74 68 65 20 75 73 | 65 0d 6f 66 20 41 53 54 |e the us|e.of AST|
|00000f20| 20 6f 62 6a 65 63 74 73 | 2e 0d 0d 0d 25 20 3d 3d | objects|....% ==|
|00000f30| 3d 3d 20 33 2e 20 3d 3d | 3d 3d 0d 25 20 4c 69 73 |== 3. ==|==.% Lis|
|00000f40| 74 20 74 68 65 20 70 75 | 62 6c 69 63 20 66 75 6e |t the pu|blic fun|
|00000f50| 63 74 69 6f 6e 73 20 64 | 65 66 69 6e 65 64 20 62 |ctions d|efined b|
|00000f60| 79 20 74 68 65 20 6d 6f | 64 75 6c 65 2e 20 20 42 |y the mo|dule. B|
|00000f70| 65 67 69 6e 20 77 69 74 | 68 20 61 0d 25 20 73 74 |egin wit|h a.% st|
|00000f80| 61 6e 64 61 72 64 20 70 | 68 72 61 73 65 2e 20 20 |andard p|hrase. |
|00000f90| 59 6f 75 20 6d 61 79 20 | 61 6c 73 6f 20 6c 69 73 |You may |also lis|
|00000fa0| 74 20 74 68 65 20 65 78 | 63 65 70 74 69 6f 6e 73 |t the ex|ceptions|
|00000fb0| 20 61 6e 64 20 6f 74 68 | 65 72 20 64 61 74 61 0d | and oth|er data.|
|00000fc0| 25 20 69 74 65 6d 73 20 | 64 65 66 69 6e 65 64 20 |% items |defined |
|00000fd0| 69 6e 20 74 68 65 20 6d | 6f 64 75 6c 65 2c 20 69 |in the m|odule, i|
|00000fe0| 6e 73 6f 66 61 72 20 61 | 73 20 74 68 65 79 20 61 |nsofar a|s they a|
|00000ff0| 72 65 20 69 6d 70 6f 72 | 74 61 6e 74 20 66 6f 72 |re impor|tant for|
|00001000| 20 74 68 65 0d 25 20 75 | 73 65 72 2e 0d 0d 54 68 | the.% u|ser...Th|
|00001010| 65 20 5c 63 6f 64 65 7b | 70 61 72 73 65 72 7d 20 |e \code{|parser} |
|00001020| 6d 6f 64 75 6c 65 20 64 | 65 66 69 6e 65 73 20 74 |module d|efines t|
|00001030| 68 65 20 66 6f 6c 6c 6f | 77 69 6e 67 20 66 75 6e |he follo|wing fun|
|00001040| 63 74 69 6f 6e 73 3a 0d | 0d 25 20 2d 2d 2d 2d 20 |ctions:.|.% ---- |
|00001050| 33 2e 31 2e 20 2d 2d 2d | 2d 0d 25 20 52 65 64 65 |3.1. ---|-.% Rede|
|00001060| 66 69 6e 65 20 74 68 65 | 20 60 60 69 6e 64 65 78 |fine the| ``index|
|00001070| 73 75 62 69 74 65 6d 27 | 27 20 6d 61 63 72 6f 20 |subitem'|' macro |
|00001080| 74 6f 20 70 6f 69 6e 74 | 20 74 6f 20 74 68 69 73 |to point| to this|
|00001090| 20 6d 6f 64 75 6c 65 0d | 25 20 28 61 6c 74 65 72 | module.|% (alter|
|000010a0| 6e 61 74 69 76 65 6c 79 | 2c 20 79 6f 75 20 63 61 |natively|, you ca|
|000010b0| 6e 20 70 75 74 20 74 68 | 69 73 20 61 74 20 74 68 |n put th|is at th|
|000010c0| 65 20 74 6f 70 20 6f 66 | 20 74 68 65 20 66 69 6c |e top of| the fil|
|000010d0| 65 29 3a 0d 0d 5c 72 65 | 6e 65 77 63 6f 6d 6d 61 |e):..\re|newcomma|
|000010e0| 6e 64 7b 5c 69 6e 64 65 | 78 73 75 62 69 74 65 6d |nd{\inde|xsubitem|
|000010f0| 7d 7b 28 69 6e 20 6d 6f | 64 75 6c 65 20 70 61 72 |}{(in mo|dule par|
|00001100| 73 65 72 29 7d 0d 0d 25 | 20 2d 2d 2d 2d 20 33 2e |ser)}..%| ---- 3.|
|00001110| 32 2e 20 2d 2d 2d 2d 0d | 25 20 46 6f 72 20 65 61 |2. ----.|% For ea|
|00001120| 63 68 20 66 75 6e 63 74 | 69 6f 6e 2c 20 75 73 65 |ch funct|ion, use|
|00001130| 20 61 20 60 60 66 75 6e | 63 64 65 73 63 27 27 20 | a ``fun|cdesc'' |
|00001140| 62 6c 6f 63 6b 2e 20 20 | 54 68 69 73 20 68 61 73 |block. |This has|
|00001150| 20 65 78 61 63 74 6c 79 | 20 74 77 6f 0d 25 20 70 | exactly| two.% p|
|00001160| 61 72 61 6d 65 74 65 72 | 73 20 28 65 61 63 68 20 |arameter|s (each |
|00001170| 70 61 72 61 6d 65 74 65 | 72 73 20 69 73 20 63 6f |paramete|rs is co|
|00001180| 6e 74 61 69 6e 65 64 20 | 69 6e 20 61 20 73 65 74 |ntained |in a set|
|00001190| 20 6f 66 20 63 75 72 6c | 79 20 62 72 61 63 65 73 | of curl|y braces|
|000011a0| 29 3a 0d 25 20 74 68 65 | 20 66 69 72 73 74 20 70 |):.% the| first p|
|000011b0| 61 72 61 6d 65 74 65 72 | 20 69 73 20 74 68 65 20 |arameter| is the |
|000011c0| 66 75 6e 63 74 69 6f 6e | 20 6e 61 6d 65 20 28 74 |function| name (t|
|000011d0| 68 69 73 20 61 75 74 6f | 6d 61 74 69 63 61 6c 6c |his auto|maticall|
|000011e0| 79 0d 25 20 67 65 6e 65 | 72 61 74 65 73 20 61 6e |y.% gene|rates an|
|000011f0| 20 69 6e 64 65 78 20 65 | 6e 74 72 79 29 3b 20 74 | index e|ntry); t|
|00001200| 68 65 20 73 65 63 6f 6e | 64 20 70 61 72 61 6d 65 |he secon|d parame|
|00001210| 74 65 72 20 69 73 20 74 | 68 65 20 66 75 6e 63 74 |ter is t|he funct|
|00001220| 69 6f 6e 27 73 0d 25 20 | 61 72 67 75 6d 65 6e 74 |ion's.% |argument|
|00001230| 20 6c 69 73 74 2e 20 20 | 49 66 20 74 68 65 72 65 | list. |If there|
|00001240| 20 61 72 65 20 6e 6f 20 | 61 72 67 75 6d 65 6e 74 | are no |argument|
|00001250| 73 2c 20 75 73 65 20 61 | 6e 20 65 6d 70 74 79 20 |s, use a|n empty |
|00001260| 70 61 69 72 20 6f 66 0d | 25 20 63 75 72 6c 79 20 |pair of.|% curly |
|00001270| 62 72 61 63 65 73 2e 20 | 20 49 66 20 74 68 65 72 |braces. | If ther|
|00001280| 65 20 69 73 20 6d 6f 72 | 65 20 74 68 61 6e 20 6f |e is mor|e than o|
|00001290| 6e 65 20 61 72 67 75 6d | 65 6e 74 2c 20 73 65 70 |ne argum|ent, sep|
|000012a0| 61 72 61 74 65 20 74 68 | 65 0d 25 20 61 72 67 75 |arate th|e.% argu|
|000012b0| 6d 65 6e 74 73 20 77 69 | 74 68 20 62 61 63 6b 73 |ments wi|th backs|
|000012c0| 6c 61 73 68 2d 63 6f 6d | 6d 61 2e 20 20 4f 70 74 |lash-com|ma. Opt|
|000012d0| 69 6f 6e 61 6c 20 70 61 | 72 74 73 20 6f 66 20 74 |ional pa|rts of t|
|000012e0| 68 65 20 70 61 72 61 6d | 65 74 65 72 0d 25 20 6c |he param|eter.% l|
|000012f0| 69 73 74 20 61 72 65 20 | 63 6f 6e 74 61 69 6e 65 |ist are |containe|
|00001300| 64 20 69 6e 20 5c 6f 70 | 74 69 6f 6e 61 6c 7b 2e |d in \op|tional{.|
|00001310| 2e 2e 7d 20 28 74 68 69 | 73 20 67 65 6e 65 72 61 |..} (thi|s genera|
|00001320| 74 65 73 20 61 20 73 65 | 74 20 6f 66 20 73 71 75 |tes a se|t of squ|
|00001330| 61 72 65 0d 25 20 62 72 | 61 63 6b 65 74 73 20 61 |are.% br|ackets a|
|00001340| 72 6f 75 6e 64 20 69 74 | 73 20 70 61 72 61 6d 65 |round it|s parame|
|00001350| 74 65 72 29 2e 20 20 41 | 72 67 75 6d 65 6e 74 73 |ter). A|rguments|
|00001360| 20 61 72 65 20 61 75 74 | 6f 6d 61 74 69 63 61 6c | are aut|omatical|
|00001370| 6c 79 20 73 65 74 20 69 | 6e 0d 25 20 69 74 61 6c |ly set i|n.% ital|
|00001380| 69 63 73 20 69 6e 20 74 | 68 65 20 70 61 72 61 6d |ics in t|he param|
|00001390| 65 74 65 72 20 6c 69 73 | 74 2e 20 20 45 61 63 68 |eter lis|t. Each|
|000013a0| 20 61 72 67 75 6d 65 6e | 74 20 73 68 6f 75 6c 64 | argumen|t should|
|000013b0| 20 62 65 20 6d 65 6e 74 | 69 6f 6e 65 64 20 61 74 | be ment|ioned at|
|000013c0| 0d 25 20 6c 65 61 73 74 | 20 6f 6e 63 65 20 69 6e |.% least| once in|
|000013d0| 20 74 68 65 20 64 65 73 | 63 72 69 70 74 69 6f 6e | the des|cription|
|000013e0| 3b 20 65 61 63 68 20 75 | 73 61 67 65 20 28 65 76 |; each u|sage (ev|
|000013f0| 65 6e 20 69 6e 73 69 64 | 65 20 5c 63 6f 64 65 7b |en insid|e \code{|
|00001400| 2e 2e 2e 7d 29 0d 25 20 | 73 68 6f 75 6c 64 20 62 |...}).% |should b|
|00001410| 65 20 65 6e 63 6c 6f 73 | 65 64 20 69 6e 20 5c 76 |e enclos|ed in \v|
|00001420| 61 72 7b 2e 2e 2e 7d 2e | 0d 0d 5c 62 65 67 69 6e |ar{...}.|..\begin|
|00001430| 7b 66 75 6e 63 64 65 73 | 63 7d 7b 61 73 74 32 74 |{funcdes|c}{ast2t|
|00001440| 75 70 6c 65 7d 7b 61 73 | 74 7d 0d 54 68 69 73 20 |uple}{as|t}.This |
|00001450| 66 75 6e 63 74 69 6f 6e | 20 61 63 63 65 70 74 73 |function| accepts|
|00001460| 20 61 6e 20 41 53 54 20 | 6f 62 6a 65 63 74 20 66 | an AST |object f|
|00001470| 72 6f 6d 20 74 68 65 20 | 63 61 6c 6c 65 72 20 69 |rom the |caller i|
|00001480| 6e 0d 5c 63 6f 64 65 7b | 5c 76 61 72 7b 61 73 74 |n.\code{|\var{ast|
|00001490| 7d 7d 20 61 6e 64 20 72 | 65 74 75 72 6e 73 20 61 |}} and r|eturns a|
|000014a0| 20 50 79 74 68 6f 6e 20 | 74 75 70 6c 65 20 72 65 | Python |tuple re|
|000014b0| 70 72 65 73 65 6e 74 69 | 6e 67 20 74 68 65 0d 65 |presenti|ng the.e|
|000014c0| 71 75 69 76 65 6c 65 6e | 74 20 70 61 72 73 65 20 |quivelen|t parse |
|000014d0| 74 72 65 65 2e 20 20 54 | 68 65 20 72 65 73 75 6c |tree. T|he resul|
|000014e0| 74 69 6e 67 20 74 75 70 | 6c 65 20 72 65 70 72 65 |ting tup|le repre|
|000014f0| 73 65 6e 74 61 74 69 6f | 6e 20 63 61 6e 20 62 65 |sentatio|n can be|
|00001500| 20 75 73 65 64 0d 66 6f | 72 20 69 6e 73 70 65 63 | used.fo|r inspec|
|00001510| 74 69 6f 6e 20 6f 72 20 | 74 68 65 20 63 72 65 61 |tion or |the crea|
|00001520| 74 69 6f 6e 20 6f 66 20 | 61 20 6e 65 77 20 70 61 |tion of |a new pa|
|00001530| 72 73 65 20 74 72 65 65 | 20 69 6e 20 74 75 70 6c |rse tree| in tupl|
|00001540| 65 20 66 6f 72 6d 2e 0d | 54 68 69 73 20 66 75 6e |e form..|This fun|
|00001550| 63 74 69 6f 6e 20 64 6f | 65 73 20 6e 6f 74 20 66 |ction do|es not f|
|00001560| 61 69 6c 20 73 6f 20 6c | 6f 6e 67 20 61 73 20 6d |ail so l|ong as m|
|00001570| 65 6d 6f 72 79 20 69 73 | 20 61 76 61 69 6c 61 62 |emory is| availab|
|00001580| 6c 65 20 74 6f 20 62 75 | 69 6c 64 0d 74 68 65 20 |le to bu|ild.the |
|00001590| 74 75 70 6c 65 20 72 65 | 70 72 65 73 65 6e 74 61 |tuple re|presenta|
|000015a0| 74 69 6f 6e 2e 0d 5c 65 | 6e 64 7b 66 75 6e 63 64 |tion..\e|nd{funcd|
|000015b0| 65 73 63 7d 0d 0d 0d 5c | 62 65 67 69 6e 7b 66 75 |esc}...\|begin{fu|
|000015c0| 6e 63 64 65 73 63 7d 7b | 63 6f 6d 70 69 6c 65 61 |ncdesc}{|compilea|
|000015d0| 73 74 7d 7b 61 73 74 5c | 6f 70 74 69 6f 6e 61 6c |st}{ast\|optional|
|000015e0| 7b 5c 2c 20 66 69 6c 65 | 6e 61 6d 65 20 5c 63 6f |{\, file|name \co|
|000015f0| 64 65 7b 3d 20 27 3c 61 | 73 74 3e 27 7d 7d 7d 0d |de{= '<a|st>'}}}.|
|00001600| 54 68 65 20 50 79 74 68 | 6f 6e 20 62 79 74 65 20 |The Pyth|on byte |
|00001610| 63 6f 6d 70 69 6c 65 72 | 20 63 61 6e 20 62 65 20 |compiler| can be |
|00001620| 69 6e 76 6f 6b 65 64 20 | 6f 6e 20 61 6e 20 41 53 |invoked |on an AS|
|00001630| 54 20 6f 62 6a 65 63 74 | 20 74 6f 20 70 72 6f 64 |T object| to prod|
|00001640| 75 63 65 0d 63 6f 64 65 | 20 6f 62 6a 65 63 74 73 |uce.code| objects|
|00001650| 20 77 68 69 63 68 20 63 | 61 6e 20 62 65 20 75 73 | which c|an be us|
|00001660| 65 64 20 61 73 20 70 61 | 72 74 20 6f 66 20 61 6e |ed as pa|rt of an|
|00001670| 20 5c 63 6f 64 65 7b 65 | 78 65 63 7d 20 73 74 61 | \code{e|xec} sta|
|00001680| 74 65 6d 65 6e 74 20 6f | 72 0d 61 20 63 61 6c 6c |tement o|r.a call|
|00001690| 20 74 6f 20 74 68 65 20 | 62 75 69 6c 74 2d 69 6e | to the |built-in|
|000016a0| 20 5c 63 6f 64 65 7b 65 | 76 61 6c 28 29 7d 20 66 | \code{e|val()} f|
|000016b0| 75 6e 63 74 69 6f 6e 2e | 20 20 54 68 69 73 20 66 |unction.| This f|
|000016c0| 75 6e 63 74 69 6f 6e 20 | 70 72 6f 76 69 64 65 73 |unction |provides|
|000016d0| 0d 74 68 65 20 69 6e 74 | 65 72 66 61 63 65 20 74 |.the int|erface t|
|000016e0| 6f 20 74 68 65 20 63 6f | 6d 70 69 6c 65 72 2c 20 |o the co|mpiler, |
|000016f0| 70 61 73 73 69 6e 67 20 | 74 68 65 20 69 6e 74 65 |passing |the inte|
|00001700| 72 6e 61 6c 20 70 61 72 | 73 65 20 74 72 65 65 20 |rnal par|se tree |
|00001710| 66 72 6f 6d 0d 5c 63 6f | 64 65 7b 5c 76 61 72 7b |from.\co|de{\var{|
|00001720| 61 73 74 7d 7d 20 74 6f | 20 74 68 65 20 70 61 72 |ast}} to| the par|
|00001730| 73 65 72 2c 20 75 73 69 | 6e 67 20 74 68 65 20 73 |ser, usi|ng the s|
|00001740| 6f 75 72 63 65 20 66 69 | 6c 65 20 6e 61 6d 65 20 |ource fi|le name |
|00001750| 73 70 65 63 69 66 69 65 | 64 0d 62 79 20 74 68 65 |specifie|d.by the|
|00001760| 20 5c 63 6f 64 65 7b 5c | 76 61 72 7b 66 69 6c 65 | \code{\|var{file|
|00001770| 6e 61 6d 65 7d 7d 20 70 | 61 72 61 6d 65 74 65 72 |name}} p|arameter|
|00001780| 2e 20 20 54 68 65 20 64 | 65 66 61 75 6c 74 20 76 |. The d|efault v|
|00001790| 61 6c 75 65 20 73 75 70 | 70 6c 69 65 64 0d 66 6f |alue sup|plied.fo|
|000017a0| 72 20 5c 63 6f 64 65 7b | 5c 76 61 72 7b 66 69 6c |r \code{|\var{fil|
|000017b0| 65 6e 61 6d 65 7d 7d 20 | 69 6e 64 69 63 61 74 65 |ename}} |indicate|
|000017c0| 73 20 74 68 61 74 20 74 | 68 65 20 73 6f 75 72 63 |s that t|he sourc|
|000017d0| 65 20 77 61 73 20 61 6e | 20 41 53 54 20 6f 62 6a |e was an| AST obj|
|000017e0| 65 63 74 2e 0d 5c 65 6e | 64 7b 66 75 6e 63 64 65 |ect..\en|d{funcde|
|000017f0| 73 63 7d 0d 0d 0d 5c 62 | 65 67 69 6e 7b 66 75 6e |sc}...\b|egin{fun|
|00001800| 63 64 65 73 63 7d 7b 65 | 78 70 72 7d 7b 73 74 72 |cdesc}{e|xpr}{str|
|00001810| 69 6e 67 7d 0d 54 68 65 | 20 5c 63 6f 64 65 7b 65 |ing}.The| \code{e|
|00001820| 78 70 72 28 29 7d 20 66 | 75 6e 63 74 69 6f 6e 20 |xpr()} f|unction |
|00001830| 70 61 72 73 65 73 20 74 | 68 65 20 70 61 72 61 6d |parses t|he param|
|00001840| 65 74 65 72 20 5c 63 6f | 64 65 7b 5c 76 61 72 7b |eter \co|de{\var{|
|00001850| 73 74 72 69 6e 67 7d 7d | 0d 61 73 20 69 66 20 69 |string}}|.as if i|
|00001860| 74 20 77 65 72 65 20 61 | 6e 20 69 6e 70 75 74 20 |t were a|n input |
|00001870| 74 6f 20 5c 63 6f 64 65 | 7b 63 6f 6d 70 69 6c 65 |to \code|{compile|
|00001880| 28 5c 76 61 72 7b 73 74 | 72 69 6e 67 7d 2c 20 27 |(\var{st|ring}, '|
|00001890| 65 76 61 6c 27 29 7d 2e | 20 20 49 66 0d 74 68 65 |eval')}.| If.the|
|000018a0| 20 70 61 72 73 65 20 73 | 75 63 63 65 65 64 73 2c | parse s|ucceeds,|
|000018b0| 20 61 6e 20 41 53 54 20 | 6f 62 6a 65 63 74 20 69 | an AST |object i|
|000018c0| 73 20 63 72 65 61 74 65 | 64 20 74 6f 20 68 6f 6c |s create|d to hol|
|000018d0| 64 20 74 68 65 20 69 6e | 74 65 72 6e 61 6c 0d 70 |d the in|ternal.p|
|000018e0| 61 72 73 65 20 74 72 65 | 65 20 72 65 70 72 65 73 |arse tre|e repres|
|000018f0| 65 6e 74 61 74 69 6f 6e | 2c 20 6f 74 68 65 72 77 |entation|, otherw|
|00001900| 69 73 65 20 61 6e 20 61 | 70 70 72 6f 70 72 69 61 |ise an a|ppropria|
|00001910| 74 65 20 65 78 63 65 70 | 74 69 6f 6e 20 69 73 0d |te excep|tion is.|
|00001920| 74 68 72 6f 77 6e 2e 0d | 5c 65 6e 64 7b 66 75 6e |thrown..|\end{fun|
|00001930| 63 64 65 73 63 7d 0d 0d | 0d 5c 62 65 67 69 6e 7b |cdesc}..|.\begin{|
|00001940| 66 75 6e 63 64 65 73 63 | 7d 7b 69 73 65 78 70 72 |funcdesc|}{isexpr|
|00001950| 7d 7b 61 73 74 7d 0d 57 | 68 65 6e 20 5c 63 6f 64 |}{ast}.W|hen \cod|
|00001960| 65 7b 5c 76 61 72 7b 61 | 73 74 7d 7d 20 72 65 70 |e{\var{a|st}} rep|
|00001970| 72 65 73 65 6e 74 73 20 | 61 6e 20 5c 63 6f 64 65 |resents |an \code|
|00001980| 7b 27 65 76 61 6c 27 7d | 20 66 6f 72 6d 2c 20 74 |{'eval'}| form, t|
|00001990| 68 69 73 20 66 75 6e 63 | 74 69 6f 6e 0d 72 65 74 |his func|tion.ret|
|000019a0| 75 72 6e 73 20 61 20 74 | 72 75 65 20 76 61 6c 75 |urns a t|rue valu|
|000019b0| 65 20 28 5c 63 6f 64 65 | 7b 31 7d 29 2c 20 6f 74 |e (\code|{1}), ot|
|000019c0| 68 65 72 77 69 73 65 20 | 69 74 20 72 65 74 75 72 |herwise |it retur|
|000019d0| 6e 73 20 66 61 6c 73 65 | 0d 28 5c 63 6f 64 65 7b |ns false|.(\code{|
|000019e0| 30 7d 29 2e 20 20 54 68 | 69 73 20 69 73 20 75 73 |0}). Th|is is us|
|000019f0| 65 66 75 6c 2c 20 73 69 | 6e 63 65 20 63 6f 64 65 |eful, si|nce code|
|00001a00| 20 6f 62 6a 65 63 74 73 | 20 6e 6f 72 6d 61 6c 6c | objects| normall|
|00001a10| 79 20 63 61 6e 6e 6f 74 | 20 62 65 0d 71 75 65 72 |y cannot| be.quer|
|00001a20| 69 65 64 20 66 6f 72 20 | 74 68 69 73 20 69 6e 66 |ied for |this inf|
|00001a30| 6f 72 6d 61 74 69 6f 6e | 20 75 73 69 6e 67 20 65 |ormation| using e|
|00001a40| 78 69 73 74 69 6e 67 20 | 62 75 69 6c 74 2d 69 6e |xisting |built-in|
|00001a50| 20 66 75 6e 63 74 69 6f | 6e 73 2e 20 20 4e 6f 74 | functio|ns. Not|
|00001a60| 65 0d 74 68 61 74 20 74 | 68 65 20 63 6f 64 65 20 |e.that t|he code |
|00001a70| 6f 62 6a 65 63 74 73 20 | 63 72 65 61 74 65 64 20 |objects |created |
|00001a80| 62 79 20 5c 63 6f 64 65 | 7b 63 6f 6d 70 69 6c 65 |by \code|{compile|
|00001a90| 61 73 74 28 29 7d 20 63 | 61 6e 6e 6f 74 20 62 65 |ast()} c|annot be|
|00001aa0| 20 71 75 65 72 69 65 64 | 0d 6c 69 6b 65 20 74 68 | queried|.like th|
|00001ab0| 69 73 20 65 69 74 68 65 | 72 2c 20 61 6e 64 20 61 |is eithe|r, and a|
|00001ac0| 72 65 20 69 64 65 6e 74 | 69 63 61 6c 20 74 6f 20 |re ident|ical to |
|00001ad0| 74 68 6f 73 65 20 63 72 | 65 61 74 65 64 20 62 79 |those cr|eated by|
|00001ae0| 20 74 68 65 20 62 75 69 | 6c 74 2d 69 6e 0d 5c 63 | the bui|lt-in.\c|
|00001af0| 6f 64 65 7b 63 6f 6d 70 | 69 6c 65 28 29 7d 20 66 |ode{comp|ile()} f|
|00001b00| 75 6e 63 74 69 6f 6e 2e | 0d 5c 65 6e 64 7b 66 75 |unction.|.\end{fu|
|00001b10| 6e 63 64 65 73 63 7d 0d | 0d 0d 5c 62 65 67 69 6e |ncdesc}.|..\begin|
|00001b20| 7b 66 75 6e 63 64 65 73 | 63 7d 7b 69 73 73 75 69 |{funcdes|c}{issui|
|00001b30| 74 65 7d 7b 61 73 74 7d | 0d 54 68 69 73 20 66 75 |te}{ast}|.This fu|
|00001b40| 6e 63 74 69 6f 6e 20 6d | 69 72 72 6f 72 73 20 5c |nction m|irrors \|
|00001b50| 63 6f 64 65 7b 69 73 65 | 78 70 72 28 29 7d 20 69 |code{ise|xpr()} i|
|00001b60| 6e 20 74 68 61 74 20 69 | 74 20 72 65 70 6f 72 74 |n that i|t report|
|00001b70| 73 20 77 68 65 74 68 65 | 72 20 61 6e 0d 41 53 54 |s whethe|r an.AST|
|00001b80| 20 6f 62 6a 65 63 74 20 | 72 65 70 72 65 73 65 6e | object |represen|
|00001b90| 74 73 20 61 20 73 75 69 | 74 65 20 6f 66 20 73 74 |ts a sui|te of st|
|00001ba0| 61 74 65 6d 65 6e 74 73 | 2e 20 20 49 74 20 69 73 |atements|. It is|
|00001bb0| 20 6e 6f 74 20 73 61 66 | 65 20 74 6f 20 61 73 73 | not saf|e to ass|
|00001bc0| 75 6d 65 0d 74 68 61 74 | 20 74 68 69 73 20 66 75 |ume.that| this fu|
|00001bd0| 6e 63 74 69 6f 6e 20 69 | 73 20 65 71 75 69 76 65 |nction i|s equive|
|00001be0| 6c 65 6e 74 20 74 6f 20 | 5c 63 6f 64 65 7b 6e 6f |lent to |\code{no|
|00001bf0| 74 20 69 73 65 78 70 72 | 28 5c 76 61 72 7b 61 73 |t isexpr|(\var{as|
|00001c00| 74 7d 29 7d 2c 20 61 73 | 0d 61 64 64 69 74 69 6f |t})}, as|.additio|
|00001c10| 6e 61 6c 20 73 79 6e 74 | 61 63 74 69 63 20 66 72 |nal synt|actic fr|
|00001c20| 61 67 6d 65 6e 74 73 20 | 6d 61 79 20 62 65 20 73 |agments |may be s|
|00001c30| 75 70 70 6f 72 74 65 64 | 20 69 6e 20 74 68 65 20 |upported| in the |
|00001c40| 66 75 74 75 72 65 2e 0d | 5c 65 6e 64 7b 66 75 6e |future..|\end{fun|
|00001c50| 63 64 65 73 63 7d 0d 0d | 0d 5c 62 65 67 69 6e 7b |cdesc}..|.\begin{|
|00001c60| 66 75 6e 63 64 65 73 63 | 7d 7b 73 75 69 74 65 7d |funcdesc|}{suite}|
|00001c70| 7b 73 74 72 69 6e 67 7d | 0d 54 68 65 20 5c 63 6f |{string}|.The \co|
|00001c80| 64 65 7b 73 75 69 74 65 | 28 29 7d 20 66 75 6e 63 |de{suite|()} func|
|00001c90| 74 69 6f 6e 20 70 61 72 | 73 65 73 20 74 68 65 20 |tion par|ses the |
|00001ca0| 70 61 72 61 6d 65 74 65 | 72 20 5c 63 6f 64 65 7b |paramete|r \code{|
|00001cb0| 5c 76 61 72 7b 73 74 72 | 69 6e 67 7d 7d 0d 61 73 |\var{str|ing}}.as|
|00001cc0| 20 69 66 20 69 74 20 77 | 65 72 65 20 61 6e 20 69 | if it w|ere an i|
|00001cd0| 6e 70 75 74 20 74 6f 20 | 5c 63 6f 64 65 7b 63 6f |nput to |\code{co|
|00001ce0| 6d 70 69 6c 65 28 5c 76 | 61 72 7b 73 74 72 69 6e |mpile(\v|ar{strin|
|00001cf0| 67 7d 2c 20 27 65 78 65 | 63 27 29 7d 2e 20 20 49 |g}, 'exe|c')}. I|
|00001d00| 66 0d 74 68 65 20 70 61 | 72 73 65 20 73 75 63 63 |f.the pa|rse succ|
|00001d10| 65 65 64 73 2c 20 61 6e | 20 41 53 54 20 6f 62 6a |eeds, an| AST obj|
|00001d20| 65 63 74 20 69 73 20 63 | 72 65 61 74 65 64 20 74 |ect is c|reated t|
|00001d30| 6f 20 68 6f 6c 64 20 74 | 68 65 20 69 6e 74 65 72 |o hold t|he inter|
|00001d40| 6e 61 6c 0d 70 61 72 73 | 65 20 74 72 65 65 20 72 |nal.pars|e tree r|
|00001d50| 65 70 72 65 73 65 6e 74 | 61 74 69 6f 6e 2c 20 6f |epresent|ation, o|
|00001d60| 74 68 65 72 77 69 73 65 | 20 61 6e 20 61 70 70 72 |therwise| an appr|
|00001d70| 6f 70 72 69 61 74 65 20 | 65 78 63 65 70 74 69 6f |opriate |exceptio|
|00001d80| 6e 20 69 73 0d 74 68 72 | 6f 77 6e 2e 0d 5c 65 6e |n is.thr|own..\en|
|00001d90| 64 7b 66 75 6e 63 64 65 | 73 63 7d 0d 0d 0d 5c 62 |d{funcde|sc}...\b|
|00001da0| 65 67 69 6e 7b 66 75 6e | 63 64 65 73 63 7d 7b 74 |egin{fun|cdesc}{t|
|00001db0| 75 70 6c 65 32 61 73 74 | 7d 7b 74 75 70 6c 65 7d |uple2ast|}{tuple}|
|00001dc0| 0d 54 68 69 73 20 66 75 | 6e 63 74 69 6f 6e 20 61 |.This fu|nction a|
|00001dd0| 63 63 65 70 74 73 20 61 | 20 70 61 72 73 65 20 74 |ccepts a| parse t|
|00001de0| 72 65 65 20 72 65 70 72 | 65 73 65 6e 74 65 64 20 |ree repr|esented |
|00001df0| 61 73 20 61 20 74 75 70 | 6c 65 20 61 6e 64 20 62 |as a tup|le and b|
|00001e00| 75 69 6c 64 73 0d 61 6e | 20 69 6e 74 65 72 6e 61 |uilds.an| interna|
|00001e10| 6c 20 72 65 70 72 65 73 | 65 6e 74 61 74 69 6f 6e |l repres|entation|
|00001e20| 20 69 66 20 70 6f 73 73 | 69 62 6c 65 2e 20 20 49 | if poss|ible. I|
|00001e30| 66 20 69 74 20 63 61 6e | 20 76 61 6c 69 64 61 74 |f it can| validat|
|00001e40| 65 20 74 68 61 74 20 74 | 68 65 0d 74 72 65 65 20 |e that t|he.tree |
|00001e50| 63 6f 6e 66 6f 72 6d 73 | 20 74 6f 20 74 68 65 20 |conforms| to the |
|00001e60| 50 79 74 68 6f 6e 20 73 | 79 6e 74 61 78 20 61 6e |Python s|yntax an|
|00001e70| 64 20 61 6c 6c 20 6e 6f | 64 65 73 20 61 72 65 20 |d all no|des are |
|00001e80| 76 61 6c 69 64 20 6e 6f | 64 65 20 74 79 70 65 73 |valid no|de types|
|00001e90| 0d 69 6e 20 74 68 65 20 | 68 6f 73 74 20 76 65 72 |.in the |host ver|
|00001ea0| 73 69 6f 6e 20 6f 66 20 | 50 79 74 68 6f 6e 2c 20 |sion of |Python, |
|00001eb0| 61 6e 20 41 53 54 20 6f | 62 6a 65 63 74 20 69 73 |an AST o|bject is|
|00001ec0| 20 63 72 65 61 74 65 64 | 20 66 72 6f 6d 20 74 68 | created| from th|
|00001ed0| 65 0d 69 6e 74 65 72 6e | 61 6c 20 72 65 70 72 65 |e.intern|al repre|
|00001ee0| 73 65 6e 74 61 74 69 6f | 6e 20 61 6e 64 20 72 65 |sentatio|n and re|
|00001ef0| 74 75 72 6e 65 64 20 74 | 6f 20 74 68 65 20 63 61 |turned t|o the ca|
|00001f00| 6c 6c 65 64 2e 20 20 49 | 66 20 74 68 65 72 65 20 |lled. I|f there |
|00001f10| 69 73 20 61 0d 70 72 6f | 62 6c 65 6d 20 63 72 65 |is a.pro|blem cre|
|00001f20| 61 74 69 6e 67 20 74 68 | 65 20 69 6e 74 65 72 6e |ating th|e intern|
|00001f30| 61 6c 20 72 65 70 72 65 | 73 65 6e 74 61 74 69 6f |al repre|sentatio|
|00001f40| 6e 2c 20 6f 72 20 69 66 | 20 74 68 65 20 74 72 65 |n, or if| the tre|
|00001f50| 65 20 63 61 6e 6e 6f 74 | 20 62 65 0d 76 61 6c 69 |e cannot| be.vali|
|00001f60| 64 61 74 65 64 2c 20 61 | 20 5c 63 6f 64 65 7b 50 |dated, a| \code{P|
|00001f70| 61 72 73 65 72 45 72 72 | 6f 72 7d 20 65 78 63 65 |arserErr|or} exce|
|00001f80| 70 74 69 6f 6e 20 69 73 | 20 74 68 72 6f 77 6e 2e |ption is| thrown.|
|00001f90| 20 20 41 6e 20 41 53 54 | 20 6f 62 6a 65 63 74 0d | An AST| object.|
|00001fa0| 63 72 65 61 74 65 64 20 | 74 68 69 73 20 77 61 79 |created |this way|
|00001fb0| 20 73 68 6f 75 6c 64 20 | 6e 6f 74 20 62 65 20 61 | should |not be a|
|00001fc0| 73 73 75 6d 65 64 20 74 | 6f 20 63 6f 6d 70 69 6c |ssumed t|o compil|
|00001fd0| 65 20 63 6f 72 72 65 63 | 74 6c 79 3b 20 6e 6f 72 |e correc|tly; nor|
|00001fe0| 6d 61 6c 0d 65 78 63 65 | 70 74 69 6f 6e 73 20 74 |mal.exce|ptions t|
|00001ff0| 68 72 6f 77 6e 20 62 79 | 20 63 6f 6d 70 69 6c 61 |hrown by| compila|
|00002000| 74 69 6f 6e 20 6d 61 79 | 20 73 74 69 6c 6c 20 62 |tion may| still b|
|00002010| 65 20 69 6e 69 74 69 61 | 74 65 64 20 77 68 65 6e |e initia|ted when|
|00002020| 20 74 68 65 20 41 53 54 | 0d 6f 62 6a 65 63 74 20 | the AST|.object |
|00002030| 69 73 20 70 61 73 73 65 | 64 20 74 6f 20 5c 63 6f |is passe|d to \co|
|00002040| 64 65 7b 63 6f 6d 70 69 | 6c 65 61 73 74 28 29 7d |de{compi|least()}|
|00002050| 2e 20 20 54 68 69 73 20 | 77 69 6c 6c 20 6e 6f 72 |. This |will nor|
|00002060| 6d 61 6c 6c 79 20 69 6e | 64 69 63 61 74 65 0d 70 |mally in|dicate.p|
|00002070| 72 6f 62 6c 65 6d 73 20 | 6e 6f 74 20 72 65 6c 61 |roblems |not rela|
|00002080| 74 65 64 20 74 6f 20 73 | 79 6e 74 61 78 20 28 73 |ted to s|yntax (s|
|00002090| 75 63 68 20 61 73 20 61 | 20 5c 63 6f 64 65 7b 4d |uch as a| \code{M|
|000020a0| 65 6d 6f 72 79 45 72 72 | 6f 72 7d 0d 65 78 63 65 |emoryErr|or}.exce|
|000020b0| 70 74 69 6f 6e 29 2e 0d | 5c 65 6e 64 7b 66 75 6e |ption)..|\end{fun|
|000020c0| 63 64 65 73 63 7d 0d 0d | 0d 25 20 2d 2d 2d 20 33 |cdesc}..|.% --- 3|
|000020d0| 2e 34 2e 20 2d 2d 2d 0d | 25 20 45 78 63 65 70 74 |.4. ---.|% Except|
|000020e0| 69 6f 6e 73 20 61 72 65 | 20 64 65 73 63 72 69 62 |ions are| describ|
|000020f0| 65 64 20 75 73 69 6e 67 | 20 61 20 60 60 65 78 63 |ed using| a ``exc|
|00002100| 64 65 73 63 27 27 20 62 | 6c 6f 63 6b 2e 20 20 54 |desc'' b|lock. T|
|00002110| 68 69 73 20 68 61 73 20 | 6f 6e 6c 79 0d 25 20 6f |his has |only.% o|
|00002120| 6e 65 20 70 61 72 61 6d | 65 74 65 72 3a 20 74 68 |ne param|eter: th|
|00002130| 65 20 65 78 63 65 70 74 | 69 6f 6e 20 6e 61 6d 65 |e except|ion name|
|00002140| 2e 0d 0d 5c 73 75 62 73 | 65 63 74 69 6f 6e 7b 45 |...\subs|ection{E|
|00002150| 78 63 65 70 74 69 6f 6e | 73 20 61 6e 64 20 45 72 |xception|s and Er|
|00002160| 72 6f 72 20 48 61 6e 64 | 6c 69 6e 67 7d 0d 0d 54 |ror Hand|ling}..T|
|00002170| 68 65 20 70 61 72 73 65 | 72 20 6d 6f 64 75 6c 65 |he parse|r module|
|00002180| 20 64 65 66 69 6e 65 73 | 20 61 20 73 69 6e 67 6c | defines| a singl|
|00002190| 65 20 65 78 63 65 70 74 | 69 6f 6e 2c 20 62 75 74 |e except|ion, but|
|000021a0| 20 6d 61 79 20 61 6c 73 | 6f 20 70 61 73 73 20 6f | may als|o pass o|
|000021b0| 74 68 65 72 0d 62 75 69 | 6c 74 2d 69 6e 20 65 78 |ther.bui|lt-in ex|
|000021c0| 63 65 70 74 69 6f 6e 73 | 20 66 72 6f 6d 20 6f 74 |ceptions| from ot|
|000021d0| 68 65 72 20 70 6f 72 74 | 69 6f 6e 73 20 6f 66 20 |her port|ions of |
|000021e0| 74 68 65 20 50 79 74 68 | 6f 6e 20 72 75 6e 74 69 |the Pyth|on runti|
|000021f0| 6d 65 0d 65 6e 76 69 72 | 6f 6e 6d 65 6e 74 2e 20 |me.envir|onment. |
|00002200| 20 53 65 65 20 65 61 63 | 68 20 66 75 6e 63 74 69 | See eac|h functi|
|00002210| 6f 6e 20 66 6f 72 20 69 | 6e 66 6f 72 6d 61 74 69 |on for i|nformati|
|00002220| 6f 6e 20 61 62 6f 75 74 | 20 74 68 65 20 65 78 63 |on about| the exc|
|00002230| 65 70 74 69 6f 6e 73 0d | 69 74 20 63 61 6e 20 72 |eptions.|it can r|
|00002240| 61 69 73 65 2e 0d 0d 5c | 62 65 67 69 6e 7b 65 78 |aise...\|begin{ex|
|00002250| 63 64 65 73 63 7d 7b 50 | 61 72 73 65 72 45 72 72 |cdesc}{P|arserErr|
|00002260| 6f 72 7d 0d 45 78 63 65 | 70 74 69 6f 6e 20 72 61 |or}.Exce|ption ra|
|00002270| 69 73 65 64 20 77 68 65 | 6e 20 61 20 66 61 69 6c |ised whe|n a fail|
|00002280| 75 72 65 20 6f 63 63 75 | 72 73 20 77 69 74 68 69 |ure occu|rs withi|
|00002290| 6e 20 74 68 65 20 70 61 | 72 73 65 72 20 6d 6f 64 |n the pa|rser mod|
|000022a0| 75 6c 65 2e 20 20 54 68 | 69 73 0d 69 73 20 67 65 |ule. Th|is.is ge|
|000022b0| 6e 65 72 61 6c 6c 79 20 | 70 72 6f 64 75 63 65 64 |nerally |produced|
|000022c0| 20 66 6f 72 20 76 61 6c | 69 64 61 74 69 6f 6e 20 | for val|idation |
|000022d0| 66 61 69 6c 75 72 65 73 | 20 72 61 74 68 65 72 20 |failures| rather |
|000022e0| 74 68 61 6e 20 74 68 65 | 20 62 75 69 6c 74 20 69 |than the| built i|
|000022f0| 6e 0d 5c 63 6f 64 65 7b | 53 79 6e 74 61 78 45 72 |n.\code{|SyntaxEr|
|00002300| 72 6f 72 7d 20 74 68 72 | 6f 77 6e 20 64 75 72 69 |ror} thr|own duri|
|00002310| 6e 67 20 6e 6f 72 6d 61 | 6c 20 70 61 72 73 69 6e |ng norma|l parsin|
|00002320| 67 2e 0d 54 68 65 20 65 | 78 63 65 70 74 69 6f 6e |g..The e|xception|
|00002330| 20 61 72 67 75 6d 65 6e | 74 20 69 73 20 65 69 74 | argumen|t is eit|
|00002340| 68 65 72 20 61 20 73 74 | 72 69 6e 67 20 64 65 73 |her a st|ring des|
|00002350| 63 72 69 62 69 6e 67 20 | 74 68 65 20 72 65 61 73 |cribing |the reas|
|00002360| 6f 6e 20 6f 66 20 74 68 | 65 0d 66 61 69 6c 75 72 |on of th|e.failur|
|00002370| 65 20 6f 72 20 61 20 74 | 75 70 6c 65 20 63 6f 6e |e or a t|uple con|
|00002380| 74 61 69 6e 69 6e 67 20 | 61 20 74 75 70 6c 65 20 |taining |a tuple |
|00002390| 63 61 75 73 69 6e 67 20 | 74 68 65 20 66 61 69 6c |causing |the fail|
|000023a0| 75 72 65 20 66 72 6f 6d | 20 61 20 70 61 72 73 65 |ure from| a parse|
|000023b0| 0d 74 72 65 65 20 70 61 | 73 73 65 64 20 74 6f 20 |.tree pa|ssed to |
|000023c0| 5c 63 6f 64 65 7b 74 75 | 70 6c 65 32 61 73 74 28 |\code{tu|ple2ast(|
|000023d0| 29 7d 20 61 6e 64 20 61 | 6e 20 65 78 70 6c 61 6e |)} and a|n explan|
|000023e0| 61 74 6f 72 79 20 73 74 | 72 69 6e 67 2e 20 20 43 |atory st|ring. C|
|000023f0| 61 6c 6c 73 20 74 6f 0d | 5c 63 6f 64 65 7b 74 75 |alls to.|\code{tu|
|00002400| 70 6c 65 32 61 73 74 28 | 29 7d 20 6e 65 65 64 20 |ple2ast(|)} need |
|00002410| 74 6f 20 62 65 20 61 62 | 6c 65 20 74 6f 20 68 61 |to be ab|le to ha|
|00002420| 6e 64 6c 65 20 65 69 74 | 68 65 72 20 74 79 70 65 |ndle eit|her type|
|00002430| 20 6f 66 20 65 78 63 65 | 70 74 69 6f 6e 2c 0d 77 | of exce|ption,.w|
|00002440| 68 69 6c 65 20 63 61 6c | 6c 73 20 74 6f 20 6f 74 |hile cal|ls to ot|
|00002450| 68 65 72 20 66 75 6e 63 | 74 69 6f 6e 73 20 69 6e |her func|tions in|
|00002460| 20 74 68 65 20 6d 6f 64 | 75 6c 65 20 77 69 6c 6c | the mod|ule will|
|00002470| 20 6f 6e 6c 79 20 6e 65 | 65 64 20 74 6f 20 62 65 | only ne|ed to be|
|00002480| 0d 61 77 61 72 65 20 6f | 66 20 74 68 65 20 73 69 |.aware o|f the si|
|00002490| 6d 70 6c 65 20 73 74 72 | 69 6e 67 20 76 61 6c 75 |mple str|ing valu|
|000024a0| 65 73 2e 0d 5c 65 6e 64 | 7b 65 78 63 64 65 73 63 |es..\end|{excdesc|
|000024b0| 7d 0d 0d 4e 6f 74 65 20 | 74 68 61 74 20 74 68 65 |}..Note |that the|
|000024c0| 20 66 75 6e 63 74 69 6f | 6e 73 20 5c 63 6f 64 65 | functio|ns \code|
|000024d0| 7b 63 6f 6d 70 69 6c 65 | 61 73 74 28 29 7d 2c 20 |{compile|ast()}, |
|000024e0| 5c 63 6f 64 65 7b 65 78 | 70 72 28 29 7d 2c 20 61 |\code{ex|pr()}, a|
|000024f0| 6e 64 0d 5c 63 6f 64 65 | 7b 73 75 69 74 65 28 29 |nd.\code|{suite()|
|00002500| 7d 20 6d 61 79 20 74 68 | 72 6f 77 20 65 78 63 65 |} may th|row exce|
|00002510| 70 74 69 6f 6e 73 20 77 | 68 69 63 68 20 61 72 65 |ptions w|hich are|
|00002520| 20 6e 6f 72 6d 61 6c 6c | 79 20 74 68 72 6f 77 6e | normall|y thrown|
|00002530| 20 62 79 20 74 68 65 0d | 70 61 72 73 69 6e 67 20 | by the.|parsing |
|00002540| 61 6e 64 20 63 6f 6d 70 | 69 6c 61 74 69 6f 6e 20 |and comp|ilation |
|00002550| 70 72 6f 63 65 73 73 2e | 20 20 54 68 65 73 65 20 |process.| These |
|00002560| 69 6e 63 6c 75 64 65 20 | 74 68 65 20 62 75 69 6c |include |the buil|
|00002570| 74 20 69 6e 0d 65 78 63 | 65 70 74 69 6f 6e 73 20 |t in.exc|eptions |
|00002580| 5c 63 6f 64 65 7b 4d 65 | 6d 6f 72 79 45 72 72 6f |\code{Me|moryErro|
|00002590| 72 7d 2c 20 5c 63 6f 64 | 65 7b 4f 76 65 72 66 6c |r}, \cod|e{Overfl|
|000025a0| 6f 77 45 72 72 6f 72 7d | 2c 0d 5c 63 6f 64 65 7b |owError}|,.\code{|
|000025b0| 53 79 6e 74 61 78 45 72 | 72 6f 72 7d 2c 20 61 6e |SyntaxEr|ror}, an|
|000025c0| 64 20 5c 63 6f 64 65 7b | 53 79 73 74 65 6d 45 72 |d \code{|SystemEr|
|000025d0| 72 6f 72 7d 2e 20 20 49 | 6e 20 74 68 65 73 65 20 |ror}. I|n these |
|000025e0| 63 61 73 65 73 2c 20 74 | 68 65 73 65 0d 65 78 63 |cases, t|hese.exc|
|000025f0| 65 70 74 69 6f 6e 73 20 | 63 61 72 72 79 20 61 6c |eptions |carry al|
|00002600| 6c 20 74 68 65 20 6d 65 | 61 6e 69 6e 67 20 6e 6f |l the me|aning no|
|00002610| 72 6d 61 6c 6c 79 20 61 | 73 73 6f 63 69 61 74 65 |rmally a|ssociate|
|00002620| 64 20 77 69 74 68 20 74 | 68 65 6d 2e 20 20 52 65 |d with t|hem. Re|
|00002630| 66 65 72 0d 74 6f 20 74 | 68 65 20 64 65 73 63 72 |fer.to t|he descr|
|00002640| 69 70 74 69 6f 6e 73 20 | 6f 66 20 65 61 63 68 20 |iptions |of each |
|00002650| 66 75 6e 63 74 69 6f 6e | 20 66 6f 72 20 64 65 74 |function| for det|
|00002660| 61 69 6c 65 64 20 69 6e | 66 6f 72 6d 61 74 69 6f |ailed in|formatio|
|00002670| 6e 2e 0d 0d 25 20 2d 2d | 2d 2d 20 33 2e 35 2e 20 |n...% --|-- 3.5. |
|00002680| 2d 2d 2d 2d 0d 25 20 54 | 68 65 72 65 20 69 73 20 |----.% T|here is |
|00002690| 6e 6f 20 73 74 61 6e 64 | 61 72 64 20 62 6c 6f 63 |no stand|ard bloc|
|000026a0| 6b 20 74 79 70 65 20 66 | 6f 72 20 63 6c 61 73 73 |k type f|or class|
|000026b0| 65 73 2e 20 20 49 20 67 | 65 6e 65 72 61 6c 6c 79 |es. I g|enerally|
|000026c0| 20 75 73 65 0d 25 20 60 | 60 66 75 6e 63 64 65 73 | use.% `|`funcdes|
|000026d0| 63 27 27 20 62 6c 6f 63 | 6b 73 2c 20 73 69 6e 63 |c'' bloc|ks, sinc|
|000026e0| 65 20 63 6c 61 73 73 20 | 69 6e 73 74 61 6e 74 69 |e class |instanti|
|000026f0| 61 74 69 6f 6e 20 6c 6f | 6f 6b 73 20 76 65 72 79 |ation lo|oks very|
|00002700| 20 6d 75 63 68 20 6c 69 | 6b 65 0d 25 20 61 20 66 | much li|ke.% a f|
|00002710| 75 6e 63 74 69 6f 6e 20 | 63 61 6c 6c 2e 0d 0d 0d |unction |call....|
|00002720| 25 20 3d 3d 3d 3d 20 34 | 2e 20 3d 3d 3d 3d 0d 25 |% ==== 4|. ====.%|
|00002730| 20 4e 6f 77 20 69 73 20 | 70 72 6f 62 61 62 6c 79 | Now is |probably|
|00002740| 20 61 20 67 6f 6f 64 20 | 74 69 6d 65 20 66 6f 72 | a good |time for|
|00002750| 20 61 20 63 6f 6d 70 6c | 65 74 65 20 65 78 61 6d | a compl|ete exam|
|00002760| 70 6c 65 2e 20 20 28 41 | 6c 74 65 72 6e 61 74 69 |ple. (A|lternati|
|00002770| 76 65 6c 79 2c 0d 25 20 | 61 6e 20 65 78 61 6d 70 |vely,.% |an examp|
|00002780| 6c 65 20 67 69 76 69 6e | 67 20 74 68 65 20 66 6c |le givin|g the fl|
|00002790| 61 76 6f 72 20 6f 66 20 | 74 68 65 20 6d 6f 64 75 |avor of |the modu|
|000027a0| 6c 65 20 6d 61 79 20 62 | 65 20 67 69 76 65 6e 20 |le may b|e given |
|000027b0| 62 65 66 6f 72 65 20 74 | 68 65 0d 25 20 64 65 74 |before t|he.% det|
|000027c0| 61 69 6c 65 64 20 6c 69 | 73 74 20 6f 66 20 66 75 |ailed li|st of fu|
|000027d0| 6e 63 74 69 6f 6e 73 2e | 29 0d 0d 5c 73 75 62 73 |nctions.|)..\subs|
|000027e0| 65 63 74 69 6f 6e 7b 45 | 78 61 6d 70 6c 65 7d 0d |ection{E|xample}.|
|000027f0| 0d 41 20 73 69 6d 70 6c | 65 20 65 78 61 6d 70 6c |.A simpl|e exampl|
|00002800| 65 3a 0d 0d 5c 62 65 67 | 69 6e 7b 76 65 72 62 61 |e:..\beg|in{verba|
|00002810| 74 69 6d 7d 0d 3e 3e 3e | 20 69 6d 70 6f 72 74 20 |tim}.>>>| import |
|00002820| 70 61 72 73 65 72 0d 3e | 3e 3e 20 61 73 74 20 3d |parser.>|>> ast =|
|00002830| 20 70 61 72 73 65 72 2e | 65 78 70 72 28 27 61 20 | parser.|expr('a |
|00002840| 2b 20 35 27 29 0d 3e 3e | 3e 20 63 6f 64 65 20 3d |+ 5').>>|> code =|
|00002850| 20 70 61 72 73 65 72 2e | 63 6f 6d 70 69 6c 65 61 | parser.|compilea|
|00002860| 73 74 28 61 73 74 29 0d | 3e 3e 3e 20 61 20 3d 20 |st(ast).|>>> a = |
|00002870| 35 0d 3e 3e 3e 20 65 76 | 61 6c 28 63 6f 64 65 29 |5.>>> ev|al(code)|
|00002880| 0d 31 30 0d 5c 65 6e 64 | 7b 76 65 72 62 61 74 69 |.10.\end|{verbati|
|00002890| 6d 7d 0d 0d 0d 5c 73 75 | 62 73 65 63 74 69 6f 6e |m}...\su|bsection|
|000028a0| 7b 41 53 54 20 4f 62 6a | 65 63 74 73 7d 0d 0d 41 |{AST Obj|ects}..A|
|000028b0| 53 54 20 6f 62 6a 65 63 | 74 73 20 28 72 65 74 75 |ST objec|ts (retu|
|000028c0| 72 6e 65 64 20 62 79 20 | 5c 63 6f 64 65 7b 65 78 |rned by |\code{ex|
|000028d0| 70 72 28 29 7d 2c 20 5c | 63 6f 64 65 7b 73 75 69 |pr()}, \|code{sui|
|000028e0| 74 65 28 29 7d 2c 20 61 | 6e 64 0d 5c 63 6f 64 65 |te()}, a|nd.\code|
|000028f0| 7b 74 75 70 6c 65 32 61 | 73 74 28 29 7d 2c 20 64 |{tuple2a|st()}, d|
|00002900| 65 73 63 72 69 62 65 64 | 20 61 62 6f 76 65 29 20 |escribed| above) |
|00002910| 68 61 76 65 20 6e 6f 20 | 6d 65 74 68 6f 64 73 20 |have no |methods |
|00002920| 6f 66 20 74 68 65 69 72 | 20 6f 77 6e 2e 0d 53 6f |of their| own..So|
|00002930| 6d 65 20 6f 66 20 74 68 | 65 20 66 75 6e 63 74 69 |me of th|e functi|
|00002940| 6f 6e 73 20 64 65 66 69 | 6e 65 64 20 77 68 69 63 |ons defi|ned whic|
|00002950| 68 20 61 63 63 65 70 74 | 20 61 6e 20 41 53 54 20 |h accept| an AST |
|00002960| 6f 62 6a 65 63 74 20 61 | 73 20 74 68 65 69 72 0d |object a|s their.|
|00002970| 66 69 72 73 74 20 61 72 | 67 75 6d 65 6e 74 20 6d |first ar|gument m|
|00002980| 61 79 20 63 68 61 6e 67 | 65 20 74 6f 20 6f 62 6a |ay chang|e to obj|
|00002990| 65 63 74 20 6d 65 74 68 | 6f 64 73 20 69 6e 20 74 |ect meth|ods in t|
|000029a0| 68 65 20 66 75 74 75 72 | 65 2e 0d 0d 4f 72 64 65 |he futur|e...Orde|
|000029b0| 72 65 64 20 61 6e 64 20 | 65 71 75 61 6c 69 74 79 |red and |equality|
|000029c0| 20 63 6f 6d 70 61 72 69 | 73 6f 6e 73 20 61 72 65 | compari|sons are|
|000029d0| 20 73 75 70 70 6f 72 74 | 65 64 20 62 65 74 77 65 | support|ed betwe|
|000029e0| 65 6e 20 41 53 54 20 6f | 62 6a 65 63 74 73 2e 0d |en AST o|bjects..|
|000029f0| 0d 5c 72 65 6e 65 77 63 | 6f 6d 6d 61 6e 64 7b 5c |.\renewc|ommand{\|
|00002a00| 69 6e 64 65 78 73 75 62 | 69 74 65 6d 7d 7b 28 61 |indexsub|item}{(a|
|00002a10| 73 74 20 6d 65 74 68 6f | 64 29 7d 0d 0d 25 5c 62 |st metho|d)}..%\b|
|00002a20| 65 67 69 6e 7b 66 75 6e | 63 64 65 73 63 7d 7b 65 |egin{fun|cdesc}{e|
|00002a30| 6d 70 74 79 7d 7b 7d 0d | 25 45 6d 70 74 79 20 74 |mpty}{}.|%Empty t|
|00002a40| 68 65 20 63 61 6e 20 69 | 6e 74 6f 20 74 68 65 20 |he can i|nto the |
|00002a50| 74 72 61 73 68 2e 0d 25 | 5c 65 6e 64 7b 66 75 6e |trash..%|\end{fun|
|00002a60| 63 64 65 73 63 7d 0d | |cdesc}. | |
+--------+-------------------------+-------------------------+--------+--------+