简体   繁体   中英

Syntactical analyzer from regex

I need to build parsing try / analyser from this:

x (x | "x+y")* x+ y

For now I have grammar:

S -> x A x B y
A -> x A
A -> x+y A
A -> epsilon
B -> x B
B -> epsilon

And code like so:

S() {
    if (token == 'x') {
        A();
        if (token == 'x') {
            B();
            if (token == 'y') {
                // success
            } else {
                // syntax err
            }
        } else {
            // syntax err
        }
    } else {
        // syntax err
    }
}

A() {
    if (token == 'x') {
        if (token == '+') {
            if (token == 'y') {
                A();
            } else {
                // syntax err
            }
        } else {
            A();
        }
    } else { 
        // epsilon 
    }
}

B() {
    if (token == 'x') {
        B();
    } else { 
        // epsilon 
    }
}

The problem is B() from main S() function never be called cuz A() function gets all x tokens because of (x | "x+y")* cycle. For example: xxxxx+yx+yx+yxxxxxy How can I solve my problem and parse it? What Im doing wrong?

ps sorry for english, tnx for attention

I guess solution is in this grammar:

S -> x A
A -> x A
A -> x+y A
A -> xy

So on, the problem disappears

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM