简体   繁体   中英

Unambiguous Grammar for Regular Expressions

I'm trying to develop a recursive decent parser for regular expressions for a homework assignment. I just wanted to ask the community if the grammar I've developed is correct or if I'm on the right track:

-= Regex Grammar (EBNF) =-
    <start> -> <expr> '\n'

    <expr>  -> <expr> { '|' <term> }         // Union
             | <expr> { <expr> }             // Concatenation
             | <expr> '*'                    // Closure
             | <term>

    <term>  -> '(' <expr> ')' | <char>       // Grouping
             | <char>

    <char>  -> a|b|c| ... |z

A few guidelines:
1. Precedence: In the order listed (highest to lowest) Closure, Concatenation, Union
2. Associativity: Closure is right-associative; Concatenation/Union are left-associative
3. Must support grouping with parens

My Question: Does the grammar (above) meet the guidelines? I feel certain but I'm not 100% and was hoping a few seasoned eyes could point out some issues/errors.

TIA Noob

<start>
<expr>
<expr><expr>
<expr><expr><expr>
<term><term><term>
'abc'

This is ambiguous, because in the third step you can either expand the first <expr> or the latter one. You should be able to work around that by removing

<expr> -> <expr> { <expr> }

and create

<term> -> <term> <expr>

instead.

You are repeating yourself here

<term>  -> '(' <expr> ')' | <char>       // Grouping
         | <char>

(you have <char> two times, did you mean to have it '(' <expr> ')' '|' <char> in the first rule?) I think it would be clearer to remove

<term> -> '(' <expr> ')'

and create

<expr> -> '(' <expr> ')'

instead.

Then you also need to add quotation marks around the characters in <char> .

This is what I see from quickly looking through your EBNF, it's been a while since I was studying this myself so some of my corrections might be wrong.

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