SlideShare a Scribd company logo
1 of 6
Download to read offline
It’s sometimes useful to make a little language for a simple problem. We are making a language
to let us play with strings and numbers, and for this assignment we are building a lexical analyzer
for this simple language. Here are the lexical rules for the language:
1. The language has identifiers. An identifier starts with a letter and is followed by zero or more
letters.
2. The language has string constants. A string constant is a sequence of characters, all on one
line, enclosed in double quotes. You do not have to support special handling for escape
characters between the quotes.
3. The language has integer constants, defined as a sequence of digits.
4. The language has 3 operators: +, * and [ ]. This requires 4 tokens.
5. The language has 2 keywords: “print” and “set”.
6. Statements in the language end in a semicolon.
7. The language supports parentheses.
8. White space is used to separate tokens and lines for readability.
9. A comment begins with two slashes (//) and ends at a newline.
The lexical analyzer is to be implemented in a C++ function. The function will be passed a
pointer to an input stream to read from. It will return a Token representing the token and lexeme
that has been recognized. The definitions for the Token class and unique values for each of the
tokens (TokenType) that you must recognize is provided in the header file p2lex.h, which is on
the course website. There is also a testmain.cpp program that shows how to include p2lex.h and
use tokens.
The lexical analyzer must ignore white space and comments. Whitespace and comments serve
only as separation between tokens. The program should maintain an external integer named
“linenum”, which should be initialized to 0 and incremented whenever a newline is seen by
getToken(). A lexical error should cause the token ERR to be returned. An end of file should
cause DONE to be returned.
You MUST use the p2lex.h header file for your assignment. You may not change it. You do not
need to hand in p2lex.h, but it doesn’t matter if you do. I will compile and test your program
against the distributed p2lex.h header file
You must produce and submit three files:
1. p2lex.cpp This file should #include p2lex.h and must provide:
-> an implementation for << operator to print a Token
o The printed version of the token must be a string representation of the TokenType. We define
the string representation of a TokenType to be the symbol for the TokenType in lowercase
letters. For example, the string representation of the TokenType value INT should be “int”.
o For the ID, STR, INT and ERR tokens, the string representation must be followed by the
lexeme for the token. The lexeme should be printed in parentheses.
-> an implementation for the getToken function
o The getToken() function should read characters from the stream pointed to by the first
argument and return the token that it recognizes.
2. project2.cpp
This file should #include p2lex.h and provide a main program to test the lexical analyzer
The specifications for the main program are as follows:
1. The program should accept at most one command line argument. If present, this one command
line argument is the name of a file to open and read for input. If not present, the program should
read from the standard input.
2. There may also, optionally, be an initial command line argument equal to the string “-v”. If it
is, then your program should be in “verbose mode” and should print each token that it recognizes
in addition to the remaining items.
3. The program should loop repeatedly, calling getToken(), until getToken returns an ERR or
DONE token
4. If the loop stopped because of an ERR token, it should print out the token
5. The program should print out a count of the number of times each type of token was seen.
6. The program should print out the number of unique lexemes for the ID, STR and INT that
appear in the input
--------------------------------------------------------------------------------------------------------------------
------------------
The source code for the lexer header file can be found below!!!!!!!!!!!!!!
Solution
# include
# include
# include
# include
# include
# include
# include
constint iRows = 34;
constint iCols = 15;
int iTT[iRows][iCols] = {0};
{
fstream File("tt.txt", ios::in|ios::nocreate);
if (!File)
{
cout << " Unable to open the input file." << endl;
cout << " Press any key to exit.";
getch( );
exit(0);
}
char sInput[100]={NULL};
for (int i = 0; i < iRows; i ++)
{
strset(sInput, NULL);
File.getline(sInput, 80);
char *sPtr=NULL;
sPtr = strtok(sInput, " ");
iTT[i][0] = atoi(sPtr);
for(int j = 1; j < iCols; j ++)
{
sPtr=strtok(NULL, " ");
iTT[i][j] = atoi(sPtr);
}
}
File.close( );
}
{
if (isalpha(cChar))
return iTT[iState][1];
elseif (isdigit(cChar))
return iTT[iState][2];
elseif (cChar == '.')
return iTT[iState][3];
elseif (cChar == '"')
return iTT[iState][4];
elseif (cChar == '_')
return iTT[iState][6];
elseif (cChar == '+')
return iTT[iState][7];
elseif (cChar == '=')
return iTT[iState][8];
elseif (cChar == '-')
return iTT[iState][9];
elseif (cChar == '%')
return iTT[iState][10];
elseif (cChar == '!')
return iTT[iState][11];
elseif (cChar == '>')
return iTT[iState][12];
elseif (cChar == '<')
return iTT[iState][13];
elseif (cChar == '/')
return iTT[iState][14];
return iTT[iState][0];
}
{
if (strlen(sToken) > 16 || strlen(sToken) == 0)
return 0;
char sKeywords[64][20] = {
"asm","auto","bool","break","case","catch",
"char","class","const","const_cast",
"continue","default","delete","do","double",
"dynamic_cast","else","enum","explicit",
"export","extern","false","float","for",
"friend","goto","if","inline","int","long",
"main","mutable","namespace","new",
"operator","private","protected","public",
"register","reinterpret_cast","return",
"short","signed","sizeof","static",
"static_cast","struct","switch","template",
"this","throw","true","try","typedef",
"typeid","typename","union","unsigned","using",
"virtual","void","volatile","wchar_t","while"
};
for(int iCount = 0; iCount < 64; iCount ++)
{
if (strcmpi(sKeywords[iCount], sToken) == 0)
return 1;
}
return 0;
}
int main( )
{
clrscr( );
loadTransitionTable( );
fstream File("input.txt", ios::in|ios::nocreate);
if (!File)
{
cout<<" Unable to open the input file."<";
iState = 0;
iTokenIndex = 0;
iFlag = 1;
strset(sToken, NULL);
break;
case 4 : cout << "";
iState = 0;
iTokenIndex = 0;
iFlag = 1;
strset(sToken, NULL);
break;
case 6 : cout << "";
iState = 0;
iTokenIndex = 0;
iFlag = 1;
strset(sToken, NULL);
break;
case 8 : cout << "";
iState = 0;
iTokenIndex = 0;
strset(sToken, NULL);
break;
case 9 :
case 11 :
case 12 :
case 13 :
case 15 :
case 16 :
case 17 :
case 19 :
case 20 :
case 21 :
case 22 :
case 23 :
case 24 :
case 27 :
case 28 : cout << "";
if (cChar != '+' && cChar != '-' && cChar != '/'
&& cChar != '>' && cChar != '<' && cChar != '=')
iFlag = 1;
iState = 0;
iTokenIndex = 0;
strset(sToken, NULL);
break;
case 30 :
case 33 : iState = 0;
iTokenIndex = 0;
strset(sToken, NULL);
break;
}
}
while(!File.eof( ));
End:
getch( );
return 0;
}

