home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Applications / Python 1.3 / source code / Doc / libparser.tex / libparser.txt
LaTeX Document  |  1995-12-17  |  10.5 KB

open in: MacOS 8.1     |     Win98     |     DOS

browse contents    |     view JSON data     |     view as text


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

ConfidenceProgramDetectionMatch TypeSupport
100% dexvert LaTeX Document (document/latex) magic Supported
1% dexvert Text File (text/txt) fallback Supported
100% file LaTeX document, ASCII text default
100% checkBytes Printable ASCII default
100% perlTextCheck Likely Text (Perl) default
100% siegfried fmt/281 LaTeX (Subdocument) default
100% gt2 Textdatei (UNIX) default
100% detectItEasy Format: plain text[LF] 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 20 49 6e 74 72 6f 64 | 75 63 74 6f 72 79 20 64 |% Introd|uctory d|
|00000020| 6f 63 75 6d 65 6e 74 61 | 74 69 6f 6e 20 66 6f 72 |ocumenta|tion for|
|00000030| 20 74 68 65 20 6e 65 77 | 20 70 61 72 73 65 72 20 | the new| parser |
|00000040| 62 75 69 6c 74 2d 69 6e | 20 6d 6f 64 75 6c 65 2e |built-in| module.|
|00000050| 0a 25 20 43 6f 70 79 72 | 69 67 68 74 20 31 39 39 |.% Copyr|ight 199|
|00000060| 35 20 56 69 72 67 69 6e | 69 61 20 50 6f 6c 79 74 |5 Virgin|ia Polyt|
|00000070| 65 63 68 6e 69 63 20 49 | 6e 73 74 69 74 75 74 65 |echnic I|nstitute|
|00000080| 20 61 6e 64 20 53 74 61 | 74 65 20 55 6e 69 76 65 | and Sta|te Unive|
|00000090| 72 73 69 74 79 0a 25 20 | 61 6e 64 20 46 72 65 64 |rsity.% |and Fred|
|000000a0| 20 4c 2e 20 44 72 61 6b | 65 2c 20 4a 72 2e 20 20 | L. Drak|e, Jr. |
|000000b0| 54 68 69 73 20 63 6f 70 | 79 72 69 67 68 74 20 6e |This cop|yright n|
|000000c0| 6f 74 69 63 65 20 6d 75 | 73 74 20 62 65 20 64 69 |otice mu|st be di|
|000000d0| 73 74 72 69 62 75 74 65 | 64 20 6f 6e 0a 25 20 61 |stribute|d on.% a|
|000000e0| 6c 6c 20 63 6f 70 69 65 | 73 2c 20 62 75 74 20 74 |ll copie|s, but t|
|000000f0| 68 69 73 20 64 6f 63 75 | 6d 65 6e 74 20 6f 74 68 |his docu|ment oth|
|00000100| 65 72 77 69 73 65 20 6d | 61 79 20 62 65 20 64 69 |erwise m|ay be di|
|00000110| 73 74 72 69 62 75 74 65 | 64 20 61 73 20 70 61 72 |stribute|d as par|
|00000120| 74 0a 25 20 6f 66 20 74 | 68 65 20 50 79 74 68 6f |t.% of t|he Pytho|
|00000130| 6e 20 64 69 73 74 72 69 | 62 75 74 69 6f 6e 2e 20 |n distri|bution. |
|00000140| 20 4e 6f 20 66 65 65 20 | 6d 61 79 20 62 65 20 63 | No fee |may be c|
|00000150| 68 61 72 67 65 64 20 66 | 6f 72 20 74 68 69 73 20 |harged f|or this |
|00000160| 64 6f 63 75 6d 65 6e 74 | 0a 25 20 69 6e 20 61 6e |document|.% in an|
|00000170| 79 20 72 65 70 72 65 73 | 65 6e 74 61 74 69 6f 6e |y repres|entation|
|00000180| 2c 20 65 69 74 68 65 72 | 20 6f 6e 20 70 61 70 65 |, either| on pape|
|00000190| 72 20 6f 72 20 65 6c 65 | 63 74 72 6f 6e 69 63 61 |r or ele|ctronica|
|000001a0| 6c 6c 79 2e 20 20 54 68 | 69 73 0a 25 20 72 65 73 |lly. Th|is.% res|
|000001b0| 74 72 69 63 74 69 6f 6e | 20 64 6f 65 73 20 6e 6f |triction| does no|
|000001c0| 74 20 61 66 66 65 63 74 | 20 6f 74 68 65 72 20 65 |t affect| other e|
|000001d0| 6c 65 6d 65 6e 74 73 20 | 69 6e 20 61 20 64 69 73 |lements |in a dis|
|000001e0| 74 72 69 62 75 74 65 64 | 20 70 61 63 6b 61 67 65 |tributed| package|
|000001f0| 0a 25 20 69 6e 20 61 6e | 79 20 77 61 79 2e 0a 5c |.% in an|y way..\|
|00000200| 73 65 63 74 69 6f 6e 7b | 42 75 69 6c 74 2d 69 6e |section{|Built-in|
|00000210| 20 4d 6f 64 75 6c 65 20 | 5c 73 65 63 74 63 6f 64 | Module |\sectcod|
|00000220| 65 7b 70 61 72 73 65 72 | 7d 7d 0a 5c 62 69 6d 6f |e{parser|}}.\bimo|
|00000230| 64 69 6e 64 65 78 7b 70 | 61 72 73 65 72 7d 0a 25 |dindex{p|arser}.%|
|00000240| 20 3d 3d 3d 3d 20 32 2e | 20 3d 3d 3d 3d 0a 25 20 | ==== 2.| ====.% |
|00000250| 47 69 76 65 20 61 20 73 | 68 6f 72 74 20 6f 76 65 |Give a s|hort ove|
|00000260| 72 76 69 65 77 20 6f 66 | 20 77 68 61 74 20 74 68 |rview of| what th|
|00000270| 65 20 6d 6f 64 75 6c 65 | 20 64 6f 65 73 2e 0a 25 |e module| does..%|
|00000280| 20 49 66 20 69 74 20 69 | 73 20 70 6c 61 74 66 6f | If it i|s platfo|
|00000290| 72 6d 20 73 70 65 63 69 | 66 69 63 2c 20 6d 65 6e |rm speci|fic, men|
|000002a0| 74 69 6f 6e 20 74 68 69 | 73 2e 0a 25 20 4d 65 6e |tion thi|s..% Men|
|000002b0| 74 69 6f 6e 20 6f 74 68 | 65 72 20 69 6d 70 6f 72 |tion oth|er impor|
|000002c0| 74 61 6e 74 20 72 65 73 | 74 72 69 63 74 69 6f 6e |tant res|triction|
|000002d0| 73 20 6f 72 20 67 65 6e | 65 72 61 6c 20 6f 70 65 |s or gen|eral ope|
|000002e0| 72 61 74 69 6e 67 20 70 | 72 69 6e 63 69 70 6c 65 |rating p|rinciple|
|000002f0| 73 2e 0a 54 68 65 20 5c | 63 6f 64 65 7b 70 61 72 |s..The \|code{par|
|00000300| 73 65 72 7d 20 6d 6f 64 | 75 6c 65 20 70 72 6f 76 |ser} mod|ule prov|
|00000310| 69 64 65 73 20 61 6e 20 | 69 6e 74 65 72 66 61 63 |ides an |interfac|
|00000320| 65 20 74 6f 20 50 79 74 | 68 6f 6e 27 73 20 69 6e |e to Pyt|hon's in|
|00000330| 74 65 72 6e 61 6c 0a 70 | 61 72 73 65 72 20 61 6e |ternal.p|arser an|
|00000340| 64 20 62 79 74 65 2d 63 | 6f 64 65 20 63 6f 6d 70 |d byte-c|ode comp|
|00000350| 69 6c 65 72 2e 20 20 54 | 68 65 20 70 72 69 6d 61 |iler. T|he prima|
|00000360| 72 79 20 70 75 72 70 6f | 73 65 20 66 6f 72 20 74 |ry purpo|se for t|
|00000370| 68 69 73 20 69 6e 74 65 | 72 66 61 63 65 0a 69 73 |his inte|rface.is|
|00000380| 20 74 6f 20 61 6c 6c 6f | 77 20 50 79 74 68 6f 6e | to allo|w Python|
|00000390| 20 63 6f 64 65 20 74 6f | 20 65 64 69 74 20 74 68 | code to| edit th|
|000003a0| 65 20 70 61 72 73 65 20 | 74 72 65 65 20 6f 66 20 |e parse |tree of |
|000003b0| 61 20 50 79 74 68 6f 6e | 20 65 78 70 72 65 73 73 |a Python| express|
|000003c0| 69 6f 6e 0a 61 6e 64 20 | 63 72 65 61 74 65 20 65 |ion.and |create e|
|000003d0| 78 65 63 75 74 61 62 6c | 65 20 63 6f 64 65 20 66 |xecutabl|e code f|
|000003e0| 72 6f 6d 20 74 68 69 73 | 2e 20 20 54 68 69 73 20 |rom this|. This |
|000003f0| 63 61 6e 20 62 65 20 62 | 65 74 74 65 72 20 74 68 |can be b|etter th|
|00000400| 61 6e 20 74 72 79 69 6e | 67 0a 74 6f 20 70 61 72 |an tryin|g.to par|
|00000410| 73 65 20 61 6e 64 20 6d | 6f 64 69 66 79 20 61 6e |se and m|odify an|
|00000420| 20 61 72 62 69 74 72 61 | 72 79 20 50 79 74 68 6f | arbitra|ry Pytho|
|00000430| 6e 20 63 6f 64 65 20 66 | 72 61 67 6d 65 6e 74 20 |n code f|ragment |
|00000440| 61 73 20 61 20 73 74 72 | 69 6e 67 2c 20 61 6e 64 |as a str|ing, and|
|00000450| 0a 65 6e 73 75 72 65 73 | 20 74 68 61 74 20 70 61 |.ensures| that pa|
|00000460| 72 73 69 6e 67 20 69 73 | 20 70 65 72 66 6f 72 6d |rsing is| perform|
|00000470| 65 64 20 69 6e 20 61 20 | 6d 61 6e 6e 65 72 20 69 |ed in a |manner i|
|00000480| 64 65 6e 74 69 63 61 6c | 20 74 6f 20 74 68 65 20 |dentical| to the |
|00000490| 63 6f 64 65 0a 66 6f 72 | 6d 69 6e 67 20 74 68 65 |code.for|ming the|
|000004a0| 20 61 70 70 6c 69 63 61 | 74 69 6f 6e 2e 20 20 49 | applica|tion. I|
|000004b0| 74 27 73 20 61 6c 73 6f | 20 66 61 73 74 65 72 2e |t's also| faster.|
|000004c0| 0a 54 68 65 72 65 20 61 | 72 65 20 61 20 66 65 77 |.There a|re a few|
|000004d0| 20 74 68 69 6e 67 73 20 | 74 6f 20 6e 6f 74 65 20 | things |to note |
|000004e0| 61 62 6f 75 74 20 74 68 | 69 73 20 6d 6f 64 75 6c |about th|is modul|
|000004f0| 65 20 77 68 69 63 68 20 | 61 72 65 20 69 6d 70 6f |e which |are impo|
|00000500| 72 74 61 6e 74 0a 74 6f | 20 6d 61 6b 69 6e 67 20 |rtant.to| making |
|00000510| 75 73 65 20 6f 66 20 74 | 68 65 20 64 61 74 61 20 |use of t|he data |
|00000520| 73 74 72 75 63 74 75 72 | 65 73 20 63 72 65 61 74 |structur|es creat|
|00000530| 65 64 2e 20 20 54 68 69 | 73 20 69 73 20 6e 6f 74 |ed. Thi|s is not|
|00000540| 20 61 20 74 75 74 6f 72 | 69 61 6c 0a 6f 6e 20 65 | a tutor|ial.on e|
|00000550| 64 69 74 69 6e 67 20 74 | 68 65 20 70 61 72 73 65 |diting t|he parse|
|00000560| 20 74 72 65 65 73 20 66 | 6f 72 20 50 79 74 68 6f | trees f|or Pytho|
|00000570| 6e 20 63 6f 64 65 2e 0a | 4d 6f 73 74 20 69 6d 70 |n code..|Most imp|
|00000580| 6f 72 74 61 6e 74 6c 79 | 2c 20 61 20 67 6f 6f 64 |ortantly|, a good|
|00000590| 20 75 6e 64 65 72 73 74 | 61 6e 64 69 6e 67 20 6f | underst|anding o|
|000005a0| 66 20 74 68 65 20 50 79 | 74 68 6f 6e 20 67 72 61 |f the Py|thon gra|
|000005b0| 6d 6d 61 72 20 70 72 6f | 63 65 73 73 65 64 0a 62 |mmar pro|cessed.b|
|000005c0| 79 20 74 68 65 20 69 6e | 74 65 72 6e 61 6c 20 70 |y the in|ternal p|
|000005d0| 61 72 73 65 72 20 69 73 | 20 72 65 71 75 69 72 65 |arser is| require|
|000005e0| 64 2e 20 20 46 6f 72 20 | 66 75 6c 6c 20 69 6e 66 |d. For |full inf|
|000005f0| 6f 72 6d 61 74 69 6f 6e | 20 6f 6e 20 74 68 65 0a |ormation| on the.|
|00000600| 6c 61 6e 67 75 61 67 65 | 20 73 79 6e 74 61 78 2c |language| syntax,|
|00000610| 20 72 65 66 65 72 20 74 | 6f 20 74 68 65 20 4c 61 | refer t|o the La|
|00000620| 6e 67 75 61 67 65 20 52 | 65 66 65 72 65 6e 63 65 |nguage R|eference|
|00000630| 2e 20 20 54 68 65 20 70 | 61 72 73 65 72 20 69 74 |. The p|arser it|
|00000640| 73 65 6c 66 0a 69 73 20 | 63 72 65 61 74 65 64 20 |self.is |created |
|00000650| 66 72 6f 6d 20 61 20 67 | 72 61 6d 6d 61 72 20 73 |from a g|rammar s|
|00000660| 70 65 63 69 66 69 63 61 | 74 69 6f 6e 20 64 65 66 |pecifica|tion def|
|00000670| 69 6e 65 64 20 69 6e 20 | 74 68 65 20 66 69 6c 65 |ined in |the file|
|00000680| 0a 5c 63 6f 64 65 7b 47 | 72 61 6d 6d 61 72 2f 47 |.\code{G|rammar/G|
|00000690| 72 61 6d 6d 61 72 7d 20 | 69 6e 20 74 68 65 20 73 |rammar} |in the s|
|000006a0| 74 61 6e 64 61 72 64 20 | 50 79 74 68 6f 6e 20 64 |tandard |Python d|
|000006b0| 69 73 74 72 69 62 75 74 | 69 6f 6e 2e 20 20 54 68 |istribut|ion. Th|
|000006c0| 65 20 70 61 72 73 65 0a | 74 72 65 65 73 20 73 74 |e parse.|trees st|
|000006d0| 6f 72 65 64 20 69 6e 20 | 74 68 65 20 60 60 41 53 |ored in |the ``AS|
|000006e0| 54 20 6f 62 6a 65 63 74 | 73 27 27 20 63 72 65 61 |T object|s'' crea|
|000006f0| 74 65 64 20 62 79 20 74 | 68 69 73 20 6d 6f 64 75 |ted by t|his modu|
|00000700| 6c 65 20 61 72 65 20 74 | 68 65 0a 61 63 74 75 61 |le are t|he.actua|
|00000710| 6c 20 6f 75 74 70 75 74 | 20 66 72 6f 6d 20 74 68 |l output| from th|
|00000720| 65 20 69 6e 74 65 72 6e | 61 6c 20 70 61 72 73 65 |e intern|al parse|
|00000730| 72 20 77 68 65 6e 20 63 | 72 65 61 74 65 64 20 62 |r when c|reated b|
|00000740| 79 20 74 68 65 0a 5c 63 | 6f 64 65 7b 65 78 70 72 |y the.\c|ode{expr|
|00000750| 28 29 7d 20 6f 72 20 5c | 63 6f 64 65 7b 73 75 69 |()} or \|code{sui|
|00000760| 74 65 28 29 7d 20 66 75 | 6e 63 74 69 6f 6e 73 2c |te()} fu|nctions,|
|00000770| 20 64 65 73 63 72 69 62 | 65 64 20 62 65 6c 6f 77 | describ|ed below|
|00000780| 2e 20 20 54 68 65 20 41 | 53 54 0a 6f 62 6a 65 63 |. The A|ST.objec|
|00000790| 74 73 20 63 72 65 61 74 | 65 64 20 62 79 20 5c 63 |ts creat|ed by \c|
|000007a0| 6f 64 65 7b 74 75 70 6c | 65 32 61 73 74 28 29 7d |ode{tupl|e2ast()}|
|000007b0| 20 66 61 69 74 68 66 75 | 6c 6c 79 20 73 69 6d 75 | faithfu|lly simu|
|000007c0| 6c 61 74 65 20 74 68 6f | 73 65 0a 73 74 72 75 63 |late tho|se.struc|
|000007d0| 74 75 72 65 73 2e 0a 45 | 61 63 68 20 65 6c 65 6d |tures..E|ach elem|
|000007e0| 65 6e 74 20 6f 66 20 74 | 68 65 20 74 75 70 6c 65 |ent of t|he tuple|
|000007f0| 73 20 72 65 74 75 72 6e | 65 64 20 62 79 20 5c 63 |s return|ed by \c|
|00000800| 6f 64 65 7b 61 73 74 32 | 74 75 70 6c 65 28 29 7d |ode{ast2|tuple()}|
|00000810| 20 68 61 73 20 61 20 73 | 69 6d 70 6c 65 0a 66 6f | has a s|imple.fo|
|00000820| 72 6d 2e 20 20 54 75 70 | 6c 65 73 20 72 65 70 72 |rm. Tup|les repr|
|00000830| 65 73 65 6e 74 69 6e 67 | 20 6e 6f 6e 2d 74 65 72 |esenting| non-ter|
|00000840| 6d 69 6e 61 6c 20 65 6c | 65 6d 65 6e 74 73 20 69 |minal el|ements i|
|00000850| 6e 20 74 68 65 20 67 72 | 61 6d 6d 61 72 20 61 6c |n the gr|ammar al|
|00000860| 77 61 79 73 0a 68 61 76 | 65 20 61 20 6c 65 6e 67 |ways.hav|e a leng|
|00000870| 74 68 20 67 72 65 61 74 | 65 72 20 74 68 61 6e 20 |th great|er than |
|00000880| 6f 6e 65 2e 20 20 54 68 | 65 20 66 69 72 73 74 20 |one. Th|e first |
|00000890| 65 6c 65 6d 65 6e 74 20 | 69 73 20 61 6e 20 69 6e |element |is an in|
|000008a0| 74 65 67 65 72 20 77 68 | 69 63 68 0a 69 64 65 6e |teger wh|ich.iden|
|000008b0| 74 69 66 69 65 73 20 61 | 20 70 72 6f 64 75 63 74 |tifies a| product|
|000008c0| 69 6f 6e 20 69 6e 20 74 | 68 65 20 67 72 61 6d 6d |ion in t|he gramm|
|000008d0| 61 72 2e 20 20 54 68 65 | 73 65 20 69 6e 74 65 67 |ar. The|se integ|
|000008e0| 65 72 73 20 61 72 65 20 | 67 69 76 65 6e 0a 73 79 |ers are |given.sy|
|000008f0| 6d 62 6f 6c 69 63 20 6e | 61 6d 65 73 20 69 6e 20 |mbolic n|ames in |
|00000900| 74 68 65 20 43 20 68 65 | 61 64 65 72 20 66 69 6c |the C he|ader fil|
|00000910| 65 20 5c 63 6f 64 65 7b | 49 6e 63 6c 75 64 65 2f |e \code{|Include/|
|00000920| 67 72 61 6d 69 6e 69 74 | 2e 68 7d 20 61 6e 64 20 |graminit|.h} and |
|00000930| 74 68 65 0a 50 79 74 68 | 6f 6e 20 6d 6f 64 75 6c |the.Pyth|on modul|
|00000940| 65 20 5c 63 6f 64 65 7b | 4c 69 62 2f 73 79 6d 62 |e \code{|Lib/symb|
|00000950| 6f 6c 2e 70 79 7d 2e 20 | 20 45 61 63 68 20 61 64 |ol.py}. | Each ad|
|00000960| 64 69 74 69 6f 6e 61 6c | 20 65 6c 65 6d 65 6e 74 |ditional| element|
|00000970| 20 6f 66 20 74 68 65 0a | 74 75 70 6c 65 20 72 65 | of the.|tuple re|
|00000980| 70 72 65 73 65 6e 74 73 | 20 61 20 63 6f 6d 70 6f |presents| a compo|
|00000990| 6e 65 6e 74 20 6f 66 20 | 74 68 65 20 70 72 6f 64 |nent of |the prod|
|000009a0| 75 63 74 69 6f 6e 20 61 | 73 20 72 65 63 6f 67 6e |uction a|s recogn|
|000009b0| 69 7a 65 64 20 69 6e 20 | 74 68 65 0a 69 6e 70 75 |ized in |the.inpu|
|000009c0| 74 20 73 74 72 69 6e 67 | 3a 20 74 68 65 73 65 20 |t string|: these |
|000009d0| 61 72 65 20 61 6c 77 61 | 79 73 20 74 75 70 6c 65 |are alwa|ys tuple|
|000009e0| 73 20 77 68 69 63 68 20 | 68 61 76 65 20 74 68 65 |s which |have the|
|000009f0| 20 73 61 6d 65 20 66 6f | 72 6d 20 61 73 20 74 68 | same fo|rm as th|
|00000a00| 65 0a 70 61 72 65 6e 74 | 2e 20 20 41 6e 20 69 6d |e.parent|. An im|
|00000a10| 70 6f 72 74 61 6e 74 20 | 61 73 70 65 63 74 20 6f |portant |aspect o|
|00000a20| 66 20 74 68 69 73 20 73 | 74 72 75 63 74 75 72 65 |f this s|tructure|
|00000a30| 20 77 68 69 63 68 20 73 | 68 6f 75 6c 64 20 62 65 | which s|hould be|
|00000a40| 20 6e 6f 74 65 64 0a 69 | 73 20 74 68 61 74 20 6b | noted.i|s that k|
|00000a50| 65 79 77 6f 72 64 73 20 | 75 73 65 64 20 74 6f 20 |eywords |used to |
|00000a60| 69 64 65 6e 74 69 66 79 | 20 74 68 65 20 70 61 72 |identify| the par|
|00000a70| 65 6e 74 20 6e 6f 64 65 | 20 74 79 70 65 2c 20 73 |ent node| type, s|
|00000a80| 75 63 68 20 61 73 20 74 | 68 65 0a 6b 65 79 77 6f |uch as t|he.keywo|
|00000a90| 72 64 20 5c 63 6f 64 65 | 7b 69 66 7d 20 69 6e 20 |rd \code|{if} in |
|00000aa0| 61 6e 20 5c 65 6d 70 68 | 7b 69 66 5c 5f 73 74 6d |an \emph|{if\_stm|
|00000ab0| 74 7d 2c 20 61 72 65 20 | 69 6e 63 6c 75 64 65 64 |t}, are |included|
|00000ac0| 20 69 6e 20 74 68 65 20 | 6e 6f 64 65 20 74 72 65 | in the |node tre|
|00000ad0| 65 0a 77 69 74 68 6f 75 | 74 20 61 6e 79 20 73 70 |e.withou|t any sp|
|00000ae0| 65 63 69 61 6c 20 74 72 | 65 61 74 6d 65 6e 74 2e |ecial tr|eatment.|
|00000af0| 20 20 46 6f 72 20 65 78 | 61 6d 70 6c 65 2c 20 74 | For ex|ample, t|
|00000b00| 68 65 20 5c 63 6f 64 65 | 7b 69 66 7d 20 6b 65 79 |he \code|{if} key|
|00000b10| 77 6f 72 64 20 69 73 0a | 72 65 70 72 65 73 65 6e |word is.|represen|
|00000b20| 74 65 64 20 62 79 20 74 | 68 65 20 74 75 70 6c 65 |ted by t|he tuple|
|00000b30| 20 5c 63 6f 64 65 7b 28 | 31 2c 20 27 69 66 27 29 | \code{(|1, 'if')|
|00000b40| 7d 2c 20 77 68 65 72 65 | 20 5c 63 6f 64 65 7b 31 |}, where| \code{1|
|00000b50| 7d 20 69 73 20 74 68 65 | 0a 6e 75 6d 65 72 69 63 |} is the|.numeric|
|00000b60| 20 76 61 6c 75 65 20 61 | 73 73 6f 63 69 61 74 65 | value a|ssociate|
|00000b70| 64 20 77 69 74 68 20 61 | 6c 6c 20 5c 63 6f 64 65 |d with a|ll \code|
|00000b80| 7b 4e 41 4d 45 7d 20 65 | 6c 65 6d 65 6e 74 73 2c |{NAME} e|lements,|
|00000b90| 20 69 6e 63 6c 75 64 69 | 6e 67 0a 76 61 72 69 61 | includi|ng.varia|
|00000ba0| 62 6c 65 20 61 6e 64 20 | 66 75 6e 63 74 69 6f 6e |ble and |function|
|00000bb0| 20 6e 61 6d 65 73 20 64 | 65 66 69 6e 65 64 20 62 | names d|efined b|
|00000bc0| 79 20 74 68 65 20 75 73 | 65 72 2e 0a 54 65 72 6d |y the us|er..Term|
|00000bd0| 69 6e 61 6c 20 65 6c 65 | 6d 65 6e 74 73 20 61 72 |inal ele|ments ar|
|00000be0| 65 20 72 65 70 72 65 73 | 65 6e 74 65 64 20 69 6e |e repres|ented in|
|00000bf0| 20 6d 75 63 68 20 74 68 | 65 20 73 61 6d 65 20 77 | much th|e same w|
|00000c00| 61 79 2c 20 62 75 74 20 | 77 69 74 68 6f 75 74 0a |ay, but |without.|
|00000c10| 61 6e 79 20 63 68 69 6c | 64 20 65 6c 65 6d 65 6e |any chil|d elemen|
|00000c20| 74 73 20 61 6e 64 20 74 | 68 65 20 61 64 64 69 74 |ts and t|he addit|
|00000c30| 69 6f 6e 20 6f 66 20 74 | 68 65 20 73 6f 75 72 63 |ion of t|he sourc|
|00000c40| 65 20 74 65 78 74 20 77 | 68 69 63 68 20 77 61 73 |e text w|hich was|
|00000c50| 0a 69 64 65 6e 74 69 66 | 69 65 64 2e 20 20 54 68 |.identif|ied. Th|
|00000c60| 65 20 65 78 61 6d 70 6c | 65 20 6f 66 20 74 68 65 |e exampl|e of the|
|00000c70| 20 5c 63 6f 64 65 7b 69 | 66 7d 20 6b 65 79 77 6f | \code{i|f} keywo|
|00000c80| 72 64 20 61 62 6f 76 65 | 20 69 73 0a 72 65 70 72 |rd above| is.repr|
|00000c90| 65 73 65 6e 74 61 74 69 | 76 65 2e 20 20 54 68 65 |esentati|ve. The|
|00000ca0| 20 76 61 72 69 6f 75 73 | 20 74 79 70 65 73 20 6f | various| types o|
|00000cb0| 66 20 74 65 72 6d 69 6e | 61 6c 20 73 79 6d 62 6f |f termin|al symbo|
|00000cc0| 6c 73 20 61 72 65 20 64 | 65 66 69 6e 65 64 20 69 |ls are d|efined i|
|00000cd0| 6e 0a 74 68 65 20 43 20 | 68 65 61 64 65 72 20 66 |n.the C |header f|
|00000ce0| 69 6c 65 20 5c 63 6f 64 | 65 7b 49 6e 63 6c 75 64 |ile \cod|e{Includ|
|00000cf0| 65 2f 74 6f 6b 65 6e 2e | 68 7d 20 61 6e 64 20 74 |e/token.|h} and t|
|00000d00| 68 65 20 50 79 74 68 6f | 6e 20 6d 6f 64 75 6c 65 |he Pytho|n module|
|00000d10| 0a 5c 63 6f 64 65 7b 4c | 69 62 2f 74 6f 6b 65 6e |.\code{L|ib/token|
|00000d20| 2e 70 79 7d 2e 0a 54 68 | 65 20 41 53 54 20 6f 62 |.py}..Th|e AST ob|
|00000d30| 6a 65 63 74 73 20 61 72 | 65 20 6e 6f 74 20 61 63 |jects ar|e not ac|
|00000d40| 74 75 61 6c 6c 79 20 72 | 65 71 75 69 72 65 64 20 |tually r|equired |
|00000d50| 74 6f 20 73 75 70 70 6f | 72 74 20 74 68 65 20 66 |to suppo|rt the f|
|00000d60| 75 6e 63 74 69 6f 6e 61 | 6c 69 74 79 0a 6f 66 20 |unctiona|lity.of |
|00000d70| 74 68 69 73 20 6d 6f 64 | 75 6c 65 2c 20 62 75 74 |this mod|ule, but|
|00000d80| 20 61 72 65 20 70 72 6f | 76 69 64 65 64 20 66 6f | are pro|vided fo|
|00000d90| 72 20 74 68 72 65 65 20 | 70 75 72 70 6f 73 65 73 |r three |purposes|
|00000da0| 3a 20 74 6f 20 61 6c 6c | 6f 77 20 61 6e 0a 61 70 |: to all|ow an.ap|
|00000db0| 70 6c 69 63 61 74 69 6f | 6e 20 74 6f 20 61 6d 6f |plicatio|n to amo|
|00000dc0| 72 74 69 7a 65 20 74 68 | 65 20 63 6f 73 74 20 6f |rtize th|e cost o|
|00000dd0| 66 20 70 72 6f 63 65 73 | 73 69 6e 67 20 63 6f 6d |f proces|sing com|
|00000de0| 70 6c 65 78 20 70 61 72 | 73 65 20 74 72 65 65 73 |plex par|se trees|
|00000df0| 2c 20 74 6f 0a 70 72 6f | 76 69 64 65 20 61 20 70 |, to.pro|vide a p|
|00000e00| 61 72 73 65 20 74 72 65 | 65 20 72 65 70 72 65 73 |arse tre|e repres|
|00000e10| 65 6e 74 61 74 69 6f 6e | 20 77 68 69 63 68 20 63 |entation| which c|
|00000e20| 6f 6e 73 65 72 76 65 73 | 20 6d 65 6d 6f 72 79 20 |onserves| memory |
|00000e30| 73 70 61 63 65 20 77 68 | 65 6e 0a 63 6f 6d 70 61 |space wh|en.compa|
|00000e40| 72 65 64 20 74 6f 20 74 | 68 65 20 50 79 74 68 6f |red to t|he Pytho|
|00000e50| 6e 20 74 75 70 6c 65 20 | 72 65 70 72 65 73 65 6e |n tuple |represen|
|00000e60| 74 61 74 69 6f 6e 2c 20 | 61 6e 64 20 74 6f 20 65 |tation, |and to e|
|00000e70| 61 73 65 20 74 68 65 20 | 63 72 65 61 74 69 6f 6e |ase the |creation|
|00000e80| 0a 6f 66 20 61 64 64 69 | 74 69 6f 6e 61 6c 20 6d |.of addi|tional m|
|00000e90| 6f 64 75 6c 65 73 20 69 | 6e 20 43 20 77 68 69 63 |odules i|n C whic|
|00000ea0| 68 20 6d 61 6e 69 70 75 | 6c 61 74 65 20 70 61 72 |h manipu|late par|
|00000eb0| 73 65 20 74 72 65 65 73 | 2e 20 20 41 20 73 69 6d |se trees|. A sim|
|00000ec0| 70 6c 65 0a 60 60 77 72 | 61 70 70 65 72 27 27 20 |ple.``wr|apper'' |
|00000ed0| 6d 6f 64 75 6c 65 20 6d | 61 79 20 62 65 20 63 72 |module m|ay be cr|
|00000ee0| 65 61 74 65 64 20 69 6e | 20 50 79 74 68 6f 6e 20 |eated in| Python |
|00000ef0| 69 66 20 64 65 73 69 72 | 65 64 20 74 6f 20 68 69 |if desir|ed to hi|
|00000f00| 64 65 20 74 68 65 20 75 | 73 65 0a 6f 66 20 41 53 |de the u|se.of AS|
|00000f10| 54 20 6f 62 6a 65 63 74 | 73 2e 0a 25 20 3d 3d 3d |T object|s..% ===|
|00000f20| 3d 20 33 2e 20 3d 3d 3d | 3d 0a 25 20 4c 69 73 74 |= 3. ===|=.% List|
|00000f30| 20 74 68 65 20 70 75 62 | 6c 69 63 20 66 75 6e 63 | the pub|lic func|
|00000f40| 74 69 6f 6e 73 20 64 65 | 66 69 6e 65 64 20 62 79 |tions de|fined by|
|00000f50| 20 74 68 65 20 6d 6f 64 | 75 6c 65 2e 20 20 42 65 | the mod|ule. Be|
|00000f60| 67 69 6e 20 77 69 74 68 | 20 61 0a 25 20 73 74 61 |gin with| a.% sta|
|00000f70| 6e 64 61 72 64 20 70 68 | 72 61 73 65 2e 20 20 59 |ndard ph|rase. Y|
|00000f80| 6f 75 20 6d 61 79 20 61 | 6c 73 6f 20 6c 69 73 74 |ou may a|lso list|
|00000f90| 20 74 68 65 20 65 78 63 | 65 70 74 69 6f 6e 73 20 | the exc|eptions |
|00000fa0| 61 6e 64 20 6f 74 68 65 | 72 20 64 61 74 61 0a 25 |and othe|r data.%|
|00000fb0| 20 69 74 65 6d 73 20 64 | 65 66 69 6e 65 64 20 69 | items d|efined i|
|00000fc0| 6e 20 74 68 65 20 6d 6f | 64 75 6c 65 2c 20 69 6e |n the mo|dule, in|
|00000fd0| 73 6f 66 61 72 20 61 73 | 20 74 68 65 79 20 61 72 |sofar as| they ar|
|00000fe0| 65 20 69 6d 70 6f 72 74 | 61 6e 74 20 66 6f 72 20 |e import|ant for |
|00000ff0| 74 68 65 0a 25 20 75 73 | 65 72 2e 0a 54 68 65 20 |the.% us|er..The |
|00001000| 5c 63 6f 64 65 7b 70 61 | 72 73 65 72 7d 20 6d 6f |\code{pa|rser} mo|
|00001010| 64 75 6c 65 20 64 65 66 | 69 6e 65 73 20 74 68 65 |dule def|ines the|
|00001020| 20 66 6f 6c 6c 6f 77 69 | 6e 67 20 66 75 6e 63 74 | followi|ng funct|
|00001030| 69 6f 6e 73 3a 0a 25 20 | 2d 2d 2d 2d 20 33 2e 31 |ions:.% |---- 3.1|
|00001040| 2e 20 2d 2d 2d 2d 0a 25 | 20 52 65 64 65 66 69 6e |. ----.%| Redefin|
|00001050| 65 20 74 68 65 20 60 60 | 69 6e 64 65 78 73 75 62 |e the ``|indexsub|
|00001060| 69 74 65 6d 27 27 20 6d | 61 63 72 6f 20 74 6f 20 |item'' m|acro to |
|00001070| 70 6f 69 6e 74 20 74 6f | 20 74 68 69 73 20 6d 6f |point to| this mo|
|00001080| 64 75 6c 65 0a 25 20 28 | 61 6c 74 65 72 6e 61 74 |dule.% (|alternat|
|00001090| 69 76 65 6c 79 2c 20 79 | 6f 75 20 63 61 6e 20 70 |ively, y|ou can p|
|000010a0| 75 74 20 74 68 69 73 20 | 61 74 20 74 68 65 20 74 |ut this |at the t|
|000010b0| 6f 70 20 6f 66 20 74 68 | 65 20 66 69 6c 65 29 3a |op of th|e file):|
|000010c0| 0a 5c 72 65 6e 65 77 63 | 6f 6d 6d 61 6e 64 7b 5c |.\renewc|ommand{\|
|000010d0| 69 6e 64 65 78 73 75 62 | 69 74 65 6d 7d 7b 28 69 |indexsub|item}{(i|
|000010e0| 6e 20 6d 6f 64 75 6c 65 | 20 70 61 72 73 65 72 29 |n module| parser)|
|000010f0| 7d 0a 25 20 2d 2d 2d 2d | 20 33 2e 32 2e 20 2d 2d |}.% ----| 3.2. --|
|00001100| 2d 2d 0a 25 20 46 6f 72 | 20 65 61 63 68 20 66 75 |--.% For| each fu|
|00001110| 6e 63 74 69 6f 6e 2c 20 | 75 73 65 20 61 20 60 60 |nction, |use a ``|
|00001120| 66 75 6e 63 64 65 73 63 | 27 27 20 62 6c 6f 63 6b |funcdesc|'' block|
|00001130| 2e 20 20 54 68 69 73 20 | 68 61 73 20 65 78 61 63 |. This |has exac|
|00001140| 74 6c 79 20 74 77 6f 0a | 25 20 70 61 72 61 6d 65 |tly two.|% parame|
|00001150| 74 65 72 73 20 28 65 61 | 63 68 20 70 61 72 61 6d |ters (ea|ch param|
|00001160| 65 74 65 72 73 20 69 73 | 20 63 6f 6e 74 61 69 6e |eters is| contain|
|00001170| 65 64 20 69 6e 20 61 20 | 73 65 74 20 6f 66 20 63 |ed in a |set of c|
|00001180| 75 72 6c 79 20 62 72 61 | 63 65 73 29 3a 0a 25 20 |urly bra|ces):.% |
|00001190| 74 68 65 20 66 69 72 73 | 74 20 70 61 72 61 6d 65 |the firs|t parame|
|000011a0| 74 65 72 20 69 73 20 74 | 68 65 20 66 75 6e 63 74 |ter is t|he funct|
|000011b0| 69 6f 6e 20 6e 61 6d 65 | 20 28 74 68 69 73 20 61 |ion name| (this a|
|000011c0| 75 74 6f 6d 61 74 69 63 | 61 6c 6c 79 0a 25 20 67 |utomatic|ally.% g|
|000011d0| 65 6e 65 72 61 74 65 73 | 20 61 6e 20 69 6e 64 65 |enerates| an inde|
|000011e0| 78 20 65 6e 74 72 79 29 | 3b 20 74 68 65 20 73 65 |x entry)|; the se|
|000011f0| 63 6f 6e 64 20 70 61 72 | 61 6d 65 74 65 72 20 69 |cond par|ameter i|
|00001200| 73 20 74 68 65 20 66 75 | 6e 63 74 69 6f 6e 27 73 |s the fu|nction's|
|00001210| 0a 25 20 61 72 67 75 6d | 65 6e 74 20 6c 69 73 74 |.% argum|ent list|
|00001220| 2e 20 20 49 66 20 74 68 | 65 72 65 20 61 72 65 20 |. If th|ere are |
|00001230| 6e 6f 20 61 72 67 75 6d | 65 6e 74 73 2c 20 75 73 |no argum|ents, us|
|00001240| 65 20 61 6e 20 65 6d 70 | 74 79 20 70 61 69 72 20 |e an emp|ty pair |
|00001250| 6f 66 0a 25 20 63 75 72 | 6c 79 20 62 72 61 63 65 |of.% cur|ly brace|
|00001260| 73 2e 20 20 49 66 20 74 | 68 65 72 65 20 69 73 20 |s. If t|here is |
|00001270| 6d 6f 72 65 20 74 68 61 | 6e 20 6f 6e 65 20 61 72 |more tha|n one ar|
|00001280| 67 75 6d 65 6e 74 2c 20 | 73 65 70 61 72 61 74 65 |gument, |separate|
|00001290| 20 74 68 65 0a 25 20 61 | 72 67 75 6d 65 6e 74 73 | the.% a|rguments|
|000012a0| 20 77 69 74 68 20 62 61 | 63 6b 73 6c 61 73 68 2d | with ba|ckslash-|
|000012b0| 63 6f 6d 6d 61 2e 20 20 | 4f 70 74 69 6f 6e 61 6c |comma. |Optional|
|000012c0| 20 70 61 72 74 73 20 6f | 66 20 74 68 65 20 70 61 | parts o|f the pa|
|000012d0| 72 61 6d 65 74 65 72 0a | 25 20 6c 69 73 74 20 61 |rameter.|% list a|
|000012e0| 72 65 20 63 6f 6e 74 61 | 69 6e 65 64 20 69 6e 20 |re conta|ined in |
|000012f0| 5c 6f 70 74 69 6f 6e 61 | 6c 7b 2e 2e 2e 7d 20 28 |\optiona|l{...} (|
|00001300| 74 68 69 73 20 67 65 6e | 65 72 61 74 65 73 20 61 |this gen|erates a|
|00001310| 20 73 65 74 20 6f 66 20 | 73 71 75 61 72 65 0a 25 | set of |square.%|
|00001320| 20 62 72 61 63 6b 65 74 | 73 20 61 72 6f 75 6e 64 | bracket|s around|
|00001330| 20 69 74 73 20 70 61 72 | 61 6d 65 74 65 72 29 2e | its par|ameter).|
|00001340| 20 20 41 72 67 75 6d 65 | 6e 74 73 20 61 72 65 20 | Argume|nts are |
|00001350| 61 75 74 6f 6d 61 74 69 | 63 61 6c 6c 79 20 73 65 |automati|cally se|
|00001360| 74 20 69 6e 0a 25 20 69 | 74 61 6c 69 63 73 20 69 |t in.% i|talics i|
|00001370| 6e 20 74 68 65 20 70 61 | 72 61 6d 65 74 65 72 20 |n the pa|rameter |
|00001380| 6c 69 73 74 2e 20 20 45 | 61 63 68 20 61 72 67 75 |list. E|ach argu|
|00001390| 6d 65 6e 74 20 73 68 6f | 75 6c 64 20 62 65 20 6d |ment sho|uld be m|
|000013a0| 65 6e 74 69 6f 6e 65 64 | 20 61 74 0a 25 20 6c 65 |entioned| at.% le|
|000013b0| 61 73 74 20 6f 6e 63 65 | 20 69 6e 20 74 68 65 20 |ast once| in the |
|000013c0| 64 65 73 63 72 69 70 74 | 69 6f 6e 3b 20 65 61 63 |descript|ion; eac|
|000013d0| 68 20 75 73 61 67 65 20 | 28 65 76 65 6e 20 69 6e |h usage |(even in|
|000013e0| 73 69 64 65 20 5c 63 6f | 64 65 7b 2e 2e 2e 7d 29 |side \co|de{...})|
|000013f0| 0a 25 20 73 68 6f 75 6c | 64 20 62 65 20 65 6e 63 |.% shoul|d be enc|
|00001400| 6c 6f 73 65 64 20 69 6e | 20 5c 76 61 72 7b 2e 2e |losed in| \var{..|
|00001410| 2e 7d 2e 0a 5c 62 65 67 | 69 6e 7b 66 75 6e 63 64 |.}..\beg|in{funcd|
|00001420| 65 73 63 7d 7b 61 73 74 | 32 74 75 70 6c 65 7d 7b |esc}{ast|2tuple}{|
|00001430| 61 73 74 7d 0a 54 68 69 | 73 20 66 75 6e 63 74 69 |ast}.Thi|s functi|
|00001440| 6f 6e 20 61 63 63 65 70 | 74 73 20 61 6e 20 41 53 |on accep|ts an AS|
|00001450| 54 20 6f 62 6a 65 63 74 | 20 66 72 6f 6d 20 74 68 |T object| from th|
|00001460| 65 20 63 61 6c 6c 65 72 | 20 69 6e 0a 5c 63 6f 64 |e caller| in.\cod|
|00001470| 65 7b 5c 76 61 72 7b 61 | 73 74 7d 7d 20 61 6e 64 |e{\var{a|st}} and|
|00001480| 20 72 65 74 75 72 6e 73 | 20 61 20 50 79 74 68 6f | returns| a Pytho|
|00001490| 6e 20 74 75 70 6c 65 20 | 72 65 70 72 65 73 65 6e |n tuple |represen|
|000014a0| 74 69 6e 67 20 74 68 65 | 0a 65 71 75 69 76 65 6c |ting the|.equivel|
|000014b0| 65 6e 74 20 70 61 72 73 | 65 20 74 72 65 65 2e 20 |ent pars|e tree. |
|000014c0| 20 54 68 65 20 72 65 73 | 75 6c 74 69 6e 67 20 74 | The res|ulting t|
|000014d0| 75 70 6c 65 20 72 65 70 | 72 65 73 65 6e 74 61 74 |uple rep|resentat|
|000014e0| 69 6f 6e 20 63 61 6e 20 | 62 65 20 75 73 65 64 0a |ion can |be used.|
|000014f0| 66 6f 72 20 69 6e 73 70 | 65 63 74 69 6f 6e 20 6f |for insp|ection o|
|00001500| 72 20 74 68 65 20 63 72 | 65 61 74 69 6f 6e 20 6f |r the cr|eation o|
|00001510| 66 20 61 20 6e 65 77 20 | 70 61 72 73 65 20 74 72 |f a new |parse tr|
|00001520| 65 65 20 69 6e 20 74 75 | 70 6c 65 20 66 6f 72 6d |ee in tu|ple form|
|00001530| 2e 0a 54 68 69 73 20 66 | 75 6e 63 74 69 6f 6e 20 |..This f|unction |
|00001540| 64 6f 65 73 20 6e 6f 74 | 20 66 61 69 6c 20 73 6f |does not| fail so|
|00001550| 20 6c 6f 6e 67 20 61 73 | 20 6d 65 6d 6f 72 79 20 | long as| memory |
|00001560| 69 73 20 61 76 61 69 6c | 61 62 6c 65 20 74 6f 20 |is avail|able to |
|00001570| 62 75 69 6c 64 0a 74 68 | 65 20 74 75 70 6c 65 20 |build.th|e tuple |
|00001580| 72 65 70 72 65 73 65 6e | 74 61 74 69 6f 6e 2e 0a |represen|tation..|
|00001590| 5c 65 6e 64 7b 66 75 6e | 63 64 65 73 63 7d 0a 5c |\end{fun|cdesc}.\|
|000015a0| 62 65 67 69 6e 7b 66 75 | 6e 63 64 65 73 63 7d 7b |begin{fu|ncdesc}{|
|000015b0| 63 6f 6d 70 69 6c 65 61 | 73 74 7d 7b 61 73 74 5c |compilea|st}{ast\|
|000015c0| 6f 70 74 69 6f 6e 61 6c | 7b 5c 2c 20 66 69 6c 65 |optional|{\, file|
|000015d0| 6e 61 6d 65 20 5c 63 6f | 64 65 7b 3d 20 27 3c 61 |name \co|de{= '<a|
|000015e0| 73 74 3e 27 7d 7d 7d 0a | 54 68 65 20 50 79 74 68 |st>'}}}.|The Pyth|
|000015f0| 6f 6e 20 62 79 74 65 20 | 63 6f 6d 70 69 6c 65 72 |on byte |compiler|
|00001600| 20 63 61 6e 20 62 65 20 | 69 6e 76 6f 6b 65 64 20 | can be |invoked |
|00001610| 6f 6e 20 61 6e 20 41 53 | 54 20 6f 62 6a 65 63 74 |on an AS|T object|
|00001620| 20 74 6f 20 70 72 6f 64 | 75 63 65 0a 63 6f 64 65 | to prod|uce.code|
|00001630| 20 6f 62 6a 65 63 74 73 | 20 77 68 69 63 68 20 63 | objects| which c|
|00001640| 61 6e 20 62 65 20 75 73 | 65 64 20 61 73 20 70 61 |an be us|ed as pa|
|00001650| 72 74 20 6f 66 20 61 6e | 20 5c 63 6f 64 65 7b 65 |rt of an| \code{e|
|00001660| 78 65 63 7d 20 73 74 61 | 74 65 6d 65 6e 74 20 6f |xec} sta|tement o|
|00001670| 72 0a 61 20 63 61 6c 6c | 20 74 6f 20 74 68 65 20 |r.a call| to the |
|00001680| 62 75 69 6c 74 2d 69 6e | 20 5c 63 6f 64 65 7b 65 |built-in| \code{e|
|00001690| 76 61 6c 28 29 7d 20 66 | 75 6e 63 74 69 6f 6e 2e |val()} f|unction.|
|000016a0| 20 20 54 68 69 73 20 66 | 75 6e 63 74 69 6f 6e 20 | This f|unction |
|000016b0| 70 72 6f 76 69 64 65 73 | 0a 74 68 65 20 69 6e 74 |provides|.the int|
|000016c0| 65 72 66 61 63 65 20 74 | 6f 20 74 68 65 20 63 6f |erface t|o the co|
|000016d0| 6d 70 69 6c 65 72 2c 20 | 70 61 73 73 69 6e 67 20 |mpiler, |passing |
|000016e0| 74 68 65 20 69 6e 74 65 | 72 6e 61 6c 20 70 61 72 |the inte|rnal par|
|000016f0| 73 65 20 74 72 65 65 20 | 66 72 6f 6d 0a 5c 63 6f |se tree |from.\co|
|00001700| 64 65 7b 5c 76 61 72 7b | 61 73 74 7d 7d 20 74 6f |de{\var{|ast}} to|
|00001710| 20 74 68 65 20 70 61 72 | 73 65 72 2c 20 75 73 69 | the par|ser, usi|
|00001720| 6e 67 20 74 68 65 20 73 | 6f 75 72 63 65 20 66 69 |ng the s|ource fi|
|00001730| 6c 65 20 6e 61 6d 65 20 | 73 70 65 63 69 66 69 65 |le name |specifie|
|00001740| 64 0a 62 79 20 74 68 65 | 20 5c 63 6f 64 65 7b 5c |d.by the| \code{\|
|00001750| 76 61 72 7b 66 69 6c 65 | 6e 61 6d 65 7d 7d 20 70 |var{file|name}} p|
|00001760| 61 72 61 6d 65 74 65 72 | 2e 20 20 54 68 65 20 64 |arameter|. The d|
|00001770| 65 66 61 75 6c 74 20 76 | 61 6c 75 65 20 73 75 70 |efault v|alue sup|
|00001780| 70 6c 69 65 64 0a 66 6f | 72 20 5c 63 6f 64 65 7b |plied.fo|r \code{|
|00001790| 5c 76 61 72 7b 66 69 6c | 65 6e 61 6d 65 7d 7d 20 |\var{fil|ename}} |
|000017a0| 69 6e 64 69 63 61 74 65 | 73 20 74 68 61 74 20 74 |indicate|s that t|
|000017b0| 68 65 20 73 6f 75 72 63 | 65 20 77 61 73 20 61 6e |he sourc|e was an|
|000017c0| 20 41 53 54 20 6f 62 6a | 65 63 74 2e 0a 5c 65 6e | AST obj|ect..\en|
|000017d0| 64 7b 66 75 6e 63 64 65 | 73 63 7d 0a 5c 62 65 67 |d{funcde|sc}.\beg|
|000017e0| 69 6e 7b 66 75 6e 63 64 | 65 73 63 7d 7b 65 78 70 |in{funcd|esc}{exp|
|000017f0| 72 7d 7b 73 74 72 69 6e | 67 7d 0a 54 68 65 20 5c |r}{strin|g}.The \|
|00001800| 63 6f 64 65 7b 65 78 70 | 72 28 29 7d 20 66 75 6e |code{exp|r()} fun|
|00001810| 63 74 69 6f 6e 20 70 61 | 72 73 65 73 20 74 68 65 |ction pa|rses the|
|00001820| 20 70 61 72 61 6d 65 74 | 65 72 20 5c 63 6f 64 65 | paramet|er \code|
|00001830| 7b 5c 76 61 72 7b 73 74 | 72 69 6e 67 7d 7d 0a 61 |{\var{st|ring}}.a|
|00001840| 73 20 69 66 20 69 74 20 | 77 65 72 65 20 61 6e 20 |s if it |were an |
|00001850| 69 6e 70 75 74 20 74 6f | 20 5c 63 6f 64 65 7b 63 |input to| \code{c|
|00001860| 6f 6d 70 69 6c 65 28 5c | 76 61 72 7b 73 74 72 69 |ompile(\|var{stri|
|00001870| 6e 67 7d 2c 20 27 65 76 | 61 6c 27 29 7d 2e 20 20 |ng}, 'ev|al')}. |
|00001880| 49 66 0a 74 68 65 20 70 | 61 72 73 65 20 73 75 63 |If.the p|arse suc|
|00001890| 63 65 65 64 73 2c 20 61 | 6e 20 41 53 54 20 6f 62 |ceeds, a|n AST ob|
|000018a0| 6a 65 63 74 20 69 73 20 | 63 72 65 61 74 65 64 20 |ject is |created |
|000018b0| 74 6f 20 68 6f 6c 64 20 | 74 68 65 20 69 6e 74 65 |to hold |the inte|
|000018c0| 72 6e 61 6c 0a 70 61 72 | 73 65 20 74 72 65 65 20 |rnal.par|se tree |
|000018d0| 72 65 70 72 65 73 65 6e | 74 61 74 69 6f 6e 2c 20 |represen|tation, |
|000018e0| 6f 74 68 65 72 77 69 73 | 65 20 61 6e 20 61 70 70 |otherwis|e an app|
|000018f0| 72 6f 70 72 69 61 74 65 | 20 65 78 63 65 70 74 69 |ropriate| excepti|
|00001900| 6f 6e 20 69 73 0a 74 68 | 72 6f 77 6e 2e 0a 5c 65 |on is.th|rown..\e|
|00001910| 6e 64 7b 66 75 6e 63 64 | 65 73 63 7d 0a 5c 62 65 |nd{funcd|esc}.\be|
|00001920| 67 69 6e 7b 66 75 6e 63 | 64 65 73 63 7d 7b 69 73 |gin{func|desc}{is|
|00001930| 65 78 70 72 7d 7b 61 73 | 74 7d 0a 57 68 65 6e 20 |expr}{as|t}.When |
|00001940| 5c 63 6f 64 65 7b 5c 76 | 61 72 7b 61 73 74 7d 7d |\code{\v|ar{ast}}|
|00001950| 20 72 65 70 72 65 73 65 | 6e 74 73 20 61 6e 20 5c | represe|nts an \|
|00001960| 63 6f 64 65 7b 27 65 76 | 61 6c 27 7d 20 66 6f 72 |code{'ev|al'} for|
|00001970| 6d 2c 20 74 68 69 73 20 | 66 75 6e 63 74 69 6f 6e |m, this |function|
|00001980| 0a 72 65 74 75 72 6e 73 | 20 61 20 74 72 75 65 20 |.returns| a true |
|00001990| 76 61 6c 75 65 20 28 5c | 63 6f 64 65 7b 31 7d 29 |value (\|code{1})|
|000019a0| 2c 20 6f 74 68 65 72 77 | 69 73 65 20 69 74 20 72 |, otherw|ise it r|
|000019b0| 65 74 75 72 6e 73 20 66 | 61 6c 73 65 0a 28 5c 63 |eturns f|alse.(\c|
|000019c0| 6f 64 65 7b 30 7d 29 2e | 20 20 54 68 69 73 20 69 |ode{0}).| This i|
|000019d0| 73 20 75 73 65 66 75 6c | 2c 20 73 69 6e 63 65 20 |s useful|, since |
|000019e0| 63 6f 64 65 20 6f 62 6a | 65 63 74 73 20 6e 6f 72 |code obj|ects nor|
|000019f0| 6d 61 6c 6c 79 20 63 61 | 6e 6e 6f 74 20 62 65 0a |mally ca|nnot be.|
|00001a00| 71 75 65 72 69 65 64 20 | 66 6f 72 20 74 68 69 73 |queried |for this|
|00001a10| 20 69 6e 66 6f 72 6d 61 | 74 69 6f 6e 20 75 73 69 | informa|tion usi|
|00001a20| 6e 67 20 65 78 69 73 74 | 69 6e 67 20 62 75 69 6c |ng exist|ing buil|
|00001a30| 74 2d 69 6e 20 66 75 6e | 63 74 69 6f 6e 73 2e 20 |t-in fun|ctions. |
|00001a40| 20 4e 6f 74 65 0a 74 68 | 61 74 20 74 68 65 20 63 | Note.th|at the c|
|00001a50| 6f 64 65 20 6f 62 6a 65 | 63 74 73 20 63 72 65 61 |ode obje|cts crea|
|00001a60| 74 65 64 20 62 79 20 5c | 63 6f 64 65 7b 63 6f 6d |ted by \|code{com|
|00001a70| 70 69 6c 65 61 73 74 28 | 29 7d 20 63 61 6e 6e 6f |pileast(|)} canno|
|00001a80| 74 20 62 65 20 71 75 65 | 72 69 65 64 0a 6c 69 6b |t be que|ried.lik|
|00001a90| 65 20 74 68 69 73 20 65 | 69 74 68 65 72 2c 20 61 |e this e|ither, a|
|00001aa0| 6e 64 20 61 72 65 20 69 | 64 65 6e 74 69 63 61 6c |nd are i|dentical|
|00001ab0| 20 74 6f 20 74 68 6f 73 | 65 20 63 72 65 61 74 65 | to thos|e create|
|00001ac0| 64 20 62 79 20 74 68 65 | 20 62 75 69 6c 74 2d 69 |d by the| built-i|
|00001ad0| 6e 0a 5c 63 6f 64 65 7b | 63 6f 6d 70 69 6c 65 28 |n.\code{|compile(|
|00001ae0| 29 7d 20 66 75 6e 63 74 | 69 6f 6e 2e 0a 5c 65 6e |)} funct|ion..\en|
|00001af0| 64 7b 66 75 6e 63 64 65 | 73 63 7d 0a 5c 62 65 67 |d{funcde|sc}.\beg|
|00001b00| 69 6e 7b 66 75 6e 63 64 | 65 73 63 7d 7b 69 73 73 |in{funcd|esc}{iss|
|00001b10| 75 69 74 65 7d 7b 61 73 | 74 7d 0a 54 68 69 73 20 |uite}{as|t}.This |
|00001b20| 66 75 6e 63 74 69 6f 6e | 20 6d 69 72 72 6f 72 73 |function| mirrors|
|00001b30| 20 5c 63 6f 64 65 7b 69 | 73 65 78 70 72 28 29 7d | \code{i|sexpr()}|
|00001b40| 20 69 6e 20 74 68 61 74 | 20 69 74 20 72 65 70 6f | in that| it repo|
|00001b50| 72 74 73 20 77 68 65 74 | 68 65 72 20 61 6e 0a 41 |rts whet|her an.A|
|00001b60| 53 54 20 6f 62 6a 65 63 | 74 20 72 65 70 72 65 73 |ST objec|t repres|
|00001b70| 65 6e 74 73 20 61 20 73 | 75 69 74 65 20 6f 66 20 |ents a s|uite of |
|00001b80| 73 74 61 74 65 6d 65 6e | 74 73 2e 20 20 49 74 20 |statemen|ts. It |
|00001b90| 69 73 20 6e 6f 74 20 73 | 61 66 65 20 74 6f 20 61 |is not s|afe to a|
|00001ba0| 73 73 75 6d 65 0a 74 68 | 61 74 20 74 68 69 73 20 |ssume.th|at this |
|00001bb0| 66 75 6e 63 74 69 6f 6e | 20 69 73 20 65 71 75 69 |function| is equi|
|00001bc0| 76 65 6c 65 6e 74 20 74 | 6f 20 5c 63 6f 64 65 7b |velent t|o \code{|
|00001bd0| 6e 6f 74 20 69 73 65 78 | 70 72 28 5c 76 61 72 7b |not isex|pr(\var{|
|00001be0| 61 73 74 7d 29 7d 2c 20 | 61 73 0a 61 64 64 69 74 |ast})}, |as.addit|
|00001bf0| 69 6f 6e 61 6c 20 73 79 | 6e 74 61 63 74 69 63 20 |ional sy|ntactic |
|00001c00| 66 72 61 67 6d 65 6e 74 | 73 20 6d 61 79 20 62 65 |fragment|s may be|
|00001c10| 20 73 75 70 70 6f 72 74 | 65 64 20 69 6e 20 74 68 | support|ed in th|
|00001c20| 65 20 66 75 74 75 72 65 | 2e 0a 5c 65 6e 64 7b 66 |e future|..\end{f|
|00001c30| 75 6e 63 64 65 73 63 7d | 0a 5c 62 65 67 69 6e 7b |uncdesc}|.\begin{|
|00001c40| 66 75 6e 63 64 65 73 63 | 7d 7b 73 75 69 74 65 7d |funcdesc|}{suite}|
|00001c50| 7b 73 74 72 69 6e 67 7d | 0a 54 68 65 20 5c 63 6f |{string}|.The \co|
|00001c60| 64 65 7b 73 75 69 74 65 | 28 29 7d 20 66 75 6e 63 |de{suite|()} func|
|00001c70| 74 69 6f 6e 20 70 61 72 | 73 65 73 20 74 68 65 20 |tion par|ses the |
|00001c80| 70 61 72 61 6d 65 74 65 | 72 20 5c 63 6f 64 65 7b |paramete|r \code{|
|00001c90| 5c 76 61 72 7b 73 74 72 | 69 6e 67 7d 7d 0a 61 73 |\var{str|ing}}.as|
|00001ca0| 20 69 66 20 69 74 20 77 | 65 72 65 20 61 6e 20 69 | if it w|ere an i|
|00001cb0| 6e 70 75 74 20 74 6f 20 | 5c 63 6f 64 65 7b 63 6f |nput to |\code{co|
|00001cc0| 6d 70 69 6c 65 28 5c 76 | 61 72 7b 73 74 72 69 6e |mpile(\v|ar{strin|
|00001cd0| 67 7d 2c 20 27 65 78 65 | 63 27 29 7d 2e 20 20 49 |g}, 'exe|c')}. I|
|00001ce0| 66 0a 74 68 65 20 70 61 | 72 73 65 20 73 75 63 63 |f.the pa|rse succ|
|00001cf0| 65 65 64 73 2c 20 61 6e | 20 41 53 54 20 6f 62 6a |eeds, an| AST obj|
|00001d00| 65 63 74 20 69 73 20 63 | 72 65 61 74 65 64 20 74 |ect is c|reated t|
|00001d10| 6f 20 68 6f 6c 64 20 74 | 68 65 20 69 6e 74 65 72 |o hold t|he inter|
|00001d20| 6e 61 6c 0a 70 61 72 73 | 65 20 74 72 65 65 20 72 |nal.pars|e tree r|
|00001d30| 65 70 72 65 73 65 6e 74 | 61 74 69 6f 6e 2c 20 6f |epresent|ation, o|
|00001d40| 74 68 65 72 77 69 73 65 | 20 61 6e 20 61 70 70 72 |therwise| an appr|
|00001d50| 6f 70 72 69 61 74 65 20 | 65 78 63 65 70 74 69 6f |opriate |exceptio|
|00001d60| 6e 20 69 73 0a 74 68 72 | 6f 77 6e 2e 0a 5c 65 6e |n is.thr|own..\en|
|00001d70| 64 7b 66 75 6e 63 64 65 | 73 63 7d 0a 5c 62 65 67 |d{funcde|sc}.\beg|
|00001d80| 69 6e 7b 66 75 6e 63 64 | 65 73 63 7d 7b 74 75 70 |in{funcd|esc}{tup|
|00001d90| 6c 65 32 61 73 74 7d 7b | 74 75 70 6c 65 7d 0a 54 |le2ast}{|tuple}.T|
|00001da0| 68 69 73 20 66 75 6e 63 | 74 69 6f 6e 20 61 63 63 |his func|tion acc|
|00001db0| 65 70 74 73 20 61 20 70 | 61 72 73 65 20 74 72 65 |epts a p|arse tre|
|00001dc0| 65 20 72 65 70 72 65 73 | 65 6e 74 65 64 20 61 73 |e repres|ented as|
|00001dd0| 20 61 20 74 75 70 6c 65 | 20 61 6e 64 20 62 75 69 | a tuple| and bui|
|00001de0| 6c 64 73 0a 61 6e 20 69 | 6e 74 65 72 6e 61 6c 20 |lds.an i|nternal |
|00001df0| 72 65 70 72 65 73 65 6e | 74 61 74 69 6f 6e 20 69 |represen|tation i|
|00001e00| 66 20 70 6f 73 73 69 62 | 6c 65 2e 20 20 49 66 20 |f possib|le. If |
|00001e10| 69 74 20 63 61 6e 20 76 | 61 6c 69 64 61 74 65 20 |it can v|alidate |
|00001e20| 74 68 61 74 20 74 68 65 | 0a 74 72 65 65 20 63 6f |that the|.tree co|
|00001e30| 6e 66 6f 72 6d 73 20 74 | 6f 20 74 68 65 20 50 79 |nforms t|o the Py|
|00001e40| 74 68 6f 6e 20 73 79 6e | 74 61 78 20 61 6e 64 20 |thon syn|tax and |
|00001e50| 61 6c 6c 20 6e 6f 64 65 | 73 20 61 72 65 20 76 61 |all node|s are va|
|00001e60| 6c 69 64 20 6e 6f 64 65 | 20 74 79 70 65 73 0a 69 |lid node| types.i|
|00001e70| 6e 20 74 68 65 20 68 6f | 73 74 20 76 65 72 73 69 |n the ho|st versi|
|00001e80| 6f 6e 20 6f 66 20 50 79 | 74 68 6f 6e 2c 20 61 6e |on of Py|thon, an|
|00001e90| 20 41 53 54 20 6f 62 6a | 65 63 74 20 69 73 20 63 | AST obj|ect is c|
|00001ea0| 72 65 61 74 65 64 20 66 | 72 6f 6d 20 74 68 65 0a |reated f|rom the.|
|00001eb0| 69 6e 74 65 72 6e 61 6c | 20 72 65 70 72 65 73 65 |internal| represe|
|00001ec0| 6e 74 61 74 69 6f 6e 20 | 61 6e 64 20 72 65 74 75 |ntation |and retu|
|00001ed0| 72 6e 65 64 20 74 6f 20 | 74 68 65 20 63 61 6c 6c |rned to |the call|
|00001ee0| 65 64 2e 20 20 49 66 20 | 74 68 65 72 65 20 69 73 |ed. If |there is|
|00001ef0| 20 61 0a 70 72 6f 62 6c | 65 6d 20 63 72 65 61 74 | a.probl|em creat|
|00001f00| 69 6e 67 20 74 68 65 20 | 69 6e 74 65 72 6e 61 6c |ing the |internal|
|00001f10| 20 72 65 70 72 65 73 65 | 6e 74 61 74 69 6f 6e 2c | represe|ntation,|
|00001f20| 20 6f 72 20 69 66 20 74 | 68 65 20 74 72 65 65 20 | or if t|he tree |
|00001f30| 63 61 6e 6e 6f 74 20 62 | 65 0a 76 61 6c 69 64 61 |cannot b|e.valida|
|00001f40| 74 65 64 2c 20 61 20 5c | 63 6f 64 65 7b 50 61 72 |ted, a \|code{Par|
|00001f50| 73 65 72 45 72 72 6f 72 | 7d 20 65 78 63 65 70 74 |serError|} except|
|00001f60| 69 6f 6e 20 69 73 20 74 | 68 72 6f 77 6e 2e 20 20 |ion is t|hrown. |
|00001f70| 41 6e 20 41 53 54 20 6f | 62 6a 65 63 74 0a 63 72 |An AST o|bject.cr|
|00001f80| 65 61 74 65 64 20 74 68 | 69 73 20 77 61 79 20 73 |eated th|is way s|
|00001f90| 68 6f 75 6c 64 20 6e 6f | 74 20 62 65 20 61 73 73 |hould no|t be ass|
|00001fa0| 75 6d 65 64 20 74 6f 20 | 63 6f 6d 70 69 6c 65 20 |umed to |compile |
|00001fb0| 63 6f 72 72 65 63 74 6c | 79 3b 20 6e 6f 72 6d 61 |correctl|y; norma|
|00001fc0| 6c 0a 65 78 63 65 70 74 | 69 6f 6e 73 20 74 68 72 |l.except|ions thr|
|00001fd0| 6f 77 6e 20 62 79 20 63 | 6f 6d 70 69 6c 61 74 69 |own by c|ompilati|
|00001fe0| 6f 6e 20 6d 61 79 20 73 | 74 69 6c 6c 20 62 65 20 |on may s|till be |
|00001ff0| 69 6e 69 74 69 61 74 65 | 64 20 77 68 65 6e 20 74 |initiate|d when t|
|00002000| 68 65 20 41 53 54 0a 6f | 62 6a 65 63 74 20 69 73 |he AST.o|bject is|
|00002010| 20 70 61 73 73 65 64 20 | 74 6f 20 5c 63 6f 64 65 | passed |to \code|
|00002020| 7b 63 6f 6d 70 69 6c 65 | 61 73 74 28 29 7d 2e 20 |{compile|ast()}. |
|00002030| 20 54 68 69 73 20 77 69 | 6c 6c 20 6e 6f 72 6d 61 | This wi|ll norma|
|00002040| 6c 6c 79 20 69 6e 64 69 | 63 61 74 65 0a 70 72 6f |lly indi|cate.pro|
|00002050| 62 6c 65 6d 73 20 6e 6f | 74 20 72 65 6c 61 74 65 |blems no|t relate|
|00002060| 64 20 74 6f 20 73 79 6e | 74 61 78 20 28 73 75 63 |d to syn|tax (suc|
|00002070| 68 20 61 73 20 61 20 5c | 63 6f 64 65 7b 4d 65 6d |h as a \|code{Mem|
|00002080| 6f 72 79 45 72 72 6f 72 | 7d 0a 65 78 63 65 70 74 |oryError|}.except|
|00002090| 69 6f 6e 29 2e 0a 5c 65 | 6e 64 7b 66 75 6e 63 64 |ion)..\e|nd{funcd|
|000020a0| 65 73 63 7d 0a 25 20 2d | 2d 2d 20 33 2e 34 2e 20 |esc}.% -|-- 3.4. |
|000020b0| 2d 2d 2d 0a 25 20 45 78 | 63 65 70 74 69 6f 6e 73 |---.% Ex|ceptions|
|000020c0| 20 61 72 65 20 64 65 73 | 63 72 69 62 65 64 20 75 | are des|cribed u|
|000020d0| 73 69 6e 67 20 61 20 60 | 60 65 78 63 64 65 73 63 |sing a `|`excdesc|
|000020e0| 27 27 20 62 6c 6f 63 6b | 2e 20 20 54 68 69 73 20 |'' block|. This |
|000020f0| 68 61 73 20 6f 6e 6c 79 | 0a 25 20 6f 6e 65 20 70 |has only|.% one p|
|00002100| 61 72 61 6d 65 74 65 72 | 3a 20 74 68 65 20 65 78 |arameter|: the ex|
|00002110| 63 65 70 74 69 6f 6e 20 | 6e 61 6d 65 2e 0a 5c 73 |ception |name..\s|
|00002120| 75 62 73 65 63 74 69 6f | 6e 7b 45 78 63 65 70 74 |ubsectio|n{Except|
|00002130| 69 6f 6e 73 20 61 6e 64 | 20 45 72 72 6f 72 20 48 |ions and| Error H|
|00002140| 61 6e 64 6c 69 6e 67 7d | 0a 54 68 65 20 70 61 72 |andling}|.The par|
|00002150| 73 65 72 20 6d 6f 64 75 | 6c 65 20 64 65 66 69 6e |ser modu|le defin|
|00002160| 65 73 20 61 20 73 69 6e | 67 6c 65 20 65 78 63 65 |es a sin|gle exce|
|00002170| 70 74 69 6f 6e 2c 20 62 | 75 74 20 6d 61 79 20 61 |ption, b|ut may a|
|00002180| 6c 73 6f 20 70 61 73 73 | 20 6f 74 68 65 72 0a 62 |lso pass| other.b|
|00002190| 75 69 6c 74 2d 69 6e 20 | 65 78 63 65 70 74 69 6f |uilt-in |exceptio|
|000021a0| 6e 73 20 66 72 6f 6d 20 | 6f 74 68 65 72 20 70 6f |ns from |other po|
|000021b0| 72 74 69 6f 6e 73 20 6f | 66 20 74 68 65 20 50 79 |rtions o|f the Py|
|000021c0| 74 68 6f 6e 20 72 75 6e | 74 69 6d 65 0a 65 6e 76 |thon run|time.env|
|000021d0| 69 72 6f 6e 6d 65 6e 74 | 2e 20 20 53 65 65 20 65 |ironment|. See e|
|000021e0| 61 63 68 20 66 75 6e 63 | 74 69 6f 6e 20 66 6f 72 |ach func|tion for|
|000021f0| 20 69 6e 66 6f 72 6d 61 | 74 69 6f 6e 20 61 62 6f | informa|tion abo|
|00002200| 75 74 20 74 68 65 20 65 | 78 63 65 70 74 69 6f 6e |ut the e|xception|
|00002210| 73 0a 69 74 20 63 61 6e | 20 72 61 69 73 65 2e 0a |s.it can| raise..|
|00002220| 5c 62 65 67 69 6e 7b 65 | 78 63 64 65 73 63 7d 7b |\begin{e|xcdesc}{|
|00002230| 50 61 72 73 65 72 45 72 | 72 6f 72 7d 0a 45 78 63 |ParserEr|ror}.Exc|
|00002240| 65 70 74 69 6f 6e 20 72 | 61 69 73 65 64 20 77 68 |eption r|aised wh|
|00002250| 65 6e 20 61 20 66 61 69 | 6c 75 72 65 20 6f 63 63 |en a fai|lure occ|
|00002260| 75 72 73 20 77 69 74 68 | 69 6e 20 74 68 65 20 70 |urs with|in the p|
|00002270| 61 72 73 65 72 20 6d 6f | 64 75 6c 65 2e 20 20 54 |arser mo|dule. T|
|00002280| 68 69 73 0a 69 73 20 67 | 65 6e 65 72 61 6c 6c 79 |his.is g|enerally|
|00002290| 20 70 72 6f 64 75 63 65 | 64 20 66 6f 72 20 76 61 | produce|d for va|
|000022a0| 6c 69 64 61 74 69 6f 6e | 20 66 61 69 6c 75 72 65 |lidation| failure|
|000022b0| 73 20 72 61 74 68 65 72 | 20 74 68 61 6e 20 74 68 |s rather| than th|
|000022c0| 65 20 62 75 69 6c 74 20 | 69 6e 0a 5c 63 6f 64 65 |e built |in.\code|
|000022d0| 7b 53 79 6e 74 61 78 45 | 72 72 6f 72 7d 20 74 68 |{SyntaxE|rror} th|
|000022e0| 72 6f 77 6e 20 64 75 72 | 69 6e 67 20 6e 6f 72 6d |rown dur|ing norm|
|000022f0| 61 6c 20 70 61 72 73 69 | 6e 67 2e 0a 54 68 65 20 |al parsi|ng..The |
|00002300| 65 78 63 65 70 74 69 6f | 6e 20 61 72 67 75 6d 65 |exceptio|n argume|
|00002310| 6e 74 20 69 73 20 65 69 | 74 68 65 72 20 61 20 73 |nt is ei|ther a s|
|00002320| 74 72 69 6e 67 20 64 65 | 73 63 72 69 62 69 6e 67 |tring de|scribing|
|00002330| 20 74 68 65 20 72 65 61 | 73 6f 6e 20 6f 66 20 74 | the rea|son of t|
|00002340| 68 65 0a 66 61 69 6c 75 | 72 65 20 6f 72 20 61 20 |he.failu|re or a |
|00002350| 74 75 70 6c 65 20 63 6f | 6e 74 61 69 6e 69 6e 67 |tuple co|ntaining|
|00002360| 20 61 20 74 75 70 6c 65 | 20 63 61 75 73 69 6e 67 | a tuple| causing|
|00002370| 20 74 68 65 20 66 61 69 | 6c 75 72 65 20 66 72 6f | the fai|lure fro|
|00002380| 6d 20 61 20 70 61 72 73 | 65 0a 74 72 65 65 20 70 |m a pars|e.tree p|
|00002390| 61 73 73 65 64 20 74 6f | 20 5c 63 6f 64 65 7b 74 |assed to| \code{t|
|000023a0| 75 70 6c 65 32 61 73 74 | 28 29 7d 20 61 6e 64 20 |uple2ast|()} and |
|000023b0| 61 6e 20 65 78 70 6c 61 | 6e 61 74 6f 72 79 20 73 |an expla|natory s|
|000023c0| 74 72 69 6e 67 2e 20 20 | 43 61 6c 6c 73 20 74 6f |tring. |Calls to|
|000023d0| 0a 5c 63 6f 64 65 7b 74 | 75 70 6c 65 32 61 73 74 |.\code{t|uple2ast|
|000023e0| 28 29 7d 20 6e 65 65 64 | 20 74 6f 20 62 65 20 61 |()} need| to be a|
|000023f0| 62 6c 65 20 74 6f 20 68 | 61 6e 64 6c 65 20 65 69 |ble to h|andle ei|
|00002400| 74 68 65 72 20 74 79 70 | 65 20 6f 66 20 65 78 63 |ther typ|e of exc|
|00002410| 65 70 74 69 6f 6e 2c 0a | 77 68 69 6c 65 20 63 61 |eption,.|while ca|
|00002420| 6c 6c 73 20 74 6f 20 6f | 74 68 65 72 20 66 75 6e |lls to o|ther fun|
|00002430| 63 74 69 6f 6e 73 20 69 | 6e 20 74 68 65 20 6d 6f |ctions i|n the mo|
|00002440| 64 75 6c 65 20 77 69 6c | 6c 20 6f 6e 6c 79 20 6e |dule wil|l only n|
|00002450| 65 65 64 20 74 6f 20 62 | 65 0a 61 77 61 72 65 20 |eed to b|e.aware |
|00002460| 6f 66 20 74 68 65 20 73 | 69 6d 70 6c 65 20 73 74 |of the s|imple st|
|00002470| 72 69 6e 67 20 76 61 6c | 75 65 73 2e 0a 5c 65 6e |ring val|ues..\en|
|00002480| 64 7b 65 78 63 64 65 73 | 63 7d 0a 4e 6f 74 65 20 |d{excdes|c}.Note |
|00002490| 74 68 61 74 20 74 68 65 | 20 66 75 6e 63 74 69 6f |that the| functio|
|000024a0| 6e 73 20 5c 63 6f 64 65 | 7b 63 6f 6d 70 69 6c 65 |ns \code|{compile|
|000024b0| 61 73 74 28 29 7d 2c 20 | 5c 63 6f 64 65 7b 65 78 |ast()}, |\code{ex|
|000024c0| 70 72 28 29 7d 2c 20 61 | 6e 64 0a 5c 63 6f 64 65 |pr()}, a|nd.\code|
|000024d0| 7b 73 75 69 74 65 28 29 | 7d 20 6d 61 79 20 74 68 |{suite()|} may th|
|000024e0| 72 6f 77 20 65 78 63 65 | 70 74 69 6f 6e 73 20 77 |row exce|ptions w|
|000024f0| 68 69 63 68 20 61 72 65 | 20 6e 6f 72 6d 61 6c 6c |hich are| normall|
|00002500| 79 20 74 68 72 6f 77 6e | 20 62 79 20 74 68 65 0a |y thrown| by the.|
|00002510| 70 61 72 73 69 6e 67 20 | 61 6e 64 20 63 6f 6d 70 |parsing |and comp|
|00002520| 69 6c 61 74 69 6f 6e 20 | 70 72 6f 63 65 73 73 2e |ilation |process.|
|00002530| 20 20 54 68 65 73 65 20 | 69 6e 63 6c 75 64 65 20 | These |include |
|00002540| 74 68 65 20 62 75 69 6c | 74 20 69 6e 0a 65 78 63 |the buil|t in.exc|
|00002550| 65 70 74 69 6f 6e 73 20 | 5c 63 6f 64 65 7b 4d 65 |eptions |\code{Me|
|00002560| 6d 6f 72 79 45 72 72 6f | 72 7d 2c 20 5c 63 6f 64 |moryErro|r}, \cod|
|00002570| 65 7b 4f 76 65 72 66 6c | 6f 77 45 72 72 6f 72 7d |e{Overfl|owError}|
|00002580| 2c 0a 5c 63 6f 64 65 7b | 53 79 6e 74 61 78 45 72 |,.\code{|SyntaxEr|
|00002590| 72 6f 72 7d 2c 20 61 6e | 64 20 5c 63 6f 64 65 7b |ror}, an|d \code{|
|000025a0| 53 79 73 74 65 6d 45 72 | 72 6f 72 7d 2e 20 20 49 |SystemEr|ror}. I|
|000025b0| 6e 20 74 68 65 73 65 20 | 63 61 73 65 73 2c 20 74 |n these |cases, t|
|000025c0| 68 65 73 65 0a 65 78 63 | 65 70 74 69 6f 6e 73 20 |hese.exc|eptions |
|000025d0| 63 61 72 72 79 20 61 6c | 6c 20 74 68 65 20 6d 65 |carry al|l the me|
|000025e0| 61 6e 69 6e 67 20 6e 6f | 72 6d 61 6c 6c 79 20 61 |aning no|rmally a|
|000025f0| 73 73 6f 63 69 61 74 65 | 64 20 77 69 74 68 20 74 |ssociate|d with t|
|00002600| 68 65 6d 2e 20 20 52 65 | 66 65 72 0a 74 6f 20 74 |hem. Re|fer.to t|
|00002610| 68 65 20 64 65 73 63 72 | 69 70 74 69 6f 6e 73 20 |he descr|iptions |
|00002620| 6f 66 20 65 61 63 68 20 | 66 75 6e 63 74 69 6f 6e |of each |function|
|00002630| 20 66 6f 72 20 64 65 74 | 61 69 6c 65 64 20 69 6e | for det|ailed in|
|00002640| 66 6f 72 6d 61 74 69 6f | 6e 2e 0a 25 20 2d 2d 2d |formatio|n..% ---|
|00002650| 2d 20 33 2e 35 2e 20 2d | 2d 2d 2d 0a 25 20 54 68 |- 3.5. -|---.% Th|
|00002660| 65 72 65 20 69 73 20 6e | 6f 20 73 74 61 6e 64 61 |ere is n|o standa|
|00002670| 72 64 20 62 6c 6f 63 6b | 20 74 79 70 65 20 66 6f |rd block| type fo|
|00002680| 72 20 63 6c 61 73 73 65 | 73 2e 20 20 49 20 67 65 |r classe|s. I ge|
|00002690| 6e 65 72 61 6c 6c 79 20 | 75 73 65 0a 25 20 60 60 |nerally |use.% ``|
|000026a0| 66 75 6e 63 64 65 73 63 | 27 27 20 62 6c 6f 63 6b |funcdesc|'' block|
|000026b0| 73 2c 20 73 69 6e 63 65 | 20 63 6c 61 73 73 20 69 |s, since| class i|
|000026c0| 6e 73 74 61 6e 74 69 61 | 74 69 6f 6e 20 6c 6f 6f |nstantia|tion loo|
|000026d0| 6b 73 20 76 65 72 79 20 | 6d 75 63 68 20 6c 69 6b |ks very |much lik|
|000026e0| 65 0a 25 20 61 20 66 75 | 6e 63 74 69 6f 6e 20 63 |e.% a fu|nction c|
|000026f0| 61 6c 6c 2e 0a 25 20 3d | 3d 3d 3d 20 34 2e 20 3d |all..% =|=== 4. =|
|00002700| 3d 3d 3d 0a 25 20 4e 6f | 77 20 69 73 20 70 72 6f |===.% No|w is pro|
|00002710| 62 61 62 6c 79 20 61 20 | 67 6f 6f 64 20 74 69 6d |bably a |good tim|
|00002720| 65 20 66 6f 72 20 61 20 | 63 6f 6d 70 6c 65 74 65 |e for a |complete|
|00002730| 20 65 78 61 6d 70 6c 65 | 2e 20 20 28 41 6c 74 65 | example|. (Alte|
|00002740| 72 6e 61 74 69 76 65 6c | 79 2c 0a 25 20 61 6e 20 |rnativel|y,.% an |
|00002750| 65 78 61 6d 70 6c 65 20 | 67 69 76 69 6e 67 20 74 |example |giving t|
|00002760| 68 65 20 66 6c 61 76 6f | 72 20 6f 66 20 74 68 65 |he flavo|r of the|
|00002770| 20 6d 6f 64 75 6c 65 20 | 6d 61 79 20 62 65 20 67 | module |may be g|
|00002780| 69 76 65 6e 20 62 65 66 | 6f 72 65 20 74 68 65 0a |iven bef|ore the.|
|00002790| 25 20 64 65 74 61 69 6c | 65 64 20 6c 69 73 74 20 |% detail|ed list |
|000027a0| 6f 66 20 66 75 6e 63 74 | 69 6f 6e 73 2e 29 0a 5c |of funct|ions.).\|
|000027b0| 73 75 62 73 65 63 74 69 | 6f 6e 7b 45 78 61 6d 70 |subsecti|on{Examp|
|000027c0| 6c 65 7d 0a 41 20 73 69 | 6d 70 6c 65 20 65 78 61 |le}.A si|mple exa|
|000027d0| 6d 70 6c 65 3a 0a 5c 62 | 65 67 69 6e 7b 76 65 72 |mple:.\b|egin{ver|
|000027e0| 62 61 74 69 6d 7d 0a 3e | 3e 3e 20 69 6d 70 6f 72 |batim}.>|>> impor|
|000027f0| 74 20 70 61 72 73 65 72 | 0a 3e 3e 3e 20 61 73 74 |t parser|.>>> ast|
|00002800| 20 3d 20 70 61 72 73 65 | 72 2e 65 78 70 72 28 27 | = parse|r.expr('|
|00002810| 61 20 2b 20 35 27 29 0a | 3e 3e 3e 20 63 6f 64 65 |a + 5').|>>> code|
|00002820| 20 3d 20 70 61 72 73 65 | 72 2e 63 6f 6d 70 69 6c | = parse|r.compil|
|00002830| 65 61 73 74 28 61 73 74 | 29 0a 3e 3e 3e 20 61 20 |east(ast|).>>> a |
|00002840| 3d 20 35 0a 3e 3e 3e 20 | 65 76 61 6c 28 63 6f 64 |= 5.>>> |eval(cod|
|00002850| 65 29 0a 5c 65 6e 64 7b | 76 65 72 62 61 74 69 6d |e).\end{|verbatim|
|00002860| 7d 0a 5c 73 75 62 73 65 | 63 74 69 6f 6e 7b 41 53 |}.\subse|ction{AS|
|00002870| 54 20 4f 62 6a 65 63 74 | 73 7d 0a 41 53 54 20 6f |T Object|s}.AST o|
|00002880| 62 6a 65 63 74 73 20 28 | 72 65 74 75 72 6e 65 64 |bjects (|returned|
|00002890| 20 62 79 20 5c 63 6f 64 | 65 7b 65 78 70 72 28 29 | by \cod|e{expr()|
|000028a0| 7d 2c 20 5c 63 6f 64 65 | 7b 73 75 69 74 65 28 29 |}, \code|{suite()|
|000028b0| 7d 2c 20 61 6e 64 0a 5c | 63 6f 64 65 7b 74 75 70 |}, and.\|code{tup|
|000028c0| 6c 65 32 61 73 74 28 29 | 7d 2c 20 64 65 73 63 72 |le2ast()|}, descr|
|000028d0| 69 62 65 64 20 61 62 6f | 76 65 29 20 68 61 76 65 |ibed abo|ve) have|
|000028e0| 20 6e 6f 20 6d 65 74 68 | 6f 64 73 20 6f 66 20 74 | no meth|ods of t|
|000028f0| 68 65 69 72 20 6f 77 6e | 2e 0a 53 6f 6d 65 20 6f |heir own|..Some o|
|00002900| 66 20 74 68 65 20 66 75 | 6e 63 74 69 6f 6e 73 20 |f the fu|nctions |
|00002910| 64 65 66 69 6e 65 64 20 | 77 68 69 63 68 20 61 63 |defined |which ac|
|00002920| 63 65 70 74 20 61 6e 20 | 41 53 54 20 6f 62 6a 65 |cept an |AST obje|
|00002930| 63 74 20 61 73 20 74 68 | 65 69 72 0a 66 69 72 73 |ct as th|eir.firs|
|00002940| 74 20 61 72 67 75 6d 65 | 6e 74 20 6d 61 79 20 63 |t argume|nt may c|
|00002950| 68 61 6e 67 65 20 74 6f | 20 6f 62 6a 65 63 74 20 |hange to| object |
|00002960| 6d 65 74 68 6f 64 73 20 | 69 6e 20 74 68 65 20 66 |methods |in the f|
|00002970| 75 74 75 72 65 2e 0a 4f | 72 64 65 72 65 64 20 61 |uture..O|rdered a|
|00002980| 6e 64 20 65 71 75 61 6c | 69 74 79 20 63 6f 6d 70 |nd equal|ity comp|
|00002990| 61 72 69 73 6f 6e 73 20 | 61 72 65 20 73 75 70 70 |arisons |are supp|
|000029a0| 6f 72 74 65 64 20 62 65 | 74 77 65 65 6e 20 41 53 |orted be|tween AS|
|000029b0| 54 20 6f 62 6a 65 63 74 | 73 2e 0a 5c 72 65 6e 65 |T object|s..\rene|
|000029c0| 77 63 6f 6d 6d 61 6e 64 | 7b 5c 69 6e 64 65 78 73 |wcommand|{\indexs|
|000029d0| 75 62 69 74 65 6d 7d 7b | 28 61 73 74 20 6d 65 74 |ubitem}{|(ast met|
|000029e0| 68 6f 64 29 7d 0a 25 5c | 62 65 67 69 6e 7b 66 75 |hod)}.%\|begin{fu|
|000029f0| 6e 63 64 65 73 63 7d 7b | 65 6d 70 74 79 7d 7b 7d |ncdesc}{|empty}{}|
|00002a00| 0a 25 45 6d 70 74 79 20 | 74 68 65 20 63 61 6e 20 |.%Empty |the can |
|00002a10| 69 6e 74 6f 20 74 68 65 | 20 74 72 61 73 68 2e 0a |into the| trash..|
|00002a20| 25 5c 65 6e 64 7b 66 75 | 6e 63 64 65 73 63 7d 0a |%\end{fu|ncdesc}.|
+--------+-------------------------+-------------------------+--------+--------+