🚀 A PCS-601 compiler design lab implementing 20 Lex/Flex & Yacc programs — covering lexical analysis, DFA simulation, tokenization, expression parsing, infix-to-postfix conversion, and arithmetic evaluation built in C.
📚 20 Programs | ⚙️ DFA Simulation | 🔍 Lexical Analysis | 🌳 Yacc Parsing | 🧮 Expression Evaluation | 📄 C Implementation
Compiler-Construction/
│
├── 📂 PCS-601/ # Lab practice programs
│ │
│ ├── ── Theory ──
│ ├── 📄 P1.txt # Study of Lex & Yacc — tools, structure, workflow
│ │
│ ├── ── Lex Programs ──
│ ├── 📄 P2.l # Count lines, spaces, tabs & other characters
│ ├── 📄 P3.l # Valid C identifier checker (numbered output)
│ ├── 📄 P4.l # Integer & floating point number classifier
│ ├── 📄 P5.l # C tokenizer — keywords, operators, separators, constants, identifiers
│ ├── 📄 P6.l # Reads Input.txt — counts characters, words & whitespaces
│ ├── 📄 P7.l # Reads Input.txt — compresses multiple spaces, writes Output.txt
│ ├── 📄 P8.l # Strips // and /* */ comments from .c file, writes out.c
│ ├── 📄 P9.l # Extracts all HTML <tags> from file, writes to output file
│ ├── 📄 P10.l # Advanced C tokenizer — KEYWORD, STRING, LITERAL, CONSTANT, IDENTIFIER
│ ├── 📄 P11.l # Even / odd number checker with invalid input detection
│ ├── 📄 P12.l # Counts vowels and consonants in input string
│ ├── 📄 P13.l # Email address validator using regex
│ ├── 📄 P14.l # DFA — accepts strings with even number of a's and even b's
│ ├── 📄 P15.l # DFA-based classifier — integers, floats & identifiers
│ ├── 📄 P16.l # DFA — accepts strings with even number of a's (ab* pattern)
│ │
│ ├── ── Lex + Yacc Programs ──
│ ├── 📄 P17.l # Lexer for standalone arithmetic expression validator
│ ├── 📄 P17.y # Yacc — standalone arithmetic expression validator with precedence
│ ├── 📄 P18.l # Lexer for arithmetic expression validator (with operation labels)
│ ├── 📄 P18.y # Yacc — validates expression & prints operation names
│ ├── 📄 P19.l # Lexer for infix to postfix converter
│ ├── 📄 P19.y # Yacc — converts infix expression to postfix using %union
│ ├── 📄 P20.l # Lexer for arithmetic calculator
│ ├── 📄 P20.y # Yacc — evaluates arithmetic expressions, handles division by zero
│ │
│ ├── ── Input / Output Files ──
│ ├── 📄 Input.txt # Sample text input (used by P6, P7)
│ ├── 📄 Output.txt # Compressed output generated by P7
│ ├── 📄 input.c # Sample C source file with comments (used by P8)
│ ├── 📄 out.c # Comment-stripped C output generated by P8
│ ├── 📄 sample.html # Sample HTML file (used by P9)
│ ├── 📄 tags.txt # HTML tags output generated by P9
│ ├── 📄 lex.yy.c # Auto-generated Lex C output (do not edit)
│ │
│ └── 📄 CompilerDesignLabManual.pdf # Official lab manual
│
├── 📂 assets/
│ └── 🖼️ CompilerDesign.png # Project banner image
│
├── 📄 .gitattributes # Git language detection config
├── 📄 .gitignore # Ignores compiled binaries, tab files, .DS_Store, etc.
├── 📄 LICENSE # MIT License
└── 📄 README.md # Project documentation
| # | 📄 File | 📚 Topic |
|---|---|---|
| 1 | P1.txt | Study of Lex & Yacc — tools, structure, workflow, regex, auxiliary functions |
| # | 📄 File | 📚 Concept | 💡 Key Detail |
|---|---|---|---|
| 2 | P2.l | Count lines, spaces, tabs & other characters | Uses \n, " ", \t, . patterns with 4 counters |
| 3 | P3.l | Valid C identifier checker | Numbered output, accepts [a-zA-Z_][a-zA-Z0-9_]* |
| 4 | P4.l | Integer & floating point number classifier | Uses ^ anchors, detects invalid numbers too |
| 11 | P11.l | Even / odd number checker | Detects invalid alphanumeric input, uses atoi() |
| 12 | P12.l | Vowel & consonant counter | Separate regex for vowels and consonants |
| # | 📄 File | 📚 Concept | 💡 Key Detail |
|---|---|---|---|
| 5 | P5.l | C tokenizer — keywords, operators, separators, constants, identifiers | Covers int, float, double, if, else, for, while |
| 10 | P10.l | Advanced C tokenizer | Classifies KEYWORD, STRING, LITERAL, CONSTANT, IDENTIFIER with named definitions |
| 13 | P13.l | Email address validator | Regex: [a-zA-Z0-9]+(\....)* @domain.tld |
| # | 📄 File | 📚 Concept | 💡 Key Detail |
|---|---|---|---|
| 6 | P6.l | Count characters, words & whitespaces from Input.txt |
Uses yyin = fopen(...), yyleng for char count |
| 7 | P7.l | Compress multiple spaces into one, write to Output.txt |
Uses space_flag to avoid double spaces |
| 8 | P8.l | Strip // and /* */ comments from .c file |
Uses %x COMMENT exclusive start condition |
| 9 | P9.l | Extract all HTML <tags> from file |
Prompts for input/output filenames, uses fprintf(out, ...) |
| # | 📄 File | 📚 Concept | 💡 Key Detail |
|---|---|---|---|
| 14 | P14.l | DFA — accepts strings with even a's and even b's |
4 states: INITIAL, A, B, C + DEAD state |
| 15 | P15.l | DFA-based token classifier | Identifies integers ([0-9]+), floats ([0-9]+.[0-9]+), identifiers |
| 16 | P16.l | DFA — accepts strings with even a's |
States: INITIAL, S0, S1, DEAD with state variable |
| # | 📄 Files | 📚 Concept | 💡 Key Detail |
|---|---|---|---|
| 17 | P17.l + P17.y | Standalone arithmetic expression validator | Yacc with %left precedence, prints Valid / Invalid Expression |
| 18 | P18.l + P18.y | Arithmetic expression validator with operation labels | Prints Addition, Subtraction, Multiplication, Division per operation |
| 19 | P19.l + P19.y | Infix to postfix converter | Uses %union { int num; }, prints postfix tokens as they reduce |
| 20 | P20.l + P20.y | Arithmetic calculator with result | Evaluates full expressions, prints Result = N, handles division by zero |
- Flex — Lexical analyzer generator
- Bison / Yacc — Parser generator
- GCC — GNU C Compiler
- Unix / Linux / macOS terminal
# macOS
brew install flex bison
# Ubuntu / Debian
sudo apt-get install flex bison
# Verify
flex --version && bison --versioncd PCS-601
# Step 1 — Generate C code from Lex file
flex P2.l
# Step 2 — Compile
gcc lex.yy.c -o P2 -ll
# Step 3 — Run
./P2# P6 — count stats from Input.txt
flex P6.l && gcc lex.yy.c -o P6 -ll && ./P6
# P7 — compress spaces, writes Output.txt
flex P7.l && gcc lex.yy.c -o P7 -ll && ./P7
# P8 — strip comments from input.c, writes out.c
flex P8.l && gcc lex.yy.c -o P8 -ll && ./P8 input.c
# P9 — extract HTML tags (prompts for filenames)
flex P9.l && gcc lex.yy.c -o P9 -ll && ./P9
# Enter: sample.html → then output filename e.g. tags.txt
# P17 — Lex + Yacc standalone expression validator
bison -d P17.y && flex P17.l && gcc P17.tab.c lex.yy.c -o P17 -ll && ./P17
# Enter: 3+5*2 → Valid Expression
# P18 — Lex + Yacc expression validator with operation labels
bison -d P18.y && flex P18.l && gcc P18.tab.c lex.yy.c -o P18 -ll && ./P18
# Enter: 3+5 → Addition \n Valid Expression
# P19 — Lex + Yacc infix to postfix converter
bison -d P19.y && flex P19.l && gcc P19.tab.c lex.yy.c -o P19 -ll && ./P19
# Enter: 3+5*2 → 3 5 2 * +
# P20 — Lex + Yacc arithmetic calculator
bison -d P20.y && flex P20.l && gcc P20.tab.c lex.yy.c -o P20 -ll && ./P20
# Enter: 3+5*(2-1) → Result = 8
# Enter: 10/0 → Error: Division by zeroNo external dependencies — just Flex, Bison, and GCC.
After exploring this project, you will understand:
✅ Lex File Structure — Definitions (%{ %}), rules (%%), and user code sections
✅ Regex Patterns — Writing patterns for token recognition using Lex syntax
✅ Lexical Analysis — Tokenizing C source code into keywords, operators, identifiers
✅ DFA Simulation — Implementing finite automata states using %s and BEGIN
✅ Start Conditions — Using %x exclusive states for comment stripping
✅ Comment Stripping — Removing // and /* */ from C programs
✅ File I/O in Lex — Using yyin, yyout, fopen, fprintf, yyleng
✅ Regex Validation — Email addresses, C identifiers, integers, floats
✅ HTML Processing — Extracting tags from real HTML files
✅ Yacc / Bison — Writing grammars, %token, %left precedence, semantic actions
✅ Infix to Postfix — Converting expressions using %union and $$, $1, $3
✅ Expression Evaluation — Computing arithmetic results with full operator precedence
✅ Error Handling — yyerror(), division-by-zero detection, invalid token reporting
✅ Lex + Yacc Integration — Connecting lexer and parser end-to-end via shared header
- 📄 20 Programs — 16 standalone Lex + 4 Lex/Yacc pairs covering the full compiler design curriculum
- 📚 Progressive Difficulty — From basic character counting to full arithmetic expression evaluation
- 🧠 DFA Simulation — Real finite automata implemented using Lex start conditions and state variables
- 🌳 Yacc Parsing — Four Yacc programs covering validation, operation labeling, postfix conversion, and evaluation
- 🧮 Expression Evaluation — Full arithmetic calculator with operator precedence and division-by-zero handling
- 📂 File I/O — Programs that read/write real files using
yyin,yyout,fopen,fprintf - 🔧 Modular Design — Each concept in its own
.l/.yfile, easy to study independently - ⚙️ Minimal Setup — Just Flex, Bison/Yacc, and GCC — no extra dependencies
💬 Questions about this project?
Feel free to reach out for help or collaboration!
👤 Abhishek Giri — Creator & Maintainer
This project is open source and available under the MIT License — see the LICENSE file for details.
🔧 Built with ❤️ for Learning Compiler Design
Mastering Lex, Flex, Yacc & C from the ground up
Compiler Construction — Lexical Analysis & Parsing Fundamentals
Building strong foundations in compiler design
© 2026 Abhishek Giri | Compiler Construction
Empowering developers with practical compiler design skills