• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            隨筆-341  評論-2670  文章-0  trackbacks-0
                我們知道Yacc和Bison都是產生C++的代碼作為編譯器的前端的。但是有時候我們需要動態地產生一個編譯器前端,極端一點講,譬如“文法調試器”。調試器總不能動態生成.y文件,讓yacc編譯,讓gcc再度編譯,然后execute,最后將程序的輸出結果讀進來。這樣就太麻煩了,于是我們需要重新寫一個生成編譯器前端的程序。

                趁著今天有空,我寫了一段代碼用來測試Syngram是否能很好的動態生成語法分析器。Syngram設計的初衷是讓C++可以直接寫文法,不過通過今天的實驗發現Syngram也是可以很好的處理動態輸入的東西的,譬如說動態的文法、動態的錯誤處理程序以及動態的語義處理函數等等。作為一個編譯器生成器的實驗,能否處理Syngram所有的結果是一件很值得注意的事情。

                這個程序仍然使用了上一篇文章所描述的Algorithm架構來處理文法。程序讀入兩個文件,一個是文法描述文件,另一個是被這個文法處理的字符串文件。程序使用Syngram處理第一個文件,將文件內容動態轉換為Syngram能夠接受的文法描述并產生第二個文法分析器,然后使用這個分析器來處理第二個文件。

                我們給出科學計算器所接受的文法:
             1 lexical
             2 {
             3     num='\d+(.\d+)?'
             4     ident='[a-zA-Z_]\w*'
             5     plus='\+'
             6     minus='\-'
             7     mul='\*'
             8     div='/'
             9     leftbrace='\('
            10     rightbrace='\)'
            11     comma=','
            12     discard='\s+'
            13 }
            14 rule
            15 {
            16     factor=num                    ;
            17     factor=[minus] factor                ;
            18     factor=leftbrace exp rightbrace            ;
            19     factor=ident[leftbrace param_list rightbrace]    ;
            20     term=factor                    ;
            21     term=term (mul|div) factor            ;
            22     exp=term                    ;
            23     exp=exp (plus|minus) term            ;
            24     param_list=exp [comma param_list]        ;
            25     program=exp                    ;
            26 }

                如果我們把factor=leftbrace exp rightbrace修改為factor=leftbrace | exp | rightbrace的話,由于出現了exp->term->factor->exp的無效循環引用,Syngram會報告文法的錯誤:
             1 規則<factor>存在長度為3的間接左遞歸。
             2 規則<exp>存在長度為3的間接左遞歸。
             3 規則<term>存在長度為3的間接左遞歸。
             4 規則<program>[0]不能產生字符串。
             5 規則<exp>[0]不能產生字符串。
             6 規則<exp>[1]不能產生字符串。
             7 規則<param_list>[0]不能產生字符串。
             8 規則<term>[0]不能產生字符串。
             9 規則<term>[1]不能產生字符串。
            10 

                <term>[0]的意思是term的第1條推導式子。

                好了,現在我們讓他讀入一個表達式來進行測試:
            1 (1+sin(2))**(sin(log(2,3))+4)
                這個表達式明顯是錯誤的,因為中間有兩個乘號。于是Syngram就會報告錯誤:
            1 --------------------------------------------------------------------------------
            2 (1+sin(2))**(sin(log(2,3))+4)
            3 --------------------------------------------------------------------------------
            4 遭遇第9個記號,類型5,狀態<term>[1=  term[0] ( mul[1| div[2] )* factor[3] 。
                第9個記號指的就是第二個乘號了。因為(1+sin(2))是一個factor,factor是一個term,因此term=term (*|/) factor走到了factor那里發現輸入的竟然是乘號,于是報錯。

                好了。我們把其中一個乘號去掉,現在Syngram就會給出分析的結果了:
             1 --------------------------------------------------------------------------------
             2 (1+sin(2))*(sin(log(2,3))+4)
             3 --------------------------------------------------------------------------------
             4 program = {
             5   exp = {
             6     term = {
             7       term = {
             8         factor = {
             9           leftbrace = (
            10           exp = {
            11             exp = {
            12               term = {
            13                 factor = {
            14                   num = 1
            15                 }
            16               }
            17             }
            18             plus = +
            19             term = {
            20               factor = {
            21                 ident = sin
            22                 leftbrace = (
            23                 param_list = {
            24                   exp = {
            25                     term = {
            26                       factor = {
            27                         num = 2
            28                       }
            29                     }
            30                   }
            31                 }
            32                 rightbrace = )
            33               }
            34             }
            35           }
            36           rightbrace = )
            37         }
            38       }
            39       mul = *
            40       factor = {
            41         leftbrace = (
            42         exp = {
            43           exp = {
            44             term = {
            45               factor = {
            46                 ident = sin
            47                 leftbrace = (
            48                 param_list = {
            49                   exp = {
            50                     term = {
            51                       factor = {
            52                         ident = log
            53                         leftbrace = (
            54                         param_list = {
            55                           exp = {
            56                             term = {
            57                               factor = {
            58                                 num = 2
            59                               }
            60                             }
            61                           }
            62                           comma = ,
            63                           param_list = {
            64                             exp = {
            65                               term = {
            66                                 factor = {
            67                                   num = 3
            68                                 }
            69                               }
            70                             }
            71                           }
            72                         }
            73                         rightbrace = )
            74                       }
            75                     }
            76                   }
            77                 }
            78                 rightbrace = )
            79               }
            80             }
            81           }
            82           plus = +
            83           term = {
            84             factor = {
            85               num = 4
            86             }
            87           }
            88         }
            89         rightbrace = )
            90       }
            91     }
            92   }
            93 }
            94 


                東西這么長是因為一個數字"1"要經過exp->term->factor->num來到達。這里表示的僅僅是文法的推導過程,實際的文法經常使用一組繼承的數據結構來消除這些多余的數據,譬如說表達式類ExpressionBase的子類經常會出現NumberExpression、BinaryOperatorExpression、MethodInvokeExpression等等。這些結構消除的就是【一個數字"1"要經過exp->term->factor->num來到達】這種類型的復雜度了。

                現在給出程序的代碼。一部分代碼已經在上一篇文章中提供了。首先是main函數:

              1 #include "..\..\..\Library\Platform\VL_Console.h"
              2 #include "..\..\..\Library\Data\VL_Stream.h"
              3 #include "..\..\..\Library\Data\VL_System.h"
              4 #include "..\Utility\GrammarTree.h"
              5 #include "..\Utility\GrammarAlgorithms.h"
              6 
              7 using namespace vl;
              8 using namespace vl::platform;
              9 using namespace vl::stream;
             10 using namespace vl::system;
             11 using namespace compiler;
             12 
             13 void vlmain(VL_Console& Con)
             14 {
             15     Con.SetTitle(L"Compile Syntax/Semantic Builder");
             16     Con.SetTestMemoryLeaks(true);
             17     Con.SetPauseOnExit(true);
             18 
             19     VUnicodeString TestDataPath=VFileName(Con.GetAppPath()).MakeAbsolute(L"..\\TestData\\").GetStrW();
             20     VUnicodeString GrammarText;
             21     VL_TextInput(new VL_FileInputStream(TestDataPath+L"計算器文法.txt"),true,vceBOM).Read(GrammarText);
             22 
             23     GrammarProvider Provider;
             24     GrammarDescription::Ptr gd=Provider.Parse(GrammarText);
             25     if(Provider.GetErrors().GetCount()==0)
             26     {
             27         {
             28             GrammarValidateParam ValidateParam;
             29             ValidateParam.Structure=new GrammarStructure;
             30             if(!GrammarValidate().Apply(gd,ValidateParam))
             31             {
             32                 for(VInt i=0;i<ValidateParam.Structure->Errors.GetCount();i++)
             33                 {
             34                     GrammarValidateError Error=ValidateParam.Structure->Errors[i];
             35                     VUnicodeString Line;
             36                     if(Error.TokenError)
             37                     {
             38                         Line+=L"詞法錯誤,";
             39                     }
             40                     else if(Error.RuleError)
             41                     {
             42                         Line+=L"文法錯誤,";
             43                     }
             44                     else
             45                     {
             46                         Line+=L"未知錯誤,";
             47                     }
             48                     Line+=L"位置:第"+VUnicodeString(Error.Index)+L"個定義,";
             49                     Line+=Error.Message;
             50                     Con.Write(Line+L"\r\n");
             51                 }
             52                 return;
             53             }
             54         }
             55         GrammarSimulateParam SimulateParam;
             56         {
             57             SimulateParam.Simulator=new GrammarSimulator;
             58             GrammarSimulate Algorithm;
             59             Algorithm.Apply(gd,&SimulateParam);
             60             if(SimulateParam.ErrorMessages.GetCount())
             61             {
             62                 for(VInt i=0;i<SimulateParam.ErrorMessages.GetCount();i++)
             63                 {
             64                     Con.Write(SimulateParam.ErrorMessages[i]+L"\r\n");
             65                 }
             66                 return;
             67             }
             68         }
             69         {
             70             Con.Write(GrammarToCode().Apply(gd,L""));
             71             Con.Write(L"--------------------------------------------------------------------------------");
             72             VUnicodeString InputExpression=L"";
             73             VL_TextInput(new VL_FileInputStream(TestDataPath+L"計算器表達式.txt"),true,vceBOM).Read(InputExpression);
             74             Con.Write(InputExpression+L"\r\n");
             75             VL_LexerFactoryPtr LexerResult;
             76             GrammarSimulatorNode::List SynerResult;
             77             SimulateParam.Simulator->Parse(InputExpression,LexerResult,SynerResult);
             78             if(SimulateParam.Simulator->ParseErrors.GetCount())
             79             {
             80                 Con.Write(L"--------------------------------------------------------------------------------");
             81                 for(VInt i=0;i<SimulateParam.Simulator->ParseErrors.GetCount();i++)
             82                 {
             83                     Con.Write(SimulateParam.Simulator->ParseErrors[i].Message+L"\r\n");
             84                 }
             85             }
             86             for(VInt i=0;i<SynerResult.GetCount();i++)
             87             {
             88                 Con.Write(L"--------------------------------------------------------------------------------");
             89                 Con.Write(SynerResult[i]->ToString(L""));
             90             }
             91         }
             92     }
             93     else
             94     {
             95         for(VInt i=0;i<Provider.GetErrors().GetCount();i++)
             96         {
             97             Con.Write(Provider.GetErrors()[i].Message+L"\r\n");
             98         }
             99     }
            100 }

                其次是四個算法,分別用于將文法的語法樹轉換為字符串、將語法樹轉換為文法文件的字符串、檢查文法依賴關系以及檢查文法的邏輯關系。這同時證明了上一篇文章的Algorithm架構是可以在心理上幫助程序員寫出獨立性較強的算法的,大大降低了維護的難度。

                頭文件:
              1 #ifndef GRAMMARALGORITHM
              2 #define GRAMMARALGORITHM
              3 
              4 #include "GrammarTree.h"
              5 #include "..\..\..\Library\Data\Data\VL_Data_Map.h"
              6 #include "..\..\..\Library\Data\Grammar2\VL_SynTools.h"
              7 
              8 namespace compiler
              9 {
             10     using namespace grammar;
             11 
             12     class GrammarValidateError : public VL_Base
             13     {
             14     public:
             15         typedef VL_List<GrammarValidateError , false>    List;
             16 
             17         VBool                TokenError;
             18         VBool                RuleError;
             19         VInt                Index;
             20         VUnicodeString        Message;
             21     };
             22 
             23 /*********************************************************************************************************
             24 GrammarToString
             25 *********************************************************************************************************/
             26 
             27     class GrammarToString : public GrammarAlgorithmEx<VUnicodeString , VUnicodeString>
             28     {
             29     public:
             30         VUnicodeString            Visit(GrammarBranch* Obj , VUnicodeString Prefix);
             31         VUnicodeString            Visit(GrammarSequence* Obj , VUnicodeString Prefix);
             32         VUnicodeString            Visit(GrammarOptional* Obj , VUnicodeString Prefix);
             33         VUnicodeString            Visit(GrammarUnit* Obj , VUnicodeString Prefix);
             34         VUnicodeString            Visit(GrammarRule* Obj , VUnicodeString Prefix);
             35         VUnicodeString            Visit(LexicalDecl* Obj , VUnicodeString Prefix);
             36         VUnicodeString            Visit(GrammarDescription* Obj , VUnicodeString Prefix);
             37     };
             38 
             39 /*********************************************************************************************************
             40 GrammarToCode
             41 *********************************************************************************************************/
             42 
             43     class GrammarToCode : public GrammarAlgorithmEx<VUnicodeString , VUnicodeString>
             44     {
             45     public:
             46         VUnicodeString            Visit(GrammarBranch* Obj , VUnicodeString Prefix);
             47         VUnicodeString            Visit(GrammarSequence* Obj , VUnicodeString Prefix);
             48         VUnicodeString            Visit(GrammarOptional* Obj , VUnicodeString Prefix);
             49         VUnicodeString            Visit(GrammarUnit* Obj , VUnicodeString Prefix);
             50         VUnicodeString            Visit(GrammarRule* Obj , VUnicodeString Prefix);
             51         VUnicodeString            Visit(LexicalDecl* Obj , VUnicodeString Prefix);
             52         VUnicodeString            Visit(GrammarDescription* Obj , VUnicodeString Prefix);
             53     };
             54 
             55 /*********************************************************************************************************
             56 GrammarValidate
             57 *********************************************************************************************************/
             58 
             59     class GrammarStructure : public VL_Base
             60     {
             61     public:
             62         typedef VL_ListedMap<VUnicodeString , VL_AutoPtr<VL_List<VInt , true>>>    _MultiIntMap;
             63         typedef VL_ListedMap<VUnicodeString , VInt>                                _IntMap;
             64     public:
             65         _IntMap                        Tokens;
             66         _MultiIntMap                Rules;
             67         GrammarValidateError::List    Errors;
             68     };
             69     class GrammarValidateParam : public VL_Base
             70     {
             71     public:
             72         VL_AutoPtr<GrammarStructure>    Structure;
             73         VInt                            Index;
             74     };
             75     class GrammarValidate : public GrammarAlgorithmEx<VBool , GrammarValidateParam>
             76     {
             77     public:
             78         VBool                    Visit(GrammarBranch* Obj , GrammarValidateParam Param);
             79         VBool                    Visit(GrammarSequence* Obj , GrammarValidateParam Param);
             80         VBool                    Visit(GrammarOptional* Obj , GrammarValidateParam Param);
             81         VBool                    Visit(GrammarUnit* Obj , GrammarValidateParam Param);
             82         VBool                    Visit(GrammarRule* Obj , GrammarValidateParam Param);
             83         VBool                    Visit(LexicalDecl* Obj , GrammarValidateParam Param);
             84         VBool                    Visit(GrammarDescription* Obj , GrammarValidateParam Param);
             85     };
             86 
             87 /*********************************************************************************************************
             88 GrammarSimulate
             89 *********************************************************************************************************/
             90 
             91     class GrammarSimulatorNode : public VL_Base
             92     {
             93     public:
             94         typedef VL_AutoPtr<GrammarSimulatorNode>                Ptr;
             95         typedef VL_List<Ptr , false , GrammarSimulatorNode*>    List;
             96 
             97         VUnicodeString            TerminatorName;
             98         VUnicodeString            Value;
             99         List                    SubExpressions;
            100 
            101         VUnicodeString            ToString(VUnicodeString Prefix);
            102     };
            103     class GrammarSimulator : public VL_Base
            104     {
            105     public:
            106         typedef VL_SynRuleHandler<GrammarSimulatorNode::Ptr>            RuleTransformer;
            107         typedef RuleTransformer::Handler                                RuleHandler;
            108         typedef VL_List<VL_AutoPtr<RuleHandler> , false , RuleHandler*>    RuleHandlerList;
            109         typedef VL_ListedMap<VInt , VUnicodeString>                        TokenIDMap;
            110     public:
            111         typedef VL_AutoPtr<GrammarSimulator>                    Ptr;
            112         typedef VL_AutoPtr<VL_LexerHandler>                        LexerHandler;
            113 
            114         VL_Lexer                Lexer;
            115         VL_Syner                Syner;
            116         RuleTransformer            Transformer;
            117         RuleHandlerList            Handlers;
            118         TokenIDMap                TokenIDs;
            119         GrammarError::List        ParseErrors;
            120         LexerHandler            LexerErrorHandler;
            121 
            122         GrammarSimulator();
            123 
            124         VInt                    Parse(VUnicodeString Input , VL_LexerFactoryPtr& LexerResult , GrammarSimulatorNode::List& Result);
            125     };
            126     class GrammarSimulateParam : public VL_Base
            127     {
            128         typedef VL_ListedMap<VUnicodeString , VSynTerm>    TermMap;
            129         typedef VL_List<VUnicodeString , false>            StringList;
            130     public:
            131         GrammarSimulator::Ptr    Simulator;
            132         TermMap                    Terms;
            133         VInt                    UsedStorageIndex;
            134         StringList                ErrorMessages;
            135     };
            136     class GrammarSimulate : public GrammarAlgorithmEx<VSynTerm , GrammarSimulateParam*>
            137     {
            138 
            139         VSynTerm                Visit(GrammarBranch* Obj , GrammarSimulateParam* Param);
            140         VSynTerm                Visit(GrammarSequence* Obj , GrammarSimulateParam* Param);
            141         VSynTerm                Visit(GrammarOptional* Obj , GrammarSimulateParam* Param);
            142         VSynTerm                Visit(GrammarUnit* Obj , GrammarSimulateParam* Param);
            143         VSynTerm                Visit(GrammarRule* Obj , GrammarSimulateParam* Param);
            144         VSynTerm                Visit(LexicalDecl* Obj , GrammarSimulateParam* Param);
            145         VSynTerm                Visit(GrammarDescription* Obj , GrammarSimulateParam* Param);
            146     };
            147 }
            148 
            149 #endif

                實現:
              1 #include "GrammarAlgorithms.h"
              2 #include "..\..\..\Library\Data\Grammar2\VL_Regexp.h"
              3 
              4 using namespace vl::grammar;
              5 
              6 namespace compiler
              7 {
              8 
              9 /*********************************************************************************************************
             10 GrammarToString
             11 *********************************************************************************************************/
             12 
             13     VUnicodeString GrammarToString::Visit(GrammarBranch* Obj , VUnicodeString Prefix)
             14     {
             15         VUnicodeString Result;
             16         Result+=Prefix+L"branch {\r\n";
             17         for(VInt i=0;i<Obj->Expressions.GetCount();i++)
             18         {
             19             Result+=Apply(Obj->Expressions[i],Prefix+L"  ");
             20         }
             21         Result+=Prefix+L"}\r\n";
             22         return Result;
             23     }
             24 
             25     VUnicodeString GrammarToString::Visit(GrammarSequence* Obj , VUnicodeString Prefix)
             26     {
             27         VUnicodeString Result;
             28         Result+=Prefix+L"sequence {\r\n";
             29         for(VInt i=0;i<Obj->Expressions.GetCount();i++)
             30         {
             31             Result+=Apply(Obj->Expressions[i],Prefix+L"  ");
             32         }
             33         Result+=Prefix+L"}\r\n";
             34         return Result;
             35     }
             36 
             37     VUnicodeString GrammarToString::Visit(GrammarOptional* Obj , VUnicodeString Prefix)
             38     {
             39         VUnicodeString Result;
             40         Result+=Prefix+L"optional {\r\n";
             41         Result+=Apply(Obj->Expression,Prefix+L"  ");
             42         Result+=Prefix+L"}\r\n";
             43         return Result;
             44     }
             45 
             46     VUnicodeString GrammarToString::Visit(GrammarUnit* Obj , VUnicodeString Prefix)
             47     {
             48         return Prefix+Obj->Name+L"\r\n";
             49     }
             50 
             51     VUnicodeString GrammarToString::Visit(GrammarRule* Obj , VUnicodeString Prefix)
             52     {
             53         VUnicodeString Result;
             54         Result+=Prefix+L"rule {\r\n";
             55         Result+=Prefix+L"  "+Obj->Name+L"\r\n";
             56         Result+=Apply(Obj->Expression,Prefix+L"  ");
             57         Result+=Prefix+L"}\r\n";
             58         return Result;
             59     }
             60 
             61     VUnicodeString GrammarToString::Visit(LexicalDecl* Obj , VUnicodeString Prefix)
             62     {
             63         VUnicodeString Result;
             64         Result+=Prefix+L"lexical inference {\r\n";
             65         Result+=Prefix+L"  "+Obj->Name+L"\r\n";
             66         Result+=Prefix+L"  "+Obj->ProcessedRegex+L"\r\n";
             67         Result+=Prefix+L"}\r\n";
             68         return Result;
             69     }
             70 
             71     VUnicodeString GrammarToString::Visit(GrammarDescription* Obj , VUnicodeString Prefix)
             72     {
             73         VUnicodeString Result;
             74         Result+=Prefix+L"lexical inferences {\r\n";
             75         for(VInt i=0;i<Obj->Tokens.GetCount();i++)
             76         {
             77             Result+=Apply(Obj->Tokens[i],Prefix+L"  ");
             78         }
             79         Result+=Prefix+L"}\r\n";
             80         Result+=Prefix+L"syntax inferences {\r\n";
             81         for(VInt i=0;i<Obj->Rules.GetCount();i++)
             82         {
             83             Result+=Apply(Obj->Rules[i],Prefix+L"  ");
             84         }
             85         Result+=Prefix+L"}\r\n";
             86         return Result;
             87     }
             88 
             89 /*********************************************************************************************************
             90 GrammarToCode
             91 *********************************************************************************************************/
             92 
             93     VUnicodeString GrammarToCode::Visit(GrammarBranch* Obj , VUnicodeString Prefix)
             94     {
             95         VUnicodeString Result;
             96         for(VInt i=0;i<Obj->Expressions.GetCount();i++)
             97         {
             98             if(i)Result+=L" | ";
             99             Result+=Apply(Obj->Expressions[i],L"");
            100         }
            101         return Prefix+L"("+Result+L")";
            102     }
            103 
            104     VUnicodeString GrammarToCode::Visit(GrammarSequence* Obj , VUnicodeString Prefix)
            105     {
            106         VUnicodeString Result;
            107         for(VInt i=0;i<Obj->Expressions.GetCount();i++)
            108         {
            109             if(i)Result+=L" ";
            110             Result+=Apply(Obj->Expressions[i],L"");
            111         }
            112         return Prefix+L"("+Result+L")";
            113     }
            114 
            115     VUnicodeString GrammarToCode::Visit(GrammarOptional* Obj , VUnicodeString Prefix)
            116     {
            117         return Prefix+L"["+Apply(Obj->Expression,L"")+L"]";
            118     }
            119 
            120     VUnicodeString GrammarToCode::Visit(GrammarUnit* Obj , VUnicodeString Prefix)
            121     {
            122         return Prefix+Obj->Name;
            123     }
            124 
            125     VUnicodeString GrammarToCode::Visit(GrammarRule* Obj , VUnicodeString Prefix)
            126     {
            127         return Prefix+Obj->Name+L" = "+Apply(Obj->Expression,L"");
            128     }
            129 
            130     VUnicodeString GrammarToCode::Visit(LexicalDecl* Obj , VUnicodeString Prefix)
            131     {
            132         return Prefix+Obj->Name+L" = "+Obj->RegularExpression;
            133     }
            134 
            135     VUnicodeString GrammarToCode::Visit(GrammarDescription* Obj , VUnicodeString Prefix)
            136     {
            137         VUnicodeString Result;
            138         Result+=Prefix+L"lexical\r\n{\r\n";
            139         for(VInt i=0;i<Obj->Tokens.GetCount();i++)
            140         {
            141             Result+=Apply(Obj->Tokens[i],L"  ")+L"\r\n";
            142         }
            143         Result+=Prefix+L"}\r\n";
            144         Result+=Prefix+L"syntax\r\n{\r\n";
            145         for(VInt i=0;i<Obj->Rules.GetCount();i++)
            146         {
            147             Result+=Apply(Obj->Rules[i],L"  ")+L"\r\n";
            148         }
            149         Result+=Prefix+L"}\r\n";
            150         return Result;
            151     }
            152 
            153 /*********************************************************************************************************
            154 GrammarValidate
            155 *********************************************************************************************************/
            156 
            157     VBool GrammarValidate::Visit(GrammarBranch* Obj , GrammarValidateParam Param)
            158     {
            159         VBool Result=true;
            160         for(VInt i=0;i<Obj->Expressions.GetCount();i++)
            161         {
            162             Result=Apply(Obj->Expressions[i],Param) && Result;
            163         }
            164         return Result;
            165     }
            166 
            167     VBool GrammarValidate::Visit(GrammarSequence* Obj , GrammarValidateParam Param)
            168     {
            169         VBool Result=true;
            170         for(VInt i=0;i<Obj->Expressions.GetCount();i++)
            171         {
            172             Result=Apply(Obj->Expressions[i],Param) && Result;
            173         }
            174         return Result;
            175     }
            176 
            177     VBool GrammarValidate::Visit(GrammarOptional* Obj , GrammarValidateParam Param)
            178     {
            179         return Apply(Obj->Expression,Param);
            180     }
            181 
            182     VBool GrammarValidate::Visit(GrammarUnit* Obj , GrammarValidateParam Param)
            183     {
            184         if(Obj->Name==L"discard")
            185         {
            186             GrammarValidateError Error;
            187             Error.Index=Param.Index;
            188             Error.RuleError=true;
            189             Error.TokenError=false;
            190             Error.Message=L"不能在文法中使用詞法記號\"discard\",\"discard\"只能被用來定義不需要的詞法記號。";
            191             Param.Structure->Errors.Add(Error);
            192             return false;
            193         }
            194         else if(Param.Structure->Rules.Exists(Obj->Name) || Param.Structure->Tokens.Exists(Obj->Name))
            195         {
            196             return true;
            197         }
            198         else
            199         {
            200             GrammarValidateError Error;
            201             Error.Index=Param.Index;
            202             Error.RuleError=true;
            203             Error.TokenError=false;
            204             Error.Message=L"標識符\""+Obj->Name+L"\"不存在,不可用于組成文法推導式。";
            205             Param.Structure->Errors.Add(Error);
            206             return false;
            207         }
            208     }
            209 
            210     VBool GrammarValidate::Visit(GrammarRule* Obj , GrammarValidateParam Param)
            211     {
            212         if(Obj->Name==L"discard")
            213         {
            214             GrammarValidateError Error;
            215             Error.Index=Param.Index;
            216             Error.RuleError=true;
            217             Error.TokenError=false;
            218             Error.Message=L"\"discard\"只能被用來定義不需要的詞法記號。";
            219             Param.Structure->Errors.Add(Error);
            220             return false;
            221         }
            222         Param.Structure->Rules[Obj->Name]->Add(Param.Index);
            223         return Apply(Obj->Expression,Param);
            224     }
            225 
            226     VBool GrammarValidate::Visit(LexicalDecl* Obj , GrammarValidateParam Param)
            227     {
            228         if(Obj->Name==L"program")
            229         {
            230             GrammarValidateError Error;
            231             Error.Index=Param.Index;
            232             Error.RuleError=false;
            233             Error.TokenError=true;
            234             Error.Message=L"\"program\"只能被用于定義文法初始符號。";
            235             Param.Structure->Errors.Add(Error);
            236             return false;
            237         }
            238         else if(Param.Structure->Tokens.Exists(Obj->Name))
            239         {
            240             GrammarValidateError Error;
            241             Error.Index=Param.Index;
            242             Error.RuleError=false;
            243             Error.TokenError=true;
            244             Error.Message=L"詞法記號定義\""+Obj->Name+L"\"已經存在。";
            245             Param.Structure->Errors.Add(Error);
            246             return false;
            247         }
            248         else
            249         {
            250             {
            251                 PWChar Buffer=new VWChar[Obj->RegularExpression.Length()];
            252                 VInt Len=0;
            253                 PWChar Read=Obj->RegularExpression.Buffer();
            254                 Read++;
            255                 while(true)
            256                 {
            257                     if(Read[0]==L'\'')
            258                     {
            259                         if(Read[1]==L'\'')
            260                         {
            261                             Buffer[Len++]=L'\'';
            262                         }
            263                         else
            264                         {
            265                             Buffer[Len]=L'\0';
            266                             break;
            267                         }
            268                     }
            269                     else
            270                     {
            271                         Buffer[Len++]=Read[0];
            272                     }
            273                     Read++;
            274                 }
            275                 Obj->ProcessedRegex=Buffer;
            276                 delete[] Buffer;
            277             }
            278             VL_RegExpResult Result=RegularExpressionAnalysis(Obj->ProcessedRegex,false);
            279             if(Result.Error)
            280             {
            281                 GrammarValidateError Error;
            282                 Error.Index=Param.Index;
            283                 Error.RuleError=false;
            284                 Error.TokenError=true;
            285                 Error.Message=L"位置:"+VUnicodeString(Result.ErrorPosition)+L",正則表達式錯誤:"+Result.ErrorMessage;
            286                 Param.Structure->Errors.Add(Error);
            287                 return false;
            288             }
            289             else
            290             {
            291                 Param.Structure->Tokens.Add(Obj->Name,Param.Index);
            292                 return true;
            293             }
            294         }
            295     }
            296 
            297     VBool GrammarValidate::Visit(GrammarDescription* Obj , GrammarValidateParam Param)
            298     {
            299         VBool Result=true;
            300         for(VInt i=0;i<Obj->Tokens.GetCount();i++)
            301         {
            302             Param.Index=i;
            303             Result=Apply(Obj->Tokens[i],Param) && Result;
            304         }
            305         for(VInt i=0;i<Obj->Rules.GetCount();i++)
            306         {
            307             VUnicodeString Name=Obj->Rules[i]->Name;
            308             if(Param.Structure->Tokens.Exists(Name))
            309             {
            310                 GrammarValidateError Error;
            311                 Error.Index=i;
            312                 Error.RuleError=true;
            313                 Error.TokenError=false;
            314                 Error.Message=L"標識符\""+Name+L"\"已經被定義為詞法記號,不可重定義為非終結符。";
            315                 Param.Structure->Errors.Add(Error);
            316                 return false;
            317             }
            318             if(!Param.Structure->Rules.Exists(Name))
            319             {
            320                 Param.Structure->Rules.Add(Name,new VL_List<VInt , true>);
            321             }
            322         }
            323         for(VInt i=0;i<Obj->Rules.GetCount();i++)
            324         {
            325             Param.Index=i;
            326             Result=Apply(Obj->Rules[i],Param) && Result;
            327         }
            328         if(!Param.Structure->Rules.Exists(L"program"))
            329         {
            330             GrammarValidateError Error;
            331             Error.Index=-1;
            332             Error.RuleError=false;
            333             Error.TokenError=false;
            334             Error.Message=L"找不到\"program\"非終結符,文法必須使用\"Program\"作為初始符號。";
            335             Param.Structure->Errors.Add(Error);
            336             return false;
            337         }
            338         return Result;
            339     }
            340 
            341 /*********************************************************************************************************
            342 GrammarSimulate
            343 *********************************************************************************************************/
            344 
            345     VUnicodeString GrammarSimulatorNode::ToString(VUnicodeString Prefix)
            346     {
            347         if(SubExpressions.GetCount())
            348         {
            349             VUnicodeString Result;
            350             Result=Prefix+TerminatorName+L" = {\r\n";
            351             for(VInt i=0;i<SubExpressions.GetCount();i++)
            352             {
            353                 Result+=SubExpressions[i]->ToString(Prefix+L"  ");
            354             }
            355             Result+=Prefix+L"}\r\n";
            356             return Result;
            357         }
            358         else
            359         {
            360             return Prefix+TerminatorName+L" = "+Value+L"\r\n";
            361         }
            362     }
            363 
            364     class GrammarSimulatorLexerHandler : public VL_LexerHandler
            365     {
            366     public:
            367         GrammarSimulator* Simulator;
            368 
            369         GrammarSimulatorLexerHandler(GrammarSimulator* aSimulator):VL_LexerHandler(L"",false)
            370         {
            371             Simulator=aSimulator;
            372         }
            373 
            374         void Handle(VL_FreeLexer<VLS_LexerTokenType>::InternalTokenData& TokenData)
            375         {
            376             Simulator->ParseErrors.Add(inner::Convert(TokenData,L"遇到無法分析的記號:\""+TokenData.Token+L"\""));
            377         }
            378     };
            379 
            380     class GrammarSimulatorHandler : public GrammarSimulator::RuleHandler
            381     {
            382     public:
            383         GrammarSimulator* Simulator;
            384         VL_LexerFactoryPtr Factory;
            385         VUnicodeString Name;
            386 
            387         GrammarSimulatorHandler(GrammarSimulator* aSimulator , VUnicodeString aName , VL_SynRuleItem* RuleItem):GrammarSimulator::RuleHandler(RuleItem)
            388         {
            389             Simulator=aSimulator;
            390             Name=aName;
            391         }
            392 
            393         GrammarSimulatorNode::Ptr Handle(GrammarSimulator::RuleTransformer::IndexedData& Data)
            394         {
            395             GrammarSimulatorNode::Ptr Node=new GrammarSimulatorNode;
            396             Node->TerminatorName=Name;
            397             for(VInt i=0;i<Data.KeyCount();i++)
            398             {
            399                 GrammarSimulator::RuleTransformer::StoreType& Type=Data.ValueOfIndex(i);
            400                 if(Type.Token)
            401                 {
            402                     GrammarSimulatorNode::Ptr Token=new GrammarSimulatorNode;
            403                     Token->TerminatorName=Simulator->TokenIDs[Type.Token->TokenID()];
            404                     Token->Value=Factory->GetDataOfStream(Type.Token).Token;
            405                     Node->SubExpressions.Add(Token);
            406                 }
            407                 else
            408                 {
            409                     Node->SubExpressions.Add(Type.Data);
            410                 }
            411             }
            412             return Node;
            413         }
            414     };
            415 
            416     GrammarSimulator::GrammarSimulator():Lexer(true)
            417     {
            418         LexerErrorHandler=new GrammarSimulatorLexerHandler(this);
            419         Lexer.AddHandler(LexerErrorHandler.Object());
            420     }
            421 
            422     VInt GrammarSimulator::Parse(VUnicodeString Input , VL_LexerFactoryPtr& LexerResult , GrammarSimulatorNode::List& SynerResult)
            423     {
            424         VL_SynMacInsListList Results;
            425         VL_SynTokenErrorList Errors;
            426         ParseErrors.Clear();
            427 
            428         LexerResult=Lexer.Parse(Input);
            429         for(VInt i=0;i<Handlers.GetCount();i++)
            430         {
            431             dynamic_cast<GrammarSimulatorHandler*>(Handlers[i].Object())->Factory=LexerResult;
            432         }
            433         if(LexerResult)
            434         {
            435             Syner.Parse(LexerResult->GetSynTokenFactory(),Results,Errors);
            436             for(VInt i=0;i<Errors.GetCount();i++)
            437             {
            438                 VL_FreeLexer<VLS_LexerTokenType>::InternalTokenData& TokenData=LexerResult->GetDataOfPosition(Errors[i].Position);
            439                 if(&TokenData)
            440                 {
            441                     ParseErrors.Add(inner::Convert(TokenData,Errors[i].Message));
            442                 }
            443                 else
            444                 {
            445                     GrammarError Error;
            446                     Error.LineInFile=-1;
            447                     Error.PosInFile=-1;
            448                     Error.PosInLine=-1;
            449                     Error.Message=Errors[i].Message;
            450                     ParseErrors.Add(Error);
            451                 }
            452             }
            453             for(VInt i=0;i<Results.GetCount();i++)
            454             {
            455                 SynerResult.Add(Transformer.Run(Results[i].Object(),LexerResult->GetSynTokenFactory()));
            456             }
            457         }
            458         for(VInt i=0;i<Handlers.GetCount();i++)
            459         {
            460             dynamic_cast<GrammarSimulatorHandler*>(Handlers[i].Object())->Factory=0;
            461         }
            462         return SynerResult.GetCount();
            463     }
            464 
            465     VSynTerm GrammarSimulate::Visit(GrammarBranch* Obj , GrammarSimulateParam* Param)
            466     {
            467         VSynTerm Term=Apply(Obj->Expressions[0],Param);
            468         for(VInt i=1;i<Obj->Expressions.GetCount();i++)
            469         {
            470             Term=Term | Apply(Obj->Expressions[i],Param);
            471         }
            472         return Term;
            473     }
            474 
            475     VSynTerm GrammarSimulate::Visit(GrammarSequence* Obj , GrammarSimulateParam* Param)
            476     {
            477         VSynTerm Term=Apply(Obj->Expressions[0],Param);
            478         for(VInt i=1;i<Obj->Expressions.GetCount();i++)
            479         {
            480             Term=Term + Apply(Obj->Expressions[i],Param);
            481         }
            482         return Term;
            483     }
            484 
            485     VSynTerm GrammarSimulate::Visit(GrammarOptional* Obj , GrammarSimulateParam* Param)
            486     {
            487         return Opt(Apply(Obj->Expression,Param));
            488     }
            489 
            490     VSynTerm GrammarSimulate::Visit(GrammarUnit* Obj , GrammarSimulateParam* Param)
            491     {
            492         return Param->Terms[Obj->Name][Param->UsedStorageIndex++];
            493     }
            494 
            495     VSynTerm GrammarSimulate::Visit(GrammarRule* Obj , GrammarSimulateParam* Param)
            496     {
            497         Param->UsedStorageIndex=0;
            498         VL_SynRuleItem* RuleItem=Param->Simulator->Syner.Infer(Param->Terms[Obj->Name])=Apply(Obj->Expression,Param);
            499         GrammarSimulatorHandler* Handler=new GrammarSimulatorHandler(Param->Simulator.Object(),Obj->Name,RuleItem);
            500         Param->Simulator->Handlers.Add(Handler);
            501         Param->Simulator->Transformer.AddHandler(Handler);
            502         return VSynTerm();
            503     }
            504 
            505     VSynTerm GrammarSimulate::Visit(LexicalDecl* Obj , GrammarSimulateParam* Param)
            506     {
            507         VInt ID=Param->Simulator->Lexer.AddHandler(Obj->ProcessedRegex,(Obj->Name==L"discard"),Obj->Name);
            508         Param->Simulator->TokenIDs.Add(ID,Obj->Name);
            509         return Param->Simulator->Syner.Token(ID,Obj->Name);
            510     }
            511 
            512     VSynTerm GrammarSimulate::Visit(GrammarDescription* Obj , GrammarSimulateParam* Param)
            513     {
            514         try
            515         {
            516             for(VInt i=0;i<Obj->Tokens.GetCount();i++)
            517             {
            518                 Param->Terms.Add(Obj->Tokens[i]->Name,Apply(Obj->Tokens[i],Param));
            519             }
            520             for(VInt i=0;i<Obj->Rules.GetCount();i++)
            521             {
            522                 VUnicodeString Name=Obj->Rules[i]->Name;
            523                 if(!Param->Terms.Exists(Name))
            524                 {
            525                     Param->Terms.Add(Name,Param->Simulator->Syner.Rule(i,Name));
            526                 }
            527             }
            528             for(VInt i=0;i<Obj->Rules.GetCount();i++)
            529             {
            530                 Apply(Obj->Rules[i],Param);
            531             }
            532             Param->Simulator->Lexer.Initialize();
            533             Param->Simulator->Syner.Initialize(Param->Terms[L"program"]);
            534             Param->Simulator->Transformer.Initialize(Param->Simulator->Syner.GetRuleItems());
            535         }
            536         catch(VL_SynError& Error)
            537         {
            538             Param->ErrorMessages=Error.Messages;
            539         }
            540         return VSynTerm();
            541     }
            542 }
            posted on 2008-09-06 02:45 陳梓瀚(vczh) 閱讀(2296) 評論(3)  編輯 收藏 引用 所屬分類: 腳本技術

            評論:
            # re: 項目實驗2:動態生成編譯器前端 2008-09-06 06:37 | Lnn
            超強!  回復  更多評論
              
            # re: 項目實驗2:動態生成編譯器前端 2008-09-06 15:31 | 沈臻豪(foxtail)
            這些就是你最近一直在寫的demo?
              回復  更多評論
              
            # re: 項目實驗2:動態生成編譯器前端 2012-12-30 06:07 | ArthasLee
            超級代碼兔又貼代碼嚇唬小盆與~_~  回復  更多評論
              
            久久婷婷五月综合色高清| 亚洲一本综合久久| 一本大道加勒比久久综合| 中文字幕无码av激情不卡久久| 久久99精品国产| 久久婷婷成人综合色综合| 少妇久久久久久被弄到高潮 | 久久亚洲私人国产精品| 亚洲国产成人久久一区WWW| 久久精品国产影库免费看| 久久亚洲sm情趣捆绑调教| 久久精品国产色蜜蜜麻豆| 国产99久久久久久免费看 | 亚洲性久久久影院| 久久激情五月丁香伊人| 久久99精品免费一区二区| 99久久夜色精品国产网站| 久久91精品国产91久久小草| AV无码久久久久不卡网站下载| 午夜不卡久久精品无码免费| 午夜欧美精品久久久久久久| 欧美大战日韩91综合一区婷婷久久青草| 久久精品国产72国产精福利| 久久久精品国产Sm最大网站| 理论片午午伦夜理片久久| 7777精品伊人久久久大香线蕉| 久久乐国产综合亚洲精品| 久久久国产精华液| 亚洲av日韩精品久久久久久a| 久久久亚洲欧洲日产国码aⅴ | 色8激情欧美成人久久综合电| 久久青青草原精品影院| 国产呻吟久久久久久久92| 四虎亚洲国产成人久久精品| 日韩乱码人妻无码中文字幕久久| 国产亚洲色婷婷久久99精品| 999久久久国产精品| 国产精品亚洲综合久久| 国产成人无码精品久久久免费| 亚洲国产精品无码久久SM| 久久亚洲2019中文字幕|