Skip to content

Darshan2139/CE365-MiniProject-Tuple-Parser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Compiler Construction Mini Project

Topic: Tuple Creation in Python


Created by    : 23CE051 - Darshan Kachhiya Contributed : 23CE050 - Samarth Kachhadiya  |  23CE059 - Aryan Kotak


Project Structure

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)

How to Run on macOS

Part 1: Python Parser

No dependencies needed — uses only Python standard library.

cd python_parser
python3 tuple_parser.py

This runs automated test cases and then enters interactive mode where you can type your own tuples.

Part 2: Lex & YACC Syntax Analyzer

Prerequisites

Install Homebrew bison and flex (macOS system versions may require full Xcode):

brew install bison flex

You also need gcc (comes with Xcode Command Line Tools):

xcode-select --install    # Install if not already present
gcc --version              # Verify installation
Build and Run
cd 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.

Clean Build Files
make clean      # Removes all generated files
Manual Build (without Make)

If 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               # Run

Note: 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

Test with Piped Input
echo "(1, 2, 3)" | ./tuple_analyzer
echo "(1, (2, 3), 4)" | ./tuple_analyzer
echo "()" | ./tuple_analyzer
echo "(42,)" | ./tuple_analyzer

How to Run on Windows

Part 1: Python Parser

Install Python from python.org (check "Add Python to PATH" during installation).

cd python_parser
python tuple_parser.py

Note: On Windows, use python instead of python3.

Part 2: Lex & YACC Syntax Analyzer (using WinFlexBison)

Method 1: Using WinFlexBison (Recommended)

Step 1: Download WinFlexBison

  1. Go to: https://github.com/lexxmark/winflexbison/releases
  2. Download the latest win_flex_bison-X.X.X.zip
  3. Extract the zip to a folder, e.g., C:\winflexbison
  4. Add C:\winflexbison to your system PATH:
    • Search "Environment Variables" in Windows Settings
    • Edit Path under System Variables
    • Add C:\winflexbison

Step 2: Install MinGW (GCC for Windows)

  1. Download MinGW-w64 from: https://www.mingw-w64.org/ (or install via MSYS2)
  2. Or use MSYS2 (easier):
  3. Add the bin folder (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.exe

Then 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.exe
Method 2: Using MSYS2 Terminal (Linux-like Environment)

If 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
Method 3: Using Flex for Windows (Standalone)
  1. Download GnuWin Flex: http://gnuwin32.sourceforge.net/packages/flex.htm
  2. Download GnuWin Bison: http://gnuwin32.sourceforge.net/packages/bison.htm
  3. Install both and add their bin folders to PATH
  4. 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
Windows Troubleshooting
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 -ll or -lfl, try compiling without it:

gcc -Wall -o tuple_analyzer.exe y.tab.c lex.yy.c

If you get an undefined reference to yywrap, add %option noyywrap in the .l file (already included).


Quick Summary

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

Examples

Valid Tuples

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

Invalid Tuples

Input Error
(1, 2, 3 Missing closing )
1, 2, 3) Missing opening (
(,) Comma without element
(, 1, 2) Leading comma

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors