What is the Difference Between Lex and Yacc

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. It goes through several phases to perform this conversion. First, there is a lexical analysis phase. It involves reading the source program one character at a time and converting it into meaningful lexemes (tokens). Next, the output of that phase goes to the syntax analysis. It takes the tokens of the lexical analysis and produces a parse tree as the output. Overall, the lexical analyzer performs the lexical analysis while syntax analyzer performs syntax analysis. Lex is a lexical analyzer whereas Yacc is a parser. Both work together. For example, Lex takes the string input to create tokens, and Yacc uses those tokenized input.

Key Areas Covered

1. What is Lex
-Definition, Functionality
2. What is Yacc
– Definition, Functionality
3. Difference Between Lex and Yacc
-Comparison of key differences

Key Terms

Lex, Lexical Analyzer, Parse Tree, Syntax Analysis, Yacc

Difference Between Lex and Yacc - Comparison Summary

What is Lex

Lex is a computer program that generates lexical analyzers. Mike Lex and Eric Schmidt are the original developers of Lex. It is a standard lexical analyzer generator on various UNIX systems. Lex is specified as a part of the POSIX standard. Generally, Lex is used with Yacc parse generator. Furthermore, Lex reads an input stream specifying the lexical analyzer. Then, it outputs the source code implementing the lexer in the C language. 

Difference Between Lex and Yacc

A Lex file consists of the following three sections:

Definition: Defines macros and imports header files written in C.

Rules: It contains regular expression patterns with C statements. When the lexer identifies that the text in the input matches a given pattern, it will execute the associated C code.

C code: This section consists of C statements and functions.

Furthermore, the open-source version of Lex is now distributed as a part of OpenSolaris and Plan 9 from Bell Labs. The most popular open-source version of Lex is called flex, which stands for Fast Lexical Analyzer.

What is Yacc

Yacc stands for Yet Another Compiler-Compiler. Stephan C. Johnson developed it, and it is used in UNIX systems. It is a standard utility on BSD and AT&T UNIX. Additionally, GNU based Linux distribution include Bison, forward-compatible Yacc replacement.

Moreover, the input to Yacc is a grammar of C code that is attached to its rules. The output is a shift-reduce parser in C. After recognizing the rule, it executes the C code associated with each rule. Further, the typical actions include generating the parse tree.

Difference Between Lex and Yacc

Definition

Lex is a computer program that operates as a lexical analyzer while Yacc is a parser that is used in the Unix Operating System.

Developer

Mike Lex and Eric Schmidt developed Lex whereas Stephan C. Johnson developed Yacc.

Functionality

While Lex reads the source program one character at a time and converts it into meaningful tokens, Yacc takes the tokens as input and generates a parse tree as output.

Conclusion

In brief, lexical analyzer and parser support the first two phases of the compilation process. In other words, Lex is a lexical analyzer, and Yacc is a parser. Thus, 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.

References:

1.“Lex (Software).” Wikipedia, Wikimedia Foundation, 29 June 2019, Available here.
2.“Yacc.” Wikipedia, Wikimedia Foundation, 23 June 2019, Available here.

Image Courtesy:

1.”85017″ via (CC0) Pxhere

About the Author: Lithmee

Lithmee holds a Bachelor of Science degree in Computer Systems Engineering and is reading for her Master’s degree in Computer Science. She is passionate about sharing her knowldge in the areas of programming, data science, and computer systems.

Leave a Reply