group1_hw1

advertisement
CS421 - Yoshii - HW1 for Weeks 1-2
Spring 06
Warm Up Topics
=================================================================
DUE:
Week 4 -- the 1st lecture at the beginning of lecture
TOTAL: 51 pts
** Group #1:
** Clark Scheff:
** Came to the meeting(s) to assemble the work together?
** Attached his/her draft?
** Jaime Soler:
** Came to the meeting(s) to assemble the work together?
** Attached his/her draft?
** Hamid Badiozamani:
** Came to the meeting(s) to assemble the work together?
** Attached his/her draft?
====================================================================
Review Questions [12pts]
-----------------------1) ##Inter1*
Give one sentence which is syntactically incorrect.
Give another sentence which is syntactically correct
but is semantically incorrect.
Syntactically incorrect: I doing am homework the.
Semantically incorrect: The bone ate the dog.
2) ##Inter2*
Write all the prefixes of abc
Write all the suffixes of abc
Prefixes = {/\,a,ab,abc}
Suffixes = {/\,c,bc,abc}
4) ##Inter4* Give an example of a palindrome using the alphabet {a,b,c}
An example of a palindrome using alphabet {a,b,c} is abbacabba.
6) ##Inter6* E = {a, b}.
E^2 = {aa,ab,ba,bb}
What is E^2 ? Give the set.
8) ##Inter8* Is English a finite set? Explain why or why not.
English is an infinite set. This is because there are strings on english
which
can be connected to form other strings. This process can be repeated an infinite number
of times.
10) ##Inter10* Complete {x | x is in A ?? } for A ^ B
{x | x is in A and x is in B}
Problem 1 [4pts]
---------------1) What are tokens of a programming language?
Tokens are the basic lexical building blocks of source code.
Characters on a programming language are combined into tokens according to
the rules of the language. A token is a unit of meaningful information
presented as a string in the language.
List 3 types and give examples from a C++ code.
#
Type
Example
1
integer
5
2
reserved word
Class
3
operator
+
2) What are the two parts of a compiler that
we will focus in this class?
The two parts of a compiler that we will focus on in this class are the scanner and
the parser.
Scanner – The scanner performs lexical analysis by reading strings character by
character. It groups the characters into tokens of the language. The scanner will
also eliminate unneeded information and it also processes compiler control directives.
The scanner will generate lexical errors if they are found.
Parser – The parser performs syntactic analysis. It groups the tokens from the
scanner into higher level units like expressions, statements, etc. The parser
verifies correct syntax and recovers from or repairs syntax errors. The scanner
builds a syntax tree or calls semantic routines directly during parsing.
Problem 2 [5pts]
---------------n
Prove by induction on n
k
n
that ∑ 2 = 2
1
−1
k =0
0
P 0 = ∑ 2 k = 20= 1
Basis P(0):
and 2
k= 0
n 1
− 1= 2 0
1
− 1= 21 − 1= 2− 1= 1 thus the formula holds for P(0).
i
Assume P(i) is true: Assume
∑ 2 k= 2 i
1
−1
k =0
Inductive Step P(i) to P(i+1): We are adding 2
i 1
to both sides of the above equation.
i
So,
∑ 2k
2i
1
= 2i
1
−1 2i
1
concentrating on the right side of the equation we
k =0
i 1
get
2 i 1−1 2i
1
=2i
1
2 i 1 − 1= 2 2 i
1
− 1= 2 i
1 1
− 1= 2
i 1
1
− 1 Therefore
∑ 2 k= 2
i 1
1
−1
k =0
Conclusion:
for any n.
Since this inductive step works for any i≥ 0, and P(0) is true, P(n)
is true
Programming Problem (Group Work) [30pts]
---------------------------------------Alphabet E is {0,1,2}
a) Write a recognizer in C++ for L = {x | x is a binary number}.
main: Given a string (from E) cined by the user, pass it to
the recognizer function.
Cout YES IN L or NO NOT IN L depending on what was returned.
The recognizer function should return TRUE or FALSE checking each character
to make sure it is 0 or 1.
Submit both the commented program and the output. [10pts]
b) Write a generator in C++ for L = {x | x is a binary number}.
main: It should create each string over E = {0,1,2}
and pass each string to the recognizer function
created in Part a above.
Only those strings for which the recognizer returned TRUE
should be displayed.
i.e.
create 0 --> returns TRUE ---> display
create 1 --> returns TRUE ---> display
create 2 --> returns FALSE
create 00 --> returns TRUE ---> display
create 01 --> returns TRUE ---> display
create 02 --> returns FALSE
Interactively terminate
the execution of the program after at least 20 binary strings
have been displayed although your program should be able to
keep on going infinitely.
Submit both the commented program and the output. [20pts]
End.
// Header file for recognizer function
//
// Group #1:
Hamid Badiozamani
//
Clark Scheff
//
Jaime Soler
#ifndef RECOGNIZER_HEADER
#define RECOGNIZER_HEADER
bool Recognizer(std::string strInput, std::string strL);
#endif
// Recognizer function for determining if an input string contains
// only characters in L, if not the function returns false.
//
// Group #1:
Hamid Badiozamani
//
Clark Scheff
//
Jaime Soler
#include <string>
#include "recognize.hpp"
// Inputs are strInput and stsrL.
// strInput is the string we are testing and strL is the string
// containing all the valid characters in the language.
bool Recognizer(std::string strInput, std::string strL)
{
bool bCharInL;
int j;
// this is simple, if a character is encounterd that is not in L
// then we return false.
for(unsigned int i=0; i<strInput.length(); i++)
{
bCharInL = false;
for(j = 0; j < strL.length() && !bCharInL; j++)
{
if(strInput[i] == strL[j])
bCharInL = true;
}
if(!bCharInL )
{
return false;
}
}
// we made it this far so this string is in L
return true;
}
// Recognizer in C++ for L = {x | x is a binary number}
// Strings are retrieved as a parameter from the command line.
//
// Group #1:
Hamid Badiozamani
//
Clark Scheff
//
Jaime Soler
#include <string>
#include <iostream>
#include "recognize.hpp"
using namespace std;
int main(int argv, char* argc[])
{
string L="01";
if(argv != 2)
{
cout << "usage: " << argc[0] << " string" << endl;
return 0;
}
string strInput(argc[1]);
if( Recognizer(strInput, L) )
{
cout << "string is in L" << endl;
}
else
{
cout << "string is not in L" << endl;
}
return EXIT_SUCCESS;
}
OUTPUT:
[schef001@empress cs421_hw1]$
string is in L
[schef001@empress cs421_hw1]$
string is not in L
[schef001@empress cs421_hw1]$
string is not in L
[schef001@empress cs421_hw1]$
string is in L
./recognizer 110100010101110
./recognizer 11110000000101010101011111120001
./recognizer 1101020010101110
./recognizer 11111010100001011110000110101010101
// Program for generating strings over E={0,1,2}
//
// Group #1:
Hamid Badiozamani
//
Clark Scheff
//
Jaime Soler
#include
#include
#include
#include
#include
#include
<iostream>
<iomanip>
<fstream>
<string>
<queue>
"recognize.hpp"
using namespace std;
const int iSizeOfE = 3;
//size of our alphabet
const unsigned int iMaxNumStrings = 20; //max number of strings to generate
int main(int argc, char *argv[])
{
queue<string> que;
char E[]=
{'0','1','2'
};
string L="01";
string strTemp,strBuf;
ofstream ofsOutput("output.txt");
int iQueSize = 0;
unsigned int iNumStringsCreated = 0;
// this code will loop until 20 unique strings have been generated
while( iNumStringsCreated < iMaxNumStrings )
{
// this is important. the size of the queue must be known prior
// to moving on. This is because we will be pushing newly
// created strings into the queue, but we do not want to work
// on these new strings until the next iteration.
iQueSize = que.size();
for(int j=0; j<iQueSize || j==0; j++)
{
if(iQueSize == 0)
{
strTemp = "";
}
else
{
strTemp = que.front();
que.pop();
}
for(int k=0; k<iSizeOfE && iNumStringsCreated < iMaxNumStrings; k++)
{
strBuf = strTemp + E[k];
que.push(strBuf);
if(Recognizer(strBuf,L) )
{
ofsOutput << setw(3) << strBuf << " --> ";
ofsOutput << "String is in L." << endl;
}
iNumStringsCreated++;
}
}
}
ofsOutput << "A total of " << iNumStringsCreated <<
" strings were generated." << endl;
ofsOutput.close();
return EXIT_SUCCESS;
}
OUTPUT:
0 -->
1 -->
00 -->
01 -->
10 -->
11 -->
000 -->
001 -->
010 -->
011 -->
A total
String is in L.
String is in L.
String is in L.
String is in L.
String is in L.
String is in L.
String is in L.
String is in L.
String is in L.
String is in L.
of 20 strings were generated.
Download