More Related Content

Similar to It’s sometimes useful to make a little language for a simple problem.pdf

Similar to It’s sometimes useful to make a little language for a simple problem.pdf (20)

Introduction to Python for Bioinformatics
Introduction to Python for BioinformaticsIntroduction to Python for Bioinformatics
Introduction to Python for Bioinformatics
 
1588147798Begining_ABUAD1.pdf
1588147798Begining_ABUAD1.pdf1588147798Begining_ABUAD1.pdf
1588147798Begining_ABUAD1.pdf
 
Python scripting kick off
Python scripting kick offPython scripting kick off
Python scripting kick off
 
Automation Testing theory notes.pptx
Automation Testing theory notes.pptxAutomation Testing theory notes.pptx
Automation Testing theory notes.pptx
 
c_programming.pdf
c_programming.pdfc_programming.pdf
c_programming.pdf
 
Basic Information About C language PDF
Basic Information About C language PDFBasic Information About C language PDF
Basic Information About C language PDF
 
Bcsl 031 solve assignment
Bcsl 031 solve assignmentBcsl 031 solve assignment
Bcsl 031 solve assignment
 
C Tutorials
C TutorialsC Tutorials
C Tutorials
 
Introduction Of C++
Introduction Of C++Introduction Of C++
Introduction Of C++
 
