cd lab alg output
cd lab alg output
ALGORITHM:
1. start the program
2. Open the file "input.txt" for reading.
3. If the file fails to open, display an error message and exit the program.
4. Read the file character by character until the end of file is reached.
5. If the character is an operator and not a keyword, print "id".
6. Check if the character is any of the operators. If so, print the corresponding operator token.
7. If the character is alphanumeric, store it in the buffer array.
8. If the character is a space or newline and the buffer is not empty, check if the buffer contains
a keyword. If so, print "kwd".
9. Close the file.
10. Return 0 to indicate successful execution of the program.
11. Stop the program
NOTE: The kwd() function is used to check if a given string (buffer) is a keyword. It compares the
string with an array of keywords and returns a flag indicating whether it is a keyword or not.
Algorithm:
4. In the main function, check if the program was executed with a command-line argument (argc
== 2). If true, open the file specified by argv[1] for reading using fopen and assign it to yyin
(input stream). If false, prompt the user to enter input and assign stdin (standard input) to
yyin.
6. Print the values of lc (line count), sc (space count), wc (word count), and cc (character count)
using printf statements.
7. Implement the yywrap() function, which is called when the end of the input is reached. In this
case, return 1 to signal the end of the analysis process.
8. Stop the program
VOWELS AND CONSONANTS
Algorithm:
1. Start the program
2. Initialize variables v and c to 0.
3. Define the lexer rules using the regular expressions and corresponding actions.
- For every vowel (lowercase or uppercase), increment v by 1.
- For every alphabet (lowercase or uppercase), increment c by 1.
- When a newline character is encountered, print the number of vowels and reset v to
0. If there are no vowels, nothing will be printed.
- When a newline character is encountered, print the number of consonants and reset
c to 0. If there are no consonants, nothing will be printed.
4. Define the main function.
- Prompt the user to enter a string of vowels and consonants.
- Call yylex to start processing the input.
- Return 0 to indicate successful execution.
5. Define the yywrap function.
- Return 1 to indicate the end of input.
6. Stop the program
NOTE: The algorithm essentially scans the input string character by character and counts the
number of vowels and consonants encountered. Once a newline character is encountered, it prints
the counts and resets the respective variables.
OUTPUT:
❯ ./a.out
Enter transitions
0e1
0e2
1e3
2e0
State q1 : {q1,q3}
State q2 : {q2,q0,q1,q3}
State q3 : {q3}
4444bhhu444ewh
444
OUTPUT:
NFA TO DFA
ALGORITHM
OUTPUT
a1a
a 2 ab
ab 1 a
ab 2 abc
abc 1 a
abc 2 abc
Final states:{abc,}
MINIMISATION OF DFA
The given code is a program that minimizes a DFA (Deterministic Finite Automaton) using the
minimization algorithm. It takes as input the DFA table, the number of DFA states, the number of
symbols, and the final states of the DFA.
The output of the program will be the minimized DFA table after the optimization process.
Note : that the code is incomplete and requires additional implementation details, such as the
functions for loading the DFA table and other utility functions. Without those missing parts, we
cannot determine the complete behavior and correctness of the code.
OUTPUT
Number of states:3
Number of symbols:2
Enter transitions
Transition of A with input 0: B
Transition of A with input 1: A
Transition of B with input 0: C
Transition of B with input 1: B
Transition of C with input 0: B
Transition of C with input 1: C
Final states withut any seperation in cap letters:B C
DFA: STATE TRANSITION TABLE
| 0 1
-----+------------
A | B A
B | C B
C | B C
Final states = B
After minimizationDFA: STATE TRANSITION TABLE
| 0 1
-----+------------
A | B A
B | A B
Final states = B
❯ ./a.out
No of prooductions : 3
Continue(0/1) : 1
Continue(0/1) : 0
Note: The 'ep' in the grammar rules represents an empty or epsilon production, meaning that
the given non-terminal can be skipped or replaced with an empty string.
OUTPUT
❯ gcc recdescent.c
❯ ./a.out
String accepted..!!!
❯ ./a.out
String rejected..!!!
GRAMMAR is
E->E+E
E->E*E
E->(E)
E->id