A
A
akass2014-11-23 16:14:11
Programming
akass, 2014-11-23 16:14:11

What is the correct way to implement a regular expression without finite automata?

I'm trying through loops and arrays, that's what happened so far.
pastebin.com/Emu
At this stage, the goal is to implement at least one regular expression -
"+" - from 1 or more
. empty result.
The built-in class is not suitable, you need an explicit implementation.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
X
xandox, 2014-11-23
@xandox

Well, for starters, regular expressions and state machine are synonyms. On this, decide what exactly you want to do.

M
Mrrl, 2014-11-25
@Mrl

Somehow like that. Not very efficient, really. And no brackets.

static string FindReg(string src,string ptrn) {
            int LP=ptrn.Length,LS=src.Length;
            for(int i=0;i<LS;i++) {
                int k=i;
                bool ok=true;
                for(int j=0;ok && j<LP;) {
                    int m=1;
                    bool plus=false;
                    char cur=ptrn[j++];
                    for(;j<LP;j++) {
                        if(ptrn[j]=='+') plus=true;
                        else if(ptrn[j]==cur) m++;
                        else break;
                    }
                    int s=0;
                    while(k+s<LS && src[k+s]==cur) s++;
                    if(s<m) {
                        ok=false; break;
                    } else if(plus) k+=s;
                    else k+=m;
                }
                if(ok) return src.Substring(i,k-i);
            }
            return null;
        }

But in general, the state machine will be needed very quickly. Already for some a * b * ac it will be difficult to do without it (if you do not write a terrible recursion).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question