home *** CD-ROM | disk | FTP | other *** search
open in:
MacOS 8.1
|
Win98
|
DOS
view JSON data
|
view as text
This file was processed as: LaTeX Document
(document/latex).
You can browse this item here: tex
Confidence | Program | Detection | Match Type | Support
|
---|
100%
| dexvert
| LaTeX Document (document/latex)
| magic
| Supported |
1%
| dexvert
| Text File (text/txt)
| fallback
| Supported |
100%
| file
| LaTeX document, ASCII text
| default
| |
100%
| checkBytes
| Printable ASCII
| default
| |
100%
| perlTextCheck
| Likely Text (Perl)
| default
| |
100%
| siegfried
| fmt/281 LaTeX (Subdocument)
| default
| |
100%
| detectItEasy
| Format: plain text[LF]
| default (weak)
| |
100%
| xdgMime
| text/x-matlab
| default (weak)
|
|
hex view+--------+-------------------------+-------------------------+--------+--------+
|00000000| 25 20 6c 69 62 70 61 72 | 73 65 72 2e 74 65 78 0a |% libpar|ser.tex.|
|00000010| 25 0a 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 0a 25 0a 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 0a 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| 0a 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 0a 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 0a 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 0a 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 0a 25 20 69 | 6e 20 61 6e 79 20 77 61 |kage.% i|n any wa|
|00000200| 79 2e 0a 25 0a 0a 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 0a 5c 62 69 6d 6f 64 | 69 6e 64 65 78 7b 70 61 |}.\bimod|index{pa|
|00000240| 72 73 65 72 7d 0a 0a 54 | 68 65 20 5c 63 6f 64 65 |rser}..T|he \code|
|00000250| 7b 70 61 72 73 65 72 7d | 20 6d 6f 64 75 6c 65 20 |{parser}| module |
|00000260| 70 72 6f 76 69 64 65 73 | 20 61 6e 20 69 6e 74 65 |provides| an inte|
|00000270| 72 66 61 63 65 20 74 6f | 20 50 79 74 68 6f 6e 27 |rface to| Python'|
|00000280| 73 20 69 6e 74 65 72 6e | 61 6c 0a 70 61 72 73 65 |s intern|al.parse|
|00000290| 72 20 61 6e 64 20 62 79 | 74 65 2d 63 6f 64 65 20 |r and by|te-code |
|000002a0| 63 6f 6d 70 69 6c 65 72 | 2e 20 20 54 68 65 20 70 |compiler|. The p|
|000002b0| 72 69 6d 61 72 79 20 70 | 75 72 70 6f 73 65 20 66 |rimary p|urpose f|
|000002c0| 6f 72 20 74 68 69 73 20 | 69 6e 74 65 72 66 61 63 |or this |interfac|
|000002d0| 65 0a 69 73 20 74 6f 20 | 61 6c 6c 6f 77 20 50 79 |e.is to |allow Py|
|000002e0| 74 68 6f 6e 20 63 6f 64 | 65 20 74 6f 20 65 64 69 |thon cod|e to edi|
|000002f0| 74 20 74 68 65 20 70 61 | 72 73 65 20 74 72 65 65 |t the pa|rse tree|
|00000300| 20 6f 66 20 61 20 50 79 | 74 68 6f 6e 20 65 78 70 | of a Py|thon exp|
|00000310| 72 65 73 73 69 6f 6e 0a | 61 6e 64 20 63 72 65 61 |ression.|and crea|
|00000320| 74 65 20 65 78 65 63 75 | 74 61 62 6c 65 20 63 6f |te execu|table co|
|00000330| 64 65 20 66 72 6f 6d 20 | 74 68 69 73 2e 20 20 54 |de from |this. T|
|00000340| 68 69 73 20 69 73 20 62 | 65 74 74 65 72 20 74 68 |his is b|etter th|
|00000350| 61 6e 20 74 72 79 69 6e | 67 0a 74 6f 20 70 61 72 |an tryin|g.to par|
|00000360| 73 65 20 61 6e 64 20 6d | 6f 64 69 66 79 20 61 6e |se and m|odify an|
|00000370| 20 61 72 62 69 74 72 61 | 72 79 20 50 79 74 68 6f | arbitra|ry Pytho|
|00000380| 6e 20 63 6f 64 65 20 66 | 72 61 67 6d 65 6e 74 20 |n code f|ragment |
|00000390| 61 73 20 61 20 73 74 72 | 69 6e 67 0a 62 65 63 61 |as a str|ing.beca|
|000003a0| 75 73 65 20 70 61 72 73 | 69 6e 67 20 69 73 20 70 |use pars|ing is p|
|000003b0| 65 72 66 6f 72 6d 65 64 | 20 69 6e 20 61 20 6d 61 |erformed| in a ma|
|000003c0| 6e 6e 65 72 20 69 64 65 | 6e 74 69 63 61 6c 20 74 |nner ide|ntical t|
|000003d0| 6f 20 74 68 65 20 63 6f | 64 65 0a 66 6f 72 6d 69 |o the co|de.formi|
|000003e0| 6e 67 20 74 68 65 20 61 | 70 70 6c 69 63 61 74 69 |ng the a|pplicati|
|000003f0| 6f 6e 2e 20 20 49 74 20 | 69 73 20 61 6c 73 6f 20 |on. It |is also |
|00000400| 66 61 73 74 65 72 2e 0a | 0a 54 68 65 72 65 20 61 |faster..|.There a|
|00000410| 72 65 20 61 20 66 65 77 | 20 74 68 69 6e 67 73 20 |re a few| things |
|00000420| 74 6f 20 6e 6f 74 65 20 | 61 62 6f 75 74 20 74 68 |to note |about th|
|00000430| 69 73 20 6d 6f 64 75 6c | 65 20 77 68 69 63 68 20 |is modul|e which |
|00000440| 61 72 65 20 69 6d 70 6f | 72 74 61 6e 74 0a 74 6f |are impo|rtant.to|
|00000450| 20 6d 61 6b 69 6e 67 20 | 75 73 65 20 6f 66 20 74 | making |use of t|
|00000460| 68 65 20 64 61 74 61 20 | 73 74 72 75 63 74 75 72 |he data |structur|
|00000470| 65 73 20 63 72 65 61 74 | 65 64 2e 20 20 54 68 69 |es creat|ed. Thi|
|00000480| 73 20 69 73 20 6e 6f 74 | 20 61 20 74 75 74 6f 72 |s is not| a tutor|
|00000490| 69 61 6c 0a 6f 6e 20 65 | 64 69 74 69 6e 67 20 74 |ial.on e|diting t|
|000004a0| 68 65 20 70 61 72 73 65 | 20 74 72 65 65 73 20 66 |he parse| trees f|
|000004b0| 6f 72 20 50 79 74 68 6f | 6e 20 63 6f 64 65 2c 20 |or Pytho|n code, |
|000004c0| 62 75 74 20 73 6f 6d 65 | 20 65 78 61 6d 70 6c 65 |but some| example|
|000004d0| 73 20 6f 66 20 75 73 69 | 6e 67 0a 74 68 65 20 5c |s of usi|ng.the \|
|000004e0| 63 6f 64 65 7b 70 61 72 | 73 65 72 7d 20 6d 6f 64 |code{par|ser} mod|
|000004f0| 75 6c 65 20 61 72 65 20 | 70 72 65 73 65 6e 74 65 |ule are |presente|
|00000500| 64 2e 0a 0a 4d 6f 73 74 | 20 69 6d 70 6f 72 74 61 |d...Most| importa|
|00000510| 6e 74 6c 79 2c 20 61 20 | 67 6f 6f 64 20 75 6e 64 |ntly, a |good und|
|00000520| 65 72 73 74 61 6e 64 69 | 6e 67 20 6f 66 20 74 68 |erstandi|ng of th|
|00000530| 65 20 50 79 74 68 6f 6e | 20 67 72 61 6d 6d 61 72 |e Python| grammar|
|00000540| 20 70 72 6f 63 65 73 73 | 65 64 0a 62 79 20 74 68 | process|ed.by th|
|00000550| 65 20 69 6e 74 65 72 6e | 61 6c 20 70 61 72 73 65 |e intern|al parse|
|00000560| 72 20 69 73 20 72 65 71 | 75 69 72 65 64 2e 20 20 |r is req|uired. |
|00000570| 46 6f 72 20 66 75 6c 6c | 20 69 6e 66 6f 72 6d 61 |For full| informa|
|00000580| 74 69 6f 6e 20 6f 6e 20 | 74 68 65 0a 6c 61 6e 67 |tion on |the.lang|
|00000590| 75 61 67 65 20 73 79 6e | 74 61 78 2c 20 72 65 66 |uage syn|tax, ref|
|000005a0| 65 72 20 74 6f 20 74 68 | 65 20 4c 61 6e 67 75 61 |er to th|e Langua|
|000005b0| 67 65 20 52 65 66 65 72 | 65 6e 63 65 2e 20 20 54 |ge Refer|ence. T|
|000005c0| 68 65 20 70 61 72 73 65 | 72 20 69 74 73 65 6c 66 |he parse|r itself|
|000005d0| 0a 69 73 20 63 72 65 61 | 74 65 64 20 66 72 6f 6d |.is crea|ted from|
|000005e0| 20 61 20 67 72 61 6d 6d | 61 72 20 73 70 65 63 69 | a gramm|ar speci|
|000005f0| 66 69 63 61 74 69 6f 6e | 20 64 65 66 69 6e 65 64 |fication| defined|
|00000600| 20 69 6e 20 74 68 65 20 | 66 69 6c 65 0a 5c 66 69 | in the |file.\fi|
|00000610| 6c 65 7b 47 72 61 6d 6d | 61 72 2f 47 72 61 6d 6d |le{Gramm|ar/Gramm|
|00000620| 61 72 7d 20 69 6e 20 74 | 68 65 20 73 74 61 6e 64 |ar} in t|he stand|
|00000630| 61 72 64 20 50 79 74 68 | 6f 6e 20 64 69 73 74 72 |ard Pyth|on distr|
|00000640| 69 62 75 74 69 6f 6e 2e | 20 20 54 68 65 20 70 61 |ibution.| The pa|
|00000650| 72 73 65 0a 74 72 65 65 | 73 20 73 74 6f 72 65 64 |rse.tree|s stored|
|00000660| 20 69 6e 20 74 68 65 20 | 60 60 41 53 54 20 6f 62 | in the |``AST ob|
|00000670| 6a 65 63 74 73 27 27 20 | 63 72 65 61 74 65 64 20 |jects'' |created |
|00000680| 62 79 20 74 68 69 73 20 | 6d 6f 64 75 6c 65 20 61 |by this |module a|
|00000690| 72 65 20 74 68 65 0a 61 | 63 74 75 61 6c 20 6f 75 |re the.a|ctual ou|
|000006a0| 74 70 75 74 20 66 72 6f | 6d 20 74 68 65 20 69 6e |tput fro|m the in|
|000006b0| 74 65 72 6e 61 6c 20 70 | 61 72 73 65 72 20 77 68 |ternal p|arser wh|
|000006c0| 65 6e 20 63 72 65 61 74 | 65 64 20 62 79 20 74 68 |en creat|ed by th|
|000006d0| 65 0a 5c 63 6f 64 65 7b | 65 78 70 72 28 29 7d 20 |e.\code{|expr()} |
|000006e0| 6f 72 20 5c 63 6f 64 65 | 7b 73 75 69 74 65 28 29 |or \code|{suite()|
|000006f0| 7d 20 66 75 6e 63 74 69 | 6f 6e 73 2c 20 64 65 73 |} functi|ons, des|
|00000700| 63 72 69 62 65 64 20 62 | 65 6c 6f 77 2e 20 20 54 |cribed b|elow. T|
|00000710| 68 65 20 41 53 54 0a 6f | 62 6a 65 63 74 73 20 63 |he AST.o|bjects c|
|00000720| 72 65 61 74 65 64 20 62 | 79 20 5c 63 6f 64 65 7b |reated b|y \code{|
|00000730| 73 65 71 75 65 6e 63 65 | 32 61 73 74 28 29 7d 20 |sequence|2ast()} |
|00000740| 66 61 69 74 68 66 75 6c | 6c 79 20 73 69 6d 75 6c |faithful|ly simul|
|00000750| 61 74 65 20 74 68 6f 73 | 65 0a 73 74 72 75 63 74 |ate thos|e.struct|
|00000760| 75 72 65 73 2e 20 20 42 | 65 20 61 77 61 72 65 20 |ures. B|e aware |
|00000770| 74 68 61 74 20 74 68 65 | 20 76 61 6c 75 65 73 20 |that the| values |
|00000780| 6f 66 20 74 68 65 20 73 | 65 71 75 65 6e 63 65 73 |of the s|equences|
|00000790| 20 77 68 69 63 68 20 61 | 72 65 0a 63 6f 6e 73 69 | which a|re.consi|
|000007a0| 64 65 72 65 64 20 60 60 | 63 6f 72 72 65 63 74 27 |dered ``|correct'|
|000007b0| 27 20 77 69 6c 6c 20 76 | 61 72 79 20 66 72 6f 6d |' will v|ary from|
|000007c0| 20 6f 6e 65 20 76 65 72 | 73 69 6f 6e 20 6f 66 20 | one ver|sion of |
|000007d0| 50 79 74 68 6f 6e 20 74 | 6f 20 61 6e 6f 74 68 65 |Python t|o anothe|
|000007e0| 72 0a 61 73 20 74 68 65 | 20 66 6f 72 6d 61 6c 20 |r.as the| formal |
|000007f0| 67 72 61 6d 6d 61 72 20 | 66 6f 72 20 74 68 65 20 |grammar |for the |
|00000800| 6c 61 6e 67 75 61 67 65 | 20 69 73 20 72 65 76 69 |language| is revi|
|00000810| 73 65 64 2e 20 20 48 6f | 77 65 76 65 72 2c 0a 74 |sed. Ho|wever,.t|
|00000820| 72 61 6e 73 70 6f 72 74 | 69 6e 67 20 63 6f 64 65 |ransport|ing code|
|00000830| 20 66 72 6f 6d 20 6f 6e | 65 20 50 79 74 68 6f 6e | from on|e Python|
|00000840| 20 76 65 72 73 69 6f 6e | 20 74 6f 20 61 6e 6f 74 | version| to anot|
|00000850| 68 65 72 20 61 73 20 73 | 6f 75 72 63 65 20 74 65 |her as s|ource te|
|00000860| 78 74 0a 77 69 6c 6c 20 | 61 6c 77 61 79 73 20 61 |xt.will |always a|
|00000870| 6c 6c 6f 77 20 63 6f 72 | 72 65 63 74 20 70 61 72 |llow cor|rect par|
|00000880| 73 65 20 74 72 65 65 73 | 20 74 6f 20 62 65 20 63 |se trees| to be c|
|00000890| 72 65 61 74 65 64 20 69 | 6e 20 74 68 65 20 74 61 |reated i|n the ta|
|000008a0| 72 67 65 74 0a 76 65 72 | 73 69 6f 6e 2c 20 77 69 |rget.ver|sion, wi|
|000008b0| 74 68 20 74 68 65 20 6f | 6e 6c 79 20 72 65 73 74 |th the o|nly rest|
|000008c0| 72 69 63 74 69 6f 6e 20 | 62 65 69 6e 67 20 74 68 |riction |being th|
|000008d0| 61 74 20 6d 69 67 72 61 | 74 69 6e 67 20 74 6f 20 |at migra|ting to |
|000008e0| 61 6e 20 6f 6c 64 65 72 | 0a 76 65 72 73 69 6f 6e |an older|.version|
|000008f0| 20 6f 66 20 74 68 65 20 | 69 6e 74 65 72 70 72 65 | of the |interpre|
|00000900| 74 65 72 20 77 69 6c 6c | 20 6e 6f 74 20 73 75 70 |ter will| not sup|
|00000910| 70 6f 72 74 20 6d 6f 72 | 65 20 72 65 63 65 6e 74 |port mor|e recent|
|00000920| 20 6c 61 6e 67 75 61 67 | 65 0a 63 6f 6e 73 74 72 | languag|e.constr|
|00000930| 75 63 74 73 2e 20 20 54 | 68 65 20 70 61 72 73 65 |ucts. T|he parse|
|00000940| 20 74 72 65 65 73 20 61 | 72 65 20 6e 6f 74 20 74 | trees a|re not t|
|00000950| 79 70 69 63 61 6c 6c 79 | 20 63 6f 6d 70 61 74 69 |ypically| compati|
|00000960| 62 6c 65 20 66 72 6f 6d | 20 6f 6e 65 0a 76 65 72 |ble from| one.ver|
|00000970| 73 69 6f 6e 20 74 6f 20 | 61 6e 6f 74 68 65 72 2c |sion to |another,|
|00000980| 20 77 68 65 72 65 61 73 | 20 73 6f 75 72 63 65 20 | whereas| source |
|00000990| 63 6f 64 65 20 68 61 73 | 20 61 6c 77 61 79 73 20 |code has| always |
|000009a0| 62 65 65 6e 0a 66 6f 72 | 77 61 72 64 2d 63 6f 6d |been.for|ward-com|
|000009b0| 70 61 74 69 62 6c 65 2e | 0a 0a 45 61 63 68 20 65 |patible.|..Each e|
|000009c0| 6c 65 6d 65 6e 74 20 6f | 66 20 74 68 65 20 73 65 |lement o|f the se|
|000009d0| 71 75 65 6e 63 65 73 20 | 72 65 74 75 72 6e 65 64 |quences |returned|
|000009e0| 20 62 79 20 5c 63 6f 64 | 65 7b 61 73 74 32 6c 69 | by \cod|e{ast2li|
|000009f0| 73 74 7d 20 6f 72 0a 5c | 63 6f 64 65 7b 61 73 74 |st} or.\|code{ast|
|00000a00| 32 74 75 70 6c 65 28 29 | 7d 20 68 61 73 20 61 20 |2tuple()|} has a |
|00000a10| 73 69 6d 70 6c 65 20 66 | 6f 72 6d 2e 20 20 53 65 |simple f|orm. Se|
|00000a20| 71 75 65 6e 63 65 73 20 | 72 65 70 72 65 73 65 6e |quences |represen|
|00000a30| 74 69 6e 67 0a 6e 6f 6e | 2d 74 65 72 6d 69 6e 61 |ting.non|-termina|
|00000a40| 6c 20 65 6c 65 6d 65 6e | 74 73 20 69 6e 20 74 68 |l elemen|ts in th|
|00000a50| 65 20 67 72 61 6d 6d 61 | 72 20 61 6c 77 61 79 73 |e gramma|r always|
|00000a60| 20 68 61 76 65 20 61 20 | 6c 65 6e 67 74 68 20 67 | have a |length g|
|00000a70| 72 65 61 74 65 72 20 74 | 68 61 6e 0a 6f 6e 65 2e |reater t|han.one.|
|00000a80| 20 20 54 68 65 20 66 69 | 72 73 74 20 65 6c 65 6d | The fi|rst elem|
|00000a90| 65 6e 74 20 69 73 20 61 | 6e 20 69 6e 74 65 67 65 |ent is a|n intege|
|00000aa0| 72 20 77 68 69 63 68 20 | 69 64 65 6e 74 69 66 69 |r which |identifi|
|00000ab0| 65 73 20 61 20 70 72 6f | 64 75 63 74 69 6f 6e 20 |es a pro|duction |
|00000ac0| 69 6e 0a 74 68 65 20 67 | 72 61 6d 6d 61 72 2e 20 |in.the g|rammar. |
|00000ad0| 20 54 68 65 73 65 20 69 | 6e 74 65 67 65 72 73 20 | These i|ntegers |
|00000ae0| 61 72 65 20 67 69 76 65 | 6e 20 73 79 6d 62 6f 6c |are give|n symbol|
|00000af0| 69 63 20 6e 61 6d 65 73 | 20 69 6e 20 74 68 65 20 |ic names| in the |
|00000b00| 43 20 68 65 61 64 65 72 | 0a 66 69 6c 65 20 5c 66 |C header|.file \f|
|00000b10| 69 6c 65 7b 49 6e 63 6c | 75 64 65 2f 67 72 61 6d |ile{Incl|ude/gram|
|00000b20| 69 6e 69 74 2e 68 7d 20 | 61 6e 64 20 74 68 65 20 |init.h} |and the |
|00000b30| 50 79 74 68 6f 6e 20 6d | 6f 64 75 6c 65 0a 5c 66 |Python m|odule.\f|
|00000b40| 69 6c 65 7b 4c 69 62 2f | 73 79 6d 62 6f 6c 2e 70 |ile{Lib/|symbol.p|
|00000b50| 79 7d 2e 20 20 45 61 63 | 68 20 61 64 64 69 74 69 |y}. Eac|h additi|
|00000b60| 6f 6e 61 6c 20 65 6c 65 | 6d 65 6e 74 20 6f 66 20 |onal ele|ment of |
|00000b70| 74 68 65 20 73 65 71 75 | 65 6e 63 65 20 72 65 70 |the sequ|ence rep|
|00000b80| 72 65 73 65 6e 74 73 0a | 61 20 63 6f 6d 70 6f 6e |resents.|a compon|
|00000b90| 65 6e 74 20 6f 66 20 74 | 68 65 20 70 72 6f 64 75 |ent of t|he produ|
|00000ba0| 63 74 69 6f 6e 20 61 73 | 20 72 65 63 6f 67 6e 69 |ction as| recogni|
|00000bb0| 7a 65 64 20 69 6e 20 74 | 68 65 20 69 6e 70 75 74 |zed in t|he input|
|00000bc0| 20 73 74 72 69 6e 67 3a | 20 74 68 65 73 65 0a 61 | string:| these.a|
|00000bd0| 72 65 20 61 6c 77 61 79 | 73 20 73 65 71 75 65 6e |re alway|s sequen|
|00000be0| 63 65 73 20 77 68 69 63 | 68 20 68 61 76 65 20 74 |ces whic|h have t|
|00000bf0| 68 65 20 73 61 6d 65 20 | 66 6f 72 6d 20 61 73 20 |he same |form as |
|00000c00| 74 68 65 20 70 61 72 65 | 6e 74 2e 20 20 41 6e 0a |the pare|nt. An.|
|00000c10| 69 6d 70 6f 72 74 61 6e | 74 20 61 73 70 65 63 74 |importan|t aspect|
|00000c20| 20 6f 66 20 74 68 69 73 | 20 73 74 72 75 63 74 75 | of this| structu|
|00000c30| 72 65 20 77 68 69 63 68 | 20 73 68 6f 75 6c 64 20 |re which| should |
|00000c40| 62 65 20 6e 6f 74 65 64 | 20 69 73 20 74 68 61 74 |be noted| is that|
|00000c50| 0a 6b 65 79 77 6f 72 64 | 73 20 75 73 65 64 20 74 |.keyword|s used t|
|00000c60| 6f 20 69 64 65 6e 74 69 | 66 79 20 74 68 65 20 70 |o identi|fy the p|
|00000c70| 61 72 65 6e 74 20 6e 6f | 64 65 20 74 79 70 65 2c |arent no|de type,|
|00000c80| 20 73 75 63 68 20 61 73 | 20 74 68 65 20 6b 65 79 | such as| the key|
|00000c90| 77 6f 72 64 0a 5c 63 6f | 64 65 7b 69 66 7d 20 69 |word.\co|de{if} i|
|00000ca0| 6e 20 61 6e 20 5c 63 6f | 64 65 7b 69 66 5f 73 74 |n an \co|de{if_st|
|00000cb0| 6d 74 7d 2c 20 61 72 65 | 20 69 6e 63 6c 75 64 65 |mt}, are| include|
|00000cc0| 64 20 69 6e 20 74 68 65 | 20 6e 6f 64 65 20 74 72 |d in the| node tr|
|00000cd0| 65 65 20 77 69 74 68 6f | 75 74 0a 61 6e 79 20 73 |ee witho|ut.any s|
|00000ce0| 70 65 63 69 61 6c 20 74 | 72 65 61 74 6d 65 6e 74 |pecial t|reatment|
|00000cf0| 2e 20 20 46 6f 72 20 65 | 78 61 6d 70 6c 65 2c 20 |. For e|xample, |
|00000d00| 74 68 65 20 5c 63 6f 64 | 65 7b 69 66 7d 20 6b 65 |the \cod|e{if} ke|
|00000d10| 79 77 6f 72 64 20 69 73 | 0a 72 65 70 72 65 73 65 |yword is|.represe|
|00000d20| 6e 74 65 64 20 62 79 20 | 74 68 65 20 74 75 70 6c |nted by |the tupl|
|00000d30| 65 20 5c 63 6f 64 65 7b | 28 31 2c 20 27 69 66 27 |e \code{|(1, 'if'|
|00000d40| 29 7d 2c 20 77 68 65 72 | 65 20 5c 63 6f 64 65 7b |)}, wher|e \code{|
|00000d50| 31 7d 20 69 73 20 74 68 | 65 0a 6e 75 6d 65 72 69 |1} is th|e.numeri|
|00000d60| 63 20 76 61 6c 75 65 20 | 61 73 73 6f 63 69 61 74 |c value |associat|
|00000d70| 65 64 20 77 69 74 68 20 | 61 6c 6c 20 5c 63 6f 64 |ed with |all \cod|
|00000d80| 65 7b 4e 41 4d 45 7d 20 | 74 6f 6b 65 6e 73 2c 20 |e{NAME} |tokens, |
|00000d90| 69 6e 63 6c 75 64 69 6e | 67 0a 76 61 72 69 61 62 |includin|g.variab|
|00000da0| 6c 65 20 61 6e 64 20 66 | 75 6e 63 74 69 6f 6e 20 |le and f|unction |
|00000db0| 6e 61 6d 65 73 20 64 65 | 66 69 6e 65 64 20 62 79 |names de|fined by|
|00000dc0| 20 74 68 65 20 75 73 65 | 72 2e 20 20 49 6e 20 61 | the use|r. In a|
|00000dd0| 6e 20 61 6c 74 65 72 6e | 61 74 65 20 66 6f 72 6d |n altern|ate form|
|00000de0| 0a 72 65 74 75 72 6e 65 | 64 20 77 68 65 6e 20 6c |.returne|d when l|
|00000df0| 69 6e 65 20 6e 75 6d 62 | 65 72 20 69 6e 66 6f 72 |ine numb|er infor|
|00000e00| 6d 61 74 69 6f 6e 20 69 | 73 20 72 65 71 75 65 73 |mation i|s reques|
|00000e10| 74 65 64 2c 20 74 68 65 | 20 73 61 6d 65 20 74 6f |ted, the| same to|
|00000e20| 6b 65 6e 0a 6d 69 67 68 | 74 20 62 65 20 72 65 70 |ken.migh|t be rep|
|00000e30| 72 65 73 65 6e 74 65 64 | 20 61 73 20 5c 63 6f 64 |resented| as \cod|
|00000e40| 65 7b 28 31 2c 20 27 69 | 66 27 2c 20 31 32 29 7d |e{(1, 'i|f', 12)}|
|00000e50| 2c 20 77 68 65 72 65 20 | 74 68 65 20 5c 63 6f 64 |, where |the \cod|
|00000e60| 65 7b 31 32 7d 0a 72 65 | 70 72 65 73 65 6e 74 73 |e{12}.re|presents|
|00000e70| 20 74 68 65 20 6c 69 6e | 65 20 6e 75 6d 62 65 72 | the lin|e number|
|00000e80| 20 61 74 20 77 68 69 63 | 68 20 74 68 65 20 74 65 | at whic|h the te|
|00000e90| 72 6d 69 6e 61 6c 20 73 | 79 6d 62 6f 6c 20 77 61 |rminal s|ymbol wa|
|00000ea0| 73 20 66 6f 75 6e 64 2e | 0a 0a 54 65 72 6d 69 6e |s found.|..Termin|
|00000eb0| 61 6c 20 65 6c 65 6d 65 | 6e 74 73 20 61 72 65 20 |al eleme|nts are |
|00000ec0| 72 65 70 72 65 73 65 6e | 74 65 64 20 69 6e 20 6d |represen|ted in m|
|00000ed0| 75 63 68 20 74 68 65 20 | 73 61 6d 65 20 77 61 79 |uch the |same way|
|00000ee0| 2c 20 62 75 74 20 77 69 | 74 68 6f 75 74 0a 61 6e |, but wi|thout.an|
|00000ef0| 79 20 63 68 69 6c 64 20 | 65 6c 65 6d 65 6e 74 73 |y child |elements|
|00000f00| 20 61 6e 64 20 74 68 65 | 20 61 64 64 69 74 69 6f | and the| additio|
|00000f10| 6e 20 6f 66 20 74 68 65 | 20 73 6f 75 72 63 65 20 |n of the| source |
|00000f20| 74 65 78 74 20 77 68 69 | 63 68 20 77 61 73 0a 69 |text whi|ch was.i|
|00000f30| 64 65 6e 74 69 66 69 65 | 64 2e 20 20 54 68 65 20 |dentifie|d. The |
|00000f40| 65 78 61 6d 70 6c 65 20 | 6f 66 20 74 68 65 20 5c |example |of the \|
|00000f50| 63 6f 64 65 7b 69 66 7d | 20 6b 65 79 77 6f 72 64 |code{if}| keyword|
|00000f60| 20 61 62 6f 76 65 20 69 | 73 0a 72 65 70 72 65 73 | above i|s.repres|
|00000f70| 65 6e 74 61 74 69 76 65 | 2e 20 20 54 68 65 20 76 |entative|. The v|
|00000f80| 61 72 69 6f 75 73 20 74 | 79 70 65 73 20 6f 66 20 |arious t|ypes of |
|00000f90| 74 65 72 6d 69 6e 61 6c | 20 73 79 6d 62 6f 6c 73 |terminal| symbols|
|00000fa0| 20 61 72 65 20 64 65 66 | 69 6e 65 64 20 69 6e 0a | are def|ined in.|
|00000fb0| 74 68 65 20 43 20 68 65 | 61 64 65 72 20 66 69 6c |the C he|ader fil|
|00000fc0| 65 20 5c 66 69 6c 65 7b | 49 6e 63 6c 75 64 65 2f |e \file{|Include/|
|00000fd0| 74 6f 6b 65 6e 2e 68 7d | 20 61 6e 64 20 74 68 65 |token.h}| and the|
|00000fe0| 20 50 79 74 68 6f 6e 20 | 6d 6f 64 75 6c 65 0a 5c | Python |module.\|
|00000ff0| 66 69 6c 65 7b 4c 69 62 | 2f 74 6f 6b 65 6e 2e 70 |file{Lib|/token.p|
|00001000| 79 7d 2e 0a 0a 54 68 65 | 20 41 53 54 20 6f 62 6a |y}...The| AST obj|
|00001010| 65 63 74 73 20 61 72 65 | 20 6e 6f 74 20 72 65 71 |ects are| not req|
|00001020| 75 69 72 65 64 20 74 6f | 20 73 75 70 70 6f 72 74 |uired to| support|
|00001030| 20 74 68 65 20 66 75 6e | 63 74 69 6f 6e 61 6c 69 | the fun|ctionali|
|00001040| 74 79 20 6f 66 20 74 68 | 69 73 0a 6d 6f 64 75 6c |ty of th|is.modul|
|00001050| 65 2c 20 62 75 74 20 61 | 72 65 20 70 72 6f 76 69 |e, but a|re provi|
|00001060| 64 65 64 20 66 6f 72 20 | 74 68 72 65 65 20 70 75 |ded for |three pu|
|00001070| 72 70 6f 73 65 73 3a 20 | 74 6f 20 61 6c 6c 6f 77 |rposes: |to allow|
|00001080| 20 61 6e 20 61 70 70 6c | 69 63 61 74 69 6f 6e 0a | an appl|ication.|
|00001090| 74 6f 20 61 6d 6f 72 74 | 69 7a 65 20 74 68 65 20 |to amort|ize the |
|000010a0| 63 6f 73 74 20 6f 66 20 | 70 72 6f 63 65 73 73 69 |cost of |processi|
|000010b0| 6e 67 20 63 6f 6d 70 6c | 65 78 20 70 61 72 73 65 |ng compl|ex parse|
|000010c0| 20 74 72 65 65 73 2c 20 | 74 6f 20 70 72 6f 76 69 | trees, |to provi|
|000010d0| 64 65 20 61 0a 70 61 72 | 73 65 20 74 72 65 65 20 |de a.par|se tree |
|000010e0| 72 65 70 72 65 73 65 6e | 74 61 74 69 6f 6e 20 77 |represen|tation w|
|000010f0| 68 69 63 68 20 63 6f 6e | 73 65 72 76 65 73 20 6d |hich con|serves m|
|00001100| 65 6d 6f 72 79 20 73 70 | 61 63 65 20 77 68 65 6e |emory sp|ace when|
|00001110| 20 63 6f 6d 70 61 72 65 | 64 0a 74 6f 20 74 68 65 | compare|d.to the|
|00001120| 20 50 79 74 68 6f 6e 20 | 6c 69 73 74 20 6f 72 20 | Python |list or |
|00001130| 74 75 70 6c 65 20 72 65 | 70 72 65 73 65 6e 74 61 |tuple re|presenta|
|00001140| 74 69 6f 6e 2c 20 61 6e | 64 20 74 6f 20 65 61 73 |tion, an|d to eas|
|00001150| 65 20 74 68 65 20 63 72 | 65 61 74 69 6f 6e 0a 6f |e the cr|eation.o|
|00001160| 66 20 61 64 64 69 74 69 | 6f 6e 61 6c 20 6d 6f 64 |f additi|onal mod|
|00001170| 75 6c 65 73 20 69 6e 20 | 43 20 77 68 69 63 68 20 |ules in |C which |
|00001180| 6d 61 6e 69 70 75 6c 61 | 74 65 20 70 61 72 73 65 |manipula|te parse|
|00001190| 20 74 72 65 65 73 2e 20 | 20 41 20 73 69 6d 70 6c | trees. | A simpl|
|000011a0| 65 0a 60 60 77 72 61 70 | 70 65 72 27 27 20 63 6c |e.``wrap|per'' cl|
|000011b0| 61 73 73 20 6d 61 79 20 | 62 65 20 63 72 65 61 74 |ass may |be creat|
|000011c0| 65 64 20 69 6e 20 50 79 | 74 68 6f 6e 20 74 6f 20 |ed in Py|thon to |
|000011d0| 68 69 64 65 20 74 68 65 | 20 75 73 65 20 6f 66 20 |hide the| use of |
|000011e0| 41 53 54 0a 6f 62 6a 65 | 63 74 73 3b 20 74 68 65 |AST.obje|cts; the|
|000011f0| 20 5c 63 6f 64 65 7b 41 | 53 54 7d 20 6c 69 62 72 | \code{A|ST} libr|
|00001200| 61 72 79 20 6d 6f 64 75 | 6c 65 20 70 72 6f 76 69 |ary modu|le provi|
|00001210| 64 65 73 20 61 20 76 61 | 72 69 65 74 79 20 6f 66 |des a va|riety of|
|00001220| 20 73 75 63 68 0a 63 6c | 61 73 73 65 73 2e 0a 0a | such.cl|asses...|
|00001230| 54 68 65 20 5c 63 6f 64 | 65 7b 70 61 72 73 65 72 |The \cod|e{parser|
|00001240| 7d 20 6d 6f 64 75 6c 65 | 20 64 65 66 69 6e 65 73 |} module| defines|
|00001250| 20 66 75 6e 63 74 69 6f | 6e 73 20 66 6f 72 20 61 | functio|ns for a|
|00001260| 20 66 65 77 20 64 69 73 | 74 69 6e 63 74 0a 70 75 | few dis|tinct.pu|
|00001270| 72 70 6f 73 65 73 2e 20 | 20 54 68 65 20 6d 6f 73 |rposes. | The mos|
|00001280| 74 20 69 6d 70 6f 72 74 | 61 6e 74 20 70 75 72 70 |t import|ant purp|
|00001290| 6f 73 65 73 20 61 72 65 | 20 74 6f 20 63 72 65 61 |oses are| to crea|
|000012a0| 74 65 20 41 53 54 20 6f | 62 6a 65 63 74 73 20 61 |te AST o|bjects a|
|000012b0| 6e 64 0a 74 6f 20 63 6f | 6e 76 65 72 74 20 41 53 |nd.to co|nvert AS|
|000012c0| 54 20 6f 62 6a 65 63 74 | 73 20 74 6f 20 6f 74 68 |T object|s to oth|
|000012d0| 65 72 20 72 65 70 72 65 | 73 65 6e 74 61 74 69 6f |er repre|sentatio|
|000012e0| 6e 73 20 73 75 63 68 20 | 61 73 20 70 61 72 73 65 |ns such |as parse|
|000012f0| 20 74 72 65 65 73 0a 61 | 6e 64 20 63 6f 6d 70 69 | trees.a|nd compi|
|00001300| 6c 65 64 20 63 6f 64 65 | 20 6f 62 6a 65 63 74 73 |led code| objects|
|00001310| 2c 20 62 75 74 20 74 68 | 65 72 65 20 61 72 65 20 |, but th|ere are |
|00001320| 61 6c 73 6f 20 66 75 6e | 63 74 69 6f 6e 73 20 77 |also fun|ctions w|
|00001330| 68 69 63 68 20 73 65 72 | 76 65 20 74 6f 0a 71 75 |hich ser|ve to.qu|
|00001340| 65 72 79 20 74 68 65 20 | 74 79 70 65 20 6f 66 20 |ery the |type of |
|00001350| 70 61 72 73 65 20 74 72 | 65 65 20 72 65 70 72 65 |parse tr|ee repre|
|00001360| 73 65 6e 74 65 64 20 62 | 79 20 61 6e 20 41 53 54 |sented b|y an AST|
|00001370| 20 6f 62 6a 65 63 74 2e | 0a 0a 5c 72 65 6e 65 77 | object.|..\renew|
|00001380| 63 6f 6d 6d 61 6e 64 7b | 5c 69 6e 64 65 78 73 75 |command{|\indexsu|
|00001390| 62 69 74 65 6d 7d 7b 28 | 69 6e 20 6d 6f 64 75 6c |bitem}{(|in modul|
|000013a0| 65 20 70 61 72 73 65 72 | 29 7d 0a 0a 0a 5c 73 75 |e parser|)}...\su|
|000013b0| 62 73 65 63 74 69 6f 6e | 7b 43 72 65 61 74 69 6e |bsection|{Creatin|
|000013c0| 67 20 41 53 54 20 4f 62 | 6a 65 63 74 73 7d 0a 0a |g AST Ob|jects}..|
|000013d0| 41 53 54 20 6f 62 6a 65 | 63 74 73 20 6d 61 79 20 |AST obje|cts may |
|000013e0| 62 65 20 63 72 65 61 74 | 65 64 20 66 72 6f 6d 20 |be creat|ed from |
|000013f0| 73 6f 75 72 63 65 20 63 | 6f 64 65 20 6f 72 20 66 |source c|ode or f|
|00001400| 72 6f 6d 20 61 20 70 61 | 72 73 65 20 74 72 65 65 |rom a pa|rse tree|
|00001410| 2e 0a 57 68 65 6e 20 63 | 72 65 61 74 69 6e 67 20 |..When c|reating |
|00001420| 61 6e 20 41 53 54 20 6f | 62 6a 65 63 74 20 66 72 |an AST o|bject fr|
|00001430| 6f 6d 20 73 6f 75 72 63 | 65 2c 20 64 69 66 66 65 |om sourc|e, diffe|
|00001440| 72 65 6e 74 20 66 75 6e | 63 74 69 6f 6e 73 20 61 |rent fun|ctions a|
|00001450| 72 65 20 75 73 65 64 0a | 74 6f 20 63 72 65 61 74 |re used.|to creat|
|00001460| 65 20 74 68 65 20 5c 63 | 6f 64 65 7b 27 65 76 61 |e the \c|ode{'eva|
|00001470| 6c 27 7d 20 61 6e 64 20 | 5c 63 6f 64 65 7b 27 65 |l'} and |\code{'e|
|00001480| 78 65 63 27 7d 20 66 6f | 72 6d 73 2e 0a 0a 5c 62 |xec'} fo|rms...\b|
|00001490| 65 67 69 6e 7b 66 75 6e | 63 64 65 73 63 7d 7b 65 |egin{fun|cdesc}{e|
|000014a0| 78 70 72 7d 7b 73 74 72 | 69 6e 67 7d 0a 54 68 65 |xpr}{str|ing}.The|
|000014b0| 20 5c 63 6f 64 65 7b 65 | 78 70 72 28 29 7d 20 66 | \code{e|xpr()} f|
|000014c0| 75 6e 63 74 69 6f 6e 20 | 70 61 72 73 65 73 20 74 |unction |parses t|
|000014d0| 68 65 20 70 61 72 61 6d | 65 74 65 72 20 5c 63 6f |he param|eter \co|
|000014e0| 64 65 7b 5c 76 61 72 7b | 73 74 72 69 6e 67 7d 7d |de{\var{|string}}|
|000014f0| 0a 61 73 20 69 66 20 69 | 74 20 77 65 72 65 20 61 |.as if i|t were a|
|00001500| 6e 20 69 6e 70 75 74 20 | 74 6f 20 5c 63 6f 64 65 |n input |to \code|
|00001510| 7b 63 6f 6d 70 69 6c 65 | 28 5c 76 61 72 7b 73 74 |{compile|(\var{st|
|00001520| 72 69 6e 67 7d 2c 20 27 | 65 76 61 6c 27 29 7d 2e |ring}, '|eval')}.|
|00001530| 20 20 49 66 0a 74 68 65 | 20 70 61 72 73 65 20 73 | If.the| parse s|
|00001540| 75 63 63 65 65 64 73 2c | 20 61 6e 20 41 53 54 20 |ucceeds,| an AST |
|00001550| 6f 62 6a 65 63 74 20 69 | 73 20 63 72 65 61 74 65 |object i|s create|
|00001560| 64 20 74 6f 20 68 6f 6c | 64 20 74 68 65 20 69 6e |d to hol|d the in|
|00001570| 74 65 72 6e 61 6c 0a 70 | 61 72 73 65 20 74 72 65 |ternal.p|arse tre|
|00001580| 65 20 72 65 70 72 65 73 | 65 6e 74 61 74 69 6f 6e |e repres|entation|
|00001590| 2c 20 6f 74 68 65 72 77 | 69 73 65 20 61 6e 20 61 |, otherw|ise an a|
|000015a0| 70 70 72 6f 70 72 69 61 | 74 65 20 65 78 63 65 70 |ppropria|te excep|
|000015b0| 74 69 6f 6e 20 69 73 0a | 74 68 72 6f 77 6e 2e 0a |tion is.|thrown..|
|000015c0| 5c 65 6e 64 7b 66 75 6e | 63 64 65 73 63 7d 0a 0a |\end{fun|cdesc}..|
|000015d0| 5c 62 65 67 69 6e 7b 66 | 75 6e 63 64 65 73 63 7d |\begin{f|uncdesc}|
|000015e0| 7b 73 75 69 74 65 7d 7b | 73 74 72 69 6e 67 7d 0a |{suite}{|string}.|
|000015f0| 54 68 65 20 5c 63 6f 64 | 65 7b 73 75 69 74 65 28 |The \cod|e{suite(|
|00001600| 29 7d 20 66 75 6e 63 74 | 69 6f 6e 20 70 61 72 73 |)} funct|ion pars|
|00001610| 65 73 20 74 68 65 20 70 | 61 72 61 6d 65 74 65 72 |es the p|arameter|
|00001620| 20 5c 63 6f 64 65 7b 5c | 76 61 72 7b 73 74 72 69 | \code{\|var{stri|
|00001630| 6e 67 7d 7d 0a 61 73 20 | 69 66 20 69 74 20 77 65 |ng}}.as |if it we|
|00001640| 72 65 20 61 6e 20 69 6e | 70 75 74 20 74 6f 20 5c |re an in|put to \|
|00001650| 63 6f 64 65 7b 63 6f 6d | 70 69 6c 65 28 5c 76 61 |code{com|pile(\va|
|00001660| 72 7b 73 74 72 69 6e 67 | 7d 2c 20 27 65 78 65 63 |r{string|}, 'exec|
|00001670| 27 29 7d 2e 20 20 49 66 | 0a 74 68 65 20 70 61 72 |')}. If|.the par|
|00001680| 73 65 20 73 75 63 63 65 | 65 64 73 2c 20 61 6e 20 |se succe|eds, an |
|00001690| 41 53 54 20 6f 62 6a 65 | 63 74 20 69 73 20 63 72 |AST obje|ct is cr|
|000016a0| 65 61 74 65 64 20 74 6f | 20 68 6f 6c 64 20 74 68 |eated to| hold th|
|000016b0| 65 20 69 6e 74 65 72 6e | 61 6c 0a 70 61 72 73 65 |e intern|al.parse|
|000016c0| 20 74 72 65 65 20 72 65 | 70 72 65 73 65 6e 74 61 | tree re|presenta|
|000016d0| 74 69 6f 6e 2c 20 6f 74 | 68 65 72 77 69 73 65 20 |tion, ot|herwise |
|000016e0| 61 6e 20 61 70 70 72 6f | 70 72 69 61 74 65 20 65 |an appro|priate e|
|000016f0| 78 63 65 70 74 69 6f 6e | 20 69 73 0a 74 68 72 6f |xception| is.thro|
|00001700| 77 6e 2e 0a 5c 65 6e 64 | 7b 66 75 6e 63 64 65 73 |wn..\end|{funcdes|
|00001710| 63 7d 0a 0a 5c 62 65 67 | 69 6e 7b 66 75 6e 63 64 |c}..\beg|in{funcd|
|00001720| 65 73 63 7d 7b 73 65 71 | 75 65 6e 63 65 32 61 73 |esc}{seq|uence2as|
|00001730| 74 7d 7b 73 65 71 75 65 | 6e 63 65 7d 0a 54 68 69 |t}{seque|nce}.Thi|
|00001740| 73 20 66 75 6e 63 74 69 | 6f 6e 20 61 63 63 65 70 |s functi|on accep|
|00001750| 74 73 20 61 20 70 61 72 | 73 65 20 74 72 65 65 20 |ts a par|se tree |
|00001760| 72 65 70 72 65 73 65 6e | 74 65 64 20 61 73 20 61 |represen|ted as a|
|00001770| 20 73 65 71 75 65 6e 63 | 65 20 61 6e 64 0a 62 75 | sequenc|e and.bu|
|00001780| 69 6c 64 73 20 61 6e 20 | 69 6e 74 65 72 6e 61 6c |ilds an |internal|
|00001790| 20 72 65 70 72 65 73 65 | 6e 74 61 74 69 6f 6e 20 | represe|ntation |
|000017a0| 69 66 20 70 6f 73 73 69 | 62 6c 65 2e 20 20 49 66 |if possi|ble. If|
|000017b0| 20 69 74 20 63 61 6e 20 | 76 61 6c 69 64 61 74 65 | it can |validate|
|000017c0| 0a 74 68 61 74 20 74 68 | 65 20 74 72 65 65 20 63 |.that th|e tree c|
|000017d0| 6f 6e 66 6f 72 6d 73 20 | 74 6f 20 74 68 65 20 50 |onforms |to the P|
|000017e0| 79 74 68 6f 6e 20 67 72 | 61 6d 6d 61 72 20 61 6e |ython gr|ammar an|
|000017f0| 64 20 61 6c 6c 20 6e 6f | 64 65 73 20 61 72 65 20 |d all no|des are |
|00001800| 76 61 6c 69 64 0a 6e 6f | 64 65 20 74 79 70 65 73 |valid.no|de types|
|00001810| 20 69 6e 20 74 68 65 20 | 68 6f 73 74 20 76 65 72 | in the |host ver|
|00001820| 73 69 6f 6e 20 6f 66 20 | 50 79 74 68 6f 6e 2c 20 |sion of |Python, |
|00001830| 61 6e 20 41 53 54 20 6f | 62 6a 65 63 74 20 69 73 |an AST o|bject is|
|00001840| 20 63 72 65 61 74 65 64 | 0a 66 72 6f 6d 20 74 68 | created|.from th|
|00001850| 65 20 69 6e 74 65 72 6e | 61 6c 20 72 65 70 72 65 |e intern|al repre|
|00001860| 73 65 6e 74 61 74 69 6f | 6e 20 61 6e 64 20 72 65 |sentatio|n and re|
|00001870| 74 75 72 6e 65 64 20 74 | 6f 20 74 68 65 20 63 61 |turned t|o the ca|
|00001880| 6c 6c 65 64 2e 20 20 49 | 66 20 74 68 65 72 65 0a |lled. I|f there.|
|00001890| 69 73 20 61 20 70 72 6f | 62 6c 65 6d 20 63 72 65 |is a pro|blem cre|
|000018a0| 61 74 69 6e 67 20 74 68 | 65 20 69 6e 74 65 72 6e |ating th|e intern|
|000018b0| 61 6c 20 72 65 70 72 65 | 73 65 6e 74 61 74 69 6f |al repre|sentatio|
|000018c0| 6e 2c 20 6f 72 20 69 66 | 20 74 68 65 20 74 72 65 |n, or if| the tre|
|000018d0| 65 0a 63 61 6e 6e 6f 74 | 20 62 65 20 76 61 6c 69 |e.cannot| be vali|
|000018e0| 64 61 74 65 64 2c 20 61 | 20 5c 63 6f 64 65 7b 50 |dated, a| \code{P|
|000018f0| 61 72 73 65 72 45 72 72 | 6f 72 7d 20 65 78 63 65 |arserErr|or} exce|
|00001900| 70 74 69 6f 6e 20 69 73 | 20 74 68 72 6f 77 6e 2e |ption is| thrown.|
|00001910| 20 20 41 6e 20 41 53 54 | 0a 6f 62 6a 65 63 74 20 | An AST|.object |
|00001920| 63 72 65 61 74 65 64 20 | 74 68 69 73 20 77 61 79 |created |this way|
|00001930| 20 73 68 6f 75 6c 64 20 | 6e 6f 74 20 62 65 20 61 | should |not be a|
|00001940| 73 73 75 6d 65 64 20 74 | 6f 20 63 6f 6d 70 69 6c |ssumed t|o compil|
|00001950| 65 20 63 6f 72 72 65 63 | 74 6c 79 3b 0a 6e 6f 72 |e correc|tly;.nor|
|00001960| 6d 61 6c 20 65 78 63 65 | 70 74 69 6f 6e 73 20 74 |mal exce|ptions t|
|00001970| 68 72 6f 77 6e 20 62 79 | 20 63 6f 6d 70 69 6c 61 |hrown by| compila|
|00001980| 74 69 6f 6e 20 6d 61 79 | 20 73 74 69 6c 6c 20 62 |tion may| still b|
|00001990| 65 20 69 6e 69 74 69 61 | 74 65 64 20 77 68 65 6e |e initia|ted when|
|000019a0| 0a 74 68 65 20 41 53 54 | 20 6f 62 6a 65 63 74 20 |.the AST| object |
|000019b0| 69 73 20 70 61 73 73 65 | 64 20 74 6f 20 5c 63 6f |is passe|d to \co|
|000019c0| 64 65 7b 63 6f 6d 70 69 | 6c 65 61 73 74 28 29 7d |de{compi|least()}|
|000019d0| 2e 20 20 54 68 69 73 20 | 6d 61 79 20 69 6e 64 69 |. This |may indi|
|000019e0| 63 61 74 65 0a 70 72 6f | 62 6c 65 6d 73 20 6e 6f |cate.pro|blems no|
|000019f0| 74 20 72 65 6c 61 74 65 | 64 20 74 6f 20 73 79 6e |t relate|d to syn|
|00001a00| 74 61 78 20 28 73 75 63 | 68 20 61 73 20 61 20 5c |tax (suc|h as a \|
|00001a10| 63 6f 64 65 7b 4d 65 6d | 6f 72 79 45 72 72 6f 72 |code{Mem|oryError|
|00001a20| 7d 0a 65 78 63 65 70 74 | 69 6f 6e 29 2c 20 62 75 |}.except|ion), bu|
|00001a30| 74 20 6d 61 79 20 61 6c | 73 6f 20 62 65 20 64 75 |t may al|so be du|
|00001a40| 65 20 74 6f 20 63 6f 6e | 73 74 72 75 63 74 73 20 |e to con|structs |
|00001a50| 73 75 63 68 20 61 73 20 | 74 68 65 20 72 65 73 75 |such as |the resu|
|00001a60| 6c 74 20 6f 66 0a 70 61 | 72 73 69 6e 67 20 5c 63 |lt of.pa|rsing \c|
|00001a70| 6f 64 65 7b 64 65 6c 20 | 66 28 30 29 7d 2c 20 77 |ode{del |f(0)}, w|
|00001a80| 68 69 63 68 20 65 73 63 | 61 70 65 73 20 74 68 65 |hich esc|apes the|
|00001a90| 20 50 79 74 68 6f 6e 20 | 70 61 72 73 65 72 20 62 | Python |parser b|
|00001aa0| 75 74 20 69 73 0a 63 68 | 65 63 6b 65 64 20 62 79 |ut is.ch|ecked by|
|00001ab0| 20 74 68 65 20 62 79 74 | 65 63 6f 64 65 20 63 6f | the byt|ecode co|
|00001ac0| 6d 70 69 6c 65 72 2e 0a | 0a 53 65 71 75 65 6e 63 |mpiler..|.Sequenc|
|00001ad0| 65 73 20 72 65 70 72 65 | 73 65 6e 74 69 6e 67 20 |es repre|senting |
|00001ae0| 74 65 72 6d 69 6e 61 6c | 20 74 6f 6b 65 6e 73 20 |terminal| tokens |
|00001af0| 6d 61 79 20 62 65 20 72 | 65 70 72 65 73 65 6e 74 |may be r|epresent|
|00001b00| 65 64 20 61 73 20 65 69 | 74 68 65 72 0a 74 77 6f |ed as ei|ther.two|
|00001b10| 2d 65 6c 65 6d 65 6e 74 | 20 6c 69 73 74 73 20 6f |-element| lists o|
|00001b20| 66 20 74 68 65 20 66 6f | 72 6d 20 5c 63 6f 64 65 |f the fo|rm \code|
|00001b30| 7b 28 31 2c 20 27 6e 61 | 6d 65 27 29 7d 20 6f 72 |{(1, 'na|me')} or|
|00001b40| 20 61 73 20 74 68 72 65 | 65 2d 65 6c 65 6d 65 6e | as thre|e-elemen|
|00001b50| 74 0a 6c 69 73 74 73 20 | 6f 66 20 74 68 65 20 66 |t.lists |of the f|
|00001b60| 6f 72 6d 20 5c 63 6f 64 | 65 7b 28 31 2c 20 27 6e |orm \cod|e{(1, 'n|
|00001b70| 61 6d 65 27 2c 20 35 36 | 29 7d 2e 20 20 49 66 20 |ame', 56|)}. If |
|00001b80| 74 68 65 20 74 68 69 72 | 64 20 65 6c 65 6d 65 6e |the thir|d elemen|
|00001b90| 74 20 69 73 0a 70 72 65 | 73 65 6e 74 2c 20 69 74 |t is.pre|sent, it|
|00001ba0| 20 69 73 20 61 73 73 75 | 6d 65 64 20 74 6f 20 62 | is assu|med to b|
|00001bb0| 65 20 61 20 76 61 6c 69 | 64 20 6c 69 6e 65 20 6e |e a vali|d line n|
|00001bc0| 75 6d 62 65 72 2e 20 20 | 54 68 65 20 6c 69 6e 65 |umber. |The line|
|00001bd0| 20 6e 75 6d 62 65 72 0a | 6d 61 79 20 62 65 20 73 | number.|may be s|
|00001be0| 70 65 63 69 66 69 65 64 | 20 66 6f 72 20 61 6e 79 |pecified| for any|
|00001bf0| 20 73 75 62 73 65 74 20 | 6f 66 20 74 68 65 20 74 | subset |of the t|
|00001c00| 65 72 6d 69 6e 61 6c 20 | 73 79 6d 62 6f 6c 73 20 |erminal |symbols |
|00001c10| 69 6e 20 74 68 65 20 69 | 6e 70 75 74 0a 74 72 65 |in the i|nput.tre|
|00001c20| 65 2e 0a 5c 65 6e 64 7b | 66 75 6e 63 64 65 73 63 |e..\end{|funcdesc|
|00001c30| 7d 0a 0a 5c 62 65 67 69 | 6e 7b 66 75 6e 63 64 65 |}..\begi|n{funcde|
|00001c40| 73 63 7d 7b 74 75 70 6c | 65 32 61 73 74 7d 7b 73 |sc}{tupl|e2ast}{s|
|00001c50| 65 71 75 65 6e 63 65 7d | 0a 54 68 69 73 20 69 73 |equence}|.This is|
|00001c60| 20 74 68 65 20 73 61 6d | 65 20 66 75 6e 63 74 69 | the sam|e functi|
|00001c70| 6f 6e 20 61 73 20 5c 63 | 6f 64 65 7b 73 65 71 75 |on as \c|ode{sequ|
|00001c80| 65 6e 63 65 32 61 73 74 | 28 29 7d 2e 20 20 54 68 |ence2ast|()}. Th|
|00001c90| 69 73 20 65 6e 74 72 79 | 20 70 6f 69 6e 74 0a 69 |is entry| point.i|
|00001ca0| 73 20 6d 61 69 6e 74 61 | 69 6e 65 64 20 66 6f 72 |s mainta|ined for|
|00001cb0| 20 62 61 63 6b 77 61 72 | 64 20 63 6f 6d 70 61 74 | backwar|d compat|
|00001cc0| 69 62 69 6c 69 74 79 2e | 0a 5c 65 6e 64 7b 66 75 |ibility.|.\end{fu|
|00001cd0| 6e 63 64 65 73 63 7d 0a | 0a 0a 5c 73 75 62 73 65 |ncdesc}.|..\subse|
|00001ce0| 63 74 69 6f 6e 7b 43 6f | 6e 76 65 72 74 69 6e 67 |ction{Co|nverting|
|00001cf0| 20 41 53 54 20 4f 62 6a | 65 63 74 73 7d 0a 0a 41 | AST Obj|ects}..A|
|00001d00| 53 54 20 6f 62 6a 65 63 | 74 73 2c 20 72 65 67 61 |ST objec|ts, rega|
|00001d10| 72 64 6c 65 73 73 20 6f | 66 20 74 68 65 20 69 6e |rdless o|f the in|
|00001d20| 70 75 74 20 75 73 65 64 | 20 74 6f 20 63 72 65 61 |put used| to crea|
|00001d30| 74 65 20 74 68 65 6d 2c | 20 6d 61 79 20 62 65 0a |te them,| may be.|
|00001d40| 63 6f 6e 76 65 72 74 65 | 64 20 74 6f 20 70 61 72 |converte|d to par|
|00001d50| 73 65 20 74 72 65 65 73 | 20 72 65 70 72 65 73 65 |se trees| represe|
|00001d60| 6e 74 65 64 20 61 73 20 | 6c 69 73 74 2d 20 6f 72 |nted as |list- or|
|00001d70| 20 74 75 70 6c 65 2d 20 | 74 72 65 65 73 2c 20 6f | tuple- |trees, o|
|00001d80| 72 20 6d 61 79 0a 62 65 | 20 63 6f 6d 70 69 6c 65 |r may.be| compile|
|00001d90| 64 20 69 6e 74 6f 20 65 | 78 65 63 75 74 61 62 6c |d into e|xecutabl|
|00001da0| 65 20 63 6f 64 65 20 6f | 62 6a 65 63 74 73 2e 20 |e code o|bjects. |
|00001db0| 20 50 61 72 73 65 20 74 | 72 65 65 73 20 6d 61 79 | Parse t|rees may|
|00001dc0| 20 62 65 0a 65 78 74 72 | 61 63 74 65 64 20 77 69 | be.extr|acted wi|
|00001dd0| 74 68 20 6f 72 20 77 69 | 74 68 6f 75 74 20 6c 69 |th or wi|thout li|
|00001de0| 6e 65 20 6e 75 6d 62 65 | 72 69 6e 67 20 69 6e 66 |ne numbe|ring inf|
|00001df0| 6f 72 6d 61 74 69 6f 6e | 2e 0a 0a 5c 62 65 67 69 |ormation|...\begi|
|00001e00| 6e 7b 66 75 6e 63 64 65 | 73 63 7d 7b 61 73 74 32 |n{funcde|sc}{ast2|
|00001e10| 6c 69 73 74 7d 7b 61 73 | 74 5c 6f 70 74 69 6f 6e |list}{as|t\option|
|00001e20| 61 6c 7b 5c 2c 20 6c 69 | 6e 65 5f 69 6e 66 6f 5c |al{\, li|ne_info\|
|00001e30| 63 6f 64 65 7b 20 3d 20 | 30 7d 7d 7d 0a 54 68 69 |code{ = |0}}}.Thi|
|00001e40| 73 20 66 75 6e 63 74 69 | 6f 6e 20 61 63 63 65 70 |s functi|on accep|
|00001e50| 74 73 20 61 6e 20 41 53 | 54 20 6f 62 6a 65 63 74 |ts an AS|T object|
|00001e60| 20 66 72 6f 6d 20 74 68 | 65 20 63 61 6c 6c 65 72 | from th|e caller|
|00001e70| 20 69 6e 0a 5c 63 6f 64 | 65 7b 5c 76 61 72 7b 61 | in.\cod|e{\var{a|
|00001e80| 73 74 7d 7d 20 61 6e 64 | 20 72 65 74 75 72 6e 73 |st}} and| returns|
|00001e90| 20 61 20 50 79 74 68 6f | 6e 20 6c 69 73 74 20 72 | a Pytho|n list r|
|00001ea0| 65 70 72 65 73 65 6e 74 | 69 6e 67 20 74 68 65 0a |epresent|ing the.|
|00001eb0| 65 71 75 69 76 65 6c 65 | 6e 74 20 70 61 72 73 65 |equivele|nt parse|
|00001ec0| 20 74 72 65 65 2e 20 20 | 54 68 65 20 72 65 73 75 | tree. |The resu|
|00001ed0| 6c 74 69 6e 67 20 6c 69 | 73 74 20 72 65 70 72 65 |lting li|st repre|
|00001ee0| 73 65 6e 74 61 74 69 6f | 6e 20 63 61 6e 20 62 65 |sentatio|n can be|
|00001ef0| 20 75 73 65 64 0a 66 6f | 72 20 69 6e 73 70 65 63 | used.fo|r inspec|
|00001f00| 74 69 6f 6e 20 6f 72 20 | 74 68 65 20 63 72 65 61 |tion or |the crea|
|00001f10| 74 69 6f 6e 20 6f 66 20 | 61 20 6e 65 77 20 70 61 |tion of |a new pa|
|00001f20| 72 73 65 20 74 72 65 65 | 20 69 6e 20 6c 69 73 74 |rse tree| in list|
|00001f30| 20 66 6f 72 6d 2e 20 20 | 54 68 69 73 0a 66 75 6e | form. |This.fun|
|00001f40| 63 74 69 6f 6e 20 64 6f | 65 73 20 6e 6f 74 20 66 |ction do|es not f|
|00001f50| 61 69 6c 20 73 6f 20 6c | 6f 6e 67 20 61 73 20 6d |ail so l|ong as m|
|00001f60| 65 6d 6f 72 79 20 69 73 | 20 61 76 61 69 6c 61 62 |emory is| availab|
|00001f70| 6c 65 20 74 6f 20 62 75 | 69 6c 64 20 74 68 65 0a |le to bu|ild the.|
|00001f80| 6c 69 73 74 20 72 65 70 | 72 65 73 65 6e 74 61 74 |list rep|resentat|
|00001f90| 69 6f 6e 2e 20 20 49 66 | 20 74 68 65 20 70 61 72 |ion. If| the par|
|00001fa0| 73 65 20 74 72 65 65 20 | 77 69 6c 6c 20 6f 6e 6c |se tree |will onl|
|00001fb0| 79 20 62 65 20 75 73 65 | 64 20 66 6f 72 0a 69 6e |y be use|d for.in|
|00001fc0| 73 70 65 63 74 69 6f 6e | 2c 20 5c 63 6f 64 65 7b |spection|, \code{|
|00001fd0| 61 73 74 32 74 75 70 6c | 65 28 29 7d 20 73 68 6f |ast2tupl|e()} sho|
|00001fe0| 75 6c 64 20 62 65 20 75 | 73 65 64 20 69 6e 73 74 |uld be u|sed inst|
|00001ff0| 65 61 64 20 74 6f 20 72 | 65 64 75 63 65 20 6d 65 |ead to r|educe me|
|00002000| 6d 6f 72 79 0a 63 6f 6e | 73 75 6d 70 74 69 6f 6e |mory.con|sumption|
|00002010| 20 61 6e 64 20 66 72 61 | 67 6d 65 6e 74 61 74 69 | and fra|gmentati|
|00002020| 6f 6e 2e 20 20 57 68 65 | 6e 20 74 68 65 20 6c 69 |on. Whe|n the li|
|00002030| 73 74 20 72 65 70 72 65 | 73 65 6e 74 61 74 69 6f |st repre|sentatio|
|00002040| 6e 20 69 73 0a 72 65 71 | 75 69 72 65 64 2c 20 74 |n is.req|uired, t|
|00002050| 68 69 73 20 66 75 6e 63 | 74 69 6f 6e 20 69 73 20 |his func|tion is |
|00002060| 73 69 67 6e 69 66 69 63 | 61 6e 74 6c 79 20 66 61 |signific|antly fa|
|00002070| 73 74 65 72 20 74 68 61 | 6e 20 72 65 74 72 69 65 |ster tha|n retrie|
|00002080| 76 69 6e 67 20 61 0a 74 | 75 70 6c 65 20 72 65 70 |ving a.t|uple rep|
|00002090| 72 65 73 65 6e 74 61 74 | 69 6f 6e 20 61 6e 64 20 |resentat|ion and |
|000020a0| 63 6f 6e 76 65 72 74 69 | 6e 67 20 74 68 61 74 20 |converti|ng that |
|000020b0| 74 6f 20 6e 65 73 74 65 | 64 20 6c 69 73 74 73 2e |to neste|d lists.|
|000020c0| 0a 0a 49 66 20 5c 63 6f | 64 65 7b 5c 76 61 72 7b |..If \co|de{\var{|
|000020d0| 6c 69 6e 65 5f 69 6e 66 | 6f 7d 7d 20 69 73 20 74 |line_inf|o}} is t|
|000020e0| 72 75 65 2c 20 6c 69 6e | 65 20 6e 75 6d 62 65 72 |rue, lin|e number|
|000020f0| 20 69 6e 66 6f 72 6d 61 | 74 69 6f 6e 20 77 69 6c | informa|tion wil|
|00002100| 6c 20 62 65 0a 69 6e 63 | 6c 75 64 65 64 20 66 6f |l be.inc|luded fo|
|00002110| 72 20 61 6c 6c 20 74 65 | 72 6d 69 6e 61 6c 20 74 |r all te|rminal t|
|00002120| 6f 6b 65 6e 73 20 61 73 | 20 61 20 74 68 69 72 64 |okens as| a third|
|00002130| 20 65 6c 65 6d 65 6e 74 | 20 6f 66 20 74 68 65 20 | element| of the |
|00002140| 6c 69 73 74 0a 72 65 70 | 72 65 73 65 6e 74 69 6e |list.rep|resentin|
|00002150| 67 20 74 68 65 20 74 6f | 6b 65 6e 2e 20 20 54 68 |g the to|ken. Th|
|00002160| 69 73 20 69 6e 66 6f 72 | 6d 61 74 69 6f 6e 20 69 |is infor|mation i|
|00002170| 73 20 6f 6d 69 74 74 65 | 64 20 69 66 20 74 68 65 |s omitte|d if the|
|00002180| 20 66 6c 61 67 20 69 73 | 0a 66 61 6c 73 65 20 6f | flag is|.false o|
|00002190| 72 20 6f 6d 69 74 74 65 | 64 2e 0a 5c 65 6e 64 7b |r omitte|d..\end{|
|000021a0| 66 75 6e 63 64 65 73 63 | 7d 0a 0a 5c 62 65 67 69 |funcdesc|}..\begi|
|000021b0| 6e 7b 66 75 6e 63 64 65 | 73 63 7d 7b 61 73 74 32 |n{funcde|sc}{ast2|
|000021c0| 74 75 70 6c 65 7d 7b 61 | 73 74 5c 6f 70 74 69 6f |tuple}{a|st\optio|
|000021d0| 6e 61 6c 7b 5c 2c 20 6c | 69 6e 65 5f 69 6e 66 6f |nal{\, l|ine_info|
|000021e0| 5c 63 6f 64 65 7b 20 3d | 20 30 7d 7d 7d 0a 54 68 |\code{ =| 0}}}.Th|
|000021f0| 69 73 20 66 75 6e 63 74 | 69 6f 6e 20 61 63 63 65 |is funct|ion acce|
|00002200| 70 74 73 20 61 6e 20 41 | 53 54 20 6f 62 6a 65 63 |pts an A|ST objec|
|00002210| 74 20 66 72 6f 6d 20 74 | 68 65 20 63 61 6c 6c 65 |t from t|he calle|
|00002220| 72 20 69 6e 0a 5c 63 6f | 64 65 7b 5c 76 61 72 7b |r in.\co|de{\var{|
|00002230| 61 73 74 7d 7d 20 61 6e | 64 20 72 65 74 75 72 6e |ast}} an|d return|
|00002240| 73 20 61 20 50 79 74 68 | 6f 6e 20 74 75 70 6c 65 |s a Pyth|on tuple|
|00002250| 20 72 65 70 72 65 73 65 | 6e 74 69 6e 67 20 74 68 | represe|nting th|
|00002260| 65 0a 65 71 75 69 76 65 | 6c 65 6e 74 20 70 61 72 |e.equive|lent par|
|00002270| 73 65 20 74 72 65 65 2e | 20 20 4f 74 68 65 72 20 |se tree.| Other |
|00002280| 74 68 61 6e 20 72 65 74 | 75 72 6e 69 6e 67 20 61 |than ret|urning a|
|00002290| 20 74 75 70 6c 65 20 69 | 6e 73 74 65 61 64 20 6f | tuple i|nstead o|
|000022a0| 66 20 61 0a 6c 69 73 74 | 2c 20 74 68 69 73 20 66 |f a.list|, this f|
|000022b0| 75 6e 63 74 69 6f 6e 20 | 69 73 20 69 64 65 6e 74 |unction |is ident|
|000022c0| 69 63 61 6c 20 74 6f 20 | 5c 63 6f 64 65 7b 61 73 |ical to |\code{as|
|000022d0| 74 32 6c 69 73 74 28 29 | 7d 2e 0a 0a 49 66 20 5c |t2list()|}...If \|
|000022e0| 63 6f 64 65 7b 5c 76 61 | 72 7b 6c 69 6e 65 5f 69 |code{\va|r{line_i|
|000022f0| 6e 66 6f 7d 7d 20 69 73 | 20 74 72 75 65 2c 20 6c |nfo}} is| true, l|
|00002300| 69 6e 65 20 6e 75 6d 62 | 65 72 20 69 6e 66 6f 72 |ine numb|er infor|
|00002310| 6d 61 74 69 6f 6e 20 77 | 69 6c 6c 20 62 65 0a 69 |mation w|ill be.i|
|00002320| 6e 63 6c 75 64 65 64 20 | 66 6f 72 20 61 6c 6c 20 |ncluded |for all |
|00002330| 74 65 72 6d 69 6e 61 6c | 20 74 6f 6b 65 6e 73 20 |terminal| tokens |
|00002340| 61 73 20 61 20 74 68 69 | 72 64 20 65 6c 65 6d 65 |as a thi|rd eleme|
|00002350| 6e 74 20 6f 66 20 74 68 | 65 20 6c 69 73 74 0a 72 |nt of th|e list.r|
|00002360| 65 70 72 65 73 65 6e 74 | 69 6e 67 20 74 68 65 20 |epresent|ing the |
|00002370| 74 6f 6b 65 6e 2e 20 20 | 54 68 69 73 20 69 6e 66 |token. |This inf|
|00002380| 6f 72 6d 61 74 69 6f 6e | 20 69 73 20 6f 6d 69 74 |ormation| is omit|
|00002390| 74 65 64 20 69 66 20 74 | 68 65 20 66 6c 61 67 20 |ted if t|he flag |
|000023a0| 69 73 0a 66 61 6c 73 65 | 20 6f 72 20 6f 6d 69 74 |is.false| or omit|
|000023b0| 74 65 64 2e 0a 5c 65 6e | 64 7b 66 75 6e 63 64 65 |ted..\en|d{funcde|
|000023c0| 73 63 7d 0a 0a 5c 62 65 | 67 69 6e 7b 66 75 6e 63 |sc}..\be|gin{func|
|000023d0| 64 65 73 63 7d 7b 63 6f | 6d 70 69 6c 65 61 73 74 |desc}{co|mpileast|
|000023e0| 7d 7b 61 73 74 5c 6f 70 | 74 69 6f 6e 61 6c 7b 5c |}{ast\op|tional{\|
|000023f0| 2c 20 66 69 6c 65 6e 61 | 6d 65 5c 63 6f 64 65 7b |, filena|me\code{|
|00002400| 20 3d 20 27 3c 61 73 74 | 3e 27 7d 7d 7d 0a 54 68 | = '<ast|>'}}}.Th|
|00002410| 65 20 50 79 74 68 6f 6e | 20 62 79 74 65 20 63 6f |e Python| byte co|
|00002420| 6d 70 69 6c 65 72 20 63 | 61 6e 20 62 65 20 69 6e |mpiler c|an be in|
|00002430| 76 6f 6b 65 64 20 6f 6e | 20 61 6e 20 41 53 54 20 |voked on| an AST |
|00002440| 6f 62 6a 65 63 74 20 74 | 6f 20 70 72 6f 64 75 63 |object t|o produc|
|00002450| 65 0a 63 6f 64 65 20 6f | 62 6a 65 63 74 73 20 77 |e.code o|bjects w|
|00002460| 68 69 63 68 20 63 61 6e | 20 62 65 20 75 73 65 64 |hich can| be used|
|00002470| 20 61 73 20 70 61 72 74 | 20 6f 66 20 61 6e 20 5c | as part| of an \|
|00002480| 63 6f 64 65 7b 65 78 65 | 63 7d 20 73 74 61 74 65 |code{exe|c} state|
|00002490| 6d 65 6e 74 20 6f 72 0a | 61 20 63 61 6c 6c 20 74 |ment or.|a call t|
|000024a0| 6f 20 74 68 65 20 62 75 | 69 6c 74 2d 69 6e 20 5c |o the bu|ilt-in \|
|000024b0| 63 6f 64 65 7b 65 76 61 | 6c 28 29 7d 20 66 75 6e |code{eva|l()} fun|
|000024c0| 63 74 69 6f 6e 2e 20 20 | 54 68 69 73 20 66 75 6e |ction. |This fun|
|000024d0| 63 74 69 6f 6e 20 70 72 | 6f 76 69 64 65 73 0a 74 |ction pr|ovides.t|
|000024e0| 68 65 20 69 6e 74 65 72 | 66 61 63 65 20 74 6f 20 |he inter|face to |
|000024f0| 74 68 65 20 63 6f 6d 70 | 69 6c 65 72 2c 20 70 61 |the comp|iler, pa|
|00002500| 73 73 69 6e 67 20 74 68 | 65 20 69 6e 74 65 72 6e |ssing th|e intern|
|00002510| 61 6c 20 70 61 72 73 65 | 20 74 72 65 65 20 66 72 |al parse| tree fr|
|00002520| 6f 6d 0a 5c 63 6f 64 65 | 7b 5c 76 61 72 7b 61 73 |om.\code|{\var{as|
|00002530| 74 7d 7d 20 74 6f 20 74 | 68 65 20 70 61 72 73 65 |t}} to t|he parse|
|00002540| 72 2c 20 75 73 69 6e 67 | 20 74 68 65 20 73 6f 75 |r, using| the sou|
|00002550| 72 63 65 20 66 69 6c 65 | 20 6e 61 6d 65 20 73 70 |rce file| name sp|
|00002560| 65 63 69 66 69 65 64 0a | 62 79 20 74 68 65 20 5c |ecified.|by the \|
|00002570| 63 6f 64 65 7b 5c 76 61 | 72 7b 66 69 6c 65 6e 61 |code{\va|r{filena|
|00002580| 6d 65 7d 7d 20 70 61 72 | 61 6d 65 74 65 72 2e 20 |me}} par|ameter. |
|00002590| 20 54 68 65 20 64 65 66 | 61 75 6c 74 20 76 61 6c | The def|ault val|
|000025a0| 75 65 20 73 75 70 70 6c | 69 65 64 0a 66 6f 72 20 |ue suppl|ied.for |
|000025b0| 5c 63 6f 64 65 7b 5c 76 | 61 72 7b 66 69 6c 65 6e |\code{\v|ar{filen|
|000025c0| 61 6d 65 7d 7d 20 69 6e | 64 69 63 61 74 65 73 20 |ame}} in|dicates |
|000025d0| 74 68 61 74 20 74 68 65 | 20 73 6f 75 72 63 65 20 |that the| source |
|000025e0| 77 61 73 20 61 6e 20 41 | 53 54 20 6f 62 6a 65 63 |was an A|ST objec|
|000025f0| 74 2e 0a 0a 43 6f 6d 70 | 69 6c 69 6e 67 20 61 6e |t...Comp|iling an|
|00002600| 20 41 53 54 20 6f 62 6a | 65 63 74 20 6d 61 79 20 | AST obj|ect may |
|00002610| 72 65 73 75 6c 74 20 69 | 6e 20 65 78 63 65 70 74 |result i|n except|
|00002620| 69 6f 6e 73 20 72 65 6c | 61 74 65 64 20 74 6f 0a |ions rel|ated to.|
|00002630| 63 6f 6d 70 69 6c 61 74 | 69 6f 6e 3b 20 61 6e 20 |compilat|ion; an |
|00002640| 65 78 61 6d 70 6c 65 20 | 77 6f 75 6c 64 20 62 65 |example |would be|
|00002650| 20 61 20 5c 63 6f 64 65 | 7b 53 79 6e 74 61 78 45 | a \code|{SyntaxE|
|00002660| 72 72 6f 72 7d 20 63 61 | 75 73 65 64 20 62 79 20 |rror} ca|used by |
|00002670| 74 68 65 0a 70 61 72 73 | 65 20 74 72 65 65 20 66 |the.pars|e tree f|
|00002680| 6f 72 20 5c 63 6f 64 65 | 7b 64 65 6c 20 66 28 30 |or \code|{del f(0|
|00002690| 29 7d 3a 20 74 68 69 73 | 20 73 74 61 74 65 6d 65 |)}: this| stateme|
|000026a0| 6e 74 20 69 73 20 63 6f | 6e 73 69 64 65 72 65 64 |nt is co|nsidered|
|000026b0| 20 6c 65 67 61 6c 0a 77 | 69 74 68 69 6e 20 74 68 | legal.w|ithin th|
|000026c0| 65 20 66 6f 72 6d 61 6c | 20 67 72 61 6d 6d 61 72 |e formal| grammar|
|000026d0| 20 66 6f 72 20 50 79 74 | 68 6f 6e 20 62 75 74 20 | for Pyt|hon but |
|000026e0| 69 73 20 6e 6f 74 20 61 | 20 6c 65 67 61 6c 20 6c |is not a| legal l|
|000026f0| 61 6e 67 75 61 67 65 0a | 63 6f 6e 73 74 72 75 63 |anguage.|construc|
|00002700| 74 2e 20 20 54 68 65 20 | 5c 63 6f 64 65 7b 53 79 |t. The |\code{Sy|
|00002710| 6e 74 61 78 45 72 72 6f | 72 7d 20 72 61 69 73 65 |ntaxErro|r} raise|
|00002720| 64 20 66 6f 72 20 74 68 | 69 73 20 63 6f 6e 64 69 |d for th|is condi|
|00002730| 74 69 6f 6e 20 69 73 0a | 61 63 74 75 61 6c 6c 79 |tion is.|actually|
|00002740| 20 67 65 6e 65 72 61 74 | 65 64 20 62 79 20 74 68 | generat|ed by th|
|00002750| 65 20 50 79 74 68 6f 6e | 20 62 79 74 65 2d 63 6f |e Python| byte-co|
|00002760| 6d 70 69 6c 65 72 20 6e | 6f 72 6d 61 6c 6c 79 2c |mpiler n|ormally,|
|00002770| 20 77 68 69 63 68 20 69 | 73 20 77 68 79 0a 69 74 | which i|s why.it|
|00002780| 20 63 61 6e 20 62 65 20 | 72 61 69 73 65 64 20 61 | can be |raised a|
|00002790| 74 20 74 68 69 73 20 70 | 6f 69 6e 74 20 62 79 20 |t this p|oint by |
|000027a0| 74 68 65 20 5c 63 6f 64 | 65 7b 70 61 72 73 65 72 |the \cod|e{parser|
|000027b0| 7d 20 6d 6f 64 75 6c 65 | 2e 20 20 4d 6f 73 74 0a |} module|. Most.|
|000027c0| 63 61 75 73 65 73 20 6f | 66 20 63 6f 6d 70 69 6c |causes o|f compil|
|000027d0| 61 74 69 6f 6e 20 66 61 | 69 6c 75 72 65 20 63 61 |ation fa|ilure ca|
|000027e0| 6e 20 62 65 20 64 69 61 | 67 6e 6f 73 65 64 20 70 |n be dia|gnosed p|
|000027f0| 72 6f 67 72 61 6d 6d 61 | 74 69 63 61 6c 6c 79 20 |rogramma|tically |
|00002800| 62 79 0a 69 6e 73 70 65 | 63 74 69 6f 6e 20 6f 66 |by.inspe|ction of|
|00002810| 20 74 68 65 20 70 61 72 | 73 65 20 74 72 65 65 2e | the par|se tree.|
|00002820| 0a 5c 65 6e 64 7b 66 75 | 6e 63 64 65 73 63 7d 0a |.\end{fu|ncdesc}.|
|00002830| 0a 0a 5c 73 75 62 73 65 | 63 74 69 6f 6e 7b 51 75 |..\subse|ction{Qu|
|00002840| 65 72 69 65 73 20 6f 6e | 20 41 53 54 20 4f 62 6a |eries on| AST Obj|
|00002850| 65 63 74 73 7d 0a 0a 54 | 77 6f 20 66 75 6e 63 74 |ects}..T|wo funct|
|00002860| 69 6f 6e 73 20 61 72 65 | 20 70 72 6f 76 69 64 65 |ions are| provide|
|00002870| 64 20 77 68 69 63 68 20 | 61 6c 6c 6f 77 20 61 6e |d which |allow an|
|00002880| 20 61 70 70 6c 69 63 61 | 74 69 6f 6e 20 74 6f 20 | applica|tion to |
|00002890| 64 65 74 65 72 6d 69 6e | 65 20 69 66 0a 61 6e 20 |determin|e if.an |
|000028a0| 41 53 54 20 77 61 73 20 | 63 72 65 61 74 65 20 61 |AST was |create a|
|000028b0| 73 20 61 6e 20 65 78 70 | 72 65 73 73 69 6f 6e 20 |s an exp|ression |
|000028c0| 6f 72 20 61 20 73 75 69 | 74 65 2e 20 20 4e 65 69 |or a sui|te. Nei|
|000028d0| 74 68 65 72 20 6f 66 20 | 74 68 65 73 65 0a 66 75 |ther of |these.fu|
|000028e0| 6e 63 74 69 6f 6e 73 20 | 63 61 6e 20 62 65 20 75 |nctions |can be u|
|000028f0| 73 65 64 20 74 6f 20 64 | 65 74 65 72 6d 69 6e 65 |sed to d|etermine|
|00002900| 20 69 66 20 61 6e 20 41 | 53 54 20 77 61 73 20 63 | if an A|ST was c|
|00002910| 72 65 61 74 65 64 20 66 | 72 6f 6d 20 73 6f 75 72 |reated f|rom sour|
|00002920| 63 65 0a 63 6f 64 65 20 | 76 69 61 20 5c 63 6f 64 |ce.code |via \cod|
|00002930| 65 7b 65 78 70 72 28 29 | 7d 20 6f 72 20 5c 63 6f |e{expr()|} or \co|
|00002940| 64 65 7b 73 75 69 74 65 | 28 29 7d 20 6f 72 20 66 |de{suite|()} or f|
|00002950| 72 6f 6d 20 61 20 70 61 | 72 73 65 20 74 72 65 65 |rom a pa|rse tree|
|00002960| 20 76 69 61 0a 5c 63 6f | 64 65 7b 73 65 71 75 65 | via.\co|de{seque|
|00002970| 6e 63 65 32 61 73 74 28 | 29 7d 2e 0a 0a 5c 62 65 |nce2ast(|)}...\be|
|00002980| 67 69 6e 7b 66 75 6e 63 | 64 65 73 63 7d 7b 69 73 |gin{func|desc}{is|
|00002990| 65 78 70 72 7d 7b 61 73 | 74 7d 0a 57 68 65 6e 20 |expr}{as|t}.When |
|000029a0| 5c 63 6f 64 65 7b 5c 76 | 61 72 7b 61 73 74 7d 7d |\code{\v|ar{ast}}|
|000029b0| 20 72 65 70 72 65 73 65 | 6e 74 73 20 61 6e 20 5c | represe|nts an \|
|000029c0| 63 6f 64 65 7b 27 65 76 | 61 6c 27 7d 20 66 6f 72 |code{'ev|al'} for|
|000029d0| 6d 2c 20 74 68 69 73 20 | 66 75 6e 63 74 69 6f 6e |m, this |function|
|000029e0| 0a 72 65 74 75 72 6e 73 | 20 61 20 74 72 75 65 20 |.returns| a true |
|000029f0| 76 61 6c 75 65 20 28 5c | 63 6f 64 65 7b 31 7d 29 |value (\|code{1})|
|00002a00| 2c 20 6f 74 68 65 72 77 | 69 73 65 20 69 74 20 72 |, otherw|ise it r|
|00002a10| 65 74 75 72 6e 73 20 66 | 61 6c 73 65 0a 28 5c 63 |eturns f|alse.(\c|
|00002a20| 6f 64 65 7b 30 7d 29 2e | 20 20 54 68 69 73 20 69 |ode{0}).| This i|
|00002a30| 73 20 75 73 65 66 75 6c | 2c 20 73 69 6e 63 65 20 |s useful|, since |
|00002a40| 63 6f 64 65 20 6f 62 6a | 65 63 74 73 20 6e 6f 72 |code obj|ects nor|
|00002a50| 6d 61 6c 6c 79 20 63 61 | 6e 6e 6f 74 20 62 65 0a |mally ca|nnot be.|
|00002a60| 71 75 65 72 69 65 64 20 | 66 6f 72 20 74 68 69 73 |queried |for this|
|00002a70| 20 69 6e 66 6f 72 6d 61 | 74 69 6f 6e 20 75 73 69 | informa|tion usi|
|00002a80| 6e 67 20 65 78 69 73 74 | 69 6e 67 20 62 75 69 6c |ng exist|ing buil|
|00002a90| 74 2d 69 6e 20 66 75 6e | 63 74 69 6f 6e 73 2e 20 |t-in fun|ctions. |
|00002aa0| 20 4e 6f 74 65 0a 74 68 | 61 74 20 74 68 65 20 63 | Note.th|at the c|
|00002ab0| 6f 64 65 20 6f 62 6a 65 | 63 74 73 20 63 72 65 61 |ode obje|cts crea|
|00002ac0| 74 65 64 20 62 79 20 5c | 63 6f 64 65 7b 63 6f 6d |ted by \|code{com|
|00002ad0| 70 69 6c 65 61 73 74 28 | 29 7d 20 63 61 6e 6e 6f |pileast(|)} canno|
|00002ae0| 74 20 62 65 20 71 75 65 | 72 69 65 64 0a 6c 69 6b |t be que|ried.lik|
|00002af0| 65 20 74 68 69 73 20 65 | 69 74 68 65 72 2c 20 61 |e this e|ither, a|
|00002b00| 6e 64 20 61 72 65 20 69 | 64 65 6e 74 69 63 61 6c |nd are i|dentical|
|00002b10| 20 74 6f 20 74 68 6f 73 | 65 20 63 72 65 61 74 65 | to thos|e create|
|00002b20| 64 20 62 79 20 74 68 65 | 20 62 75 69 6c 74 2d 69 |d by the| built-i|
|00002b30| 6e 0a 5c 63 6f 64 65 7b | 63 6f 6d 70 69 6c 65 28 |n.\code{|compile(|
|00002b40| 29 7d 20 66 75 6e 63 74 | 69 6f 6e 2e 0a 5c 65 6e |)} funct|ion..\en|
|00002b50| 64 7b 66 75 6e 63 64 65 | 73 63 7d 0a 0a 0a 5c 62 |d{funcde|sc}...\b|
|00002b60| 65 67 69 6e 7b 66 75 6e | 63 64 65 73 63 7d 7b 69 |egin{fun|cdesc}{i|
|00002b70| 73 73 75 69 74 65 7d 7b | 61 73 74 7d 0a 54 68 69 |ssuite}{|ast}.Thi|
|00002b80| 73 20 66 75 6e 63 74 69 | 6f 6e 20 6d 69 72 72 6f |s functi|on mirro|
|00002b90| 72 73 20 5c 63 6f 64 65 | 7b 69 73 65 78 70 72 28 |rs \code|{isexpr(|
|00002ba0| 29 7d 20 69 6e 20 74 68 | 61 74 20 69 74 20 72 65 |)} in th|at it re|
|00002bb0| 70 6f 72 74 73 20 77 68 | 65 74 68 65 72 20 61 6e |ports wh|ether an|
|00002bc0| 0a 41 53 54 20 6f 62 6a | 65 63 74 20 72 65 70 72 |.AST obj|ect repr|
|00002bd0| 65 73 65 6e 74 73 20 61 | 6e 20 5c 63 6f 64 65 7b |esents a|n \code{|
|00002be0| 27 65 78 65 63 27 7d 20 | 66 6f 72 6d 2c 20 63 6f |'exec'} |form, co|
|00002bf0| 6d 6d 6f 6e 6c 79 20 6b | 6e 6f 77 6e 20 61 73 20 |mmonly k|nown as |
|00002c00| 61 0a 60 60 73 75 69 74 | 65 2e 27 27 20 20 49 74 |a.``suit|e.'' It|
|00002c10| 20 69 73 20 6e 6f 74 20 | 73 61 66 65 20 74 6f 20 | is not |safe to |
|00002c20| 61 73 73 75 6d 65 20 74 | 68 61 74 20 74 68 69 73 |assume t|hat this|
|00002c30| 20 66 75 6e 63 74 69 6f | 6e 20 69 73 20 65 71 75 | functio|n is equ|
|00002c40| 69 76 65 6c 65 6e 74 0a | 74 6f 20 5c 63 6f 64 65 |ivelent.|to \code|
|00002c50| 7b 6e 6f 74 20 69 73 65 | 78 70 72 28 5c 76 61 72 |{not ise|xpr(\var|
|00002c60| 7b 61 73 74 7d 29 7d 2c | 20 61 73 20 61 64 64 69 |{ast})},| as addi|
|00002c70| 74 69 6f 6e 61 6c 20 73 | 79 6e 74 61 63 74 69 63 |tional s|yntactic|
|00002c80| 20 66 72 61 67 6d 65 6e | 74 73 20 6d 61 79 0a 62 | fragmen|ts may.b|
|00002c90| 65 20 73 75 70 70 6f 72 | 74 65 64 20 69 6e 20 74 |e suppor|ted in t|
|00002ca0| 68 65 20 66 75 74 75 72 | 65 2e 0a 5c 65 6e 64 7b |he futur|e..\end{|
|00002cb0| 66 75 6e 63 64 65 73 63 | 7d 0a 0a 0a 5c 73 75 62 |funcdesc|}...\sub|
|00002cc0| 73 65 63 74 69 6f 6e 7b | 45 78 63 65 70 74 69 6f |section{|Exceptio|
|00002cd0| 6e 73 20 61 6e 64 20 45 | 72 72 6f 72 20 48 61 6e |ns and E|rror Han|
|00002ce0| 64 6c 69 6e 67 7d 0a 0a | 54 68 65 20 70 61 72 73 |dling}..|The pars|
|00002cf0| 65 72 20 6d 6f 64 75 6c | 65 20 64 65 66 69 6e 65 |er modul|e define|
|00002d00| 73 20 61 20 73 69 6e 67 | 6c 65 20 65 78 63 65 70 |s a sing|le excep|
|00002d10| 74 69 6f 6e 2c 20 62 75 | 74 20 6d 61 79 20 61 6c |tion, bu|t may al|
|00002d20| 73 6f 20 70 61 73 73 20 | 6f 74 68 65 72 0a 62 75 |so pass |other.bu|
|00002d30| 69 6c 74 2d 69 6e 20 65 | 78 63 65 70 74 69 6f 6e |ilt-in e|xception|
|00002d40| 73 20 66 72 6f 6d 20 6f | 74 68 65 72 20 70 6f 72 |s from o|ther por|
|00002d50| 74 69 6f 6e 73 20 6f 66 | 20 74 68 65 20 50 79 74 |tions of| the Pyt|
|00002d60| 68 6f 6e 20 72 75 6e 74 | 69 6d 65 0a 65 6e 76 69 |hon runt|ime.envi|
|00002d70| 72 6f 6e 6d 65 6e 74 2e | 20 20 53 65 65 20 65 61 |ronment.| See ea|
|00002d80| 63 68 20 66 75 6e 63 74 | 69 6f 6e 20 66 6f 72 20 |ch funct|ion for |
|00002d90| 69 6e 66 6f 72 6d 61 74 | 69 6f 6e 20 61 62 6f 75 |informat|ion abou|
|00002da0| 74 20 74 68 65 20 65 78 | 63 65 70 74 69 6f 6e 73 |t the ex|ceptions|
|00002db0| 0a 69 74 20 63 61 6e 20 | 72 61 69 73 65 2e 0a 0a |.it can |raise...|
|00002dc0| 5c 62 65 67 69 6e 7b 65 | 78 63 64 65 73 63 7d 7b |\begin{e|xcdesc}{|
|00002dd0| 50 61 72 73 65 72 45 72 | 72 6f 72 7d 0a 45 78 63 |ParserEr|ror}.Exc|
|00002de0| 65 70 74 69 6f 6e 20 72 | 61 69 73 65 64 20 77 68 |eption r|aised wh|
|00002df0| 65 6e 20 61 20 66 61 69 | 6c 75 72 65 20 6f 63 63 |en a fai|lure occ|
|00002e00| 75 72 73 20 77 69 74 68 | 69 6e 20 74 68 65 20 70 |urs with|in the p|
|00002e10| 61 72 73 65 72 20 6d 6f | 64 75 6c 65 2e 20 20 54 |arser mo|dule. T|
|00002e20| 68 69 73 0a 69 73 20 67 | 65 6e 65 72 61 6c 6c 79 |his.is g|enerally|
|00002e30| 20 70 72 6f 64 75 63 65 | 64 20 66 6f 72 20 76 61 | produce|d for va|
|00002e40| 6c 69 64 61 74 69 6f 6e | 20 66 61 69 6c 75 72 65 |lidation| failure|
|00002e50| 73 20 72 61 74 68 65 72 | 20 74 68 61 6e 20 74 68 |s rather| than th|
|00002e60| 65 20 62 75 69 6c 74 20 | 69 6e 0a 5c 63 6f 64 65 |e built |in.\code|
|00002e70| 7b 53 79 6e 74 61 78 45 | 72 72 6f 72 7d 20 74 68 |{SyntaxE|rror} th|
|00002e80| 72 6f 77 6e 20 64 75 72 | 69 6e 67 20 6e 6f 72 6d |rown dur|ing norm|
|00002e90| 61 6c 20 70 61 72 73 69 | 6e 67 2e 0a 54 68 65 20 |al parsi|ng..The |
|00002ea0| 65 78 63 65 70 74 69 6f | 6e 20 61 72 67 75 6d 65 |exceptio|n argume|
|00002eb0| 6e 74 20 69 73 20 65 69 | 74 68 65 72 20 61 20 73 |nt is ei|ther a s|
|00002ec0| 74 72 69 6e 67 20 64 65 | 73 63 72 69 62 69 6e 67 |tring de|scribing|
|00002ed0| 20 74 68 65 20 72 65 61 | 73 6f 6e 20 6f 66 20 74 | the rea|son of t|
|00002ee0| 68 65 0a 66 61 69 6c 75 | 72 65 20 6f 72 20 61 20 |he.failu|re or a |
|00002ef0| 74 75 70 6c 65 20 63 6f | 6e 74 61 69 6e 69 6e 67 |tuple co|ntaining|
|00002f00| 20 61 20 73 65 71 75 65 | 6e 63 65 20 63 61 75 73 | a seque|nce caus|
|00002f10| 69 6e 67 20 74 68 65 20 | 66 61 69 6c 75 72 65 20 |ing the |failure |
|00002f20| 66 72 6f 6d 20 61 20 70 | 61 72 73 65 0a 74 72 65 |from a p|arse.tre|
|00002f30| 65 20 70 61 73 73 65 64 | 20 74 6f 20 5c 63 6f 64 |e passed| to \cod|
|00002f40| 65 7b 73 65 71 75 65 6e | 63 65 32 61 73 74 28 29 |e{sequen|ce2ast()|
|00002f50| 7d 20 61 6e 64 20 61 6e | 20 65 78 70 6c 61 6e 61 |} and an| explana|
|00002f60| 74 6f 72 79 20 73 74 72 | 69 6e 67 2e 20 20 43 61 |tory str|ing. Ca|
|00002f70| 6c 6c 73 20 74 6f 0a 5c | 63 6f 64 65 7b 73 65 71 |lls to.\|code{seq|
|00002f80| 75 65 6e 63 65 32 61 73 | 74 28 29 7d 20 6e 65 65 |uence2as|t()} nee|
|00002f90| 64 20 74 6f 20 62 65 20 | 61 62 6c 65 20 74 6f 20 |d to be |able to |
|00002fa0| 68 61 6e 64 6c 65 20 65 | 69 74 68 65 72 20 74 79 |handle e|ither ty|
|00002fb0| 70 65 20 6f 66 20 65 78 | 63 65 70 74 69 6f 6e 2c |pe of ex|ception,|
|00002fc0| 0a 77 68 69 6c 65 20 63 | 61 6c 6c 73 20 74 6f 20 |.while c|alls to |
|00002fd0| 6f 74 68 65 72 20 66 75 | 6e 63 74 69 6f 6e 73 20 |other fu|nctions |
|00002fe0| 69 6e 20 74 68 65 20 6d | 6f 64 75 6c 65 20 77 69 |in the m|odule wi|
|00002ff0| 6c 6c 20 6f 6e 6c 79 20 | 6e 65 65 64 20 74 6f 20 |ll only |need to |
|00003000| 62 65 0a 61 77 61 72 65 | 20 6f 66 20 74 68 65 20 |be.aware| of the |
|00003010| 73 69 6d 70 6c 65 20 73 | 74 72 69 6e 67 20 76 61 |simple s|tring va|
|00003020| 6c 75 65 73 2e 0a 5c 65 | 6e 64 7b 65 78 63 64 65 |lues..\e|nd{excde|
|00003030| 73 63 7d 0a 0a 4e 6f 74 | 65 20 74 68 61 74 20 74 |sc}..Not|e that t|
|00003040| 68 65 20 66 75 6e 63 74 | 69 6f 6e 73 20 5c 63 6f |he funct|ions \co|
|00003050| 64 65 7b 63 6f 6d 70 69 | 6c 65 61 73 74 28 29 7d |de{compi|least()}|
|00003060| 2c 20 5c 63 6f 64 65 7b | 65 78 70 72 28 29 7d 2c |, \code{|expr()},|
|00003070| 20 61 6e 64 0a 5c 63 6f | 64 65 7b 73 75 69 74 65 | and.\co|de{suite|
|00003080| 28 29 7d 20 6d 61 79 20 | 74 68 72 6f 77 20 65 78 |()} may |throw ex|
|00003090| 63 65 70 74 69 6f 6e 73 | 20 77 68 69 63 68 20 61 |ceptions| which a|
|000030a0| 72 65 20 6e 6f 72 6d 61 | 6c 6c 79 20 74 68 72 6f |re norma|lly thro|
|000030b0| 77 6e 20 62 79 20 74 68 | 65 0a 70 61 72 73 69 6e |wn by th|e.parsin|
|000030c0| 67 20 61 6e 64 20 63 6f | 6d 70 69 6c 61 74 69 6f |g and co|mpilatio|
|000030d0| 6e 20 70 72 6f 63 65 73 | 73 2e 20 20 54 68 65 73 |n proces|s. Thes|
|000030e0| 65 20 69 6e 63 6c 75 64 | 65 20 74 68 65 20 62 75 |e includ|e the bu|
|000030f0| 69 6c 74 20 69 6e 0a 65 | 78 63 65 70 74 69 6f 6e |ilt in.e|xception|
|00003100| 73 20 5c 63 6f 64 65 7b | 4d 65 6d 6f 72 79 45 72 |s \code{|MemoryEr|
|00003110| 72 6f 72 7d 2c 20 5c 63 | 6f 64 65 7b 4f 76 65 72 |ror}, \c|ode{Over|
|00003120| 66 6c 6f 77 45 72 72 6f | 72 7d 2c 0a 5c 63 6f 64 |flowErro|r},.\cod|
|00003130| 65 7b 53 79 6e 74 61 78 | 45 72 72 6f 72 7d 2c 20 |e{Syntax|Error}, |
|00003140| 61 6e 64 20 5c 63 6f 64 | 65 7b 53 79 73 74 65 6d |and \cod|e{System|
|00003150| 45 72 72 6f 72 7d 2e 20 | 20 49 6e 20 74 68 65 73 |Error}. | In thes|
|00003160| 65 20 63 61 73 65 73 2c | 20 74 68 65 73 65 0a 65 |e cases,| these.e|
|00003170| 78 63 65 70 74 69 6f 6e | 73 20 63 61 72 72 79 20 |xception|s carry |
|00003180| 61 6c 6c 20 74 68 65 20 | 6d 65 61 6e 69 6e 67 20 |all the |meaning |
|00003190| 6e 6f 72 6d 61 6c 6c 79 | 20 61 73 73 6f 63 69 61 |normally| associa|
|000031a0| 74 65 64 20 77 69 74 68 | 20 74 68 65 6d 2e 20 20 |ted with| them. |
|000031b0| 52 65 66 65 72 0a 74 6f | 20 74 68 65 20 64 65 73 |Refer.to| the des|
|000031c0| 63 72 69 70 74 69 6f 6e | 73 20 6f 66 20 65 61 63 |cription|s of eac|
|000031d0| 68 20 66 75 6e 63 74 69 | 6f 6e 20 66 6f 72 20 64 |h functi|on for d|
|000031e0| 65 74 61 69 6c 65 64 20 | 69 6e 66 6f 72 6d 61 74 |etailed |informat|
|000031f0| 69 6f 6e 2e 0a 0a 0a 5c | 73 75 62 73 65 63 74 69 |ion....\|subsecti|
|00003200| 6f 6e 7b 41 53 54 20 4f | 62 6a 65 63 74 73 7d 0a |on{AST O|bjects}.|
|00003210| 0a 41 53 54 20 6f 62 6a | 65 63 74 73 20 72 65 74 |.AST obj|ects ret|
|00003220| 75 72 6e 65 64 20 62 79 | 20 5c 63 6f 64 65 7b 65 |urned by| \code{e|
|00003230| 78 70 72 28 29 7d 2c 20 | 5c 63 6f 64 65 7b 73 75 |xpr()}, |\code{su|
|00003240| 69 74 65 28 29 7d 2c 20 | 61 6e 64 0a 5c 63 6f 64 |ite()}, |and.\cod|
|00003250| 65 7b 73 65 71 75 65 6e | 63 65 32 61 73 74 28 29 |e{sequen|ce2ast()|
|00003260| 7d 20 68 61 76 65 20 6e | 6f 20 6d 65 74 68 6f 64 |} have n|o method|
|00003270| 73 20 6f 66 20 74 68 65 | 69 72 20 6f 77 6e 2e 0a |s of the|ir own..|
|00003280| 53 6f 6d 65 20 6f 66 20 | 74 68 65 20 66 75 6e 63 |Some of |the func|
|00003290| 74 69 6f 6e 73 20 64 65 | 66 69 6e 65 64 20 77 68 |tions de|fined wh|
|000032a0| 69 63 68 20 61 63 63 65 | 70 74 20 61 6e 20 41 53 |ich acce|pt an AS|
|000032b0| 54 20 6f 62 6a 65 63 74 | 20 61 73 20 74 68 65 69 |T object| as thei|
|000032c0| 72 0a 66 69 72 73 74 20 | 61 72 67 75 6d 65 6e 74 |r.first |argument|
|000032d0| 20 6d 61 79 20 63 68 61 | 6e 67 65 20 74 6f 20 6f | may cha|nge to o|
|000032e0| 62 6a 65 63 74 20 6d 65 | 74 68 6f 64 73 20 69 6e |bject me|thods in|
|000032f0| 20 74 68 65 20 66 75 74 | 75 72 65 2e 20 20 54 68 | the fut|ure. Th|
|00003300| 65 20 74 79 70 65 0a 6f | 66 20 74 68 65 73 65 20 |e type.o|f these |
|00003310| 6f 62 6a 65 63 74 73 20 | 69 73 20 61 76 61 69 6c |objects |is avail|
|00003320| 61 62 6c 65 20 61 73 20 | 5c 63 6f 64 65 7b 41 53 |able as |\code{AS|
|00003330| 54 54 79 70 65 7d 20 69 | 6e 20 74 68 65 20 6d 6f |TType} i|n the mo|
|00003340| 64 75 6c 65 2e 0a 0a 4f | 72 64 65 72 65 64 20 61 |dule...O|rdered a|
|00003350| 6e 64 20 65 71 75 61 6c | 69 74 79 20 63 6f 6d 70 |nd equal|ity comp|
|00003360| 61 72 69 73 6f 6e 73 20 | 61 72 65 20 73 75 70 70 |arisons |are supp|
|00003370| 6f 72 74 65 64 20 62 65 | 74 77 65 65 6e 20 41 53 |orted be|tween AS|
|00003380| 54 20 6f 62 6a 65 63 74 | 73 2e 0a 0a 0a 5c 73 75 |T object|s....\su|
|00003390| 62 73 65 63 74 69 6f 6e | 7b 45 78 61 6d 70 6c 65 |bsection|{Example|
|000033a0| 73 7d 0a 0a 54 68 65 20 | 70 61 72 73 65 72 20 6d |s}..The |parser m|
|000033b0| 6f 64 75 6c 65 73 20 61 | 6c 6c 6f 77 73 20 6f 70 |odules a|llows op|
|000033c0| 65 72 61 74 69 6f 6e 73 | 20 74 6f 20 62 65 20 70 |erations| to be p|
|000033d0| 65 72 66 6f 72 6d 65 64 | 20 6f 6e 20 74 68 65 20 |erformed| on the |
|000033e0| 70 61 72 73 65 20 74 72 | 65 65 0a 6f 66 20 50 79 |parse tr|ee.of Py|
|000033f0| 74 68 6f 6e 20 73 6f 75 | 72 63 65 20 63 6f 64 65 |thon sou|rce code|
|00003400| 20 62 65 66 6f 72 65 20 | 74 68 65 20 62 79 74 65 | before |the byte|
|00003410| 63 6f 64 65 20 69 73 20 | 67 65 6e 65 72 61 74 65 |code is |generate|
|00003420| 64 2c 20 61 6e 64 20 70 | 72 6f 76 69 64 65 73 0a |d, and p|rovides.|
|00003430| 66 6f 72 20 69 6e 73 70 | 65 63 74 69 6f 6e 20 6f |for insp|ection o|
|00003440| 66 20 74 68 65 20 70 61 | 72 73 65 20 74 72 65 65 |f the pa|rse tree|
|00003450| 20 66 6f 72 20 69 6e 66 | 6f 72 6d 61 74 69 6f 6e | for inf|ormation|
|00003460| 20 67 61 74 68 65 72 69 | 6e 67 20 70 75 72 70 6f | gatheri|ng purpo|
|00003470| 73 65 73 2e 0a 54 77 6f | 20 65 78 61 6d 70 6c 65 |ses..Two| example|
|00003480| 73 20 61 72 65 20 70 72 | 65 73 65 6e 74 65 64 2e |s are pr|esented.|
|00003490| 20 20 54 68 65 20 73 69 | 6d 70 6c 65 20 65 78 61 | The si|mple exa|
|000034a0| 6d 70 6c 65 20 64 65 6d | 6f 6e 73 74 72 61 74 65 |mple dem|onstrate|
|000034b0| 73 20 65 6d 75 6c 61 74 | 69 6f 6e 0a 6f 66 20 74 |s emulat|ion.of t|
|000034c0| 68 65 20 5c 63 6f 64 65 | 7b 63 6f 6d 70 69 6c 65 |he \code|{compile|
|000034d0| 28 29 7d 20 62 75 69 6c | 74 2d 69 6e 20 66 75 6e |()} buil|t-in fun|
|000034e0| 63 74 69 6f 6e 20 61 6e | 64 20 74 68 65 20 63 6f |ction an|d the co|
|000034f0| 6d 70 6c 65 78 20 65 78 | 61 6d 70 6c 65 0a 73 68 |mplex ex|ample.sh|
|00003500| 6f 77 73 20 74 68 65 20 | 75 73 65 20 6f 66 20 61 |ows the |use of a|
|00003510| 20 70 61 72 73 65 20 74 | 72 65 65 20 66 6f 72 20 | parse t|ree for |
|00003520| 69 6e 66 6f 72 6d 61 74 | 69 6f 6e 20 64 69 73 63 |informat|ion disc|
|00003530| 6f 76 65 72 79 2e 0a 0a | 5c 73 75 62 73 75 62 73 |overy...|\subsubs|
|00003540| 65 63 74 69 6f 6e 7b 45 | 6d 75 6c 61 74 69 6f 6e |ection{E|mulation|
|00003550| 20 6f 66 20 5c 73 65 63 | 74 63 6f 64 65 7b 63 6f | of \sec|tcode{co|
|00003560| 6d 70 69 6c 65 28 29 7d | 7d 0a 0a 57 68 69 6c 65 |mpile()}|}..While|
|00003570| 20 6d 61 6e 79 20 75 73 | 65 66 75 6c 20 6f 70 65 | many us|eful ope|
|00003580| 72 61 74 69 6f 6e 73 20 | 6d 61 79 20 74 61 6b 65 |rations |may take|
|00003590| 20 70 6c 61 63 65 20 62 | 65 74 77 65 65 6e 20 70 | place b|etween p|
|000035a0| 61 72 73 69 6e 67 20 61 | 6e 64 0a 62 79 74 65 63 |arsing a|nd.bytec|
|000035b0| 6f 64 65 20 67 65 6e 65 | 72 61 74 69 6f 6e 2c 20 |ode gene|ration, |
|000035c0| 74 68 65 20 73 69 6d 70 | 6c 65 73 74 20 6f 70 65 |the simp|lest ope|
|000035d0| 72 61 74 69 6f 6e 20 69 | 73 20 74 6f 20 64 6f 20 |ration i|s to do |
|000035e0| 6e 6f 74 68 69 6e 67 2e | 20 20 46 6f 72 0a 74 68 |nothing.| For.th|
|000035f0| 69 73 20 70 75 72 70 6f | 73 65 2c 20 75 73 69 6e |is purpo|se, usin|
|00003600| 67 20 74 68 65 20 5c 63 | 6f 64 65 7b 70 61 72 73 |g the \c|ode{pars|
|00003610| 65 72 7d 20 6d 6f 64 75 | 6c 65 20 74 6f 20 70 72 |er} modu|le to pr|
|00003620| 6f 64 75 63 65 20 61 6e | 0a 69 6e 74 65 72 6d 65 |oduce an|.interme|
|00003630| 64 69 61 74 65 20 64 61 | 74 61 20 73 74 72 75 63 |diate da|ta struc|
|00003640| 74 75 72 65 20 69 73 20 | 65 71 75 69 76 65 6c 65 |ture is |equivele|
|00003650| 6e 74 20 74 6f 20 74 68 | 65 20 63 6f 64 65 0a 0a |nt to th|e code..|
|00003660| 5c 62 65 67 69 6e 7b 76 | 65 72 62 61 74 69 6d 7d |\begin{v|erbatim}|
|00003670| 0a 3e 3e 3e 20 63 6f 64 | 65 20 3d 20 63 6f 6d 70 |.>>> cod|e = comp|
|00003680| 69 6c 65 28 27 61 20 2b | 20 35 27 2c 20 27 65 76 |ile('a +| 5', 'ev|
|00003690| 61 6c 27 29 0a 3e 3e 3e | 20 61 20 3d 20 35 0a 3e |al').>>>| a = 5.>|
|000036a0| 3e 3e 20 65 76 61 6c 28 | 63 6f 64 65 29 0a 31 30 |>> eval(|code).10|
|000036b0| 0a 5c 65 6e 64 7b 76 65 | 72 62 61 74 69 6d 7d 0a |.\end{ve|rbatim}.|
|000036c0| 0a 54 68 65 20 65 71 75 | 69 76 65 6c 65 6e 74 20 |.The equ|ivelent |
|000036d0| 6f 70 65 72 61 74 69 6f | 6e 20 75 73 69 6e 67 20 |operatio|n using |
|000036e0| 74 68 65 20 5c 63 6f 64 | 65 7b 70 61 72 73 65 72 |the \cod|e{parser|
|000036f0| 7d 20 6d 6f 64 75 6c 65 | 20 69 73 20 73 6f 6d 65 |} module| is some|
|00003700| 77 68 61 74 0a 6c 6f 6e | 67 65 72 2c 20 61 6e 64 |what.lon|ger, and|
|00003710| 20 61 6c 6c 6f 77 73 20 | 74 68 65 20 69 6e 74 65 | allows |the inte|
|00003720| 72 6d 65 64 69 61 74 65 | 20 69 6e 74 65 72 6e 61 |rmediate| interna|
|00003730| 6c 20 70 61 72 73 65 20 | 74 72 65 65 20 74 6f 20 |l parse |tree to |
|00003740| 62 65 20 72 65 74 61 69 | 6e 65 64 0a 61 73 20 61 |be retai|ned.as a|
|00003750| 6e 20 41 53 54 20 6f 62 | 6a 65 63 74 3a 0a 0a 5c |n AST ob|ject:..\|
|00003760| 62 65 67 69 6e 7b 76 65 | 72 62 61 74 69 6d 7d 0a |begin{ve|rbatim}.|
|00003770| 3e 3e 3e 20 69 6d 70 6f | 72 74 20 70 61 72 73 65 |>>> impo|rt parse|
|00003780| 72 0a 3e 3e 3e 20 61 73 | 74 20 3d 20 70 61 72 73 |r.>>> as|t = pars|
|00003790| 65 72 2e 65 78 70 72 28 | 27 61 20 2b 20 35 27 29 |er.expr(|'a + 5')|
|000037a0| 0a 3e 3e 3e 20 63 6f 64 | 65 20 3d 20 70 61 72 73 |.>>> cod|e = pars|
|000037b0| 65 72 2e 63 6f 6d 70 69 | 6c 65 61 73 74 28 61 73 |er.compi|least(as|
|000037c0| 74 29 0a 3e 3e 3e 20 61 | 20 3d 20 35 0a 3e 3e 3e |t).>>> a| = 5.>>>|
|000037d0| 20 65 76 61 6c 28 63 6f | 64 65 29 0a 31 30 0a 5c | eval(co|de).10.\|
|000037e0| 65 6e 64 7b 76 65 72 62 | 61 74 69 6d 7d 0a 0a 41 |end{verb|atim}..A|
|000037f0| 6e 20 61 70 70 6c 69 63 | 61 74 69 6f 6e 20 77 68 |n applic|ation wh|
|00003800| 69 63 68 20 6e 65 65 64 | 73 20 62 6f 74 68 20 41 |ich need|s both A|
|00003810| 53 54 20 61 6e 64 20 63 | 6f 64 65 20 6f 62 6a 65 |ST and c|ode obje|
|00003820| 63 74 73 20 63 61 6e 20 | 70 61 63 6b 61 67 65 20 |cts can |package |
|00003830| 74 68 69 73 0a 63 6f 64 | 65 20 69 6e 74 6f 20 72 |this.cod|e into r|
|00003840| 65 61 64 69 6c 79 20 61 | 76 61 69 6c 61 62 6c 65 |eadily a|vailable|
|00003850| 20 66 75 6e 63 74 69 6f | 6e 73 3a 0a 0a 5c 62 65 | functio|ns:..\be|
|00003860| 67 69 6e 7b 76 65 72 62 | 61 74 69 6d 7d 0a 69 6d |gin{verb|atim}.im|
|00003870| 70 6f 72 74 20 70 61 72 | 73 65 72 0a 0a 64 65 66 |port par|ser..def|
|00003880| 20 6c 6f 61 64 5f 73 75 | 69 74 65 28 73 6f 75 72 | load_su|ite(sour|
|00003890| 63 65 5f 73 74 72 69 6e | 67 29 3a 0a 20 20 20 20 |ce_strin|g):. |
|000038a0| 61 73 74 20 3d 20 70 61 | 72 73 65 72 2e 73 75 69 |ast = pa|rser.sui|
|000038b0| 74 65 28 73 6f 75 72 63 | 65 5f 73 74 72 69 6e 67 |te(sourc|e_string|
|000038c0| 29 0a 20 20 20 20 63 6f | 64 65 20 3d 20 70 61 72 |). co|de = par|
|000038d0| 73 65 72 2e 63 6f 6d 70 | 69 6c 65 61 73 74 28 61 |ser.comp|ileast(a|
|000038e0| 73 74 29 0a 20 20 20 20 | 72 65 74 75 72 6e 20 61 |st). |return a|
|000038f0| 73 74 2c 20 63 6f 64 65 | 0a 0a 64 65 66 20 6c 6f |st, code|..def lo|
|00003900| 61 64 5f 65 78 70 72 65 | 73 73 69 6f 6e 28 73 6f |ad_expre|ssion(so|
|00003910| 75 72 63 65 5f 73 74 72 | 69 6e 67 29 3a 0a 20 20 |urce_str|ing):. |
|00003920| 20 20 61 73 74 20 3d 20 | 70 61 72 73 65 72 2e 65 | ast = |parser.e|
|00003930| 78 70 72 28 73 6f 75 72 | 63 65 5f 73 74 72 69 6e |xpr(sour|ce_strin|
|00003940| 67 29 0a 20 20 20 20 63 | 6f 64 65 20 3d 20 70 61 |g). c|ode = pa|
|00003950| 72 73 65 72 2e 63 6f 6d | 70 69 6c 65 61 73 74 28 |rser.com|pileast(|
|00003960| 61 73 74 29 0a 20 20 20 | 20 72 65 74 75 72 6e 20 |ast). | return |
|00003970| 61 73 74 2c 20 63 6f 64 | 65 0a 5c 65 6e 64 7b 76 |ast, cod|e.\end{v|
|00003980| 65 72 62 61 74 69 6d 7d | 0a 0a 5c 73 75 62 73 75 |erbatim}|..\subsu|
|00003990| 62 73 65 63 74 69 6f 6e | 7b 49 6e 66 6f 72 6d 61 |bsection|{Informa|
|000039a0| 74 69 6f 6e 20 44 69 73 | 63 6f 76 65 72 79 7d 0a |tion Dis|covery}.|
|000039b0| 0a 53 6f 6d 65 20 61 70 | 70 6c 69 63 61 74 69 6f |.Some ap|plicatio|
|000039c0| 6e 73 20 62 65 6e 65 66 | 69 74 20 66 72 6f 6d 20 |ns benef|it from |
|000039d0| 64 69 72 65 63 74 20 61 | 63 63 65 73 73 20 74 6f |direct a|ccess to|
|000039e0| 20 74 68 65 20 70 61 72 | 73 65 20 74 72 65 65 2e | the par|se tree.|
|000039f0| 20 20 54 68 65 0a 72 65 | 6d 61 69 6e 64 65 72 20 | The.re|mainder |
|00003a00| 6f 66 20 74 68 69 73 20 | 73 65 63 74 69 6f 6e 20 |of this |section |
|00003a10| 64 65 6d 6f 6e 73 74 72 | 61 74 65 73 20 68 6f 77 |demonstr|ates how|
|00003a20| 20 74 68 65 20 70 61 72 | 73 65 20 74 72 65 65 20 | the par|se tree |
|00003a30| 70 72 6f 76 69 64 65 73 | 0a 61 63 63 65 73 73 20 |provides|.access |
|00003a40| 74 6f 20 6d 6f 64 75 6c | 65 20 64 6f 63 75 6d 65 |to modul|e docume|
|00003a50| 6e 74 61 74 69 6f 6e 20 | 64 65 66 69 6e 65 64 20 |ntation |defined |
|00003a60| 69 6e 20 64 6f 63 73 74 | 72 69 6e 67 73 20 77 69 |in docst|rings wi|
|00003a70| 74 68 6f 75 74 20 72 65 | 71 75 69 72 69 6e 67 0a |thout re|quiring.|
|00003a80| 74 68 61 74 20 74 68 65 | 20 63 6f 64 65 20 62 65 |that the| code be|
|00003a90| 69 6e 67 20 65 78 61 6d | 69 6e 65 64 20 62 65 20 |ing exam|ined be |
|00003aa0| 6c 6f 61 64 65 64 20 69 | 6e 74 6f 20 61 20 72 75 |loaded i|nto a ru|
|00003ab0| 6e 6e 69 6e 67 20 69 6e | 74 65 72 70 72 65 74 65 |nning in|terprete|
|00003ac0| 72 20 76 69 61 0a 5c 63 | 6f 64 65 7b 69 6d 70 6f |r via.\c|ode{impo|
|00003ad0| 72 74 7d 2e 20 20 54 68 | 69 73 20 63 61 6e 20 62 |rt}. Th|is can b|
|00003ae0| 65 20 76 65 72 79 20 75 | 73 65 66 75 6c 20 66 6f |e very u|seful fo|
|00003af0| 72 20 70 65 72 66 6f 72 | 6d 69 6e 67 20 61 6e 61 |r perfor|ming ana|
|00003b00| 6c 79 73 65 73 20 6f 66 | 0a 75 6e 74 72 75 73 74 |lyses of|.untrust|
|00003b10| 65 64 20 63 6f 64 65 2e | 0a 0a 47 65 6e 65 72 61 |ed code.|..Genera|
|00003b20| 6c 6c 79 2c 20 74 68 65 | 20 65 78 61 6d 70 6c 65 |lly, the| example|
|00003b30| 20 77 69 6c 6c 20 64 65 | 6d 6f 6e 73 74 72 61 74 | will de|monstrat|
|00003b40| 65 20 68 6f 77 20 74 68 | 65 20 70 61 72 73 65 20 |e how th|e parse |
|00003b50| 74 72 65 65 20 6d 61 79 | 20 62 65 0a 74 72 61 76 |tree may| be.trav|
|00003b60| 65 72 73 65 64 20 74 6f | 20 64 69 73 74 69 6c 6c |ersed to| distill|
|00003b70| 20 69 6e 74 65 72 65 73 | 74 69 6e 67 20 69 6e 66 | interes|ting inf|
|00003b80| 6f 72 6d 61 74 69 6f 6e | 2e 20 20 54 77 6f 20 66 |ormation|. Two f|
|00003b90| 75 6e 63 74 69 6f 6e 73 | 20 61 6e 64 20 61 20 73 |unctions| and a s|
|00003ba0| 65 74 0a 6f 66 20 63 6c | 61 73 73 65 73 20 61 72 |et.of cl|asses ar|
|00003bb0| 65 20 64 65 76 65 6c 6f | 70 65 64 20 77 68 69 63 |e develo|ped whic|
|00003bc0| 68 20 70 72 6f 76 69 64 | 65 20 70 72 6f 67 72 61 |h provid|e progra|
|00003bd0| 6d 6d 61 74 69 63 20 61 | 63 63 65 73 73 20 74 6f |mmatic a|ccess to|
|00003be0| 20 68 69 67 68 0a 6c 65 | 76 65 6c 20 66 75 6e 63 | high.le|vel func|
|00003bf0| 74 69 6f 6e 20 61 6e 64 | 20 63 6c 61 73 73 20 64 |tion and| class d|
|00003c00| 65 66 69 6e 69 74 69 6f | 6e 73 20 70 72 6f 76 69 |efinitio|ns provi|
|00003c10| 64 65 64 20 62 79 20 61 | 20 6d 6f 64 75 6c 65 2e |ded by a| module.|
|00003c20| 20 20 54 68 65 0a 63 6c | 61 73 73 65 73 20 65 78 | The.cl|asses ex|
|00003c30| 74 72 61 63 74 20 69 6e | 66 6f 72 6d 61 74 69 6f |tract in|formatio|
|00003c40| 6e 20 66 72 6f 6d 20 74 | 68 65 20 70 61 72 73 65 |n from t|he parse|
|00003c50| 20 74 72 65 65 20 61 6e | 64 20 70 72 6f 76 69 64 | tree an|d provid|
|00003c60| 65 20 61 63 63 65 73 73 | 20 74 6f 0a 74 68 65 20 |e access| to.the |
|00003c70| 69 6e 66 6f 72 6d 61 74 | 69 6f 6e 20 61 74 20 61 |informat|ion at a|
|00003c80| 20 75 73 65 66 75 6c 20 | 73 65 6d 61 6e 74 69 63 | useful |semantic|
|00003c90| 20 6c 65 76 65 6c 2c 20 | 6f 6e 65 20 66 75 6e 63 | level, |one func|
|00003ca0| 74 69 6f 6e 20 70 72 6f | 76 69 64 65 73 20 61 0a |tion pro|vides a.|
|00003cb0| 73 69 6d 70 6c 65 20 6c | 6f 77 2d 6c 65 76 65 6c |simple l|ow-level|
|00003cc0| 20 70 61 74 74 65 72 6e | 20 6d 61 74 63 68 69 6e | pattern| matchin|
|00003cd0| 67 20 63 61 70 61 62 69 | 6c 69 74 79 2c 20 61 6e |g capabi|lity, an|
|00003ce0| 64 20 74 68 65 20 6f 74 | 68 65 72 20 66 75 6e 63 |d the ot|her func|
|00003cf0| 74 69 6f 6e 0a 64 65 66 | 69 6e 65 73 20 61 20 68 |tion.def|ines a h|
|00003d00| 69 67 68 2d 6c 65 76 65 | 6c 20 69 6e 74 65 72 66 |igh-leve|l interf|
|00003d10| 61 63 65 20 74 6f 20 74 | 68 65 20 63 6c 61 73 73 |ace to t|he class|
|00003d20| 65 73 20 62 79 20 68 61 | 6e 64 6c 69 6e 67 20 66 |es by ha|ndling f|
|00003d30| 69 6c 65 0a 6f 70 65 72 | 61 74 69 6f 6e 73 20 6f |ile.oper|ations o|
|00003d40| 6e 20 62 65 68 61 6c 66 | 20 6f 66 20 74 68 65 20 |n behalf| of the |
|00003d50| 63 61 6c 6c 65 72 2e 20 | 20 41 6c 6c 20 73 6f 75 |caller. | All sou|
|00003d60| 72 63 65 20 66 69 6c 65 | 73 20 6d 65 6e 74 69 6f |rce file|s mentio|
|00003d70| 6e 65 64 20 68 65 72 65 | 0a 77 68 69 63 68 20 61 |ned here|.which a|
|00003d80| 72 65 20 6e 6f 74 20 70 | 61 72 74 20 6f 66 20 74 |re not p|art of t|
|00003d90| 68 65 20 50 79 74 68 6f | 6e 20 69 6e 73 74 61 6c |he Pytho|n instal|
|00003da0| 6c 61 74 69 6f 6e 20 61 | 72 65 20 6c 6f 63 61 74 |lation a|re locat|
|00003db0| 65 64 20 69 6e 20 74 68 | 65 0a 5c 66 69 6c 65 7b |ed in th|e.\file{|
|00003dc0| 44 65 6d 6f 2f 70 61 72 | 73 65 72 2f 7d 20 64 69 |Demo/par|ser/} di|
|00003dd0| 72 65 63 74 6f 72 79 20 | 6f 66 20 74 68 65 20 64 |rectory |of the d|
|00003de0| 69 73 74 72 69 62 75 74 | 69 6f 6e 2e 0a 0a 54 68 |istribut|ion...Th|
|00003df0| 65 20 64 79 6e 61 6d 69 | 63 20 6e 61 74 75 72 65 |e dynami|c nature|
|00003e00| 20 6f 66 20 50 79 74 68 | 6f 6e 20 61 6c 6c 6f 77 | of Pyth|on allow|
|00003e10| 73 20 74 68 65 20 70 72 | 6f 67 72 61 6d 6d 65 72 |s the pr|ogrammer|
|00003e20| 20 61 20 67 72 65 61 74 | 20 64 65 61 6c 20 6f 66 | a great| deal of|
|00003e30| 0a 66 6c 65 78 69 62 69 | 6c 69 74 79 2c 20 62 75 |.flexibi|lity, bu|
|00003e40| 74 20 6d 6f 73 74 20 6d | 6f 64 75 6c 65 73 20 6e |t most m|odules n|
|00003e50| 65 65 64 20 6f 6e 6c 79 | 20 61 20 6c 69 6d 69 74 |eed only| a limit|
|00003e60| 65 64 20 6d 65 61 73 75 | 72 65 20 6f 66 20 74 68 |ed measu|re of th|
|00003e70| 69 73 20 77 68 65 6e 0a | 64 65 66 69 6e 69 6e 67 |is when.|defining|
|00003e80| 20 63 6c 61 73 73 65 73 | 2c 20 66 75 6e 63 74 69 | classes|, functi|
|00003e90| 6f 6e 73 2c 20 61 6e 64 | 20 6d 65 74 68 6f 64 73 |ons, and| methods|
|00003ea0| 2e 20 20 49 6e 20 74 68 | 69 73 20 65 78 61 6d 70 |. In th|is examp|
|00003eb0| 6c 65 2c 20 74 68 65 20 | 6f 6e 6c 79 0a 64 65 66 |le, the |only.def|
|00003ec0| 69 6e 69 74 69 6f 6e 73 | 20 74 68 61 74 20 77 69 |initions| that wi|
|00003ed0| 6c 6c 20 62 65 20 63 6f | 6e 73 69 64 65 72 65 64 |ll be co|nsidered|
|00003ee0| 20 61 72 65 20 74 68 6f | 73 65 20 77 68 69 63 68 | are tho|se which|
|00003ef0| 20 61 72 65 20 64 65 66 | 69 6e 65 64 20 69 6e 20 | are def|ined in |
|00003f00| 74 68 65 0a 74 6f 70 20 | 6c 65 76 65 6c 20 6f 66 |the.top |level of|
|00003f10| 20 74 68 65 69 72 20 63 | 6f 6e 74 65 78 74 2c 20 | their c|ontext, |
|00003f20| 65 2e 67 2e 2c 20 61 20 | 66 75 6e 63 74 69 6f 6e |e.g., a |function|
|00003f30| 20 64 65 66 69 6e 65 64 | 20 62 79 20 61 20 5c 63 | defined| by a \c|
|00003f40| 6f 64 65 7b 64 65 66 7d | 0a 73 74 61 74 65 6d 65 |ode{def}|.stateme|
|00003f50| 6e 74 20 61 74 20 63 6f | 6c 75 6d 6e 20 7a 65 72 |nt at co|lumn zer|
|00003f60| 6f 20 6f 66 20 61 20 6d | 6f 64 75 6c 65 2c 20 62 |o of a m|odule, b|
|00003f70| 75 74 20 6e 6f 74 20 61 | 20 66 75 6e 63 74 69 6f |ut not a| functio|
|00003f80| 6e 20 64 65 66 69 6e 65 | 64 0a 77 69 74 68 69 6e |n define|d.within|
|00003f90| 20 61 20 62 72 61 6e 63 | 68 20 6f 66 20 61 6e 20 | a branc|h of an |
|00003fa0| 5c 63 6f 64 65 7b 69 66 | 7d 20 2e 2e 2e 20 5c 63 |\code{if|} ... \c|
|00003fb0| 6f 64 65 7b 65 6c 73 65 | 7d 20 63 6f 6e 73 74 72 |ode{else|} constr|
|00003fc0| 75 63 74 2c 20 74 68 6f | 75 67 68 0a 74 68 65 72 |uct, tho|ugh.ther|
|00003fd0| 65 20 61 72 65 20 73 6f | 6d 65 20 67 6f 6f 64 20 |e are so|me good |
|00003fe0| 72 65 61 73 6f 6e 73 20 | 66 6f 72 20 64 6f 69 6e |reasons |for doin|
|00003ff0| 67 20 73 6f 20 69 6e 20 | 73 6f 6d 65 20 73 69 74 |g so in |some sit|
|00004000| 75 61 74 69 6f 6e 73 2e | 20 20 4e 65 73 74 69 6e |uations.| Nestin|
|00004010| 67 0a 6f 66 20 64 65 66 | 69 6e 69 74 69 6f 6e 73 |g.of def|initions|
|00004020| 20 77 69 6c 6c 20 62 65 | 20 68 61 6e 64 6c 65 64 | will be| handled|
|00004030| 20 62 79 20 74 68 65 20 | 63 6f 64 65 20 64 65 76 | by the |code dev|
|00004040| 65 6c 6f 70 65 64 20 69 | 6e 20 74 68 65 20 65 78 |eloped i|n the ex|
|00004050| 61 6d 70 6c 65 2e 0a 0a | 54 6f 20 63 6f 6e 73 74 |ample...|To const|
|00004060| 72 75 63 74 20 74 68 65 | 20 75 70 70 65 72 2d 6c |ruct the| upper-l|
|00004070| 65 76 65 6c 20 65 78 74 | 72 61 63 74 69 6f 6e 20 |evel ext|raction |
|00004080| 6d 65 74 68 6f 64 73 2c | 20 77 65 20 6e 65 65 64 |methods,| we need|
|00004090| 20 74 6f 20 6b 6e 6f 77 | 20 77 68 61 74 0a 74 68 | to know| what.th|
|000040a0| 65 20 70 61 72 73 65 20 | 74 72 65 65 20 73 74 72 |e parse |tree str|
|000040b0| 75 63 74 75 72 65 20 6c | 6f 6f 6b 73 20 6c 69 6b |ucture l|ooks lik|
|000040c0| 65 20 61 6e 64 20 68 6f | 77 20 6d 75 63 68 20 6f |e and ho|w much o|
|000040d0| 66 20 69 74 20 77 65 20 | 61 63 74 75 61 6c 6c 79 |f it we |actually|
|000040e0| 0a 6e 65 65 64 20 74 6f | 20 62 65 20 63 6f 6e 63 |.need to| be conc|
|000040f0| 65 72 6e 65 64 20 61 62 | 6f 75 74 2e 20 20 50 79 |erned ab|out. Py|
|00004100| 74 68 6f 6e 20 75 73 65 | 73 20 61 20 6d 6f 64 65 |thon use|s a mode|
|00004110| 72 61 74 65 6c 79 20 64 | 65 65 70 20 70 61 72 73 |rately d|eep pars|
|00004120| 65 20 74 72 65 65 0a 73 | 6f 20 74 68 65 72 65 20 |e tree.s|o there |
|00004130| 61 72 65 20 61 20 6c 61 | 72 67 65 20 6e 75 6d 62 |are a la|rge numb|
|00004140| 65 72 20 6f 66 20 69 6e | 74 65 72 6d 65 64 69 61 |er of in|termedia|
|00004150| 74 65 20 6e 6f 64 65 73 | 2e 20 20 49 74 20 69 73 |te nodes|. It is|
|00004160| 20 69 6d 70 6f 72 74 61 | 6e 74 20 74 6f 0a 72 65 | importa|nt to.re|
|00004170| 61 64 20 61 6e 64 20 75 | 6e 64 65 72 73 74 61 6e |ad and u|nderstan|
|00004180| 64 20 74 68 65 20 66 6f | 72 6d 61 6c 20 67 72 61 |d the fo|rmal gra|
|00004190| 6d 6d 61 72 20 75 73 65 | 64 20 62 79 20 50 79 74 |mmar use|d by Pyt|
|000041a0| 68 6f 6e 2e 20 20 54 68 | 69 73 20 69 73 0a 73 70 |hon. Th|is is.sp|
|000041b0| 65 63 69 66 69 65 64 20 | 69 6e 20 74 68 65 20 66 |ecified |in the f|
|000041c0| 69 6c 65 20 5c 66 69 6c | 65 7b 47 72 61 6d 6d 61 |ile \fil|e{Gramma|
|000041d0| 72 2f 47 72 61 6d 6d 61 | 72 7d 20 69 6e 20 74 68 |r/Gramma|r} in th|
|000041e0| 65 20 64 69 73 74 72 69 | 62 75 74 69 6f 6e 2e 0a |e distri|bution..|
|000041f0| 43 6f 6e 73 69 64 65 72 | 20 74 68 65 20 73 69 6d |Consider| the sim|
|00004200| 70 6c 65 73 74 20 63 61 | 73 65 20 6f 66 20 69 6e |plest ca|se of in|
|00004210| 74 65 72 65 73 74 20 77 | 68 65 6e 20 73 65 61 72 |terest w|hen sear|
|00004220| 63 68 69 6e 67 20 66 6f | 72 20 64 6f 63 73 74 72 |ching fo|r docstr|
|00004230| 69 6e 67 73 3a 0a 61 20 | 6d 6f 64 75 6c 65 20 63 |ings:.a |module c|
|00004240| 6f 6e 73 69 73 74 69 6e | 67 20 6f 66 20 61 20 64 |onsistin|g of a d|
|00004250| 6f 63 73 74 72 69 6e 67 | 20 61 6e 64 20 6e 6f 74 |ocstring| and not|
|00004260| 68 69 6e 67 20 65 6c 73 | 65 2e 20 20 28 53 65 65 |hing els|e. (See|
|00004270| 20 66 69 6c 65 0a 5c 66 | 69 6c 65 7b 64 6f 63 73 | file.\f|ile{docs|
|00004280| 74 72 69 6e 67 2e 70 79 | 7d 2e 29 0a 0a 5c 62 65 |tring.py|}.)..\be|
|00004290| 67 69 6e 7b 76 65 72 62 | 61 74 69 6d 7d 0a 22 22 |gin{verb|atim}.""|
|000042a0| 22 53 6f 6d 65 20 64 6f | 63 75 6d 65 6e 74 61 74 |"Some do|cumentat|
|000042b0| 69 6f 6e 2e 0a 22 22 22 | 0a 5c 65 6e 64 7b 76 65 |ion.."""|.\end{ve|
|000042c0| 72 62 61 74 69 6d 7d 0a | 0a 55 73 69 6e 67 20 74 |rbatim}.|.Using t|
|000042d0| 68 65 20 69 6e 74 65 72 | 70 72 65 74 65 72 20 74 |he inter|preter t|
|000042e0| 6f 20 74 61 6b 65 20 61 | 20 6c 6f 6f 6b 20 61 74 |o take a| look at|
|000042f0| 20 74 68 65 20 70 61 72 | 73 65 20 74 72 65 65 2c | the par|se tree,|
|00004300| 20 77 65 20 66 69 6e 64 | 20 61 0a 62 65 77 69 6c | we find| a.bewil|
|00004310| 64 65 72 69 6e 67 20 6d | 61 73 73 20 6f 66 20 6e |dering m|ass of n|
|00004320| 75 6d 62 65 72 73 20 61 | 6e 64 20 70 61 72 65 6e |umbers a|nd paren|
|00004330| 74 68 65 73 65 73 2c 20 | 77 69 74 68 20 74 68 65 |theses, |with the|
|00004340| 20 64 6f 63 75 6d 65 6e | 74 61 74 69 6f 6e 0a 62 | documen|tation.b|
|00004350| 75 72 69 65 64 20 64 65 | 65 70 20 69 6e 20 6e 65 |uried de|ep in ne|
|00004360| 73 74 65 64 20 74 75 70 | 6c 65 73 2e 0a 0a 5c 62 |sted tup|les...\b|
|00004370| 65 67 69 6e 7b 76 65 72 | 62 61 74 69 6d 7d 0a 3e |egin{ver|batim}.>|
|00004380| 3e 3e 20 69 6d 70 6f 72 | 74 20 70 61 72 73 65 72 |>> impor|t parser|
|00004390| 0a 3e 3e 3e 20 69 6d 70 | 6f 72 74 20 70 70 72 69 |.>>> imp|ort ppri|
|000043a0| 6e 74 0a 3e 3e 3e 20 61 | 73 74 20 3d 20 70 61 72 |nt.>>> a|st = par|
|000043b0| 73 65 72 2e 73 75 69 74 | 65 28 6f 70 65 6e 28 27 |ser.suit|e(open('|
|000043c0| 64 6f 63 73 74 72 69 6e | 67 2e 70 79 27 29 2e 72 |docstrin|g.py').r|
|000043d0| 65 61 64 28 29 29 0a 3e | 3e 3e 20 74 75 70 20 3d |ead()).>|>> tup =|
|000043e0| 20 70 61 72 73 65 72 2e | 61 73 74 32 74 75 70 6c | parser.|ast2tupl|
|000043f0| 65 28 61 73 74 29 0a 3e | 3e 3e 20 70 70 72 69 6e |e(ast).>|>> pprin|
|00004400| 74 2e 70 70 72 69 6e 74 | 28 74 75 70 29 0a 28 32 |t.pprint|(tup).(2|
|00004410| 35 37 2c 0a 20 28 32 36 | 34 2c 0a 20 20 28 32 36 |57,. (26|4,. (26|
|00004420| 35 2c 0a 20 20 20 28 32 | 36 36 2c 0a 20 20 20 20 |5,. (2|66,. |
|00004430| 28 32 36 37 2c 0a 20 20 | 20 20 20 28 33 30 37 2c |(267,. | (307,|
|00004440| 0a 20 20 20 20 20 20 28 | 32 38 37 2c 0a 20 20 20 |. (|287,. |
|00004450| 20 20 20 20 28 32 38 38 | 2c 0a 20 20 20 20 20 20 | (288|,. |
|00004460| 20 20 28 32 38 39 2c 0a | 20 20 20 20 20 20 20 20 | (289,.| |
|00004470| 20 28 32 39 30 2c 0a 20 | 20 20 20 20 20 20 20 20 | (290,. | |
|00004480| 20 28 32 39 32 2c 0a 20 | 20 20 20 20 20 20 20 20 | (292,. | |
|00004490| 20 20 28 32 39 33 2c 0a | 20 20 20 20 20 20 20 20 | (293,.| |
|000044a0| 20 20 20 20 28 32 39 34 | 2c 0a 20 20 20 20 20 20 | (294|,. |
|000044b0| 20 20 20 20 20 20 20 28 | 32 39 35 2c 0a 20 20 20 | (|295,. |
|000044c0| 20 20 20 20 20 20 20 20 | 20 20 20 28 32 39 36 2c | | (296,|
|000044d0| 0a 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 |. | |
|000044e0| 28 32 39 37 2c 0a 20 20 | 20 20 20 20 20 20 20 20 |(297,. | |
|000044f0| 20 20 20 20 20 20 28 32 | 39 38 2c 0a 20 20 20 20 | (2|98,. |
|00004500| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 28 32 39 | | (29|
|00004510| 39 2c 0a 20 20 20 20 20 | 20 20 20 20 20 20 20 20 |9,. | |
|00004520| 20 20 20 20 20 28 33 30 | 30 2c 20 28 33 2c 20 27 | (30|0, (3, '|
|00004530| 22 22 22 53 6f 6d 65 20 | 64 6f 63 75 6d 65 6e 74 |"""Some |document|
|00004540| 61 74 69 6f 6e 2e 5c 30 | 31 32 22 22 22 27 29 29 |ation.\0|12"""'))|
|00004550| 29 29 29 29 29 29 29 29 | 29 29 29 29 29 29 29 2c |))))))))|))))))),|
|00004560| 0a 20 20 20 28 34 2c 20 | 27 27 29 29 29 2c 0a 20 |. (4, |''))),. |
|00004570| 28 34 2c 20 27 27 29 2c | 0a 20 28 30 2c 20 27 27 |(4, ''),|. (0, ''|
|00004580| 29 29 0a 5c 65 6e 64 7b | 76 65 72 62 61 74 69 6d |)).\end{|verbatim|
|00004590| 7d 0a 0a 54 68 65 20 6e | 75 6d 62 65 72 73 20 61 |}..The n|umbers a|
|000045a0| 74 20 74 68 65 20 66 69 | 72 73 74 20 65 6c 65 6d |t the fi|rst elem|
|000045b0| 65 6e 74 20 6f 66 20 65 | 61 63 68 20 6e 6f 64 65 |ent of e|ach node|
|000045c0| 20 69 6e 20 74 68 65 20 | 74 72 65 65 20 61 72 65 | in the |tree are|
|000045d0| 20 74 68 65 20 6e 6f 64 | 65 0a 74 79 70 65 73 3b | the nod|e.types;|
|000045e0| 20 74 68 65 79 20 6d 61 | 70 20 64 69 72 65 63 74 | they ma|p direct|
|000045f0| 6c 79 20 74 6f 20 74 65 | 72 6d 69 6e 61 6c 20 61 |ly to te|rminal a|
|00004600| 6e 64 20 6e 6f 6e 2d 74 | 65 72 6d 69 6e 61 6c 20 |nd non-t|erminal |
|00004610| 73 79 6d 62 6f 6c 73 20 | 69 6e 20 74 68 65 0a 67 |symbols |in the.g|
|00004620| 72 61 6d 6d 61 72 2e 20 | 20 55 6e 66 6f 72 74 75 |rammar. | Unfortu|
|00004630| 6e 61 74 65 6c 79 2c 20 | 74 68 65 79 20 61 72 65 |nately, |they are|
|00004640| 20 72 65 70 72 65 73 65 | 6e 74 65 64 20 61 73 20 | represe|nted as |
|00004650| 69 6e 74 65 67 65 72 73 | 20 69 6e 20 74 68 65 0a |integers| in the.|
|00004660| 69 6e 74 65 72 6e 61 6c | 20 72 65 70 72 65 73 65 |internal| represe|
|00004670| 6e 74 61 74 69 6f 6e 2c | 20 61 6e 64 20 74 68 65 |ntation,| and the|
|00004680| 20 50 79 74 68 6f 6e 20 | 73 74 72 75 63 74 75 72 | Python |structur|
|00004690| 65 73 20 67 65 6e 65 72 | 61 74 65 64 20 64 6f 20 |es gener|ated do |
|000046a0| 6e 6f 74 0a 63 68 61 6e | 67 65 20 74 68 61 74 2e |not.chan|ge that.|
|000046b0| 20 20 48 6f 77 65 76 65 | 72 2c 20 74 68 65 20 5c | Howeve|r, the \|
|000046c0| 63 6f 64 65 7b 73 79 6d | 62 6f 6c 7d 20 61 6e 64 |code{sym|bol} and|
|000046d0| 20 5c 63 6f 64 65 7b 74 | 6f 6b 65 6e 7d 20 6d 6f | \code{t|oken} mo|
|000046e0| 64 75 6c 65 73 0a 70 72 | 6f 76 69 64 65 20 73 79 |dules.pr|ovide sy|
|000046f0| 6d 62 6f 6c 69 63 20 6e | 61 6d 65 73 20 66 6f 72 |mbolic n|ames for|
|00004700| 20 74 68 65 20 6e 6f 64 | 65 20 74 79 70 65 73 20 | the nod|e types |
|00004710| 61 6e 64 20 64 69 63 74 | 69 6f 6e 61 72 69 65 73 |and dict|ionaries|
|00004720| 20 77 68 69 63 68 20 6d | 61 70 0a 66 72 6f 6d 20 | which m|ap.from |
|00004730| 74 68 65 20 69 6e 74 65 | 67 65 72 73 20 74 6f 20 |the inte|gers to |
|00004740| 74 68 65 20 73 79 6d 62 | 6f 6c 69 63 20 6e 61 6d |the symb|olic nam|
|00004750| 65 73 20 66 6f 72 20 74 | 68 65 20 6e 6f 64 65 20 |es for t|he node |
|00004760| 74 79 70 65 73 2e 0a 0a | 49 6e 20 74 68 65 20 6f |types...|In the o|
|00004770| 75 74 70 75 74 20 70 72 | 65 73 65 6e 74 65 64 20 |utput pr|esented |
|00004780| 61 62 6f 76 65 2c 20 74 | 68 65 20 6f 75 74 65 72 |above, t|he outer|
|00004790| 6d 6f 73 74 20 74 75 70 | 6c 65 20 63 6f 6e 74 61 |most tup|le conta|
|000047a0| 69 6e 73 20 66 6f 75 72 | 0a 65 6c 65 6d 65 6e 74 |ins four|.element|
|000047b0| 73 3a 20 74 68 65 20 69 | 6e 74 65 67 65 72 20 5c |s: the i|nteger \|
|000047c0| 63 6f 64 65 7b 32 35 37 | 7d 20 61 6e 64 20 74 68 |code{257|} and th|
|000047d0| 72 65 65 20 61 64 64 69 | 74 69 6f 6e 61 6c 20 74 |ree addi|tional t|
|000047e0| 75 70 6c 65 73 2e 20 20 | 4e 6f 64 65 0a 74 79 70 |uples. |Node.typ|
|000047f0| 65 20 5c 63 6f 64 65 7b | 32 35 37 7d 20 68 61 73 |e \code{|257} has|
|00004800| 20 74 68 65 20 73 79 6d | 62 6f 6c 69 63 20 6e 61 | the sym|bolic na|
|00004810| 6d 65 20 5c 63 6f 64 65 | 7b 66 69 6c 65 5f 69 6e |me \code|{file_in|
|00004820| 70 75 74 7d 2e 20 20 45 | 61 63 68 20 6f 66 0a 74 |put}. E|ach of.t|
|00004830| 68 65 73 65 20 69 6e 6e | 65 72 20 74 75 70 6c 65 |hese inn|er tuple|
|00004840| 73 20 63 6f 6e 74 61 69 | 6e 73 20 61 6e 20 69 6e |s contai|ns an in|
|00004850| 74 65 67 65 72 20 61 73 | 20 74 68 65 20 66 69 72 |teger as| the fir|
|00004860| 73 74 20 65 6c 65 6d 65 | 6e 74 3b 20 74 68 65 73 |st eleme|nt; thes|
|00004870| 65 0a 69 6e 74 65 67 65 | 72 73 2c 20 5c 63 6f 64 |e.intege|rs, \cod|
|00004880| 65 7b 32 36 34 7d 2c 20 | 5c 63 6f 64 65 7b 34 7d |e{264}, |\code{4}|
|00004890| 2c 20 61 6e 64 20 5c 63 | 6f 64 65 7b 30 7d 2c 20 |, and \c|ode{0}, |
|000048a0| 72 65 70 72 65 73 65 6e | 74 20 74 68 65 20 6e 6f |represen|t the no|
|000048b0| 64 65 20 74 79 70 65 73 | 0a 5c 63 6f 64 65 7b 73 |de types|.\code{s|
|000048c0| 74 6d 74 7d 2c 20 5c 63 | 6f 64 65 7b 4e 45 57 4c |tmt}, \c|ode{NEWL|
|000048d0| 49 4e 45 7d 2c 20 61 6e | 64 20 5c 63 6f 64 65 7b |INE}, an|d \code{|
|000048e0| 45 4e 44 4d 41 52 4b 45 | 52 7d 2c 20 72 65 73 70 |ENDMARKE|R}, resp|
|000048f0| 65 63 74 69 76 65 6c 79 | 2e 0a 4e 6f 74 65 20 74 |ectively|..Note t|
|00004900| 68 61 74 20 74 68 65 73 | 65 20 76 61 6c 75 65 73 |hat thes|e values|
|00004910| 20 6d 61 79 20 63 68 61 | 6e 67 65 20 64 65 70 65 | may cha|nge depe|
|00004920| 6e 64 69 6e 67 20 6f 6e | 20 74 68 65 20 76 65 72 |nding on| the ver|
|00004930| 73 69 6f 6e 20 6f 66 20 | 50 79 74 68 6f 6e 0a 79 |sion of |Python.y|
|00004940| 6f 75 20 61 72 65 20 75 | 73 69 6e 67 3b 20 63 6f |ou are u|sing; co|
|00004950| 6e 73 75 6c 74 20 5c 66 | 69 6c 65 7b 73 79 6d 62 |nsult \f|ile{symb|
|00004960| 6f 6c 2e 70 79 7d 20 61 | 6e 64 20 5c 66 69 6c 65 |ol.py} a|nd \file|
|00004970| 7b 74 6f 6b 65 6e 2e 70 | 79 7d 20 66 6f 72 0a 64 |{token.p|y} for.d|
|00004980| 65 74 61 69 6c 73 20 6f | 66 20 74 68 65 20 6d 61 |etails o|f the ma|
|00004990| 70 70 69 6e 67 2e 20 20 | 49 74 20 73 68 6f 75 6c |pping. |It shoul|
|000049a0| 64 20 62 65 20 66 61 69 | 72 6c 79 20 63 6c 65 61 |d be fai|rly clea|
|000049b0| 72 20 74 68 61 74 20 74 | 68 65 20 6f 75 74 65 72 |r that t|he outer|
|000049c0| 6d 6f 73 74 0a 6e 6f 64 | 65 20 69 73 20 72 65 6c |most.nod|e is rel|
|000049d0| 61 74 65 64 20 70 72 69 | 6d 61 72 69 6c 79 20 74 |ated pri|marily t|
|000049e0| 6f 20 74 68 65 20 69 6e | 70 75 74 20 73 6f 75 72 |o the in|put sour|
|000049f0| 63 65 20 72 61 74 68 65 | 72 20 74 68 61 6e 20 74 |ce rathe|r than t|
|00004a00| 68 65 20 63 6f 6e 74 65 | 6e 74 73 0a 6f 66 20 74 |he conte|nts.of t|
|00004a10| 68 65 20 66 69 6c 65 2c | 20 61 6e 64 20 6d 61 79 |he file,| and may|
|00004a20| 20 62 65 20 64 69 73 72 | 65 67 61 72 64 65 64 20 | be disr|egarded |
|00004a30| 66 6f 72 20 74 68 65 20 | 6d 6f 6d 65 6e 74 2e 20 |for the |moment. |
|00004a40| 20 54 68 65 20 5c 63 6f | 64 65 7b 73 74 6d 74 7d | The \co|de{stmt}|
|00004a50| 0a 6e 6f 64 65 20 69 73 | 20 6d 75 63 68 20 6d 6f |.node is| much mo|
|00004a60| 72 65 20 69 6e 74 65 72 | 65 73 74 69 6e 67 2e 20 |re inter|esting. |
|00004a70| 20 49 6e 20 70 61 72 74 | 69 63 75 6c 61 72 2c 20 | In part|icular, |
|00004a80| 61 6c 6c 20 64 6f 63 73 | 74 72 69 6e 67 73 20 61 |all docs|trings a|
|00004a90| 72 65 0a 66 6f 75 6e 64 | 20 69 6e 20 73 75 62 74 |re.found| in subt|
|00004aa0| 72 65 65 73 20 77 68 69 | 63 68 20 61 72 65 20 66 |rees whi|ch are f|
|00004ab0| 6f 72 6d 65 64 20 65 78 | 61 63 74 6c 79 20 61 73 |ormed ex|actly as|
|00004ac0| 20 74 68 69 73 20 6e 6f | 64 65 20 69 73 20 66 6f | this no|de is fo|
|00004ad0| 72 6d 65 64 2c 0a 77 69 | 74 68 20 74 68 65 20 6f |rmed,.wi|th the o|
|00004ae0| 6e 6c 79 20 64 69 66 66 | 65 72 65 6e 63 65 20 62 |nly diff|erence b|
|00004af0| 65 69 6e 67 20 74 68 65 | 20 73 74 72 69 6e 67 20 |eing the| string |
|00004b00| 69 74 73 65 6c 66 2e 20 | 20 54 68 65 20 61 73 73 |itself. | The ass|
|00004b10| 6f 63 69 61 74 69 6f 6e | 0a 62 65 74 77 65 65 6e |ociation|.between|
|00004b20| 20 74 68 65 20 64 6f 63 | 73 74 72 69 6e 67 20 69 | the doc|string i|
|00004b30| 6e 20 61 20 73 69 6d 69 | 6c 61 72 20 74 72 65 65 |n a simi|lar tree|
|00004b40| 20 61 6e 64 20 74 68 65 | 20 64 65 66 69 6e 65 64 | and the| defined|
|00004b50| 20 65 6e 74 69 74 79 20 | 28 63 6c 61 73 73 2c 0a | entity |(class,.|
|00004b60| 66 75 6e 63 74 69 6f 6e | 2c 20 6f 72 20 6d 6f 64 |function|, or mod|
|00004b70| 75 6c 65 29 20 77 68 69 | 63 68 20 69 74 20 64 65 |ule) whi|ch it de|
|00004b80| 73 63 72 69 62 65 73 20 | 69 73 20 67 69 76 65 6e |scribes |is given|
|00004b90| 20 62 79 20 74 68 65 20 | 70 6f 73 69 74 69 6f 6e | by the |position|
|00004ba0| 20 6f 66 0a 74 68 65 20 | 64 6f 63 73 74 72 69 6e | of.the |docstrin|
|00004bb0| 67 20 73 75 62 74 72 65 | 65 20 77 69 74 68 69 6e |g subtre|e within|
|00004bc0| 20 74 68 65 20 74 72 65 | 65 20 64 65 66 69 6e 69 | the tre|e defini|
|00004bd0| 6e 67 20 74 68 65 20 64 | 65 73 63 72 69 62 65 64 |ng the d|escribed|
|00004be0| 0a 73 74 72 75 63 74 75 | 72 65 2e 0a 0a 42 79 20 |.structu|re...By |
|00004bf0| 72 65 70 6c 61 63 69 6e | 67 20 74 68 65 20 61 63 |replacin|g the ac|
|00004c00| 74 75 61 6c 20 64 6f 63 | 73 74 72 69 6e 67 20 77 |tual doc|string w|
|00004c10| 69 74 68 20 73 6f 6d 65 | 74 68 69 6e 67 20 74 6f |ith some|thing to|
|00004c20| 20 73 69 67 6e 69 66 79 | 20 61 20 76 61 72 69 61 | signify| a varia|
|00004c30| 62 6c 65 0a 63 6f 6d 70 | 6f 6e 65 6e 74 20 6f 66 |ble.comp|onent of|
|00004c40| 20 74 68 65 20 74 72 65 | 65 2c 20 77 65 20 61 6c | the tre|e, we al|
|00004c50| 6c 6f 77 20 61 20 73 69 | 6d 70 6c 65 20 70 61 74 |low a si|mple pat|
|00004c60| 74 65 72 6e 20 6d 61 74 | 63 68 69 6e 67 20 61 70 |tern mat|ching ap|
|00004c70| 70 72 6f 61 63 68 20 74 | 6f 0a 63 68 65 63 6b 20 |proach t|o.check |
|00004c80| 61 6e 79 20 67 69 76 65 | 6e 20 73 75 62 74 72 65 |any give|n subtre|
|00004c90| 65 20 66 6f 72 20 65 71 | 75 69 76 65 6c 65 6e 63 |e for eq|uivelenc|
|00004ca0| 65 20 74 6f 20 74 68 65 | 20 67 65 6e 65 72 61 6c |e to the| general|
|00004cb0| 20 70 61 74 74 65 72 6e | 20 66 6f 72 0a 64 6f 63 | pattern| for.doc|
|00004cc0| 73 74 72 69 6e 67 73 2e | 20 20 53 69 6e 63 65 20 |strings.| Since |
|00004cd0| 74 68 65 20 65 78 61 6d | 70 6c 65 20 64 65 6d 6f |the exam|ple demo|
|00004ce0| 6e 73 74 72 61 74 65 73 | 20 69 6e 66 6f 72 6d 61 |nstrates| informa|
|00004cf0| 74 69 6f 6e 20 65 78 74 | 72 61 63 74 69 6f 6e 2c |tion ext|raction,|
|00004d00| 20 77 65 0a 63 61 6e 20 | 73 61 66 65 6c 79 20 72 | we.can |safely r|
|00004d10| 65 71 75 69 72 65 20 74 | 68 61 74 20 74 68 65 20 |equire t|hat the |
|00004d20| 74 72 65 65 20 62 65 20 | 69 6e 20 74 75 70 6c 65 |tree be |in tuple|
|00004d30| 20 66 6f 72 6d 20 72 61 | 74 68 65 72 20 74 68 61 | form ra|ther tha|
|00004d40| 6e 20 6c 69 73 74 0a 66 | 6f 72 6d 2c 20 61 6c 6c |n list.f|orm, all|
|00004d50| 6f 77 69 6e 67 20 61 20 | 73 69 6d 70 6c 65 20 76 |owing a |simple v|
|00004d60| 61 72 69 61 62 6c 65 20 | 72 65 70 72 65 73 65 6e |ariable |represen|
|00004d70| 74 61 74 69 6f 6e 20 74 | 6f 20 62 65 0a 5c 63 6f |tation t|o be.\co|
|00004d80| 64 65 7b 5b 27 76 61 72 | 69 61 62 6c 65 5f 6e 61 |de{['var|iable_na|
|00004d90| 6d 65 27 5d 7d 2e 20 20 | 41 20 73 69 6d 70 6c 65 |me']}. |A simple|
|00004da0| 20 72 65 63 75 72 73 69 | 76 65 20 66 75 6e 63 74 | recursi|ve funct|
|00004db0| 69 6f 6e 20 63 61 6e 20 | 69 6d 70 6c 65 6d 65 6e |ion can |implemen|
|00004dc0| 74 0a 74 68 65 20 70 61 | 74 74 65 72 6e 20 6d 61 |t.the pa|ttern ma|
|00004dd0| 74 63 68 69 6e 67 2c 20 | 72 65 74 75 72 6e 69 6e |tching, |returnin|
|00004de0| 67 20 61 20 62 6f 6f 6c | 65 61 6e 20 61 6e 64 20 |g a bool|ean and |
|00004df0| 61 20 64 69 63 74 69 6f | 6e 61 72 79 20 6f 66 20 |a dictio|nary of |
|00004e00| 76 61 72 69 61 62 6c 65 | 0a 6e 61 6d 65 20 74 6f |variable|.name to|
|00004e10| 20 76 61 6c 75 65 20 6d | 61 70 70 69 6e 67 73 2e | value m|appings.|
|00004e20| 20 20 28 53 65 65 20 66 | 69 6c 65 20 5c 66 69 6c | (See f|ile \fil|
|00004e30| 65 7b 65 78 61 6d 70 6c | 65 2e 70 79 7d 2e 29 0a |e{exampl|e.py}.).|
|00004e40| 0a 5c 62 65 67 69 6e 7b | 76 65 72 62 61 74 69 6d |.\begin{|verbatim|
|00004e50| 7d 0a 66 72 6f 6d 20 74 | 79 70 65 73 20 69 6d 70 |}.from t|ypes imp|
|00004e60| 6f 72 74 20 4c 69 73 74 | 54 79 70 65 2c 20 54 75 |ort List|Type, Tu|
|00004e70| 70 6c 65 54 79 70 65 0a | 0a 64 65 66 20 6d 61 74 |pleType.|.def mat|
|00004e80| 63 68 28 70 61 74 74 65 | 72 6e 2c 20 64 61 74 61 |ch(patte|rn, data|
|00004e90| 2c 20 76 61 72 73 3d 4e | 6f 6e 65 29 3a 0a 20 20 |, vars=N|one):. |
|00004ea0| 20 20 69 66 20 76 61 72 | 73 20 69 73 20 4e 6f 6e | if var|s is Non|
|00004eb0| 65 3a 0a 20 20 20 20 20 | 20 20 20 76 61 72 73 20 |e:. | vars |
|00004ec0| 3d 20 7b 7d 0a 20 20 20 | 20 69 66 20 74 79 70 65 |= {}. | if type|
|00004ed0| 28 70 61 74 74 65 72 6e | 29 20 69 73 20 4c 69 73 |(pattern|) is Lis|
|00004ee0| 74 54 79 70 65 3a 0a 20 | 20 20 20 20 20 20 20 76 |tType:. | v|
|00004ef0| 61 72 73 5b 70 61 74 74 | 65 72 6e 5b 30 5d 5d 20 |ars[patt|ern[0]] |
|00004f00| 3d 20 64 61 74 61 0a 20 | 20 20 20 20 20 20 20 72 |= data. | r|
|00004f10| 65 74 75 72 6e 20 31 2c | 20 76 61 72 73 0a 20 20 |eturn 1,| vars. |
|00004f20| 20 20 69 66 20 74 79 70 | 65 28 70 61 74 74 65 72 | if typ|e(patter|
|00004f30| 6e 29 20 69 73 20 6e 6f | 74 20 54 75 70 6c 65 54 |n) is no|t TupleT|
|00004f40| 79 70 65 3a 0a 20 20 20 | 20 20 20 20 20 72 65 74 |ype:. | ret|
|00004f50| 75 72 6e 20 28 70 61 74 | 74 65 72 6e 20 3d 3d 20 |urn (pat|tern == |
|00004f60| 64 61 74 61 29 2c 20 76 | 61 72 73 0a 20 20 20 20 |data), v|ars. |
|00004f70| 69 66 20 6c 65 6e 28 64 | 61 74 61 29 20 21 3d 20 |if len(d|ata) != |
|00004f80| 6c 65 6e 28 70 61 74 74 | 65 72 6e 29 3a 0a 20 20 |len(patt|ern):. |
|00004f90| 20 20 20 20 20 20 72 65 | 74 75 72 6e 20 30 2c 20 | re|turn 0, |
|00004fa0| 76 61 72 73 0a 20 20 20 | 20 66 6f 72 20 70 61 74 |vars. | for pat|
|00004fb0| 74 65 72 6e 2c 20 64 61 | 74 61 20 69 6e 20 6d 61 |tern, da|ta in ma|
|00004fc0| 70 28 4e 6f 6e 65 2c 20 | 70 61 74 74 65 72 6e 2c |p(None, |pattern,|
|00004fd0| 20 64 61 74 61 29 3a 0a | 20 20 20 20 20 20 20 20 | data):.| |
|00004fe0| 73 61 6d 65 2c 20 76 61 | 72 73 20 3d 20 6d 61 74 |same, va|rs = mat|
|00004ff0| 63 68 28 70 61 74 74 65 | 72 6e 2c 20 64 61 74 61 |ch(patte|rn, data|
|00005000| 2c 20 76 61 72 73 29 0a | 20 20 20 20 20 20 20 20 |, vars).| |
|00005010| 69 66 20 6e 6f 74 20 73 | 61 6d 65 3a 0a 20 20 20 |if not s|ame:. |
|00005020| 20 20 20 20 20 20 20 20 | 20 62 72 65 61 6b 0a 20 | | break. |
|00005030| 20 20 20 72 65 74 75 72 | 6e 20 73 61 6d 65 2c 20 | retur|n same, |
|00005040| 76 61 72 73 0a 5c 65 6e | 64 7b 76 65 72 62 61 74 |vars.\en|d{verbat|
|00005050| 69 6d 7d 0a 0a 55 73 69 | 6e 67 20 74 68 69 73 20 |im}..Usi|ng this |
|00005060| 73 69 6d 70 6c 65 20 72 | 65 70 72 65 73 65 6e 74 |simple r|epresent|
|00005070| 61 74 69 6f 6e 20 66 6f | 72 20 73 79 6e 74 61 63 |ation fo|r syntac|
|00005080| 74 69 63 20 76 61 72 69 | 61 62 6c 65 73 20 61 6e |tic vari|ables an|
|00005090| 64 20 74 68 65 20 73 79 | 6d 62 6f 6c 69 63 0a 6e |d the sy|mbolic.n|
|000050a0| 6f 64 65 20 74 79 70 65 | 73 2c 20 74 68 65 20 70 |ode type|s, the p|
|000050b0| 61 74 74 65 72 6e 20 66 | 6f 72 20 74 68 65 20 63 |attern f|or the c|
|000050c0| 61 6e 64 69 64 61 74 65 | 20 64 6f 63 73 74 72 69 |andidate| docstri|
|000050d0| 6e 67 20 73 75 62 74 72 | 65 65 73 20 62 65 63 6f |ng subtr|ees beco|
|000050e0| 6d 65 73 0a 66 61 69 72 | 6c 79 20 72 65 61 64 61 |mes.fair|ly reada|
|000050f0| 62 6c 65 2e 20 20 28 53 | 65 65 20 66 69 6c 65 20 |ble. (S|ee file |
|00005100| 5c 66 69 6c 65 7b 65 78 | 61 6d 70 6c 65 2e 70 79 |\file{ex|ample.py|
|00005110| 7d 2e 29 0a 0a 5c 62 65 | 67 69 6e 7b 76 65 72 62 |}.)..\be|gin{verb|
|00005120| 61 74 69 6d 7d 0a 69 6d | 70 6f 72 74 20 73 79 6d |atim}.im|port sym|
|00005130| 62 6f 6c 0a 69 6d 70 6f | 72 74 20 74 6f 6b 65 6e |bol.impo|rt token|
|00005140| 0a 0a 44 4f 43 53 54 52 | 49 4e 47 5f 53 54 4d 54 |..DOCSTR|ING_STMT|
|00005150| 5f 50 41 54 54 45 52 4e | 20 3d 20 28 0a 20 20 20 |_PATTERN| = (. |
|00005160| 20 73 79 6d 62 6f 6c 2e | 73 74 6d 74 2c 0a 20 20 | symbol.|stmt,. |
|00005170| 20 20 28 73 79 6d 62 6f | 6c 2e 73 69 6d 70 6c 65 | (symbo|l.simple|
|00005180| 5f 73 74 6d 74 2c 0a 20 | 20 20 20 20 28 73 79 6d |_stmt,. | (sym|
|00005190| 62 6f 6c 2e 73 6d 61 6c | 6c 5f 73 74 6d 74 2c 0a |bol.smal|l_stmt,.|
|000051a0| 20 20 20 20 20 20 28 73 | 79 6d 62 6f 6c 2e 65 78 | (s|ymbol.ex|
|000051b0| 70 72 5f 73 74 6d 74 2c | 0a 20 20 20 20 20 20 20 |pr_stmt,|. |
|000051c0| 28 73 79 6d 62 6f 6c 2e | 74 65 73 74 6c 69 73 74 |(symbol.|testlist|
|000051d0| 2c 0a 20 20 20 20 20 20 | 20 20 28 73 79 6d 62 6f |,. | (symbo|
|000051e0| 6c 2e 74 65 73 74 2c 0a | 20 20 20 20 20 20 20 20 |l.test,.| |
|000051f0| 20 28 73 79 6d 62 6f 6c | 2e 61 6e 64 5f 74 65 73 | (symbol|.and_tes|
|00005200| 74 2c 0a 20 20 20 20 20 | 20 20 20 20 20 28 73 79 |t,. | (sy|
|00005210| 6d 62 6f 6c 2e 6e 6f 74 | 5f 74 65 73 74 2c 0a 20 |mbol.not|_test,. |
|00005220| 20 20 20 20 20 20 20 20 | 20 20 28 73 79 6d 62 6f | | (symbo|
|00005230| 6c 2e 63 6f 6d 70 61 72 | 69 73 6f 6e 2c 0a 20 20 |l.compar|ison,. |
|00005240| 20 20 20 20 20 20 20 20 | 20 20 28 73 79 6d 62 6f | | (symbo|
|00005250| 6c 2e 65 78 70 72 2c 0a | 20 20 20 20 20 20 20 20 |l.expr,.| |
|00005260| 20 20 20 20 20 28 73 79 | 6d 62 6f 6c 2e 78 6f 72 | (sy|mbol.xor|
|00005270| 5f 65 78 70 72 2c 0a 20 | 20 20 20 20 20 20 20 20 |_expr,. | |
|00005280| 20 20 20 20 20 28 73 79 | 6d 62 6f 6c 2e 61 6e 64 | (sy|mbol.and|
|00005290| 5f 65 78 70 72 2c 0a 20 | 20 20 20 20 20 20 20 20 |_expr,. | |
|000052a0| 20 20 20 20 20 20 28 73 | 79 6d 62 6f 6c 2e 73 68 | (s|ymbol.sh|
|000052b0| 69 66 74 5f 65 78 70 72 | 2c 0a 20 20 20 20 20 20 |ift_expr|,. |
|000052c0| 20 20 20 20 20 20 20 20 | 20 20 28 73 79 6d 62 6f | | (symbo|
|000052d0| 6c 2e 61 72 69 74 68 5f | 65 78 70 72 2c 0a 20 20 |l.arith_|expr,. |
|000052e0| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 28 | | (|
|000052f0| 73 79 6d 62 6f 6c 2e 74 | 65 72 6d 2c 0a 20 20 20 |symbol.t|erm,. |
|00005300| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 28 | | (|
|00005310| 73 79 6d 62 6f 6c 2e 66 | 61 63 74 6f 72 2c 0a 20 |symbol.f|actor,. |
|00005320| 20 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 | | |
|00005330| 20 20 28 73 79 6d 62 6f | 6c 2e 70 6f 77 65 72 2c | (symbo|l.power,|
|00005340| 0a 20 20 20 20 20 20 20 | 20 20 20 20 20 20 20 20 |. | |
|00005350| 20 20 20 20 20 28 73 79 | 6d 62 6f 6c 2e 61 74 6f | (sy|mbol.ato|
|00005360| 6d 2c 0a 20 20 20 20 20 | 20 20 20 20 20 20 20 20 |m,. | |
|00005370| 20 20 20 20 20 20 20 20 | 28 74 6f 6b 65 6e 2e 53 | |(token.S|
|00005380| 54 52 49 4e 47 2c 20 5b | 27 64 6f 63 73 74 72 69 |TRING, [|'docstri|
|00005390| 6e 67 27 5d 29 0a 20 20 | 20 20 20 20 20 20 20 20 |ng']). | |
|000053a0| 20 20 20 20 20 20 20 20 | 20 20 20 29 29 29 29 29 | | )))))|
|000053b0| 29 29 29 29 29 29 29 29 | 29 29 29 2c 0a 20 20 20 |))))))))|))),. |
|000053c0| 20 20 28 74 6f 6b 65 6e | 2e 4e 45 57 4c 49 4e 45 | (token|.NEWLINE|
|000053d0| 2c 20 27 27 29 0a 20 20 | 20 20 20 29 29 0a 5c 65 |, ''). | )).\e|
|000053e0| 6e 64 7b 76 65 72 62 61 | 74 69 6d 7d 0a 0a 55 73 |nd{verba|tim}..Us|
|000053f0| 69 6e 67 20 74 68 65 20 | 5c 63 6f 64 65 7b 6d 61 |ing the |\code{ma|
|00005400| 74 63 68 28 29 7d 20 66 | 75 6e 63 74 69 6f 6e 20 |tch()} f|unction |
|00005410| 77 69 74 68 20 74 68 69 | 73 20 70 61 74 74 65 72 |with thi|s patter|
|00005420| 6e 2c 20 65 78 74 72 61 | 63 74 69 6e 67 20 74 68 |n, extra|cting th|
|00005430| 65 0a 6d 6f 64 75 6c 65 | 20 64 6f 63 73 74 72 69 |e.module| docstri|
|00005440| 6e 67 20 66 72 6f 6d 20 | 74 68 65 20 70 61 72 73 |ng from |the pars|
|00005450| 65 20 74 72 65 65 20 63 | 72 65 61 74 65 64 20 70 |e tree c|reated p|
|00005460| 72 65 76 69 6f 75 73 6c | 79 20 69 73 20 65 61 73 |reviousl|y is eas|
|00005470| 79 3a 0a 0a 5c 62 65 67 | 69 6e 7b 76 65 72 62 61 |y:..\beg|in{verba|
|00005480| 74 69 6d 7d 0a 3e 3e 3e | 20 66 6f 75 6e 64 2c 20 |tim}.>>>| found, |
|00005490| 76 61 72 73 20 3d 20 6d | 61 74 63 68 28 44 4f 43 |vars = m|atch(DOC|
|000054a0| 53 54 52 49 4e 47 5f 53 | 54 4d 54 5f 50 41 54 54 |STRING_S|TMT_PATT|
|000054b0| 45 52 4e 2c 20 74 75 70 | 5b 31 5d 29 0a 3e 3e 3e |ERN, tup|[1]).>>>|
|000054c0| 20 66 6f 75 6e 64 0a 31 | 0a 3e 3e 3e 20 76 61 72 | found.1|.>>> var|
|000054d0| 73 0a 7b 27 64 6f 63 73 | 74 72 69 6e 67 27 3a 20 |s.{'docs|tring': |
|000054e0| 27 22 22 22 53 6f 6d 65 | 20 64 6f 63 75 6d 65 6e |'"""Some| documen|
|000054f0| 74 61 74 69 6f 6e 2e 5c | 30 31 32 22 22 22 27 7d |tation.\|012"""'}|
|00005500| 0a 5c 65 6e 64 7b 76 65 | 72 62 61 74 69 6d 7d 0a |.\end{ve|rbatim}.|
|00005510| 0a 4f 6e 63 65 20 73 70 | 65 63 69 66 69 63 20 64 |.Once sp|ecific d|
|00005520| 61 74 61 20 63 61 6e 20 | 62 65 20 65 78 74 72 61 |ata can |be extra|
|00005530| 63 74 65 64 20 66 72 6f | 6d 20 61 20 6c 6f 63 61 |cted fro|m a loca|
|00005540| 74 69 6f 6e 20 77 68 65 | 72 65 20 69 74 20 69 73 |tion whe|re it is|
|00005550| 0a 65 78 70 65 63 74 65 | 64 2c 20 74 68 65 20 71 |.expecte|d, the q|
|00005560| 75 65 73 74 69 6f 6e 20 | 6f 66 20 77 68 65 72 65 |uestion |of where|
|00005570| 20 69 6e 66 6f 72 6d 61 | 74 69 6f 6e 20 63 61 6e | informa|tion can|
|00005580| 20 62 65 20 65 78 70 65 | 63 74 65 64 0a 6e 65 65 | be expe|cted.nee|
|00005590| 64 73 20 74 6f 20 62 65 | 20 61 6e 73 77 65 72 65 |ds to be| answere|
|000055a0| 64 2e 20 20 57 68 65 6e | 20 64 65 61 6c 69 6e 67 |d. When| dealing|
|000055b0| 20 77 69 74 68 20 64 6f | 63 73 74 72 69 6e 67 73 | with do|cstrings|
|000055c0| 2c 20 74 68 65 20 61 6e | 73 77 65 72 20 69 73 0a |, the an|swer is.|
|000055d0| 66 61 69 72 6c 79 20 73 | 69 6d 70 6c 65 3a 20 74 |fairly s|imple: t|
|000055e0| 68 65 20 64 6f 63 73 74 | 72 69 6e 67 20 69 73 20 |he docst|ring is |
|000055f0| 74 68 65 20 66 69 72 73 | 74 20 5c 63 6f 64 65 7b |the firs|t \code{|
|00005600| 73 74 6d 74 7d 20 6e 6f | 64 65 20 69 6e 20 61 20 |stmt} no|de in a |
|00005610| 63 6f 64 65 0a 62 6c 6f | 63 6b 20 28 5c 63 6f 64 |code.blo|ck (\cod|
|00005620| 65 7b 66 69 6c 65 5f 69 | 6e 70 75 74 7d 20 6f 72 |e{file_i|nput} or|
|00005630| 20 5c 63 6f 64 65 7b 73 | 75 69 74 65 7d 20 6e 6f | \code{s|uite} no|
|00005640| 64 65 20 74 79 70 65 73 | 29 2e 20 20 41 20 6d 6f |de types|). A mo|
|00005650| 64 75 6c 65 0a 63 6f 6e | 73 69 73 74 73 20 6f 66 |dule.con|sists of|
|00005660| 20 61 20 73 69 6e 67 6c | 65 20 5c 63 6f 64 65 7b | a singl|e \code{|
|00005670| 66 69 6c 65 5f 69 6e 70 | 75 74 7d 20 6e 6f 64 65 |file_inp|ut} node|
|00005680| 2c 20 61 6e 64 20 63 6c | 61 73 73 20 61 6e 64 20 |, and cl|ass and |
|00005690| 66 75 6e 63 74 69 6f 6e | 0a 64 65 66 69 6e 69 74 |function|.definit|
|000056a0| 69 6f 6e 73 20 65 61 63 | 68 20 63 6f 6e 74 61 69 |ions eac|h contai|
|000056b0| 6e 20 65 78 61 63 74 6c | 79 20 6f 6e 65 20 5c 63 |n exactl|y one \c|
|000056c0| 6f 64 65 7b 73 75 69 74 | 65 7d 20 6e 6f 64 65 2e |ode{suit|e} node.|
|000056d0| 20 20 43 6c 61 73 73 65 | 73 20 61 6e 64 0a 66 75 | Classe|s and.fu|
|000056e0| 6e 63 74 69 6f 6e 73 20 | 61 72 65 20 72 65 61 64 |nctions |are read|
|000056f0| 69 6c 79 20 69 64 65 6e | 74 69 66 69 65 64 20 61 |ily iden|tified a|
|00005700| 73 20 73 75 62 74 72 65 | 65 73 20 6f 66 20 63 6f |s subtre|es of co|
|00005710| 64 65 20 62 6c 6f 63 6b | 20 6e 6f 64 65 73 20 77 |de block| nodes w|
|00005720| 68 69 63 68 0a 73 74 61 | 72 74 20 77 69 74 68 20 |hich.sta|rt with |
|00005730| 5c 63 6f 64 65 7b 28 73 | 74 6d 74 2c 20 28 63 6f |\code{(s|tmt, (co|
|00005740| 6d 70 6f 75 6e 64 5f 73 | 74 6d 74 2c 20 28 63 6c |mpound_s|tmt, (cl|
|00005750| 61 73 73 64 65 66 2c 20 | 2e 2e 2e 7d 20 6f 72 0a |assdef, |...} or.|
|00005760| 5c 63 6f 64 65 7b 28 73 | 74 6d 74 2c 20 28 63 6f |\code{(s|tmt, (co|
|00005770| 6d 70 6f 75 6e 64 5f 73 | 74 6d 74 2c 20 28 66 75 |mpound_s|tmt, (fu|
|00005780| 6e 63 64 65 66 2c 20 2e | 2e 2e 7d 2e 20 20 4e 6f |ncdef, .|..}. No|
|00005790| 74 65 20 74 68 61 74 20 | 74 68 65 73 65 20 73 75 |te that |these su|
|000057a0| 62 74 72 65 65 73 0a 63 | 61 6e 6e 6f 74 20 62 65 |btrees.c|annot be|
|000057b0| 20 6d 61 74 63 68 65 64 | 20 62 79 20 5c 63 6f 64 | matched| by \cod|
|000057c0| 65 7b 6d 61 74 63 68 28 | 29 7d 20 73 69 6e 63 65 |e{match(|)} since|
|000057d0| 20 69 74 20 64 6f 65 73 | 20 6e 6f 74 20 73 75 70 | it does| not sup|
|000057e0| 70 6f 72 74 20 6d 75 6c | 74 69 70 6c 65 0a 73 69 |port mul|tiple.si|
|000057f0| 62 6c 69 6e 67 20 6e 6f | 64 65 73 20 74 6f 20 6d |bling no|des to m|
|00005800| 61 74 63 68 20 77 69 74 | 68 6f 75 74 20 72 65 67 |atch wit|hout reg|
|00005810| 61 72 64 20 74 6f 20 6e | 75 6d 62 65 72 2e 20 20 |ard to n|umber. |
|00005820| 41 20 6d 6f 72 65 20 65 | 6c 61 62 6f 72 61 74 65 |A more e|laborate|
|00005830| 0a 6d 61 74 63 68 69 6e | 67 20 66 75 6e 63 74 69 |.matchin|g functi|
|00005840| 6f 6e 20 63 6f 75 6c 64 | 20 62 65 20 75 73 65 64 |on could| be used|
|00005850| 20 74 6f 20 6f 76 65 72 | 63 6f 6d 65 20 74 68 69 | to over|come thi|
|00005860| 73 20 6c 69 6d 69 74 61 | 74 69 6f 6e 2c 20 62 75 |s limita|tion, bu|
|00005870| 74 20 74 68 69 73 0a 69 | 73 20 73 75 66 66 69 63 |t this.i|s suffic|
|00005880| 69 65 6e 74 20 66 6f 72 | 20 74 68 65 20 65 78 61 |ient for| the exa|
|00005890| 6d 70 6c 65 2e 0a 0a 47 | 69 76 65 6e 20 74 68 65 |mple...G|iven the|
|000058a0| 20 61 62 69 6c 69 74 79 | 20 74 6f 20 64 65 74 65 | ability| to dete|
|000058b0| 72 6d 69 6e 65 20 77 68 | 65 74 68 65 72 20 61 20 |rmine wh|ether a |
|000058c0| 73 74 61 74 65 6d 65 6e | 74 20 6d 69 67 68 74 20 |statemen|t might |
|000058d0| 62 65 20 61 0a 64 6f 63 | 73 74 72 69 6e 67 20 61 |be a.doc|string a|
|000058e0| 6e 64 20 65 78 74 72 61 | 63 74 20 74 68 65 20 61 |nd extra|ct the a|
|000058f0| 63 74 75 61 6c 20 73 74 | 72 69 6e 67 20 66 72 6f |ctual st|ring fro|
|00005900| 6d 20 74 68 65 20 73 74 | 61 74 65 6d 65 6e 74 2c |m the st|atement,|
|00005910| 20 73 6f 6d 65 20 77 6f | 72 6b 0a 6e 65 65 64 73 | some wo|rk.needs|
|00005920| 20 74 6f 20 62 65 20 70 | 65 72 66 6f 72 6d 65 64 | to be p|erformed|
|00005930| 20 74 6f 20 77 61 6c 6b | 20 74 68 65 20 70 61 72 | to walk| the par|
|00005940| 73 65 20 74 72 65 65 20 | 66 6f 72 20 61 6e 20 65 |se tree |for an e|
|00005950| 6e 74 69 72 65 20 6d 6f | 64 75 6c 65 20 61 6e 64 |ntire mo|dule and|
|00005960| 0a 65 78 74 72 61 63 74 | 20 69 6e 66 6f 72 6d 61 |.extract| informa|
|00005970| 74 69 6f 6e 20 61 62 6f | 75 74 20 74 68 65 20 6e |tion abo|ut the n|
|00005980| 61 6d 65 73 20 64 65 66 | 69 6e 65 64 20 69 6e 20 |ames def|ined in |
|00005990| 65 61 63 68 20 63 6f 6e | 74 65 78 74 20 6f 66 20 |each con|text of |
|000059a0| 74 68 65 0a 6d 6f 64 75 | 6c 65 20 61 6e 64 20 61 |the.modu|le and a|
|000059b0| 73 73 6f 63 69 61 74 65 | 20 61 6e 79 20 64 6f 63 |ssociate| any doc|
|000059c0| 73 74 72 69 6e 67 73 20 | 77 69 74 68 20 74 68 65 |strings |with the|
|000059d0| 20 6e 61 6d 65 73 2e 20 | 20 54 68 65 20 63 6f 64 | names. | The cod|
|000059e0| 65 20 74 6f 0a 70 65 72 | 66 6f 72 6d 20 74 68 69 |e to.per|form thi|
|000059f0| 73 20 77 6f 72 6b 20 69 | 73 20 6e 6f 74 20 63 6f |s work i|s not co|
|00005a00| 6d 70 6c 69 63 61 74 65 | 64 2c 20 62 75 74 20 62 |mplicate|d, but b|
|00005a10| 65 61 72 73 20 73 6f 6d | 65 20 65 78 70 6c 61 6e |ears som|e explan|
|00005a20| 61 74 69 6f 6e 2e 0a 0a | 54 68 65 20 70 75 62 6c |ation...|The publ|
|00005a30| 69 63 20 69 6e 74 65 72 | 66 61 63 65 20 74 6f 20 |ic inter|face to |
|00005a40| 74 68 65 20 63 6c 61 73 | 73 65 73 20 69 73 20 73 |the clas|ses is s|
|00005a50| 74 72 61 69 67 68 74 66 | 6f 72 77 61 72 64 20 61 |traightf|orward a|
|00005a60| 6e 64 20 73 68 6f 75 6c | 64 0a 70 72 6f 62 61 62 |nd shoul|d.probab|
|00005a70| 6c 79 20 62 65 20 73 6f | 6d 65 77 68 61 74 20 6d |ly be so|mewhat m|
|00005a80| 6f 72 65 20 66 6c 65 78 | 69 62 6c 65 2e 20 20 45 |ore flex|ible. E|
|00005a90| 61 63 68 20 60 60 6d 61 | 6a 6f 72 27 27 20 62 6c |ach ``ma|jor'' bl|
|00005aa0| 6f 63 6b 20 6f 66 20 74 | 68 65 0a 6d 6f 64 75 6c |ock of t|he.modul|
|00005ab0| 65 20 69 73 20 64 65 73 | 63 72 69 62 65 64 20 62 |e is des|cribed b|
|00005ac0| 79 20 61 6e 20 6f 62 6a | 65 63 74 20 70 72 6f 76 |y an obj|ect prov|
|00005ad0| 69 64 69 6e 67 20 73 65 | 76 65 72 61 6c 20 6d 65 |iding se|veral me|
|00005ae0| 74 68 6f 64 73 20 66 6f | 72 20 69 6e 71 75 69 72 |thods fo|r inquir|
|00005af0| 79 0a 61 6e 64 20 61 20 | 63 6f 6e 73 74 72 75 63 |y.and a |construc|
|00005b00| 74 6f 72 20 77 68 69 63 | 68 20 61 63 63 65 70 74 |tor whic|h accept|
|00005b10| 73 20 61 74 20 6c 65 61 | 73 74 20 74 68 65 20 73 |s at lea|st the s|
|00005b20| 75 62 74 72 65 65 20 6f | 66 20 74 68 65 20 63 6f |ubtree o|f the co|
|00005b30| 6d 70 6c 65 74 65 0a 70 | 61 72 73 65 20 74 72 65 |mplete.p|arse tre|
|00005b40| 65 20 77 68 69 63 68 20 | 69 74 20 72 65 70 72 65 |e which |it repre|
|00005b50| 73 65 6e 74 73 2e 20 20 | 54 68 65 20 5c 63 6f 64 |sents. |The \cod|
|00005b60| 65 7b 4d 6f 64 75 6c 65 | 49 6e 66 6f 7d 20 63 6f |e{Module|Info} co|
|00005b70| 6e 73 74 72 75 63 74 6f | 72 0a 61 63 63 65 70 74 |nstructo|r.accept|
|00005b80| 73 20 61 6e 20 6f 70 74 | 69 6f 6e 61 6c 20 5c 63 |s an opt|ional \c|
|00005b90| 6f 64 65 7b 5c 76 61 72 | 7b 6e 61 6d 65 7d 7d 20 |ode{\var|{name}} |
|00005ba0| 70 61 72 61 6d 65 74 65 | 72 20 73 69 6e 63 65 20 |paramete|r since |
|00005bb0| 69 74 20 63 61 6e 6e 6f | 74 0a 6f 74 68 65 72 77 |it canno|t.otherw|
|00005bc0| 69 73 65 20 64 65 74 65 | 72 6d 69 6e 65 20 74 68 |ise dete|rmine th|
|00005bd0| 65 20 6e 61 6d 65 20 6f | 66 20 74 68 65 20 6d 6f |e name o|f the mo|
|00005be0| 64 75 6c 65 2e 0a 0a 54 | 68 65 20 70 75 62 6c 69 |dule...T|he publi|
|00005bf0| 63 20 63 6c 61 73 73 65 | 73 20 69 6e 63 6c 75 64 |c classe|s includ|
|00005c00| 65 20 5c 63 6f 64 65 7b | 43 6c 61 73 73 49 6e 66 |e \code{|ClassInf|
|00005c10| 6f 7d 2c 20 5c 63 6f 64 | 65 7b 46 75 6e 63 74 69 |o}, \cod|e{Functi|
|00005c20| 6f 6e 49 6e 66 6f 7d 2c | 0a 61 6e 64 20 5c 63 6f |onInfo},|.and \co|
|00005c30| 64 65 7b 4d 6f 64 75 6c | 65 49 6e 66 6f 7d 2e 20 |de{Modul|eInfo}. |
|00005c40| 20 41 6c 6c 20 6f 62 6a | 65 63 74 73 20 70 72 6f | All obj|ects pro|
|00005c50| 76 69 64 65 20 74 68 65 | 0a 6d 65 74 68 6f 64 73 |vide the|.methods|
|00005c60| 20 5c 63 6f 64 65 7b 67 | 65 74 5f 6e 61 6d 65 28 | \code{g|et_name(|
|00005c70| 29 7d 2c 20 5c 63 6f 64 | 65 7b 67 65 74 5f 64 6f |)}, \cod|e{get_do|
|00005c80| 63 73 74 72 69 6e 67 28 | 29 7d 2c 0a 5c 63 6f 64 |cstring(|)},.\cod|
|00005c90| 65 7b 67 65 74 5f 63 6c | 61 73 73 5f 6e 61 6d 65 |e{get_cl|ass_name|
|00005ca0| 73 28 29 7d 2c 20 61 6e | 64 20 5c 63 6f 64 65 7b |s()}, an|d \code{|
|00005cb0| 67 65 74 5f 63 6c 61 73 | 73 5f 69 6e 66 6f 28 29 |get_clas|s_info()|
|00005cc0| 7d 2e 20 20 54 68 65 0a | 5c 63 6f 64 65 7b 43 6c |}. The.|\code{Cl|
|00005cd0| 61 73 73 49 6e 66 6f 7d | 20 6f 62 6a 65 63 74 73 |assInfo}| objects|
|00005ce0| 20 73 75 70 70 6f 72 74 | 20 5c 63 6f 64 65 7b 67 | support| \code{g|
|00005cf0| 65 74 5f 6d 65 74 68 6f | 64 5f 6e 61 6d 65 73 28 |et_metho|d_names(|
|00005d00| 29 7d 20 61 6e 64 0a 5c | 63 6f 64 65 7b 67 65 74 |)} and.\|code{get|
|00005d10| 5f 6d 65 74 68 6f 64 5f | 69 6e 66 6f 28 29 7d 20 |_method_|info()} |
|00005d20| 77 68 69 6c 65 20 74 68 | 65 20 6f 74 68 65 72 20 |while th|e other |
|00005d30| 63 6c 61 73 73 65 73 20 | 70 72 6f 76 69 64 65 0a |classes |provide.|
|00005d40| 5c 63 6f 64 65 7b 67 65 | 74 5f 66 75 6e 63 74 69 |\code{ge|t_functi|
|00005d50| 6f 6e 5f 6e 61 6d 65 73 | 28 29 7d 20 61 6e 64 20 |on_names|()} and |
|00005d60| 5c 63 6f 64 65 7b 67 65 | 74 5f 66 75 6e 63 74 69 |\code{ge|t_functi|
|00005d70| 6f 6e 5f 69 6e 66 6f 28 | 29 7d 2e 0a 0a 57 69 74 |on_info(|)}...Wit|
|00005d80| 68 69 6e 20 65 61 63 68 | 20 6f 66 20 74 68 65 20 |hin each| of the |
|00005d90| 66 6f 72 6d 73 20 6f 66 | 20 63 6f 64 65 20 62 6c |forms of| code bl|
|00005da0| 6f 63 6b 20 74 68 61 74 | 20 74 68 65 20 70 75 62 |ock that| the pub|
|00005db0| 6c 69 63 20 63 6c 61 73 | 73 65 73 0a 72 65 70 72 |lic clas|ses.repr|
|00005dc0| 65 73 65 6e 74 2c 20 6d | 6f 73 74 20 6f 66 20 74 |esent, m|ost of t|
|00005dd0| 68 65 20 72 65 71 75 69 | 72 65 64 20 69 6e 66 6f |he requi|red info|
|00005de0| 72 6d 61 74 69 6f 6e 20 | 69 73 20 69 6e 20 74 68 |rmation |is in th|
|00005df0| 65 20 73 61 6d 65 20 66 | 6f 72 6d 20 61 6e 64 20 |e same f|orm and |
|00005e00| 69 73 0a 61 63 63 65 73 | 73 65 64 20 69 6e 20 74 |is.acces|sed in t|
|00005e10| 68 65 20 73 61 6d 65 20 | 77 61 79 2c 20 77 69 74 |he same |way, wit|
|00005e20| 68 20 63 6c 61 73 73 65 | 73 20 68 61 76 69 6e 67 |h classe|s having|
|00005e30| 20 74 68 65 20 64 69 73 | 74 69 6e 63 74 69 6f 6e | the dis|tinction|
|00005e40| 20 74 68 61 74 0a 66 75 | 6e 63 74 69 6f 6e 73 20 | that.fu|nctions |
|00005e50| 64 65 66 69 6e 65 64 20 | 61 74 20 74 68 65 20 74 |defined |at the t|
|00005e60| 6f 70 20 6c 65 76 65 6c | 20 61 72 65 20 72 65 66 |op level| are ref|
|00005e70| 65 72 72 65 64 20 74 6f | 20 61 73 20 60 60 6d 65 |erred to| as ``me|
|00005e80| 74 68 6f 64 73 2e 27 27 | 0a 53 69 6e 63 65 20 74 |thods.''|.Since t|
|00005e90| 68 65 20 64 69 66 66 65 | 72 65 6e 63 65 20 69 6e |he diffe|rence in|
|00005ea0| 20 6e 6f 6d 65 6e 63 6c | 61 74 75 72 65 20 72 65 | nomencl|ature re|
|00005eb0| 66 6c 65 63 74 73 20 61 | 20 72 65 61 6c 20 73 65 |flects a| real se|
|00005ec0| 6d 61 6e 74 69 63 0a 64 | 69 73 74 69 6e 63 74 69 |mantic.d|istincti|
|00005ed0| 6f 6e 20 66 72 6f 6d 20 | 66 75 6e 63 74 69 6f 6e |on from |function|
|00005ee0| 73 20 64 65 66 69 6e 65 | 64 20 6f 75 74 73 69 64 |s define|d outsid|
|00005ef0| 65 20 6f 66 20 61 20 63 | 6c 61 73 73 2c 20 74 68 |e of a c|lass, th|
|00005f00| 65 0a 69 6d 70 6c 65 6d | 65 6e 74 61 74 69 6f 6e |e.implem|entation|
|00005f10| 20 6e 65 65 64 73 20 74 | 6f 20 6d 61 69 6e 74 61 | needs t|o mainta|
|00005f20| 69 6e 20 74 68 65 20 64 | 69 73 74 69 6e 63 74 69 |in the d|istincti|
|00005f30| 6f 6e 2e 0a 48 65 6e 63 | 65 2c 20 6d 6f 73 74 20 |on..Henc|e, most |
|00005f40| 6f 66 20 74 68 65 20 66 | 75 6e 63 74 69 6f 6e 61 |of the f|unctiona|
|00005f50| 6c 69 74 79 20 6f 66 20 | 74 68 65 20 70 75 62 6c |lity of |the publ|
|00005f60| 69 63 20 63 6c 61 73 73 | 65 73 20 63 61 6e 20 62 |ic class|es can b|
|00005f70| 65 0a 69 6d 70 6c 65 6d | 65 6e 74 65 64 20 69 6e |e.implem|ented in|
|00005f80| 20 61 20 63 6f 6d 6d 6f | 6e 20 62 61 73 65 20 63 | a commo|n base c|
|00005f90| 6c 61 73 73 2c 20 5c 63 | 6f 64 65 7b 53 75 69 74 |lass, \c|ode{Suit|
|00005fa0| 65 49 6e 66 6f 42 61 73 | 65 7d 2c 20 77 69 74 68 |eInfoBas|e}, with|
|00005fb0| 20 74 68 65 0a 61 63 63 | 65 73 73 6f 72 73 20 66 | the.acc|essors f|
|00005fc0| 6f 72 20 66 75 6e 63 74 | 69 6f 6e 20 61 6e 64 20 |or funct|ion and |
|00005fd0| 6d 65 74 68 6f 64 20 69 | 6e 66 6f 72 6d 61 74 69 |method i|nformati|
|00005fe0| 6f 6e 20 70 72 6f 76 69 | 64 65 64 20 65 6c 73 65 |on provi|ded else|
|00005ff0| 77 68 65 72 65 2e 0a 4e | 6f 74 65 20 74 68 61 74 |where..N|ote that|
|00006000| 20 74 68 65 72 65 20 69 | 73 20 6f 6e 6c 79 20 6f | there i|s only o|
|00006010| 6e 65 20 63 6c 61 73 73 | 20 77 68 69 63 68 20 72 |ne class| which r|
|00006020| 65 70 72 65 73 65 6e 74 | 73 20 66 75 6e 63 74 69 |epresent|s functi|
|00006030| 6f 6e 20 61 6e 64 20 6d | 65 74 68 6f 64 0a 69 6e |on and m|ethod.in|
|00006040| 66 6f 72 6d 61 74 69 6f | 6e 3b 20 74 68 69 73 20 |formatio|n; this |
|00006050| 70 61 72 61 6c 65 6c 73 | 20 74 68 65 20 75 73 65 |paralels| the use|
|00006060| 20 6f 66 20 74 68 65 20 | 5c 63 6f 64 65 7b 64 65 | of the |\code{de|
|00006070| 66 7d 20 73 74 61 74 65 | 6d 65 6e 74 20 74 6f 0a |f} state|ment to.|
|00006080| 64 65 66 69 6e 65 20 62 | 6f 74 68 20 74 79 70 65 |define b|oth type|
|00006090| 73 20 6f 66 20 65 6c 65 | 6d 65 6e 74 73 2e 0a 0a |s of ele|ments...|
|000060a0| 4d 6f 73 74 20 6f 66 20 | 74 68 65 20 61 63 63 65 |Most of |the acce|
|000060b0| 73 73 6f 72 20 66 75 6e | 63 74 69 6f 6e 73 20 61 |ssor fun|ctions a|
|000060c0| 72 65 20 64 65 63 6c 61 | 72 65 64 20 69 6e 20 5c |re decla|red in \|
|000060d0| 63 6f 64 65 7b 53 75 69 | 74 65 49 6e 66 6f 42 61 |code{Sui|teInfoBa|
|000060e0| 73 65 7d 0a 61 6e 64 20 | 64 6f 20 6e 6f 74 20 6e |se}.and |do not n|
|000060f0| 65 65 64 20 74 6f 20 62 | 65 20 6f 76 65 72 72 69 |eed to b|e overri|
|00006100| 64 65 6e 20 62 79 20 73 | 75 62 63 6c 61 73 73 65 |den by s|ubclasse|
|00006110| 73 2e 20 20 4d 6f 72 65 | 20 69 6d 70 6f 72 74 61 |s. More| importa|
|00006120| 6e 74 6c 79 2c 20 74 68 | 65 0a 65 78 74 72 61 63 |ntly, th|e.extrac|
|00006130| 74 69 6f 6e 20 6f 66 20 | 6d 6f 73 74 20 69 6e 66 |tion of |most inf|
|00006140| 6f 72 6d 61 74 69 6f 6e | 20 66 72 6f 6d 20 61 20 |ormation| from a |
|00006150| 70 61 72 73 65 20 74 72 | 65 65 20 69 73 20 68 61 |parse tr|ee is ha|
|00006160| 6e 64 6c 65 64 20 74 68 | 72 6f 75 67 68 20 61 0a |ndled th|rough a.|
|00006170| 6d 65 74 68 6f 64 20 63 | 61 6c 6c 65 64 20 62 79 |method c|alled by|
|00006180| 20 74 68 65 20 5c 63 6f | 64 65 7b 53 75 69 74 65 | the \co|de{Suite|
|00006190| 49 6e 66 6f 42 61 73 65 | 7d 20 63 6f 6e 73 74 72 |InfoBase|} constr|
|000061a0| 75 63 74 6f 72 2e 20 20 | 54 68 65 20 65 78 61 6d |uctor. |The exam|
|000061b0| 70 6c 65 0a 63 6f 64 65 | 20 66 6f 72 20 6d 6f 73 |ple.code| for mos|
|000061c0| 74 20 6f 66 20 74 68 65 | 20 63 6c 61 73 73 65 73 |t of the| classes|
|000061d0| 20 69 73 20 63 6c 65 61 | 72 20 77 68 65 6e 20 72 | is clea|r when r|
|000061e0| 65 61 64 20 61 6c 6f 6e | 67 73 69 64 65 20 74 68 |ead alon|gside th|
|000061f0| 65 20 66 6f 72 6d 61 6c | 0a 67 72 61 6d 6d 61 72 |e formal|.grammar|
|00006200| 2c 20 62 75 74 20 74 68 | 65 20 6d 65 74 68 6f 64 |, but th|e method|
|00006210| 20 77 68 69 63 68 20 72 | 65 63 75 72 73 69 76 65 | which r|ecursive|
|00006220| 6c 79 20 63 72 65 61 74 | 65 73 20 6e 65 77 20 69 |ly creat|es new i|
|00006230| 6e 66 6f 72 6d 61 74 69 | 6f 6e 0a 6f 62 6a 65 63 |nformati|on.objec|
|00006240| 74 73 20 72 65 71 75 69 | 72 65 73 20 66 75 72 74 |ts requi|res furt|
|00006250| 68 65 72 20 65 78 61 6d | 69 6e 61 74 69 6f 6e 2e |her exam|ination.|
|00006260| 20 20 48 65 72 65 20 69 | 73 20 74 68 65 20 72 65 | Here i|s the re|
|00006270| 6c 65 76 61 6e 74 20 70 | 61 72 74 20 6f 66 0a 74 |levant p|art of.t|
|00006280| 68 65 20 5c 63 6f 64 65 | 7b 53 75 69 74 65 49 6e |he \code|{SuiteIn|
|00006290| 66 6f 42 61 73 65 7d 20 | 64 65 66 69 6e 69 74 69 |foBase} |definiti|
|000062a0| 6f 6e 20 66 72 6f 6d 20 | 5c 66 69 6c 65 7b 65 78 |on from |\file{ex|
|000062b0| 61 6d 70 6c 65 2e 70 79 | 7d 3a 0a 0a 5c 62 65 67 |ample.py|}:..\beg|
|000062c0| 69 6e 7b 76 65 72 62 61 | 74 69 6d 7d 0a 63 6c 61 |in{verba|tim}.cla|
|000062d0| 73 73 20 53 75 69 74 65 | 49 6e 66 6f 42 61 73 65 |ss Suite|InfoBase|
|000062e0| 3a 0a 20 20 20 20 5f 64 | 6f 63 73 74 72 69 6e 67 |:. _d|ocstring|
|000062f0| 20 3d 20 27 27 0a 20 20 | 20 20 5f 6e 61 6d 65 20 | = ''. | _name |
|00006300| 3d 20 27 27 0a 0a 20 20 | 20 20 64 65 66 20 5f 5f |= ''.. | def __|
|00006310| 69 6e 69 74 5f 5f 28 73 | 65 6c 66 2c 20 74 72 65 |init__(s|elf, tre|
|00006320| 65 20 3d 20 4e 6f 6e 65 | 29 3a 0a 20 20 20 20 20 |e = None|):. |
|00006330| 20 20 20 73 65 6c 66 2e | 5f 63 6c 61 73 73 5f 69 | self.|_class_i|
|00006340| 6e 66 6f 20 3d 20 7b 7d | 0a 20 20 20 20 20 20 20 |nfo = {}|. |
|00006350| 20 73 65 6c 66 2e 5f 66 | 75 6e 63 74 69 6f 6e 5f | self._f|unction_|
|00006360| 69 6e 66 6f 20 3d 20 7b | 7d 0a 20 20 20 20 20 20 |info = {|}. |
|00006370| 20 20 69 66 20 74 72 65 | 65 3a 0a 20 20 20 20 20 | if tre|e:. |
|00006380| 20 20 20 20 20 20 20 73 | 65 6c 66 2e 5f 65 78 74 | s|elf._ext|
|00006390| 72 61 63 74 5f 69 6e 66 | 6f 28 74 72 65 65 29 0a |ract_inf|o(tree).|
|000063a0| 0a 20 20 20 20 64 65 66 | 20 5f 65 78 74 72 61 63 |. def| _extrac|
|000063b0| 74 5f 69 6e 66 6f 28 73 | 65 6c 66 2c 20 74 72 65 |t_info(s|elf, tre|
|000063c0| 65 29 3a 0a 20 20 20 20 | 20 20 20 20 23 20 65 78 |e):. | # ex|
|000063d0| 74 72 61 63 74 20 64 6f | 63 73 74 72 69 6e 67 0a |tract do|cstring.|
|000063e0| 20 20 20 20 20 20 20 20 | 69 66 20 6c 65 6e 28 74 | |if len(t|
|000063f0| 72 65 65 29 20 3d 3d 20 | 32 3a 0a 20 20 20 20 20 |ree) == |2:. |
+--------+-------------------------+-------------------------+--------+--------+
Only 25.0 KB of data is shown above.