¿ìËÙÒµÎñͨµÀ

Ò»¸ö¼òµ¥µÄÓïÑÔµÄÓï·¨£¨Èý£©£º×öЩСµ÷Õû£¬²¢½«Éú³ÉÄ¿±ê»»µ½CSharp2 - ±à³ÌÈëÃÅÍø

×÷Õß ØýÃû¼¼Êõ À´Ô´ NET±à³Ì ä¯ÀÀ ·¢²¼Ê±¼ä 2012-06-14

Ò»¸ö¼òµ¥µÄÓïÑÔµÄÓï·¨£¨Èý£©£º×öЩСµ÷Õû£¬²¢½«Éú³ÉÄ¿±ê»»µ½CSharp2

ʱ¼ä:2011-10-07 javaeye RednaxelaFX

ΪÁ˺óÃæµÄtree grammar¸ü¼ò½à£¬±¾Æª¶ÔÉÏһƪµÄÊ÷ÖØд¹æÔòºÍһЩÓ﷨ϸ½Ú×öÁËЩµ÷Õû¡£²¢ÇÒ£¬½« Éú³ÉµÄlexerºÍparserµÄÔ´ÂëÄ¿±ê»»µ½ÁËCSharp2£¬ÒÔ±ãºóÃæÄÜʹÓÃһЩ.NETµÄ¿â¡£

ҪʹÓÃCSharp2µÄÄ¿±ê£¬ÐèÒª´Ó¹ÙÍøÏÂÔØÏàÓ¦µÄÔËÐÐʱ¿â¡£µ±Ç°µÄ×îаæÊÇ3.1.1£¬¿ÉÒÔ´ÓÕâÀï»ñÈ¡¡£ CSharp/CSharp2Ä¿±êµÄÏêϸÇé¿ö£¬¿ÉÒÔ²éÔĹÙÍøÉϵÄÎĵµ¡£ÒÔÉÏһƪµÄÓ﷨Ϊ»ù´¡£¬Òª»»µ½CSharp2Ä¿±ê Ö»Òª°Ñ¼¸¸öǶÈ붯×÷ÀïµÄSystem.out.println»»³ÉConsole.WriteLine£¬°ÑtoStringTree»»³É ToStringTree£¬°Ñclear»»³ÉClear¾Í¿ÉÒÔÁË¡£±àÒëµÄʱºòÖÁÉÙÐèÒªÒýÓÃAntlr3.Runtime.dll¡£

ÄÇô³ýÈ¥¸ü»»Éú³ÉÄ¿±ê´øÀ´µÄÓ°Ï죬Õâ´Î×öÁËЩÔõÑùµÄÐÞ¸ÄÄØ£¿

Ê×ÏÈ£¬Óï·¨×öÁËЩϸ΢µÄµ÷Õû¡£ÀýÈç˵£¬program¹æÔò´ÓÔ­±¾ÔÊÐíûÓÐÓï¾äµ½ÏÖÔÚÒªÇóÖÁÉÙÓÐÒ»ÌõÓï¾ä £»blockStatementΪ¿ÕblockдÁËÌõרÃŵķÖÖ§£»expressionStatementÒ²Ìí¼ÓÁËÒ»¸öEXPR_STMTµÄÐé¹¹ tokenΪ¸ù½Úµã£¬µÈµÈ¡£

±ä»¯×î´óµÄ»¹ÊÇvariableDeclaration¼°Ïà¹Ø¹æÔò¡£ÉÏһƪÀïÕâÌõ¹æÔòµÄÖØд¹æÔò²¢²»Çø·ÖÓгõʼ»¯Óë ÎÞ³õʼ»¯¡¢¼òµ¥ÀàÐÍÓëÊý×éÀàÐ͵ÄÇø±ð£»±¾ÆªÀïÔò½«ÕâÁ½¸öÇø±ð¶¼Ã÷È·µÄдÔÚÁËÖØд¹æÔòÀÒÔ²»Í¬µÄÐé ¹¹tokenÀ´×÷ΪÉú³ÉµÄÊ÷µÄ¸ù½Úµã¡£ÕâÑù£¬µ****óÃæµÄtree grammarµÄʱºò£¬ÐèÒªµÄlookaheadÊý¾Í¿ÉÒÔ¼õ ÉÙ¡£

ANTLRËùÉú³ÉµÄAST£¬ÒÔÉî¶ÈÓÅÏȵķ½Ê½±éÀú£¬¿ÉÒÔ¿´×öÒ»¸öһάµÄÁ÷£ºÃ¿Ò»²ã¸¸×Ó¹Øϵ¶¼¿ÉÒÔ±íʾΪ £º

root -> "down" -> element1 -> element2 -> ... -> elementN -> "up" -> ...

ÆäÖÐ"down"ºÍ"up"ÊÇANTLR²åÈëµÄÐé¹¹token£¬ÓÃÓÚÖ¸¶¨Ê÷µÄ²ã´Î¡£

ÕâÑù£¬ºóÃæʹÓÃtree grammarÀ´±éÀúASTʱ£¬Êµ¼ÊÉϱéÀúµÄ¾ÍÊÇÕâÑùÒ»¸öһάµÄÁ÷ £¨CommonTreeNodeStream£©¡£ËùÒÔÎÒÃÇÒ²¿ÉÒÔ°Ñtree grammar¿´×öÊÇÒþº¬ÁË"down"ºÍ"up"Ðé¹¹tokenµÄÆÕ Í¨parser grammar¡£ÄÇô£¬tree grammarÖÐÐèÒªµÄlookahead¸öÊýµÄ·ÖÎö£¬Ò²¾Í¸úparser grammarµÄÒ»Ñù ¡£

¿´¿´ÏÂÃæµÄÀý×Ó¡£¶ÔÓÚÉÏһƪvariableDeclarationµÄÖØд¹æÔòÖгöÏֵıäÁ¿ÉùÃ÷µÄÀàÐÍ£¬¿ÉÒÔÓÃÕâÑù µÄtree grammarÀ´Æ¥Å䣺

Java´úÂë

type
  : ^( SIMPLE_TYPE INT )
  | ^( SIMPLE_TYPE REAL )
  | ^( ARRAY_TYPE INT Integer+ )
  | ^( ARRAY_TYPE REAL Integer+ )
  ;

Ê÷Óï·¨µÄ^( ... )¾ÍÒþº¬ÁË"down"ºÍ"up"ÕâÁ½¸öÐé¹¹token¡£Êµ¼ÊÉÏÕâÌõ¹æÔòÆ¥ÅäµÄÊÇ£º

Ò»¸ö¼òµ¥µÄÓïÑÔµÄÓï·¨£¨Èý£©£º×öЩСµ÷Õû£¬²¢½«Éú³ÉÄ¿±ê»»µ½CSharp2(2)

ʱ¼ä:2011-10-07 javaeye RednaxelaFX

¿ÉÒÔºÜÇå³þµÄ¿´µ½"down"ºÍ"up"ÔÚ¹æÔòÖеÄλÖá£

ÔÚ½øÈëÕâÌõ¹æÔòÖ®ºó£¬ÐèÒª¶àÉÙ¸ölookahead²Å×ãÒÔÅжÏÓ¦¸ÃÑ¡ÔñÄÄÌõ·ÖÖ§ÄØ£¿

ÏòÇ°¿´Ò»Î»£ºÖ»ÄÜÅųýµôÁ½¸ö·ÖÖ§£¬»¹ÓÐÁ½¸ö£¬²»¹»£»

ÏòÇ°¿´Á½Î»£ºµÚ¶þλÊÇʲôÄØ£¿Ëĸö·ÖÖ§ÀïµÚ¶þλ¶¼ÊÇ"down"½Úµã£¬¶ÔÅжϷÖ֧û°ïÖú£¬»¹ÊDz»¹»Óà £»

ÏòÇ°¿´Èýλ£ºSIMPLEºÍARRAY¡¢INTºÍREAL¶¼ÄÜ·Ö¿ªÁË£¬×ã¹»¡£

ÄÇô¶ÔÕâÌõ¹æÔò¶øÑÔ£¬ÐèÒª2¸ölookahead¡£ÔĶÁANTLRÉú³ÉµÄÔ´Â룬¿ÉÒÔ¿´µ½input.LA(3)ÕâÑùµÄµ÷Óà £¬±íʾÏòÇ°¿´µÚÈýλµÄtoken¡£Ã¿¶àÒ»¸ölookahead£¬Éú³ÉµÄ´úÂë¾ÍµÃ¶àÒÔ²ãǶÌ×µÄif-else£¬ºÜÊÇÂé·³¡£

Èç¹ûÄܵ÷ÕûÒ»ÏÂparserÕâ±ßÉú³ÉµÄASTµÄ½á¹¹£¬ÈÃtree grammarÄDZßÄÜд³É£º

Java´úÂë

simpleType
  : INT
  | REAL
  ;

arrayType
  : ^( INT Integer+ )
  | ^( REAL Integer+ )
  ;

ÄÇôÕâÁ½Ìõ¹æÔò¶¼Ö»ÐèÒª1¸ölookahead¾Í×ãÒÔÅжϷÖÖ§ÁË£¬±ÈÔ­±¾µÄд·¨Òª¼òµ¥£¬Ò²»áÉÔ΢¿ìһЩ¡£ дÁ˸öRuby½Å±¾À´¼ì²éÉú³ÉµÄÔ´

ÁèÖڿƼ¼×¨ÒµÌṩ·þÎñÆ÷×âÓᢷþÎñÆ÷Íйܡ¢ÆóÒµÓʾ֡¢ÐéÄâÖ÷»úµÈ·þÎñ£¬¹«Ë¾ÍøÕ¾£ºhttp://www.lingzhong.cn ΪÁ˸ø¹ã´ó¿Í»§Á˽â¸ü¶àµÄ¼¼ÊõÐÅÏ¢£¬±¾¼¼ÊõÎÄÕÂÊÕ¼¯À´Ô´ÓÚÍøÂç,ÁèÖڿƼ¼×ðÖØÎÄÕÂ×÷ÕߵİæȨ£¬Èç¹ûÓÐÉæ¼°ÄãµÄ°æȨÓбØҪɾ³ýÄãµÄÎÄÕ£¬ÇëºÍÎÒÃÇÁªÏµ¡£ÒÔÉÏÐÅÏ¢ÓëÎÄÕÂÕýÎÄÊDz»¿É·Ö¸îµÄÒ»²¿·Ö,Èç¹ûÄúҪתÔر¾ÎÄÕÂ,Çë±£ÁôÒÔÉÏÐÅÏ¢£¬Ð»Ð»!

·ÖÏíµ½£º ¸ü¶à

Copyright ©1999-2011 ÏÃÃÅÁèÖڿƼ¼ÓÐÏÞ¹«Ë¾ ÏÃÃÅÓÅͨ»¥Áª¿Æ¼¼¿ª·¢ÓÐÏÞ¹«Ë¾ All rights reserved

µØÖ·(ADD)£ºÏÃÃÅÈí¼þÔ°¶þÆÚÍûº£Â·63ºÅ701E£¨¶«ÄÏÈÚͨÅÔ£© Óʱà(ZIP)£º361008

µç»°£º0592-5908028 ´«Õ棺0592-5908039 ×ÉѯÐÅÏ䣺web@lingzhong.cn ×ÉѯOICQ£º173723134

¡¶ÖлªÈËÃñ¹²ºÍ¹úÔöÖµµçÐÅÒµÎñ¾­ÓªÐí¿ÉÖ¤¡·ÃöB2-20100024  ICP±¸°¸:ÃöICP±¸05037997ºÅ