This repo contains some examples of using lex and yacc to build lexical analyzers and parsers for various languages.
Lex and yacc are tools that help programmers to create compilers and interpreters. Lex is a tool that generates scanners, which are programs that recognize lexical patterns in text. Yacc is a tool that generates parsers, which are programs that analyze the structure and syntax of text.
The examples in this repo are:
- hello_world: echos Started, Hello yourself, You are welcome, or Stopped if it gets begin, hello, thanks, or end respectively.
- line_number: outputs the same text with line numbers prepended.
- word_count: works the same as
wc
in Linux. - simple_calculator: A simple calculator that can evaluate arithmetic expressions with integers and variables.
- advanced_calculator: A calculator that can evaluate more complex expressions.
To run the examples, you need to have lex and yacc installed on your system. You can use the following commands to install them on Ubuntu:
sudo apt update
sudo apt install flex bison
Then, you can use the following commands to compile and run each example:
cd "example_name"
make
./"program_name"
For more information about lex and yacc, you can check the following resources: