SlideShare a Scribd company logo
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

Project lexical analyser compiler _1.pdf
Project lexical analyser compiler _1.pdfProject lexical analyser compiler _1.pdf
Project lexical analyser compiler _1.pdf
abhimanyukumar28203
 
Introduction to Python for Bioinformatics
Introduction to Python for BioinformaticsIntroduction to Python for Bioinformatics
Introduction to Python for Bioinformatics
José Héctor Gálvez
 
1588147798Begining_ABUAD1.pdf
1588147798Begining_ABUAD1.pdf1588147798Begining_ABUAD1.pdf
1588147798Begining_ABUAD1.pdf
SemsemSameer1
 
Python scripting kick off
Python scripting kick offPython scripting kick off
Python scripting kick off
Andrea Gangemi
 
Automation Testing theory notes.pptx
Automation Testing theory notes.pptxAutomation Testing theory notes.pptx
Automation Testing theory notes.pptx
NileshBorkar12
 
c_programming.pdf
c_programming.pdfc_programming.pdf
c_programming.pdf
Home
 
Basic Information About C language PDF
Basic Information About C language PDFBasic Information About C language PDF
Basic Information About C language PDF
Suraj Das
 
Bcsl 031 solve assignment
Bcsl 031 solve assignmentBcsl 031 solve assignment
Introduction Of C++
Introduction Of C++Introduction Of C++
Introduction Of C++
Sangharsh agarwal
 
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
sajjad ali khan
 
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
aishwaryaequipment
 
Lecture 1
Lecture 1Lecture 1
Lecture 1
marvellous2
 
lecture 2.pptx
lecture 2.pptxlecture 2.pptx
lecture 2.pptx
Anonymous9etQKwW
 
Cp week _2.
Cp week _2.Cp week _2.
Cp week _2.
shahidullah57
 
C++ AND CATEGORIES OF SOFTWARE
C++ AND CATEGORIES OF SOFTWAREC++ AND CATEGORIES OF SOFTWARE
C++ AND CATEGORIES OF SOFTWARE
UNIVERSITY OF ENGINEERING AND TECHNOLOGY TAXILA
 
Compiler Design
Compiler DesignCompiler Design
Compiler Design
Dr. Jaydeep Patil
 
Unit 1 c - all topics
Unit 1   c - all topicsUnit 1   c - all topics
Unit 1 c - all topics
veningstonk
 
Module 2
Module 2 Module 2
Module 2
ShwetaNirmanik
 

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

Project lexical analyser compiler _1.pdf
Project lexical analyser compiler _1.pdfProject lexical analyser compiler _1.pdf
Project lexical analyser compiler _1.pdf
 
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
 
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 .pdf
arri2009av
 
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
arri2009av
 
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
arri2009av
 
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
arri2009av
 
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
arri2009av
 
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
arri2009av
 
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
arri2009av
 
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
arri2009av
 
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
arri2009av
 
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
arri2009av
 
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
arri2009av
 
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
arri2009av
 
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
arri2009av
 
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
arri2009av
 
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
arri2009av
 
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
arri2009av
 
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
arri2009av
 
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
arri2009av
 
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
arri2009av
 
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
arri2009av
 

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

1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
Marketing internship report file for MBA
Marketing internship report file for MBAMarketing internship report file for MBA
Marketing internship report file for MBA
gb193092
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 

Recently uploaded (20)

1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
Marketing internship report file for MBA
Marketing internship report file for MBAMarketing internship report file for MBA
Marketing internship report file for MBA
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 

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; }