C++ programming language basic to advance level
C++ programming language basic to advance levelC++ programming language basic to advance level
C++ programming language basic to advance level
 
Code In PythonFile 1 main.pyYou will implement two algorithms t.pdf
Code In PythonFile 1 main.pyYou will implement two algorithms t.pdfCode In PythonFile 1 main.pyYou will implement two algorithms t.pdf
Code In PythonFile 1 main.pyYou will implement two algorithms t.pdf
 
7512635.ppt
7512635.ppt7512635.ppt
7512635.ppt
 
Lecture 1
Lecture 1Lecture 1
Lecture 1
 
lecture 2.pptx
lecture 2.pptxlecture 2.pptx
lecture 2.pptx
 
Cp week _2.
Cp week _2.Cp week _2.
Cp week _2.
 
C++ AND CATEGORIES OF SOFTWARE
C++ AND CATEGORIES OF SOFTWAREC++ AND CATEGORIES OF SOFTWARE
C++ AND CATEGORIES OF SOFTWARE
 
Compiler Design
Compiler DesignCompiler Design
Compiler Design
 
Unit 1 c - all topics
Unit 1   c - all topicsUnit 1   c - all topics
Unit 1 c - all topics
 
SS & CD Module 3
SS & CD Module 3 SS & CD Module 3
SS & CD Module 3
 
Module 2
Module 2 Module 2
Module 2
 

More from arri2009av

Identify five muscles of the head area that have a name that is very .pdf
Identify five muscles of the head area that have a name that is very .pdfIdentify five muscles of the head area that have a name that is very .pdf
Identify five muscles of the head area that have a name that is very .pdfarri2009av
 
Identify non-neoplastic conditions effecting pregnancy. Describe STI.pdf
Identify non-neoplastic conditions effecting pregnancy. Describe STI.pdfIdentify non-neoplastic conditions effecting pregnancy. Describe STI.pdf
Identify non-neoplastic conditions effecting pregnancy. Describe STI.pdfarri2009av
 
From a mixed field, what is easier to facilitate through artificial s.pdf
From a mixed field, what is easier to facilitate through artificial s.pdfFrom a mixed field, what is easier to facilitate through artificial s.pdf
From a mixed field, what is easier to facilitate through artificial s.pdfarri2009av
 
