Compiling principle: Formal conversion into NFA algorithm

Source: Internet
Author: User

Importjava.util.ArrayList;ImportJava.util.Map;ImportJava.util.TreeMap;classedge{Private intu, v; Private Charkey;  PublicEdge (intUintVCharkey) {        Super();  This. u =u;  This. v =v;  This. Key =key; } @Override PublicString toString () {returnU + "+" + V + "" +key; }}classnfa{PrivateString Formal_ceremony;//Regular String    Private intcnt_node=1;//number of record nodes    PrivateMap<integer, integer> Endnode =NewTreemap<integer, integer> ();//each endpoint corresponding to the start nodeArraylist<edge> nodeal =NewArraylist<edge>();  PublicNFA (String formal_ceremony) {Super();  This. Formal_ceremony =Formal_ceremony; }         Public BooleanKernel_way (intFaintLdintRdBooleanIsclosure) {//FA represents the starting point of the interval, the normal interval [ld, RD], and Isclosure indicates whether a closure is found in this interval        if(LD < 0 | | Rd >=formal_ceremony.length ()) {System.out.println ("Normal type is incorrect---an array is out of bounds!"); return false; }        intPre_node =FA; intInbracket = 0;//Judging ' | ' is within brackets         for(intI=ld; i<=rd; ++i) {            if(Formal_ceremony.charat (i) = = ' (') + +Inbracket; Else if(Formal_ceremony.charat (i) = = ') ')--Inbracket; Else if(Formal_ceremony.charat (i) = = ' | ' && 0==inbracket) {                if(!kernel_way (FA, LD, i-1, isclosure))return false; if(!kernel_way (FA, i+1, RD, Isclosure))return false; return true; }        }         for(intI=ld; i<=rd; ++i) {            if(Formal_ceremony.charat (i) = = ' (') {//It's a sub-zone again .//Look for the ' (' matching ') '                intCntleftbracket = 0;//Statistics Traversal process ' (' occurrences, encounters ') ' minus 1                intPosrightbracket =-1;//record matches the ') ' position                intPosleftbracket =i;  for(intj=i+1; j<=rd; ++j) {                    if(Formal_ceremony.charat (j) = = ' (')                        ++Cntleftbracket; Else if(Formal_ceremony.charat (j) = = ') '){                        if(Cntleftbracket = = 0) {Posrightbracket=J;  Break; }                        --Cntleftbracket; }                }                if(Posrightbracket = =-1) {//ErrorSystem.out.println ("Normal type error----brackets mismatch!")); return false; }                intNodefather = 0;//The starting node of the regular type in parentheses                if(posrightbracket+1 <= Rd && Formal_ceremony.charat (posrightbracket+1) = = ' * ') {i= posrightbracket+1;//filter Out "() *"Nodeal.add (NewEdge (Pre_node, ++cnt_node, ' $ '));//indicates that this edge is emptyPre_node =Cnt_node; Nodefather=Cnt_node; Nodeal.add (NewEdge (Pre_node, ++cnt_node, ' $ '));//indicates that this edge is emptyPre_node =Cnt_node; //processing () * Regular type in brackets                    if(!kernel_way (Nodefather, posleftbracket+1, PosRightBracket-1,true))return false; } Else{Nodefather=Pre_node; if(!kernel_way (Nodefather, posleftbracket+1, PosRightBracket-1,false))//for "(101)", as 101                        return false; I=Posrightbracket; }                            } Else{//Single character                if(Formal_ceremony.charat (i) = = ') ')Continue; if(i+1 <= Rd && Formal_ceremony.charat (i+1) = = ' * ') {Nodeal.add (NewEdge (Pre_node, ++cnt_node, ' $ '));//indicates that this edge is emptyPre_node =Cnt_node; Nodeal.add (NewEdge (Pre_node, Pre_node, Formal_ceremony.charat (i)));//itself to one side of itself                    if(I+1==rd &&isclosure) Nodeal.add (NewEdge (Pre_node, FA, ' $ '));//indicates that this edge is empty and is connected to the Father node                    Else{                        if(Endnode.containskey (FA)) Nodeal.add (NewEdge (Pre_node, Endnode.get (FA), ' $ ')); Else{Nodeal.add (NewEdge (Pre_node, ++cnt_node, ' $ '));//indicates that this edge is empty                            if(I==RD) endnode.put (FA, Cnt_node);//record the last node corresponding to the first node in a non-closure state}} Pre_node=Cnt_node; ++i;//Filter *}Else {                    if(I==rd && isclosure) {//It's a closure case.Nodeal.add (NewEdge (Pre_node, FA, Formal_ceremony.charat (i))); } Else{                        if(Endnode.containskey (FA)) Nodeal.add (NewEdge (Pre_node, Endnode.get (FA), Formal_ceremony.charat (i))); Else{Nodeal.add (NewEdge (Pre_node, + +Cnt_node, Formal_ceremony.charat (i))); if(I==RD) endnode.put (FA, Cnt_node);//record the last node corresponding to the first node in a non-closure state}} Pre_node=Cnt_node; }            }        }        return true; }         Public voidOutputnfa () { for(Edge e:nodeal) System.out.println (e); }}/** Convert regular form to NFA **/ Public classTonfa { Public Static voidMain (string[] args) {//String formal_ceremony = "1 (1010*|1 (010) * *) *0";//String formal_ceremony = "1 (0|1) *101";//String formal_ceremony = "0*1* (010) 0*1*";//String formal_ceremony = "(0|1|2) *";//String formal_ceremony = "0|1";//String formal_ceremony = "0|1|2|3";//String formal_ceremony = "(0|1|6) | (2|3) | (4|5) ";//String formal_ceremony = "(0|1) *| (2|3) * ";String formal_ceremony = "((10) | ( 01) *| (0|1)) "; NFA NFA=NewNFA (Formal_ceremony); if(Nfa.kernel_way (1, 0, Formal_ceremony.length ()-1,false) ) Nfa.outputnfa (); }}

Compiling principle: Formal conversion into NFA algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.