Lex and Yacc are compiler-writing tools that are used to specify lexical tokens and context-free grammars. Lex is used to specify lexical tokens and their processing order, while Yacc is used to specify context-free grammars for LALR(1) parsing. Both tools have a long history in computing and were originally developed in the early days of Unix on minicomputers. Lex reads a specification of lexical patterns and actions and generates a C program that implements a lexical analyzer. The generated C program defines a function called yylex() that scans the input and returns the recognized tokens.