Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is "regex" in modern programming languages really "context sensitive grammar"?

Over the years, "regex" pattern matching has been getting more and more powerful to the point where I wonder: is it really just context-sensitive-grammar matching? Is it a variation/extension of context-free-grammar matching? Where is it right now and why don't we just call it that instead of the old, restrictive "regular expression"?

like image 933
notnot Avatar asked Mar 04 '09 22:03

notnot


People also ask

Are regular expressions context-sensitive?

Regular Expressions are capable of describing the syntax of Tokens. Any syntactic construct that can be described by Regular Expression can also be described by the Context free grammar.

Is regex a context free grammar?

The difference between regular expression and context free grammar is that the regular expressions help to describe all the strings of a regular language while the context free grammar helps to define all possible strings of a context free language.

Are programming languages context-sensitive?

Programming languages, for example, are context-free grammars — a compiler reads your code to make sure it conforms to specific rules and informs you of any errors.

Is regex a grammar?

We also talk about a specialized form of a grammar called a regular expression. In addition to being used for specification and parsing, regular expressions are a widely-used tool for many string-processing tasks that need to disassemble a string, extract information from it, or transform it.


1 Answers

In particular backreferences to capturing parentheses make regular expressions more complex than regular, context-free, or context-sensitive grammars. The name is simply historically grown (as many words). See also this section in Wikipedia and this explanation with an example from Perl.

like image 106
Fabian Steeg Avatar answered Sep 26 '22 03:09

Fabian Steeg