Is “regex” in modern programming languages really “context sensitive grammar”?

前端 未结 3 1619
死守一世寂寞
死守一世寂寞 2020-12-16 19:39

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

3条回答
  •  没有蜡笔的小新
    2020-12-16 20:15

    The way I see it:

    • Regular languages:
      • Matched by state machines. Only one variable can be used to represent the current "location" in the grammar to be matched: Recursion cannot be implemented
    • Context-free languages:
      • Matched by a stack machine. The current "location" in the grammar is represented by a stack in one or another form. Cannot "remember" anything that occurred before
    • Context-sensitive languages:
      • Most programming languages
      • All Most human languages

    I do know of regular expression parsers that allow you to match against something the parser has already encountered, achieving something like a context-sensitive grammar.

    Still, regular expression parsers, however sophisticated they may be, don't allow for recursive application of rules, which is a definite requirement for context-free grammars.

    The term regex, in my opinion, mostly refers to the syntax used to express those regular grammars (the stars and question marks).

提交回复
热议问题