Created by : 23CE051 - Darshan Kachhiya Contributed : 23CE050 - Samarth Kachhadiya | 23CE059 - Aryan Kotak
Compiler Construction Mini Project/
├── README.md # This file
├── cfg/
│ └── grammar.txt # Context-Free Grammar with explanation
├── python_parser/
│ └── tuple_parser.py # Python-based syntax analyzer
├── lex_yacc/
│ ├── tuple_lexer.l # Lex specification file
│ ├── tuple_parser.y # YACC specification file
│ └── Makefile # Build automation
└── examples/
└── test_tuples.txt # Test cases (valid and invalid)
No dependencies needed — uses only Python standard library.
cd python_parser
python3 tuple_parser.pyThis runs automated test cases and then enters interactive mode where you can type your own tuples.
Install Homebrew bison and flex (macOS system versions may require full Xcode):
brew install bison flexYou also need gcc (comes with Xcode Command Line Tools):
xcode-select --install # Install if not already present
gcc --version # Verify installationcd lex_yacc
make # Builds the tuple_analyzer binary
make run # Runs the analyzer (or ./tuple_analyzer)Then type a tuple expression and press Enter:
(1, 2, 3)
Press Ctrl+D to send EOF and end input.
make clean # Removes all generated filesIf make is unavailable:
cd lex_yacc
bison -y -d tuple_parser.y # Generates y.tab.c and y.tab.h
flex tuple_lexer.l # Generates lex.yy.c
gcc -Wall -o tuple_analyzer y.tab.c lex.yy.c -ll # Compile
./tuple_analyzer # RunNote: If -ll gives a linker error, try -lfl instead.
If bison/flex not found in PATH, use full paths:
/opt/homebrew/Cellar/bison/3.8.2/bin/bison and /opt/homebrew/Cellar/flex/2.6.4_2/bin/flex
echo "(1, 2, 3)" | ./tuple_analyzer
echo "(1, (2, 3), 4)" | ./tuple_analyzer
echo "()" | ./tuple_analyzer
echo "(42,)" | ./tuple_analyzerInstall Python from python.org (check "Add Python to PATH" during installation).
cd python_parser
python tuple_parser.pyNote: On Windows, use
pythoninstead ofpython3.
Step 1: Download WinFlexBison
- Go to: https://github.com/lexxmark/winflexbison/releases
- Download the latest
win_flex_bison-X.X.X.zip - Extract the zip to a folder, e.g.,
C:\winflexbison - Add
C:\winflexbisonto your system PATH:- Search "Environment Variables" in Windows Settings
- Edit
Pathunder System Variables - Add
C:\winflexbison
Step 2: Install MinGW (GCC for Windows)
- Download MinGW-w64 from: https://www.mingw-w64.org/ (or install via MSYS2)
- Or use MSYS2 (easier):
- Download MSYS2 from: https://www.msys2.org/
- Open MSYS2 terminal and run:
pacman -S mingw-w64-x86_64-gcc
- Add the
binfolder (e.g.,C:\msys64\mingw64\bin) to your system PATH
Step 3: Build and Run
Open Command Prompt (cmd) and navigate to the lex_yacc folder:
cd lex_yacc
win_bison -y -d tuple_parser.y
win_flex tuple_lexer.l
gcc -Wall -o tuple_analyzer.exe y.tab.c lex.yy.c
tuple_analyzer.exeThen type a tuple expression and press Enter. Press Ctrl+Z then Enter to send EOF.
Step 4: Test with Piped Input
echo (1, 2, 3) | tuple_analyzer.exe
echo (1, (2, 3), 4) | tuple_analyzer.exe
echo () | tuple_analyzer.exe
echo (42,) | tuple_analyzer.exeIf you installed MSYS2, you can use it as a Linux-like terminal:
# Install required tools inside MSYS2
pacman -S bison flex gcc make
# Navigate to lex_yacc folder
cd /c/path/to/lex_yacc
# Build using the Makefile (edit Makefile to use plain 'bison' and 'flex')
bison -y -d tuple_parser.y
flex tuple_lexer.l
gcc -Wall -o tuple_analyzer.exe y.tab.c lex.yy.c
./tuple_analyzer.exe- Download GnuWin Flex: http://gnuwin32.sourceforge.net/packages/flex.htm
- Download GnuWin Bison: http://gnuwin32.sourceforge.net/packages/bison.htm
- Install both and add their
binfolders to PATH - Then run:
cd lex_yacc
bison -y -d tuple_parser.y
flex tuple_lexer.l
gcc -Wall -o tuple_analyzer.exe y.tab.c lex.yy.c
tuple_analyzer.exe| Problem | Solution |
|---|---|
'python' is not recognized |
Reinstall Python, check "Add to PATH" |
'gcc' is not recognized |
Install MinGW/MSYS2 and add to PATH |
'win_bison' is not recognized |
Add WinFlexBison folder to PATH |
-ll linker error |
Remove -ll flag or replace with -lfl |
unistd.h not found |
Add %option nounistd in the .l file |
Tip for Windows users: If you face issues with
-llor-lfl, try compiling without it:gcc -Wall -o tuple_analyzer.exe y.tab.c lex.yy.cIf you get an undefined reference to
yywrap, add%option noyywrapin the.lfile (already included).
| Step | macOS | Windows |
|---|---|---|
| Python parser | python3 tuple_parser.py |
python tuple_parser.py |
| Generate parser | make |
win_bison -y -d tuple_parser.y |
| Generate lexer | (included in make) | win_flex tuple_lexer.l |
| Compile | (included in make) | gcc -Wall -o tuple_analyzer.exe y.tab.c lex.yy.c |
| Run | ./tuple_analyzer |
tuple_analyzer.exe |
| EOF key | Ctrl+D |
Ctrl+Z + Enter |
| Input | Description |
|---|---|
() |
Empty tuple |
(1,) |
Single-element tuple |
(1, 2, 3) |
Multi-element tuple |
("hello", "world") |
String tuple |
(1, "two", x) |
Mixed types |
(1, (2, 3), 4) |
Nested tuple |
((1, 2), (3, 4)) |
Tuple of tuples |
| Input | Error |
|---|---|
(1, 2, 3 |
Missing closing ) |
1, 2, 3) |
Missing opening ( |
(,) |
Comma without element |
(, 1, 2) |
Leading comma |