Pathak 13 Feb Download
Pathak 13 Feb Download
Source Code:
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int main(){
string s;
char ac ve = 'a';
cout << "ini al state is q0\n final state is q3\n";
cout << "\t0\t1\n";
cout << "q0\tq1\td\n";
cout << "q1\td\tq2\n";
cout << "q2\tq3\td\n";
cout << "q3\tq3\tq3\n";
int n;
cout << "enter the string" << endl;
cin >> s;
n = s.length();
for (int i = 0; i < n; i++)
{
if ((s[i] != '0') && (s[i] != '1')) {
ac ve == 'a';
break;
}
if (s[i] == '1' && ac ve == 'a') {
ac ve = 'e';
cout << "symbol passed is 1 and next state is dead state (d)" << endl;
break;
}
else if (s[i] == '0' && ac ve == 'a')
{
ac ve = 'b';
cout << "symbol passed is 0 and next state is q1" << endl;
}
else if (s[i] == '0' && ac ve == 'b')
{
ac ve = 'e';
cout << "symbol passed is 0 and next state is dead state(d)" << endl;
break;
}
else if (s[i] == '1' && ac ve == 'b')
{
ac ve = 'c';
cout << "symbol passed is 1 and next state is q2" << endl;
}
else if (s[i] == '1' && ac ve == 'c')
{
ac ve = 'e';
cout << "symbol passed is 1 and final state is dead state (d)" << endl;
break;
}
else if (s[i] == '0' && ac ve == 'c')
{
ac ve = 'd';
cout << "symbol passed is 1 and final state is q3" << endl;
}
else if (((s[i] == '1') || (s[i] == '0')) && ac ve == 'd') {
ac ve = 'd';
cout << "symbol passed is" << s[i] << "and final state is q3" << endl;
}
}
if (ac ve == 'd') {
cout << "string of 0 and 1 contains 010 as star ng substring" << endl;
}
else{
cout << "string of 0 or 1 not contains 010 as star ng substring" << endl;
}
return 0;}
Output of Experiment 2(b):
Experiment No: 2(c)
Source Code:
#include <iostream>
using namespace std;
int main()
{
string st;
cout << "enter the string" << endl;
cin >> st;
char state = 'A';
int n = st.size();
if (n < 3)
{
cout << "string is less than 3 characters " << endl;
return 0;
}
string res = st.substr(n - 3, n);
cout << " in al state is q0 and final state is q3 \n";
cout << "\t 0 \t1 \n";
cout << "q0\t q4\t q1\n";
cout << "q1\t q4\t q2\n";
cout << "q2\t q3\t q4\n";
cout << "q3\t q4\t q4\n";
cout << "q4\t q4\t q4\n";
for (int i = 0; i < 3; i++)
{
if (res[i] != '0' && res[i] != '1')
{
state = 'A';
cout << "unrecognized symbol scanned\n";
break;
}
if (res[i] == '1' && state == 'A')
{
state = 'B';
cout << "symbol passed is 1 and next state is B" << endl;
}
else if (res[i] == '0' && state == 'A')
{
state = 'D';
cout << "symbol passed is 0 and next is state D which is dead string is
not accepted " << endl;
return 0;
}
else if (res[i] == '1' && state == 'B')
{
state = 'C';
cout << "symbol passed is 1 and next state is C" << endl;
}
else if (res[i] == '0' && state == 'B')
{
state = 'D';
cout << "symbol passed is 0 and next is state D which is dead string is
not accepted " << endl;
break;
}
else if (res[i] == '0' && state == 'C')
{
state = 'E';
cout << "symbol passed is 0 ans next state is E which is final state " <<
endl;
}
else if (res[i] == '1' && state == 'C')
{
state = 'D';
cout << "symbol passed is 1 and next state is D which is dead" << endl;
break;
}
else if (res[i] == '1' && state == 'E')
{
state = 'D';
cout << "symbol passed is 1 and next state is D which is dead" << endl;
break;
}
else if (res[i] == '0' && state == 'E')
{
state = 'D';
cout << "symbol passed is 0 and next state is D which is dead" << endl;
break;
}
}
if (state == 'E')
{
cout << "string ends with 110" << endl;
}
else
{
cout << "string does not end with 110" << endl;
}
return 0;
}
Output of Experiment 2(c):
Experiment No: 3
What is LEX?
Lex is a tool or a computer program that generates Lexical Analyzers (converts the stream
of characters into tokens). The Lex tool itself is a compiler. The Lex compiler takes the
input and transforms that input into input patterns. It is commonly used with YACC (Yet
Another Compiler Compiler). It was written by Mike Lesk and Eric Schmidt.
Function of Lex
1. In the irst step the source code which is in the Lex language having the ile name
‘File.l’ gives as input to the Lex Compiler commonly known as Lex to get the output as
lex.yy.c.
2. After that, the output lex.yy.c will be used as input to the C compiler which gives the
output in the form of an ‘a.out’ ile, and inally, the output ile a.out will take the stream
of character and generates tokens as output.
lex.yy.c: It is a C program.
File.l: It is a Lex source program
a.out: It is a Lexical analyzer
Lex File Format
A Lex program consists of three parts and is separated by %% delimiters:-
Declarations
%%
Translation rules
%%
Auxiliary procedures
Auxiliary procedures: The Auxilary section holds auxiliary functions used in the actions.
What is YACC?
Yacc (Yet Another Compiler Compiler) is a tool used to create a parser. It parses the stream
of tokens from the Lex ile and performs the semantic analysis. Yacc translates a given
Context-Free Grammar (CFG) speci ications into a C implementation y.tab.c. This C
program when compiled, yields an executable parser. A Yacc ile is in many ways, similar
to the Lex ile.
/* de initions */
....
%%
/* rules */
....
%%
/* auxiliary routines */
....
The declarations and subroutines are the same as those in Lex, but the rules are slightly
different. Here, the rules are not regular expressions, rather they are grammar de initions
in CFG. These rules, like in Lex, have two parts — productions and actions.
Input File: De inition Part:
The de inition part includes information about the tokens used in the syntax
de inition:
%token NUMBER
%token ID
Yacc automatically assigns numbers for tokens, but it can be overridden by
%token NUMBER 621
Yacc also recognizes single characters as tokens. Therefore, assigned token numbers
should no overlap ASCII codes.
The de inition part can include C code external to the de inition of the parser and
variable declarations, within %{ and %} in the irst column.
It can also include the speci ication of the starting symbol in the grammar:
%start nonterminal
Input File:
If yylex() is not de ined in the auxiliary routines sections, then it should be included:
#include "lex.yy.c"
YACC input ile generally inishes with:
.y
Output Files:
The output of YACC is a ile named y.tab.c
If it contains the main() de inition, it must be compiled to be executable.
Otherwise, the code can be an external function de inition for the function int
yyparse()
If called with the –d option in the command line, Yacc produces as output a header ile
y.tab.h with all its speci ic de inition (particularly important are token de initions to
be included, for example, in a Lex input ile).
If called with the –v option, Yacc produces as output a ile y.output containing a textual
description of the LALR(1) parsing table used by the parser. This is useful for tracking
down how the parser solves con licts.
Example: Yacc File (.y)