This document explains how to compile and run the following programs:
- Lexical Analyzer in C
- Lexical Analyzer using Lex Tool
- YACC Specifications for Syntactic Categories
- Find ε-Closure of all states in an NFA
- Convert NFA with ε-transition to NFA without ε-transition
- Convert NFA to DFA
- Minimize a DFA
- GCC compiler installed
- Basic knowledge of terminal commands
gcc lexical_analyzer.c -o lexical_analyzer
./lexical_analyzer- Enter a C program as input.
- Press
Ctrl+D(Linux/Mac) orCtrl+Z(Windows) to end input.
int main() {
int a = 10;
float b = 5.5;
if (a > b) {
printf("Hello World\n");
}
return 0;
}int is a keyword
main is an identifier
( is a special symbol
) is a special symbol
{ is a special symbol
int is a keyword
a is an identifier
10 is a number
; is a special symbol
float is a keyword
b is an identifier
5.5 is a number
; is a special symbol
if is a keyword
( is a special symbol
a is an identifier
> is a special symbol
b is an identifier
) is a special symbol
{ is a special symbol
printf is an identifier
( is a special symbol
"Hello World\n" is a string
) is a special symbol
; is a special symbol
} is a special symbol
return is a keyword
0 is a number
; is a special symbol
} is a special symbol
Total number of lines: 9
%{
#include <stdio.h>
%}
DIGIT [0-9]+
ID [a-zA-Z_][a-zA-Z0-9_]*
WHITESPACE [ \t\n]+
%%
{DIGIT} { printf("%s is a number\n", yytext); }
{ID} { printf("%s is an identifier\n", yytext); }
[+\-*/=;] { printf("%s is an operator or special symbol\n", yytext); }
{WHITESPACE} { /* Ignore whitespace */ }
. { printf("%s is an unknown character\n", yytext); }
%%
int yywrap() {
return 1;
}
int main() {
yylex();
return 0;
}lex lexical_analyzer.l
gcc lex.yy.c -o lexical_analyzer -ll
./lexical_analyzeryacc -d arith.y
lex arith.l
gcc y.tab.c lex.yy.c -o arith -ll
./arith- Recognizing valid arithmetic expressions (
arith.y,arith.l) - Recognizing valid variable names (
var.y,var.l) - Calculator implementation using Lex and YACC (
calc.y,calc.l) - Abstract syntax tree generation (
ast.y,ast.l)
gcc epsilon_closure.c -o epsilon_closure
./epsilon_closure11
8
0 1
1 2
1 3
6 1
0 7
4 6
5 6
6 7
ε-Closure of states:
ε-Closure(q0): { q0 q1 q2 q3 q7 }
ε-Closure(q1): { q1 q2 q3 }
ε-Closure(q2): { q2 }
ε-Closure(q3): { q3 }
ε-Closure(q4): { q1 q2 q3 q4 q6 q7 }
ε-Closure(q5): { q1 q2 q3 q5 q6 q7 }
ε-Closure(q6): { q1 q2 q3 q6 q7 }
ε-Closure(q7): { q7 }
ε-Closure(q8): { q8 }
ε-Closure(q9): { q9 }
ε-Closure(q10): { q10 }
gcc nfa_epsilon_to_nfa.c -o nfa_epsilon_to_nfa
./nfa_epsilon_to_nfaNFA after removing ε-transitions:
δ(q0, a) -> { q1 }
δ(q0, b) -> { q2 }
δ(q1, a) -> { }
δ(q1, b) -> { q2 }
gcc nfa_to_dfa.c -o nfa_to_dfa
./nfa_to_dfaDFA Transition Table:
δ(q0, a) -> q1
δ(q0, b) -> q2
δ(q1, a) -> {}
δ(q1, b) -> q2
gcc minimize_dfa.c -o minimize_dfa
./minimize_dfaMinimized DFA Table:
q0 -> { q1, q2 }
q1 -> { q2, q3 }
This guide provides the steps to run each automata conversion program. Modify the input values as needed to test different cases!
