Are regular expressions used to build parsers?

后端 未结 8 1767
Happy的楠姐
Happy的楠姐 2020-12-16 15:58

This is just a question out of curiosity since I have been needing to get more and more into parsing and using regex lately.. it seems, for questions I come across in my sea

相关标签:
8条回答
  • 2020-12-16 16:39

    No, parsers are built from grammars.

    But most compilers (interpreters) will use a separate scanner (lexer) to recognize the input-tokens. A scanner can be specified with regular expressions, but afaik they are not built using the usual RegEx library classes.

    A separate scanner is a practical approach. It is possible to define full grammars all the way down to the character level, but that is impractical. Regular expressions handle the endpoint subset of the grammars easier.

    For reference, see Yacc and Lex. They both have modern successors.

    0 讨论(0)
  • 2020-12-16 16:44

    Typically, you use some sort of pattern-matching (not necessarily regular expressions) in a lexer, to turn your stream of characters into a stream of tokens, and have your parser look at those tokens instead of the raw character input.

    If you're looking to produce your own parsers, you're probably better off looking at something like Bison to assist with that.

    0 讨论(0)
提交回复
热议问题