I'm learning F# because I'd like to write a lexer and parser. I have a tiny bit of experience with this sort of processing but really need to learn it properly as well as F#.
When learning the lexing/parsing functionality of F#, is studying lex and yacc sufficient?
Or are there some differences that means code for lex/yacc will not work with fslex and fsyacc?
The main difference between Lex and Yacc is that Lex is a lexical analyzer which converts the source program into meaningful tokens while Yacc is a parser that generates a parse tree from the tokens generated by Lex. Generally, a compiler is a software program that converts the source code into machine code.
lex and yacc are a pair of programs that help write other programs. Input to lex and yacc describes how you want your final program to work. The output is source code in the C programming language; you can compile this source code to get a program that works the way that you originally described.
Lex and Yacc were the first popular and efficient lexers and parsers generators, flex and Bison were the first widespread open-source versions compatible with the original software. Each of these software has more than 30 years of history, which is an achievement in itself.
Bison is the GNU implementation/extension of Yacc, Flex is the successor of Lex. In either case, it's fine (and recommended) to use bison / flex. Additionally, byacc, the Berkeley implementation of yacc, is widely available (I see it in my Debian repository list). flex is called that because it is (was?)
I personally found these OcamlLex and OcamlYacc tutorials excellent resources to get started -- easy to follow, and you can translate most everything in those tutorials for FsLex/FsYacc almost verbatim.
Well, with lex and yacc, you put C/C++ code in the 'actions', whereas with fslex and fsyacc you put F# code there, but I presume you know this?
I think they are otherwise based on the same (established/ancient) tokenizing and parsing technologies, so the general structure/behavior of the grammar should be similar, if that's what you're after...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With