Can regular expressions be made unambiguous? - MathOverflow most recent 30 from http://mathoverflow.net 2013-05-24T04:48:09Z http://mathoverflow.net/feeds/question/45149 http://www.creativecommons.org/licenses/by-nc/2.5/rdf http://mathoverflow.net/questions/45149/can-regular-expressions-be-made-unambiguous Can regular expressions be made unambiguous? Max 2010-11-07T08:38:21Z 2010-12-04T02:46:30Z <p>When investigating regular languages, regular expressions are obviously a useful characterisation, not least because they are amenable to nice inductions. On the other hand ambiguity can get in the way of some proofs.</p> <p>Every regular language is recognized by an unambiguous context-free grammar (take a deterministic automaton which recognises it, and make a production $R \rightarrow tS$ for every edge $R \stackrel{t}{\rightarrow} S$ in the DFA, and $R \rightarrow \epsilon$ for every accepting state $R$).</p> <p>On the other hand, the natural "grammar" for a regular language is its regular expression. Can <em>these</em> be made unambiguous?</p> <p>To be precise, let's define a parse for a regular expression (this is I think a natural definition, but not one I've seen named before).</p> <ul> <li>$x$ is an $x$-parse of $x$, if $x$ is a symbol or $x=\varepsilon$</li> <li>$(y, 0)$ is an $R\cup R'$-parse of $x$, if $y$ is an $R$-parse of $x$</li> <li>Similarly, $(y,1)$ is an $R\cup R'$-parse of $x$, if $y$ is an $R'$-parse of $x$</li> <li>$(y_1, y_2)$ is an $RR'$-parse for $x_1x_2$, if $y_i$ is an $R$-parse for $x_i$ for $i=1,2$</li> <li>$[]$ is an $R^*$-parse for $\varepsilon$</li> <li>$[y_1, y_2, \dots, y_n]$ is an $R^*$-parse of $x_1x_2\cdots x_n$, if $y_i$ is an $R$-parse for $x_i$ for $1 \le i \le n$</li> </ul> <p>In short, the parses of a string tell us <em>how</em> a regular expression matches a string <em>if</em> it does.</p> <p>A regular expression $R$ is <em>unambiguous</em> if, for every $x \in L(R)$, there is only one $R$-parse of $x$.</p> <blockquote> Given a regular expression, is there an unambiguous regular expression which matches the same language? </blockquote> http://mathoverflow.net/questions/45149/can-regular-expressions-be-made-unambiguous/45163#45163 Answer by David Eppstein for Can regular expressions be made unambiguous? David Eppstein 2010-11-07T12:00:45Z 2010-11-07T12:00:45Z <p>There's a standard construction of a regular expression from a DFA: define an expression R(i,j,k) for the language of strings that take state i to state j of the DFA while using intermediate states that belong only to the subset of states from state 1 to state k, as follows.</p> <ul> <li>R(i,j,0) is [xyz...] where x, y, z etc are the symbols that occur as labels of transitions from state i to state j (there can be no intermediate states). If there is no such transition then R(i,j,0)=0 (the expression for the empty language).</li> <li>Similarly, R(i,i,0) is e + [xyz...] where e is the expression that represents the empty string.</li> <li>For k > 0, R(i,j,k) is R(i,j,k-1) + R(i,k,k-1) R(k,k,k-1)* R(k,j,k-1). That is, any string that takes you from i to j using intermediate states up to k either goes from i to j without going through k, or can be parsed into a sequence of substrings that go from i to k, k back to itself zero or more times, and then k to j.</li> <li>The regular expression for the whole language is then the sum of the expressions R(1,i,n) where i is one of the accepting states.</li> </ul> <p>The first two of these rules obviously give you unambiguous expressions, the third is unambiguous because there's only one way of parsing the string into substrings as described, and the fourth is unambiguous because any given string can only go to a single accepting state. Therefore, the final regular expression constructed in this way is unambiguous.</p> http://mathoverflow.net/questions/45149/can-regular-expressions-be-made-unambiguous/48243#48243 Answer by Lamont C for Can regular expressions be made unambiguous? Lamont C 2010-12-04T02:46:30Z 2010-12-04T02:46:30Z <p>Regarding your question about disambiguation without exponential increase: if you're willing to move to generalized regular expressions (regular expressions with the complement and intersection operators as primitives, rather than exponential-blowup-inducing derived operations), then yes: assuming longest-match-convention on the Kleene star the only source of ambiguity will be the alternation (vertical bar) construct, and you can disambiguate every instance of this by simply turning "a|b" into "a|(b&amp;~a)" ("a or b-and-not-a").</p> <p>Note that adding intersection and complementation does not alter the class of languages which may be defined. The languages which can be defined are still exactly the regular languages.</p> <p>I think this suggests that you probably can't disambiguate in general without exponential blowup because disambiguation is "pretty similar to" (vague term, I know) intersection with the complement.</p> <p>Here's a nice <a href="http://drops.dagstuhl.de/opus/volltexte/2008/1354/" rel="nofollow">paper</a> which presents many of the results needed above.</p>