DParser For Python ¹®¼­(¹ø¿ª) (¿ø¹®: http://staff.washington.edu/sabbey/py_dparser/py_dparser_manual.html) ÀÌ ÆäÀÌÁö´Â [http://staff.washington.edu/sabbey/py_dparser/index.html DParser ¿¡ ´ëÇÑ Python interface] ¸¦ ¼³¸íÇÑ´Ù. DParser ¿¡ ´ëÇÑ ´õ ÀÚ¼¼ÇÑ Á¤º¸¸¦ ¿øÇϸé [http://dparser.sourceforge.net/d/manual.html DParser ¸Þ´º¾ó]À» º¸¾Æ¶ó [[TableOfContents]] = ±âº» ¾ÆÀ̵ð¾î = ¹®¹ý ±ÔÄ¢Àº Python function document strings ¸¦ »ç¿ëÇÏ¿© DParser ¿¡ ÀԷµȴÙ.(Python function ÀÇ Ã¹¹ø° ¶óÀο¡ À§Ä¡ÇÑ string ÀÌ ±× function ÀÇ documentation string ÀÌ´Ù) DParser ·Î ÇÏ¿©±Ý ƯÁ¤ÇÑ function ÀÇ documentation string À» ´ç½ÅÀÇ ¹®¹ýÀÇ ÀϺκÐÀ¸·Î ÀÎÁöÇÏ°Ô ÇÏ·Á¸é, function ÀÇ À̸§À» "d_" ·Î ½ÃÀÛÇÏ°Ô ÇÏ¸é µÈ´Ù. ±×·¯¸é ±× function Àº documentation string ¿¡ Á¤ÀÇµÈ production ÀÌ ÁÙ¾îµé ¶§¸¶´Ù ½ÇÇàµÇ´Â ¾×¼Ç ÀÌ µÈ´Ù. ¿¹¸¦ µé¸é {{{#!vim python def d_action1(t): " sentence : noun 'runs' " print 'found a sentence' # ... }}} ÀÌ function Àº ¾×¼Ç, d_action1, °ú production, sentence, ¸¦ DParser ¿¡°Ô ¸»ÇØÁØ´Ù. DParser °¡ sentence ¸¦ ÀνÄÇÒ ¶§ d_action1 Àº È£ÃâµÈ´Ù. d_action ¿¡ ´ëÇÑ ¾Æ±Ô¸ÕÆ®, t, ´Â array ´Ù. ÀÌ array ´Â production À» ±¸¼ºÇÏ´Â element µéÀÇ ¸®ÅÏ°ªµé·Î ±¸¼ºµÇ°Å³ª ¶Ç´Â terminal element µé¿¡ ´ëÇؼ­´Â terminal ÀÌ ¸ÅÄ¡ÇÏ´Â string À¸·Î ±¸¼ºµÈ´Ù. À§¿¡ ¿¹¿¡¼­ array t ´Â noun ÀÇ ¾×¼ÇÀ» ù¹ø° element ·Î Python string 'runs' ¸¦ µÎ¹ø° element ·Î Æ÷ÇÔÇÑ´Ù. Á¤±ÔÇ¥Çö½ÄÀº double quotes(") ·Î µÑ·¯ ½Î¼­ Ç¥ÇöÇÑ´Ù. {{{#!vim python def d_number(t): ' number : "[0-9]+" ' # match a positive integer return int(t[0]) # turn the matched string into an integer # ... }}} ´ç½ÅÀÇ documentation string ÀÌ ¸¸¾à [http://www.python.org/doc/current/ref/strings.html Python escape sequences] ¸¦ Æ÷ÇÔÇÑ´Ù¸é ±×°ÍÀÌ Python raw string(r ·Î ½ÃÀÛÇÏ´Â) ÀÌ µÇµµ·Ï ÇؾßÇÑ´Ù. ¿ì¼±¼øÀ§³ª ¿¬°ü¼º °°Àº productions ÀÇ Áøº¸ÇÑ Æ¯Â¡À» ¾Ë°í ½ÍÀ¸¸é, [http://dparser.sourceforge.net/d/manual.html the DParser manual] À» º¸¾Æ¶ó. °£´ÜÇÏ°í ¿Ïº®ÇÑ µ¡¼À ¿¹Á¦´Â [http://staff.washington.edu/sabbey/py_dparser/index.html home page] ¸¦ º¸¾Æ¶ó. = ¾×¼Çµé¿¡ ´ëÇÑ ¾Æ±Ô¸ÕÆ®µé = ¸ðµç ¾×¼ÇÀº À§¿¡ ¼³¸íÇÑ Àû¾îµµ ÇϳªÀÇ array ¾Æ±Ô¸ÕÆ®¸¦ °¡Áø´Ù. ´Ù¸¥ ¾Æ±Ô¸ÕÆ®µéÀº ¼±ÅûçÇ×ÀÌ´Ù(optional). ÀÎÅÍÆäÀ̽º´Â ´ç½ÅÀÌ ¿øÇÏ´Â ¾Æ±Ô¸ÕÆ®°¡ ¾î´À °ÍÀÎÁö ÀÎÁöÇϴµ¥ ´ç½ÅÀÌ ¾Æ±Ô¸ÕÆ®¿¡ ÁØ À̸§À» »ç¿ëÇÑ´Ù. °¡´ÉÇÑ À̸§µéÀº ¾Æ·¡¿Í °°´Ù. * spec, spec_only ¾×¼Ç¿¡ spec ¾Æ±Ô¸ÕÆ®°¡ ÀÖ´Ù¸é ÇØ´ç ¾×¼ÇÀº will be called for both speculative and final parses (±×·¸Áö ¾ÊÀ¸¸é ¾×¼ÇÀº ´ÜÁö ÃÖÁ¾ ÆĽ̿¡¼­ È£ÃâµÉ °ÍÀδÙ) spec ÀÇ °ªÀº ÆĽÌÀÌ final ÀÎÁö speculative ÀÎÁö¸¦ °¡¸®Å²´Ù.(1 Àº speculative, 0 Àº final) speculative ÆĽÌÀ» °ÅºÎÇϱâ À§Çؼ­´Â dparser.Reject ¸¦ ¸®ÅÏÇ϶ó. ¾×¼ÇÀÌ spec_only ¸¦ ÀÎÀÚ·Î °¡Áø´Ù¸é, ÇØ´ç ¾×¼ÇÀº speculative ÆĽ̿¡¼­¸¸ È£ÃâµÉ °ÍÀδÙ. ±×¸®°í final ÆĽÌÀÇ ¾×¼ÇÀÇ ¸®ÅÏ °ªÀº speculative ÆĽÌÀÌ ¸®ÅÏÇß´ø ÆÄÀ̽㠿ÀºêÁ§Æ®¿Í °°À» °ÍÀÌ´Ù. [http://staff.washington.edu/sabbey/py_dparser/test5.py ¿Ïº®ÇÑ ¿¹Á¦]. * g DParser ÀÇ Àü¿ª state. g ´Â ½ÇÁ¦·Î ù¹ø° ¿ø¼Ò°¡ Àü¿ª state ÀÎ array ÀÌ´Ù.(¿ø¼Ò°¡ ÇϳªÀÎ array ¸¦ »ç¿ëÇÏ´Â ÀÌ·± ¹æ½ÄÀº ¾×¼ÇÀ¸·Î ÇÏ¿©±Ý Àü¿ª state ¸¦ ¹Ù²Ü ¼ö ÀÖ°Ô ÇØÁØ´Ù) * s ÀÌ reduction À» ±¸¼ºÇÏ´Â string µéÀÇ array ¸¦ Æ÷ÇÔÇÑ´Ù. s ´Â ´ç½ÅÀÇ Æļ­ÀÇ ¸ñÀûÀÌ ¾î¶² ÅؽºÆ®¸¦ °ÅÀÇ ¼Õ»ó½ÃÅ°Áö ¾Ê°í °íÄ¡´Â °ÍÀ̶ó¸é ¸Å¿ì À¯¿ëÇÏ´Ù. ¿Ïº®ÇÑ ¿¹Á¦´Â [http://staff.washington.edu/sabbey/py_dparser/test6.py ¿©±â] ¸¦ º¸¾Æ¶ó * nodes reduction ÀÇ D_ParseNode µéÀÇ ÆÄÀ̽ã wrapper ÀÇ array ÀÌ´Ù. line number °°Àº Á¤º¸¸¦ °®°í ÀÖ´Ù. À¯¿ëÇÑ fields ´Â [http://staff.washington.edu/sabbey/py_dparser/test7.py ¿©±â] ¸¦ º¸¶ó * this ÇöÀç production ÀÇ D_ParseNode ÀÌ´Ù. ($$ in DParser.) ÀÌ [http://staff.washington.edu/sabbey/py_dparser/test7.py ¿¹Á¦] ¸¦ º¸¶ó * parser ´ç½ÅÀÇ Æļ­(°¡²û ¿©·¯ ÆÄÀÏÀ» ´Ù·ê ¶§ À¯¿ëÇÏ´Ù) = dparser.Parser() ¿¡ ´ëÇÑ ¾Æ±Ô¸ÕÆ®µé = ¸ðµç ¾Æ±Ô¸ÕÆ®µéÀº ¼±ÅûçÇ×ÀÌ´Ù. * modules ´ç½ÅÀÇ Æļ­¿¡¼­ »ç¿ëÇÒ ¾×¼ÇµéÀ» Æ÷ÇÔÇÏ´Â ¸ðµâµéÀÇ array ÀÌ´Ù. ¸í½ÃµÇÁö ¾ÊÀ¸¸é È£ÃâÇÑ ¸ðµâÀÌ »ç¿ëµÈ´Ù. * file_prefix ÆÄ½Ì Å×À̺í ij½ÃÀÇ ÆÄÀÏÀ̸§À» À§ÇÑ prefix ´Ù. default ´Â "d_parser_mach_gen" ÀÌ´Ù. = dparser.Parser.parse() ¿¡ ´ëÇÑ ¾Æ±Ô¸ÕÆ®µé = dparser.Parser.parse ÀÇ Ã¹¹ø° ¾Æ±Ô¸ÕÆ®´Â ¾ðÁ¦³ª ÆĽÌÇÒ string ÀÌ´Ù. ´Ù¸¥ ¾Æ±Ô¸ÕÆ®µéÀº ¼±ÅûçÇ×ÀÌ´Ù. * start_symbol ½ÃÀÛ ½Éº¼ÀÌ´Ù. default ´Â Á¦ÀÏ Ã³À½ Á¤ÀÇµÈ ½Éº¼ÀÌ´Ù. * print_debug_info non-zero ¸é È£ÃâµÇ´Â ¾×¼ÇµéÀÇ ¸®½ºÆ®¸¦ Ãâ·ÂÇÑ´Ù. Question mark(?) ´Â ¾×¼ÇÀÌ speculative ÀÓÀ» °¡¸®Å²´Ù. * dont_fixup_internal_productions, dont_merge_epsilon_trees, commit_actions_interval, error_recovery D_Parser ÀÇ ¸â¹öµé¿¡ ´ëÀÀµÈ´Ù. (DParser ¸Þ´º¾óÀ» º¸¾Æ¶ó) * initial_skip_space_fn »ç¿ëÀÚ Á¤ÀÇ °ø¹é(as does the whitespace production, and instead of the built-in, c-like whitespace parser)À» Çã¿ëÇÑ´Ù. ¾Æ±Ô¸ÕÆ®´Â d_loc_t structure ÀÌ´Ù. ÀÌ structure ÀÇ ¸â¹ö, s, ´Â ÆĽ̵ǰíÀÖ´Â string ¿¡ ´ëÇÑ index ÀÌ´Ù. °ø¹é(whitespace)À» °Ç³Ê¶Ù°í(skip) ½ÍÀ¸¸é ÀÌ index ¸¦ °íÃĶó. {{{#!vim python def whitespace(loc): # no d_ prefix while loc.s < len(loc.buf) and loc.buf[loc.s:loc.s+2] == ':)': # make smiley face the whitespace loc.s = loc.s + 2 #... Parser().parse('int:)var:)=:)2', initial_skip_space_fn = whitespace) }}} * syntax_error_fn syntax ¿¡·¯ ¶§ È£Ãâ. default ·Î´Â ¿¹¿Ü(exception)°¡ ¹ß»ýÇÑ´Ù. ¿¡·¯ÀÇ À§Ä¡¸¦ °¡¸®Å°´Â d_loc_t structure(initial_skip_space_fn À» º¸¾Æ¶ó) ¸¦ ³Ñ°ÜÁØ´Ù. ¾Æ·¡ ÇÔ¼ö´Â '<--error' ¿Í ¿¡·¯ À§Ä¡ÀÇ line break ¸¦ Ãâ·ÂÇÑ´Ù. {{{#!vim python def syntax_error(loc): mn = max(loc.s - 10, 0) mx = min(loc.s + 10, len(loc.buf)) begin = loc.buf[mn:loc.s] end = loc.buf[loc.s:mx] space = ' '*len(begin) print begin + '\n' + space + '<--error' + '\n' + space + end }}} * ambiguity_fn: ¸ðÈ£¼ºÀ» ÇØ°áÇÑ´Ù. D_ParseNode ÀÇ array ¸¦ ÃëÇÑ´Ù.(´Ù½Ã [http://staff.washington.edu/sabbey/py_dparser/test7.py ¿©±â] ¸¦ º¸¶ó) ±×¸®°í ±×µé Áß Çϳª°¡ ¸®ÅϵDZ⸦ ±â´ëÇÑ´Ù. default ·Î´Â dparser.AmbiguityException ÀÌ ¹ß»ýÇÑ´Ù. = À§Çèµé(pitfalls)°ú ÆÁµé = ¸¸ÀÏ ´ç½ÅÀÌ pitfall ¿¡ ºüÁö°Å³ª ÆÁÀ» ¾Ë°í ÀÖ´Ù¸é ³»°Ô ¾Ë·Á´Þ¶ó. 'o'/ ¿©±â¿¡ Ãß°¡ÇÏ°Ú´Ù. * Grammar µð¹ö±ë Parser.parse() ¿¡ print_debug_info=1 À» ³Ñ°ÜÁÖ¸é È£ÃâµÈ ¾×¼ÇµéÀÇ ¸®½ºÆ®¸¦ º¼ ¼ö ÀÖ´Ù. (2¸¦ ³Ñ°ÜÁÖ¸é final ¾×¼Ç¸¸ º¼ ¼ö ÀÖ´Ù) ¶ÇÇÑ »ý¼ºµÈ grammar file À» º¸¾Æ¶ó, d_parser_mach_gen.g * Á¤±ÔÇ¥Çö½Ä DParser ´Â ÆÄÀ̽ã Á¤±Ô½Ä ¸ðµâÀÌ ÀÌÇØÇÏ´Â ¸ðµç Á¤±ÔÇ¥Çö½ÄÀ» ÀÌÇØÇÏÁö´Â ¸øÇÑ´Ù. DParser °¡ ÀÌÇØÇÏ´Â Á¤±ÔÇ¥Çö½ÄÀ» »ç¿ëÇ϶ó. * °ø¹é(whitespace) ±âº»ÀûÀ¸·Î DParser ´Â ÅÇ, ½ºÆäÀ̽º, °³Ç๮ÀÚ, #line command ¸¦ °ø¹éÀ¸·Î »ç¿ëÇÑ´Ù. ÀÌ·¯ÇÑ °ÍµéÀ» ´ç½Å ½º½º·Î Á¦¾îÇÏ°í ½ÍÀ¸¸é(ƯÈ÷ # ¹®ÀÚ´Â Á¶½ÉÇؼ­ ´Ù·ï¶ó) initial_skip_space_fn À» ±¸ÇöÇϰųª special whitespace production À» Á¤ÀÇÇÏ¸é µÈ´Ù. {{{#!vim python def d_whitespace(t): 'whitespace : "[ \t\n]*" ' # treat space, tab and newline as whitespace, but treat the # character normally print 'found whitespace:' + t[0] }}} * DParser specifiers/declarations DParser ´Â documentation string ¿¡ ¼±¾ðÇÔÀ¸·Î½á ³Ñ°ÜÁÙ ¼ö ÀÖ´Ù. ¿¹¸¦ µé¸é {{{#!vim python from dparser imoprt Parser def d_somefunc(t) : '${declare longest_match}' #... }}} (specifiers ¿Í declarations ¿¡ ´ëÇÑ ¼³¸íÀ» º¸°í ½ÍÀ¸¸é DParser ¸Þ´º¾óÀ» ºÁ¶ó) * ¾×¼Ç¿¡¼­ ´ÙÁß production ÇϳªÀÇ documentation string ¿¡ ¿©·¯°³ÀÇ production À»(½ÉÁö¾î Àüü grammar±îÁö) ³ÖÀ» ¼ö ÀÖ´Ù. °¢°¢ÀÇ production ¿¡ ¼¼¹ÌÄÝ·ÐÀ» ºÙ¿©¶ó. {{{#!vim python from dparser import Parser def d_grammar(t): '''sentence : noun verb; noun : 'dog' | 'cat'; verb : 'run' ''' print 'this function gets called for every reduction' Parser().parse("dog run") }}}