Lex and Yacc replacement


#1

Is there a Rust version of lex and yacc (or bison and flex)?

Or how do people write parsers to be used with Rust? From scratch?

Any links to existing crates or any thoughts about the topic would be appreciated.


#2

I don’t know of any direct ports, but there is quite some amount of parser crates that should fit your style:

https://crates.io/crates/lalrpop
https://crates.io/crates/nom
https://crates.io/crates/combine


#3

My approach would be to first use one of the mentioned parsers to produce tokens (what flex does), and the use the same library to parse the token stream to produce an AST.

I’ve only used nom, which is generic over the input type, so you can use the default &[char] / &[u8] input type for the lexing, produce a Vec of tokens, and then use &[Token] as the input for the parser.

This approach works pretty well, and I’ve used it for years. Also in other languages with parser combinators like parsec (Haskell).


#4

Lalrpop worked well for me (though I wish that someone wrote antlr backend for rust).


#5

Interesting, I hadn’t thought of that. Thanks <3.


#6

Thanks for your suggestion, that sounds very promising.


#7

Thanks for the links. I will have a deeper look …