Explain how you would tell if something that looks like a leaf (flat.pdf
Explain how you would tell if something that looks like a leaf (flat.pdfExplain how you would tell if something that looks like a leaf (flat.pdf
Explain how you would tell if something that looks like a leaf (flat.pdfarri2009av
 
Explain what a standard deviation value measures in quantitative dat.pdf
Explain what a standard deviation value measures in quantitative dat.pdfExplain what a standard deviation value measures in quantitative dat.pdf
Explain what a standard deviation value measures in quantitative dat.pdfarri2009av
 
Einstein, in his famous photoelectric effect experiment demonstr.pdf
Einstein, in his famous photoelectric effect experiment demonstr.pdfEinstein, in his famous photoelectric effect experiment demonstr.pdf
Einstein, in his famous photoelectric effect experiment demonstr.pdfarri2009av
 
Contrast autochthonous and allochthonous food webs. Which type would.pdf
Contrast autochthonous and allochthonous food webs. Which type would.pdfContrast autochthonous and allochthonous food webs. Which type would.pdf
Contrast autochthonous and allochthonous food webs. Which type would.pdfarri2009av
 
Based on the below and using the 12 categories of threats identify 3 .pdf
Based on the below and using the 12 categories of threats identify 3 .pdfBased on the below and using the 12 categories of threats identify 3 .pdf
Based on the below and using the 12 categories of threats identify 3 .pdfarri2009av
 
Blair, R. B. 1996. Land use and avian species diversity along an urb.pdf
Blair, R. B. 1996. Land use and avian species diversity along an urb.pdfBlair, R. B. 1996. Land use and avian species diversity along an urb.pdf
Blair, R. B. 1996. Land use and avian species diversity along an urb.pdfarri2009av
 
An attack in which an authentic-looking e-mail or website entices a .pdf
An attack in which an authentic-looking e-mail or website entices a .pdfAn attack in which an authentic-looking e-mail or website entices a .pdf
An attack in which an authentic-looking e-mail or website entices a .pdfarri2009av
 
Consider a relation T with six attributes ABCDEF where AB is a compo.pdf
Consider a relation T with six attributes ABCDEF where AB is a compo.pdfConsider a relation T with six attributes ABCDEF where AB is a compo.pdf
Consider a relation T with six attributes ABCDEF where AB is a compo.pdfarri2009av
 
A vague appointment Four people make an appointment to meet each ot.pdf
A vague appointment Four people make an appointment to meet each ot.pdfA vague appointment Four people make an appointment to meet each ot.pdf
A vague appointment Four people make an appointment to meet each ot.pdfarri2009av
 
Assume real numbers R for now. Consider relation on R, x y iff x .pdf
Assume real numbers R for now. Consider relation  on R, x  y iff x  .pdfAssume real numbers R for now. Consider relation  on R, x  y iff x  .pdf
Assume real numbers R for now. Consider relation on R, x y iff x .pdfarri2009av
 
An enzyme aggase requires 16 units of activity for wild type functio.pdf
An enzyme aggase requires 16 units of activity for wild type functio.pdfAn enzyme aggase requires 16 units of activity for wild type functio.pdf
An enzyme aggase requires 16 units of activity for wild type functio.pdfarri2009av
 
17. Of these, which represents a heterozygote a. aa b. Ab c. .pdf
17. Of these, which represents a heterozygote a. aa b. Ab c. .pdf17. Of these, which represents a heterozygote a. aa b. Ab c. .pdf
17. Of these, which represents a heterozygote a. aa b. Ab c. .pdfarri2009av
 
1. Match the decription listed with the corresponding structureA. .pdf
1. Match the decription listed with the corresponding structureA. .pdf1. Match the decription listed with the corresponding structureA. .pdf
1. Match the decription listed with the corresponding structureA. .pdfarri2009av
 
1.) What are some factors that should be taken into account when est.pdf
1.) What are some factors that should be taken into account when est.pdf1.) What are some factors that should be taken into account when est.pdf
1.) What are some factors that should be taken into account when est.pdfarri2009av
 
Write a program that asks the user for the name of a file. The progr.pdf
Write a program that asks the user for the name of a file. The progr.pdfWrite a program that asks the user for the name of a file. The progr.pdf
Write a program that asks the user for the name of a file. The progr.pdfarri2009av
 
Write a program that obtains the execution time of selection sort, bu.pdf
Write a program that obtains the execution time of selection sort, bu.pdfWrite a program that obtains the execution time of selection sort, bu.pdf
Write a program that obtains the execution time of selection sort, bu.pdfarri2009av
 
Wings of bats area. Plesiomorphic (ancestral) feature for mammals.pdf
Wings of bats area. Plesiomorphic (ancestral) feature for mammals.pdfWings of bats area. Plesiomorphic (ancestral) feature for mammals.pdf
Wings of bats area. Plesiomorphic (ancestral) feature for mammals.pdfarri2009av
 

More from arri2009av (20)

Identify five muscles of the head area that have a name that is very .pdf
Identify five muscles of the head area that have a name that is very .pdfIdentify five muscles of the head area that have a name that is very .pdf
Identify five muscles of the head area that have a name that is very .pdf
 
Identify non-neoplastic conditions effecting pregnancy. Describe STI.pdf
Identify non-neoplastic conditions effecting pregnancy. Describe STI.pdfIdentify non-neoplastic conditions effecting pregnancy. Describe STI.pdf
Identify non-neoplastic conditions effecting pregnancy. Describe STI.pdf
 
From a mixed field, what is easier to facilitate through artificial s.pdf
From a mixed field, what is easier to facilitate through artificial s.pdfFrom a mixed field, what is easier to facilitate through artificial s.pdf
From a mixed field, what is easier to facilitate through artificial s.pdf
 
Explain how you would tell if something that looks like a leaf (flat.pdf
Explain how you would tell if something that looks like a leaf (flat.pdfExplain how you would tell if something that looks like a leaf (flat.pdf
Explain how you would tell if something that looks like a leaf (flat.pdf
 
Explain what a standard deviation value measures in quantitative dat.pdf
Explain what a standard deviation value measures in quantitative dat.pdfExplain what a standard deviation value measures in quantitative dat.pdf
Explain what a standard deviation value measures in quantitative dat.pdf
 
Einstein, in his famous photoelectric effect experiment demonstr.pdf
Einstein, in his famous photoelectric effect experiment demonstr.pdfEinstein, in his famous photoelectric effect experiment demonstr.pdf
Einstein, in his famous photoelectric effect experiment demonstr.pdf
 
Contrast autochthonous and allochthonous food webs. Which type would.pdf
Contrast autochthonous and allochthonous food webs. Which type would.pdfContrast autochthonous and allochthonous food webs. Which type would.pdf
Contrast autochthonous and allochthonous food webs. Which type would.pdf
 
Based on the below and using the 12 categories of threats identify 3 .pdf
Based on the below and using the 12 categories of threats identify 3 .pdfBased on the below and using the 12 categories of threats identify 3 .pdf
Based on the below and using the 12 categories of threats identify 3 .pdf
 
Blair, R. B. 1996. Land use and avian species diversity along an urb.pdf
Blair, R. B. 1996. Land use and avian species diversity along an urb.pdfBlair, R. B. 1996. Land use and avian species diversity along an urb.pdf
Blair, R. B. 1996. Land use and avian species diversity along an urb.pdf
 
An attack in which an authentic-looking e-mail or website entices a .pdf
An attack in which an authentic-looking e-mail or website entices a .pdfAn attack in which an authentic-looking e-mail or website entices a .pdf
An attack in which an authentic-looking e-mail or website entices a .pdf
 
Consider a relation T with six attributes ABCDEF where AB is a compo.pdf
Consider a relation T with six attributes ABCDEF where AB is a compo.pdfConsider a relation T with six attributes ABCDEF where AB is a compo.pdf
Consider a relation T with six attributes ABCDEF where AB is a compo.pdf
 
A vague appointment Four people make an appointment to meet each ot.pdf
A vague appointment Four people make an appointment to meet each ot.pdfA vague appointment Four people make an appointment to meet each ot.pdf
A vague appointment Four people make an appointment to meet each ot.pdf
 
Assume real numbers R for now. Consider relation on R, x y iff x .pdf
Assume real numbers R for now. Consider relation  on R, x  y iff x  .pdfAssume real numbers R for now. Consider relation  on R, x  y iff x  .pdf
Assume real numbers R for now. Consider relation on R, x y iff x .pdf
 
An enzyme aggase requires 16 units of activity for wild type functio.pdf
An enzyme aggase requires 16 units of activity for wild type functio.pdfAn enzyme aggase requires 16 units of activity for wild type functio.pdf
An enzyme aggase requires 16 units of activity for wild type functio.pdf
 
17. Of these, which represents a heterozygote a. aa b. Ab c. .pdf
17. Of these, which represents a heterozygote a. aa b. Ab c. .pdf17. Of these, which represents a heterozygote a. aa b. Ab c. .pdf
17. Of these, which represents a heterozygote a. aa b. Ab c. .pdf
 
1. Match the decription listed with the corresponding structureA. .pdf
1. Match the decription listed with the corresponding structureA. .pdf1. Match the decription listed with the corresponding structureA. .pdf
1. Match the decription listed with the corresponding structureA. .pdf
 
1.) What are some factors that should be taken into account when est.pdf
1.) What are some factors that should be taken into account when est.pdf1.) What are some factors that should be taken into account when est.pdf
1.) What are some factors that should be taken into account when est.pdf
 
Write a program that asks the user for the name of a file. The progr.pdf
Write a program that asks the user for the name of a file. The progr.pdfWrite a program that asks the user for the name of a file. The progr.pdf
Write a program that asks the user for the name of a file. The progr.pdf
 
Write a program that obtains the execution time of selection sort, bu.pdf
Write a program that obtains the execution time of selection sort, bu.pdfWrite a program that obtains the execution time of selection sort, bu.pdf
Write a program that obtains the execution time of selection sort, bu.pdf
 
Wings of bats area. Plesiomorphic (ancestral) feature for mammals.pdf
Wings of bats area. Plesiomorphic (ancestral) feature for mammals.pdfWings of bats area. Plesiomorphic (ancestral) feature for mammals.pdf
Wings of bats area. Plesiomorphic (ancestral) feature for mammals.pdf
 

Recently uploaded

internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 

Recently uploaded (20)

internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 

It’s sometimes useful to make a little language for a simple problem.pdf

  • 1. It’s sometimes useful to make a little language for a simple problem. We are making a language to let us play with strings and numbers, and for this assignment we are building a lexical analyzer for this simple language. Here are the lexical rules for the language: 1. The language has identifiers. An identifier starts with a letter and is followed by zero or more letters. 2. The language has string constants. A string constant is a sequence of characters, all on one line, enclosed in double quotes. You do not have to support special handling for escape characters between the quotes. 3. The language has integer constants, defined as a sequence of digits. 4. The language has 3 operators: +, * and [ ]. This requires 4 tokens. 5. The language has 2 keywords: “print” and “set”. 6. Statements in the language end in a semicolon. 7. The language supports parentheses. 8. White space is used to separate tokens and lines for readability. 9. A comment begins with two slashes (//) and ends at a newline. The lexical analyzer is to be implemented in a C++ function. The function will be passed a pointer to an input stream to read from. It will return a Token representing the token and lexeme that has been recognized. The definitions for the Token class and unique values for each of the tokens (TokenType) that you must recognize is provided in the header file p2lex.h, which is on the course website. There is also a testmain.cpp program that shows how to include p2lex.h and use tokens. The lexical analyzer must ignore white space and comments. Whitespace and comments serve only as separation between tokens. The program should maintain an external integer named “linenum”, which should be initialized to 0 and incremented whenever a newline is seen by getToken(). A lexical error should cause the token ERR to be returned. An end of file should cause DONE to be returned. You MUST use the p2lex.h header file for your assignment. You may not change it. You do not need to hand in p2lex.h, but it doesn’t matter if you do. I will compile and test your program against the distributed p2lex.h header file You must produce and submit three files: 1. p2lex.cpp This file should #include p2lex.h and must provide: -> an implementation for << operator to print a Token o The printed version of the token must be a string representation of the TokenType. We define the string representation of a TokenType to be the symbol for the TokenType in lowercase letters. For example, the string representation of the TokenType value INT should be “int”.
  • 2. o For the ID, STR, INT and ERR tokens, the string representation must be followed by the lexeme for the token. The lexeme should be printed in parentheses. -> an implementation for the getToken function o The getToken() function should read characters from the stream pointed to by the first argument and return the token that it recognizes. 2. project2.cpp This file should #include p2lex.h and provide a main program to test the lexical analyzer The specifications for the main program are as follows: 1. The program should accept at most one command line argument. If present, this one command line argument is the name of a file to open and read for input. If not present, the program should read from the standard input. 2. There may also, optionally, be an initial command line argument equal to the string “-v”. If it is, then your program should be in “verbose mode” and should print each token that it recognizes in addition to the remaining items. 3. The program should loop repeatedly, calling getToken(), until getToken returns an ERR or DONE token 4. If the loop stopped because of an ERR token, it should print out the token 5. The program should print out a count of the number of times each type of token was seen. 6. The program should print out the number of unique lexemes for the ID, STR and INT that appear in the input -------------------------------------------------------------------------------------------------------------------- ------------------ The source code for the lexer header file can be found below!!!!!!!!!!!!!! Solution # include # include # include # include # include # include # include constint iRows = 34; constint iCols = 15; int iTT[iRows][iCols] = {0};
  • 3. { fstream File("tt.txt", ios::in|ios::nocreate); if (!File) { cout << " Unable to open the input file." << endl; cout << " Press any key to exit."; getch( ); exit(0); } char sInput[100]={NULL}; for (int i = 0; i < iRows; i ++) { strset(sInput, NULL); File.getline(sInput, 80); char *sPtr=NULL; sPtr = strtok(sInput, " "); iTT[i][0] = atoi(sPtr); for(int j = 1; j < iCols; j ++) { sPtr=strtok(NULL, " "); iTT[i][j] = atoi(sPtr); } } File.close( ); } { if (isalpha(cChar)) return iTT[iState][1]; elseif (isdigit(cChar)) return iTT[iState][2]; elseif (cChar == '.') return iTT[iState][3]; elseif (cChar == '"') return iTT[iState][4]; elseif (cChar == '_') return iTT[iState][6];
  • 4. elseif (cChar == '+') return iTT[iState][7]; elseif (cChar == '=') return iTT[iState][8]; elseif (cChar == '-') return iTT[iState][9]; elseif (cChar == '%') return iTT[iState][10]; elseif (cChar == '!') return iTT[iState][11]; elseif (cChar == '>') return iTT[iState][12]; elseif (cChar == '<') return iTT[iState][13]; elseif (cChar == '/') return iTT[iState][14]; return iTT[iState][0]; } { if (strlen(sToken) > 16 || strlen(sToken) == 0) return 0; char sKeywords[64][20] = { "asm","auto","bool","break","case","catch", "char","class","const","const_cast", "continue","default","delete","do","double", "dynamic_cast","else","enum","explicit", "export","extern","false","float","for", "friend","goto","if","inline","int","long", "main","mutable","namespace","new", "operator","private","protected","public", "register","reinterpret_cast","return", "short","signed","sizeof","static", "static_cast","struct","switch","template", "this","throw","true","try","typedef", "typeid","typename","union","unsigned","using", "virtual","void","volatile","wchar_t","while"
  • 5. }; for(int iCount = 0; iCount < 64; iCount ++) { if (strcmpi(sKeywords[iCount], sToken) == 0) return 1; } return 0; } int main( ) { clrscr( ); loadTransitionTable( ); fstream File("input.txt", ios::in|ios::nocreate); if (!File) { cout<<" Unable to open the input file."<"; iState = 0; iTokenIndex = 0; iFlag = 1; strset(sToken, NULL); break; case 4 : cout << ""; iState = 0; iTokenIndex = 0; iFlag = 1; strset(sToken, NULL); break; case 6 : cout << ""; iState = 0; iTokenIndex = 0; iFlag = 1; strset(sToken, NULL); break; case 8 : cout << ""; iState = 0; iTokenIndex = 0;
  • 6. strset(sToken, NULL); break; case 9 : case 11 : case 12 : case 13 : case 15 : case 16 : case 17 : case 19 : case 20 : case 21 : case 22 : case 23 : case 24 : case 27 : case 28 : cout << ""; if (cChar != '+' && cChar != '-' && cChar != '/' && cChar != '>' && cChar != '<' && cChar != '=') iFlag = 1; iState = 0; iTokenIndex = 0; strset(sToken, NULL); break; case 30 : case 33 : iState = 0; iTokenIndex = 0; strset(sToken, NULL); break; } } while(!File.eof( )); End: getch( ); return 0; }