SlideShare una empresa de Scribd logo
1 de 7
Descargar para leer sin conexión
Lex.h
/*
* lex.h
*
* CS280
* Spring 2023
*/
#ifndef LEX_H_
#define LEX_H_
#include <string>
#include <iostream>
#include <map>
using namespace std;
//Definition of all the possible token types
enum Token {
// keywords
WRITELN, IF, ELSE,
// identifiers
IDENT, NIDENT, SIDENT,
// an integer, real, and string constant
ICONST, RCONST, SCONST,
// the numeric operators, assignment, numeric and string comparison operators
PLUS, MINUS, MULT, DIV, EXPONENT, ASSOP, NEQ,
NGTHAN, NLTHAN, CAT, SREPEAT, SEQ, SLTHAN, SGTHAN,
//Delimiters
COMMA, SEMICOL, LPAREN, RPAREN, LBRACES, RBRACES,
// any error returns this token
ERR,
// when completed (EOF), return this token
DONE,
};
//Class definition of LexItem
class LexItem {
Token token;
string lexeme;
int lnum;
public:
LexItem() {
token = ERR;
lnum = -1;
}
LexItem(Token token, string lexeme, int line) {
this->token = token;
this->lexeme = lexeme;
this->lnum = line;
}
bool operator==(const Token token) const { return this->token == token; }
bool operator!=(const Token token) const { return this->token != token; }
Token GetToken() const { return token; }
string GetLexeme() const { return lexeme; }
int GetLinenum() const { return lnum; }
};
extern ostream& operator<<(ostream& out, const LexItem& tok);
extern LexItem id_or_kw(const string& lexeme, int linenum);
extern LexItem getNextToken(istream& in, int& linenum);
#endif /* LEX_H_ */
tokensListing.cpp
#include "lex.h"
extern ostream operator <<(ostream & out, const tok & tok)
{
out<< "Token: "<< tok.GetToken( )<< "lex:" << tok.Getlexeme( )<< "Line No: "
<<tok.linenum( );
}
#ifndef LEX_H_
#define LEX_H_
#include<string>
#include<iostream>
using std:: string;
using std:: istream;
using std:: ostream;
Enum Token {
//Keywords
PRINT,PRINTLN, REPEAT, BEGIN, END
// an identifier
INDENT,
// an Integer and String constant
ICONST, SCONST,
//Operators, Parens, semicolon
PLUS, MINUS, STAR, SLASH, EQ, LPAREN, RPAREN, SC,
// any error returns this token
ERR,
//When completed (EDF), return this token
DONE
};
Class Tok
{
Token token;
String lexeme;
int inum;
public
Tok( ){
token = ERR;
lnum = -1;
}
Token( Token token, string lexeme, int line){
this -> token = token;
this -> lexeme = lexeme;
this -> lnum = line;
}
bool operator == (const Token token) const { return this -> token == token; }
bool operator != (const Token token) const { return this -> token == token; }
Token GetToken ( ) const { return token; }
String Getlexeme( ) const { return lexeme;}
int GetLinenum( ) const { return lnum;}
};
extern ostream &operator << (ostream &out, const Tok &tok);
extern Tok getNextToken (istream&in, int & line num);
#endif /*LEX_H_*/
#include 'lex.h'
int main( )
{
Tok toks[ ] = {
TOK(PRINT, "PRINT", 3);
TOK(PRINTLN, "PRINTLN",3);
TOK(REPEAT, "REPEAT", 3);
TOK(BEGIN, "BEGIN",3);
TOK(END, "END", 3);
TOK(IDENT, "foo", 3);
TOK(CONST,"347",3);
TOK(SCONST, "Hello world", 3);
TOK(PLUS, " +" , 3);
TOK(MINUS, "-", 3);
TOK(STAR, "*",3);
TOK(SLASH, "/",3);
TOK(EQ, "=",3);
TOK(LPAREN, "(",3);
TOK(RPAREN, ")", 3);
TOK(SC, ":",3);
TOK(DONE,"DONE",3);
};
for (int i = 0; toks[i]! = DONE; i++)
cout<< toks[i] <<endl;
return 0;
}
Case 1 input
IDENT PROGRAM 0
IDENT $PROG1 3
IDENT INT 2
IDENT _X1 2
COMMA , 2
IDENT @Y_1 2
COMMA , 2
IDENT Z 2
MINUS - 2
COMMA , 2
IDENT W234 2
SEMICOL ; 2
IDENT FLOAT 3
IDENT Z 3
ASSOP = 3
RCONST 0.75 3
SEMICOL ; 3
IDENT $BOOL 4
IDENT @FLAG 4
ASSOP = 4
SEMICOL ; 4
IDENT R 6
ERR $ 6
IDENT FLAG 7
ASSOP = 7
SEMICOL ; 7
IDENT IF 8
LPAREN ( 8
IDENT FLAG 8
RPAREN ) 8
IDENT THEN 8
IDENT Y_1 9
ASSOP = 9
ICONST 5 9
SEMICOL ; 9
IDENT ELSE 10
IDENT Y_1 11
ASSOP = 11
RCONST 7.5 11
SEMICOL ; 11
IDENT END 12
IDENT IF 12
IDENT PRINT 13
SCONST 8 "Value = " 13
COMMA , 13
IDENT R 13
COMMA , 13
SCONST 4 "z = " 13
COMMA , 13
IDENT Z 13
SEMICOL ; 13
IDENT END 14
IDENT PROGRAM 14
Case 1 output
IDENTIFIER: PROGRAM at Line 0
NIDENT: "$PROG1" at Line 3
IDENTIFIER: INT at Line 2
IDENTIFIER: _X1 at Line 2
COMMA: "," at Line 2
SIDENT: "@Y_1" at Line 2
COMMA: "," at Line 2
IDENTIFIER: Z at Line 2
MINUS: "-" at Line 2
COMMA: "," at Line 2
IDENTIFIER: W234 at Line 2
SEMICOL: ";" at Line 2
IDENTIFIER: FLOAT at Line 3
IDENTIFIER: Z at Line 3
ASSOP: "=" at Line 3
RCONST: (0.75) at Line 3
SEMICOL: ";" at Line 3
NIDENT: "$BOOL" at Line 4
SIDENT: "@FLAG" at Line 4
ASSOP: "=" at Line 4
SEMICOL: ";" at Line 4
IDENTIFIER: R at Line 6
Error: : "$" at Line 6
IDENTIFIER: FLAG at Line 7
ASSOP: "=" at Line 7
SEMICOL: ";" at Line 7
IF: "IF" at Line 8
LPAREN: "(" at Line 8
IDENTIFIER: FLAG at Line 8
RPAREN: ")" at Line 8
IDENTIFIER: THEN at Line 8
IDENTIFIER: Y_1 at Line 9
ASSOP: "=" at Line 9
ICONST: (5) at Line 9
SEMICOL: ";" at Line 9
ELSE: "ELSE" at Line 10
IDENTIFIER: Y_1 at Line 11
ASSOP: "=" at Line 11
RCONST: (7.5) at Line 11
SEMICOL: ";" at Line 11
IDENTIFIER: END at Line 12
IF: "IF" at Line 12
IDENTIFIER: PRINT at Line 13
SCONST: 'Value = ' at Line 13
COMMA: "," at Line 13
IDENTIFIER: R at Line 13
COMMA: "," at Line 13
SCONST: 'z = ' at Line 13
COMMA: "," at Line 13
IDENTIFIER: Z at Line 13
SEMICOL: ";" at Line 13
IDENTIFIER: END at Line 14
IDENTIFIER: PROGRAM at Line 14

Más contenido relacionado

Similar a CS280 Lexical Analyzer Header

prog 5~$AD FOR WHAT TO DO.docxprog 5alerts.txt2009-09-13.docx
prog 5~$AD FOR WHAT TO DO.docxprog 5alerts.txt2009-09-13.docxprog 5~$AD FOR WHAT TO DO.docxprog 5alerts.txt2009-09-13.docx
prog 5~$AD FOR WHAT TO DO.docxprog 5alerts.txt2009-09-13.docxwkyra78
 
Falcon初印象
Falcon初印象Falcon初印象
Falcon初印象勇浩 赖
 
Please fill in the code to run the program based on the following in.pdf
Please fill in the code to run the program based on the following in.pdfPlease fill in the code to run the program based on the following in.pdf
Please fill in the code to run the program based on the following in.pdfamarnathmahajansport
 
You have already implemented a lexical analyzer in Assignment #1 and.pdf
You have already implemented a lexical analyzer in Assignment #1 and.pdfYou have already implemented a lexical analyzer in Assignment #1 and.pdf
You have already implemented a lexical analyzer in Assignment #1 and.pdfalbert20021
 
Java Programming Below are the lexer token and shank file.pdf
Java Programming Below are the lexer token and shank file.pdfJava Programming Below are the lexer token and shank file.pdf
Java Programming Below are the lexer token and shank file.pdfabdulkadar1977
 
C++ code, please help! Troubleshooting and cannot for the life of me.pdf
C++ code, please help! Troubleshooting and cannot for the life of me.pdfC++ code, please help! Troubleshooting and cannot for the life of me.pdf
C++ code, please help! Troubleshooting and cannot for the life of me.pdfrahulfancycorner21
 
Ejemplo completo de integración JLex y CUP
Ejemplo completo de integración JLex y CUPEjemplo completo de integración JLex y CUP
Ejemplo completo de integración JLex y CUPEgdares Futch H.
 
Modify the Simple lexer program given in the link below to .pdf
Modify the Simple lexer program given in the link below to .pdfModify the Simple lexer program given in the link below to .pdf
Modify the Simple lexer program given in the link below to .pdfadityaenterprise32
 
Endevor api an introduction to the endevor application programming interface
Endevor api   an introduction to the endevor application programming interface Endevor api   an introduction to the endevor application programming interface
Endevor api an introduction to the endevor application programming interface Kevin Grimes
 
C++ Advanced
C++ AdvancedC++ Advanced
C++ AdvancedVivek Das
 
... now write an interpreter (PHPem 2016)
... now write an interpreter (PHPem 2016)... now write an interpreter (PHPem 2016)
... now write an interpreter (PHPem 2016)James Titcumb
 
Start with the inclusion of libraries#include iostream .docx
 Start with the inclusion of libraries#include iostream .docx Start with the inclusion of libraries#include iostream .docx
Start with the inclusion of libraries#include iostream .docxMARRY7
 
calc3build# calc3bison -y -d calc3.yflex calc3.lgcc -c .docx
calc3build# calc3bison -y -d calc3.yflex calc3.lgcc -c .docxcalc3build# calc3bison -y -d calc3.yflex calc3.lgcc -c .docx
calc3build# calc3bison -y -d calc3.yflex calc3.lgcc -c .docxRAHUL126667
 
[FT-11][suhorng] “Poor Man's” Undergraduate Compilers
[FT-11][suhorng] “Poor Man's” Undergraduate Compilers[FT-11][suhorng] “Poor Man's” Undergraduate Compilers
[FT-11][suhorng] “Poor Man's” Undergraduate CompilersFunctional Thursday
 
COMP360 Assembler Write an assembler that reads the source code of an.pdf
COMP360 Assembler Write an assembler that reads the source code of an.pdfCOMP360 Assembler Write an assembler that reads the source code of an.pdf
COMP360 Assembler Write an assembler that reads the source code of an.pdffazalenterprises
 

Similar a CS280 Lexical Analyzer Header (20)

prog 5~$AD FOR WHAT TO DO.docxprog 5alerts.txt2009-09-13.docx
prog 5~$AD FOR WHAT TO DO.docxprog 5alerts.txt2009-09-13.docxprog 5~$AD FOR WHAT TO DO.docxprog 5alerts.txt2009-09-13.docx
prog 5~$AD FOR WHAT TO DO.docxprog 5alerts.txt2009-09-13.docx
 
Falcon初印象
Falcon初印象Falcon初印象
Falcon初印象
 
Please fill in the code to run the program based on the following in.pdf
Please fill in the code to run the program based on the following in.pdfPlease fill in the code to run the program based on the following in.pdf
Please fill in the code to run the program based on the following in.pdf
 
You have already implemented a lexical analyzer in Assignment #1 and.pdf
You have already implemented a lexical analyzer in Assignment #1 and.pdfYou have already implemented a lexical analyzer in Assignment #1 and.pdf
You have already implemented a lexical analyzer in Assignment #1 and.pdf
 
5_IntermediateCodeGeneration.ppt
5_IntermediateCodeGeneration.ppt5_IntermediateCodeGeneration.ppt
5_IntermediateCodeGeneration.ppt
 
Java Programming Below are the lexer token and shank file.pdf
Java Programming Below are the lexer token and shank file.pdfJava Programming Below are the lexer token and shank file.pdf
Java Programming Below are the lexer token and shank file.pdf
 
LEX & YACC TOOL
LEX & YACC TOOLLEX & YACC TOOL
LEX & YACC TOOL
 
Antlr V3
Antlr V3Antlr V3
Antlr V3
 
C++ code, please help! Troubleshooting and cannot for the life of me.pdf
C++ code, please help! Troubleshooting and cannot for the life of me.pdfC++ code, please help! Troubleshooting and cannot for the life of me.pdf
C++ code, please help! Troubleshooting and cannot for the life of me.pdf
 
Rcpp11
Rcpp11Rcpp11
Rcpp11
 
oop Lecture 4
oop Lecture 4oop Lecture 4
oop Lecture 4
 
Ejemplo completo de integración JLex y CUP
Ejemplo completo de integración JLex y CUPEjemplo completo de integración JLex y CUP
Ejemplo completo de integración JLex y CUP
 
Modify the Simple lexer program given in the link below to .pdf
Modify the Simple lexer program given in the link below to .pdfModify the Simple lexer program given in the link below to .pdf
Modify the Simple lexer program given in the link below to .pdf
 
Endevor api an introduction to the endevor application programming interface
Endevor api   an introduction to the endevor application programming interface Endevor api   an introduction to the endevor application programming interface
Endevor api an introduction to the endevor application programming interface
 
C++ Advanced
C++ AdvancedC++ Advanced
C++ Advanced
 
... now write an interpreter (PHPem 2016)
... now write an interpreter (PHPem 2016)... now write an interpreter (PHPem 2016)
... now write an interpreter (PHPem 2016)
 
Start with the inclusion of libraries#include iostream .docx
 Start with the inclusion of libraries#include iostream .docx Start with the inclusion of libraries#include iostream .docx
Start with the inclusion of libraries#include iostream .docx
 
calc3build# calc3bison -y -d calc3.yflex calc3.lgcc -c .docx
calc3build# calc3bison -y -d calc3.yflex calc3.lgcc -c .docxcalc3build# calc3bison -y -d calc3.yflex calc3.lgcc -c .docx
calc3build# calc3bison -y -d calc3.yflex calc3.lgcc -c .docx
 
[FT-11][suhorng] “Poor Man's” Undergraduate Compilers
[FT-11][suhorng] “Poor Man's” Undergraduate Compilers[FT-11][suhorng] “Poor Man's” Undergraduate Compilers
[FT-11][suhorng] “Poor Man's” Undergraduate Compilers
 
COMP360 Assembler Write an assembler that reads the source code of an.pdf
COMP360 Assembler Write an assembler that reads the source code of an.pdfCOMP360 Assembler Write an assembler that reads the source code of an.pdf
COMP360 Assembler Write an assembler that reads the source code of an.pdf
 

Más de PierschdJohnstonb

Match chemical structures of the following nucleotides (designated A-E.pdf
Match chemical structures of the following nucleotides (designated A-E.pdfMatch chemical structures of the following nucleotides (designated A-E.pdf
Match chemical structures of the following nucleotides (designated A-E.pdfPierschdJohnstonb
 
Match each bulleted item on the left with ALL of the items on the righ.pdf
Match each bulleted item on the left with ALL of the items on the righ.pdfMatch each bulleted item on the left with ALL of the items on the righ.pdf
Match each bulleted item on the left with ALL of the items on the righ.pdfPierschdJohnstonb
 
Masego Hadebe- a South African resident- passed away on 14 February 20.pdf
Masego Hadebe- a South African resident- passed away on 14 February 20.pdfMasego Hadebe- a South African resident- passed away on 14 February 20.pdf
Masego Hadebe- a South African resident- passed away on 14 February 20.pdfPierschdJohnstonb
 
Mary has an IV solution of normal saline infusing in her left arm and.pdf
Mary has an IV solution of normal saline infusing in her left arm and.pdfMary has an IV solution of normal saline infusing in her left arm and.pdf
Mary has an IV solution of normal saline infusing in her left arm and.pdfPierschdJohnstonb
 
Mary Beth Maris- the manager of an apartment complex- feots overwheime.pdf
Mary Beth Maris- the manager of an apartment complex- feots overwheime.pdfMary Beth Maris- the manager of an apartment complex- feots overwheime.pdf
Mary Beth Maris- the manager of an apartment complex- feots overwheime.pdfPierschdJohnstonb
 
Martin Corporation has the following amounts as of December 31-2018- (.pdf
Martin Corporation has the following amounts as of December 31-2018- (.pdfMartin Corporation has the following amounts as of December 31-2018- (.pdf
Martin Corporation has the following amounts as of December 31-2018- (.pdfPierschdJohnstonb
 
Marina Andros works in the Financials Department of Eastern Mobile- a.pdf
Marina Andros works in the Financials Department of Eastern Mobile- a.pdfMarina Andros works in the Financials Department of Eastern Mobile- a.pdf
Marina Andros works in the Financials Department of Eastern Mobile- a.pdfPierschdJohnstonb
 
Mark (50) and Phoebe (30)- Cristina (45) and Michael (49)- and Jason (.pdf
Mark (50) and Phoebe (30)- Cristina (45) and Michael (49)- and Jason (.pdfMark (50) and Phoebe (30)- Cristina (45) and Michael (49)- and Jason (.pdf
Mark (50) and Phoebe (30)- Cristina (45) and Michael (49)- and Jason (.pdfPierschdJohnstonb
 
Marin Company's income statement for the year ended December 31- 2025-.pdf
Marin Company's income statement for the year ended December 31- 2025-.pdfMarin Company's income statement for the year ended December 31- 2025-.pdf
Marin Company's income statement for the year ended December 31- 2025-.pdfPierschdJohnstonb
 
Many people think the most widely used tool in a construction project.pdf
Many people think the most widely used tool in a construction project.pdfMany people think the most widely used tool in a construction project.pdf
Many people think the most widely used tool in a construction project.pdfPierschdJohnstonb
 
Many aspects of aquaculture are regulated by federal legislation- Some.pdf
Many aspects of aquaculture are regulated by federal legislation- Some.pdfMany aspects of aquaculture are regulated by federal legislation- Some.pdf
Many aspects of aquaculture are regulated by federal legislation- Some.pdfPierschdJohnstonb
 
Map this ERR model into a relational model visually- The investment co.pdf
Map this ERR model into a relational model visually- The investment co.pdfMap this ERR model into a relational model visually- The investment co.pdf
Map this ERR model into a relational model visually- The investment co.pdfPierschdJohnstonb
 
Many COVID-19 patients who die despite being intubated and put on a ve.pdf
Many COVID-19 patients who die despite being intubated and put on a ve.pdfMany COVID-19 patients who die despite being intubated and put on a ve.pdf
Many COVID-19 patients who die despite being intubated and put on a ve.pdfPierschdJohnstonb
 
Many of the regulations you have read about this week were developed f.pdf
Many of the regulations you have read about this week were developed f.pdfMany of the regulations you have read about this week were developed f.pdf
Many of the regulations you have read about this week were developed f.pdfPierschdJohnstonb
 
Managers may engage in classification shifting by- Multiple Choice rep.pdf
Managers may engage in classification shifting by- Multiple Choice rep.pdfManagers may engage in classification shifting by- Multiple Choice rep.pdf
Managers may engage in classification shifting by- Multiple Choice rep.pdfPierschdJohnstonb
 
Make a Gantt Chart from the activities and tasks - Case Descriptio.pdf
Make a Gantt Chart  from the activities and tasks -    Case Descriptio.pdfMake a Gantt Chart  from the activities and tasks -    Case Descriptio.pdf
Make a Gantt Chart from the activities and tasks - Case Descriptio.pdfPierschdJohnstonb
 
M- Burry started business on 1 March 2022- During the month of March 2.pdf
M- Burry started business on 1 March 2022- During the month of March 2.pdfM- Burry started business on 1 March 2022- During the month of March 2.pdf
M- Burry started business on 1 March 2022- During the month of March 2.pdfPierschdJohnstonb
 
M- Oriole Corporation has 10-000 shares of 9--$100 par value- cumulati.pdf
M- Oriole Corporation has 10-000 shares of 9--$100 par value- cumulati.pdfM- Oriole Corporation has 10-000 shares of 9--$100 par value- cumulati.pdf
M- Oriole Corporation has 10-000 shares of 9--$100 par value- cumulati.pdfPierschdJohnstonb
 
Lonnie Shark has ensembled a money laundering team that travels around.pdf
Lonnie Shark has ensembled a money laundering team that travels around.pdfLonnie Shark has ensembled a money laundering team that travels around.pdf
Lonnie Shark has ensembled a money laundering team that travels around.pdfPierschdJohnstonb
 
Louis and Pearl live in a one-story single-family dwelling located in.pdf
Louis and Pearl live in a one-story single-family dwelling located in.pdfLouis and Pearl live in a one-story single-family dwelling located in.pdf
Louis and Pearl live in a one-story single-family dwelling located in.pdfPierschdJohnstonb
 

Más de PierschdJohnstonb (20)

Match chemical structures of the following nucleotides (designated A-E.pdf
Match chemical structures of the following nucleotides (designated A-E.pdfMatch chemical structures of the following nucleotides (designated A-E.pdf
Match chemical structures of the following nucleotides (designated A-E.pdf
 
Match each bulleted item on the left with ALL of the items on the righ.pdf
Match each bulleted item on the left with ALL of the items on the righ.pdfMatch each bulleted item on the left with ALL of the items on the righ.pdf
Match each bulleted item on the left with ALL of the items on the righ.pdf
 
Masego Hadebe- a South African resident- passed away on 14 February 20.pdf
Masego Hadebe- a South African resident- passed away on 14 February 20.pdfMasego Hadebe- a South African resident- passed away on 14 February 20.pdf
Masego Hadebe- a South African resident- passed away on 14 February 20.pdf
 
Mary has an IV solution of normal saline infusing in her left arm and.pdf
Mary has an IV solution of normal saline infusing in her left arm and.pdfMary has an IV solution of normal saline infusing in her left arm and.pdf
Mary has an IV solution of normal saline infusing in her left arm and.pdf
 
Mary Beth Maris- the manager of an apartment complex- feots overwheime.pdf
Mary Beth Maris- the manager of an apartment complex- feots overwheime.pdfMary Beth Maris- the manager of an apartment complex- feots overwheime.pdf
Mary Beth Maris- the manager of an apartment complex- feots overwheime.pdf
 
Martin Corporation has the following amounts as of December 31-2018- (.pdf
Martin Corporation has the following amounts as of December 31-2018- (.pdfMartin Corporation has the following amounts as of December 31-2018- (.pdf
Martin Corporation has the following amounts as of December 31-2018- (.pdf
 
Marina Andros works in the Financials Department of Eastern Mobile- a.pdf
Marina Andros works in the Financials Department of Eastern Mobile- a.pdfMarina Andros works in the Financials Department of Eastern Mobile- a.pdf
Marina Andros works in the Financials Department of Eastern Mobile- a.pdf
 
Mark (50) and Phoebe (30)- Cristina (45) and Michael (49)- and Jason (.pdf
Mark (50) and Phoebe (30)- Cristina (45) and Michael (49)- and Jason (.pdfMark (50) and Phoebe (30)- Cristina (45) and Michael (49)- and Jason (.pdf
Mark (50) and Phoebe (30)- Cristina (45) and Michael (49)- and Jason (.pdf
 
Marin Company's income statement for the year ended December 31- 2025-.pdf
Marin Company's income statement for the year ended December 31- 2025-.pdfMarin Company's income statement for the year ended December 31- 2025-.pdf
Marin Company's income statement for the year ended December 31- 2025-.pdf
 
Many people think the most widely used tool in a construction project.pdf
Many people think the most widely used tool in a construction project.pdfMany people think the most widely used tool in a construction project.pdf
Many people think the most widely used tool in a construction project.pdf
 
Many aspects of aquaculture are regulated by federal legislation- Some.pdf
Many aspects of aquaculture are regulated by federal legislation- Some.pdfMany aspects of aquaculture are regulated by federal legislation- Some.pdf
Many aspects of aquaculture are regulated by federal legislation- Some.pdf
 
Map this ERR model into a relational model visually- The investment co.pdf
Map this ERR model into a relational model visually- The investment co.pdfMap this ERR model into a relational model visually- The investment co.pdf
Map this ERR model into a relational model visually- The investment co.pdf
 
Many COVID-19 patients who die despite being intubated and put on a ve.pdf
Many COVID-19 patients who die despite being intubated and put on a ve.pdfMany COVID-19 patients who die despite being intubated and put on a ve.pdf
Many COVID-19 patients who die despite being intubated and put on a ve.pdf
 
Many of the regulations you have read about this week were developed f.pdf
Many of the regulations you have read about this week were developed f.pdfMany of the regulations you have read about this week were developed f.pdf
Many of the regulations you have read about this week were developed f.pdf
 
Managers may engage in classification shifting by- Multiple Choice rep.pdf
Managers may engage in classification shifting by- Multiple Choice rep.pdfManagers may engage in classification shifting by- Multiple Choice rep.pdf
Managers may engage in classification shifting by- Multiple Choice rep.pdf
 
Make a Gantt Chart from the activities and tasks - Case Descriptio.pdf
Make a Gantt Chart  from the activities and tasks -    Case Descriptio.pdfMake a Gantt Chart  from the activities and tasks -    Case Descriptio.pdf
Make a Gantt Chart from the activities and tasks - Case Descriptio.pdf
 
M- Burry started business on 1 March 2022- During the month of March 2.pdf
M- Burry started business on 1 March 2022- During the month of March 2.pdfM- Burry started business on 1 March 2022- During the month of March 2.pdf
M- Burry started business on 1 March 2022- During the month of March 2.pdf
 
M- Oriole Corporation has 10-000 shares of 9--$100 par value- cumulati.pdf
M- Oriole Corporation has 10-000 shares of 9--$100 par value- cumulati.pdfM- Oriole Corporation has 10-000 shares of 9--$100 par value- cumulati.pdf
M- Oriole Corporation has 10-000 shares of 9--$100 par value- cumulati.pdf
 
Lonnie Shark has ensembled a money laundering team that travels around.pdf
Lonnie Shark has ensembled a money laundering team that travels around.pdfLonnie Shark has ensembled a money laundering team that travels around.pdf
Lonnie Shark has ensembled a money laundering team that travels around.pdf
 
Louis and Pearl live in a one-story single-family dwelling located in.pdf
Louis and Pearl live in a one-story single-family dwelling located in.pdfLouis and Pearl live in a one-story single-family dwelling located in.pdf
Louis and Pearl live in a one-story single-family dwelling located in.pdf
 

Último

18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 

Último (20)

18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
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
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 

CS280 Lexical Analyzer Header

  • 1. Lex.h /* * lex.h * * CS280 * Spring 2023 */ #ifndef LEX_H_ #define LEX_H_ #include <string> #include <iostream> #include <map> using namespace std; //Definition of all the possible token types enum Token { // keywords WRITELN, IF, ELSE, // identifiers IDENT, NIDENT, SIDENT, // an integer, real, and string constant ICONST, RCONST, SCONST, // the numeric operators, assignment, numeric and string comparison operators PLUS, MINUS, MULT, DIV, EXPONENT, ASSOP, NEQ, NGTHAN, NLTHAN, CAT, SREPEAT, SEQ, SLTHAN, SGTHAN, //Delimiters COMMA, SEMICOL, LPAREN, RPAREN, LBRACES, RBRACES, // any error returns this token ERR, // when completed (EOF), return this token DONE, }; //Class definition of LexItem class LexItem { Token token;
  • 2. string lexeme; int lnum; public: LexItem() { token = ERR; lnum = -1; } LexItem(Token token, string lexeme, int line) { this->token = token; this->lexeme = lexeme; this->lnum = line; } bool operator==(const Token token) const { return this->token == token; } bool operator!=(const Token token) const { return this->token != token; } Token GetToken() const { return token; } string GetLexeme() const { return lexeme; } int GetLinenum() const { return lnum; } }; extern ostream& operator<<(ostream& out, const LexItem& tok); extern LexItem id_or_kw(const string& lexeme, int linenum); extern LexItem getNextToken(istream& in, int& linenum); #endif /* LEX_H_ */ tokensListing.cpp #include "lex.h" extern ostream operator <<(ostream & out, const tok & tok) { out<< "Token: "<< tok.GetToken( )<< "lex:" << tok.Getlexeme( )<< "Line No: " <<tok.linenum( ); } #ifndef LEX_H_ #define LEX_H_ #include<string> #include<iostream>
  • 3. using std:: string; using std:: istream; using std:: ostream; Enum Token { //Keywords PRINT,PRINTLN, REPEAT, BEGIN, END // an identifier INDENT, // an Integer and String constant ICONST, SCONST, //Operators, Parens, semicolon PLUS, MINUS, STAR, SLASH, EQ, LPAREN, RPAREN, SC, // any error returns this token ERR, //When completed (EDF), return this token DONE }; Class Tok { Token token; String lexeme; int inum; public Tok( ){ token = ERR; lnum = -1; } Token( Token token, string lexeme, int line){ this -> token = token; this -> lexeme = lexeme; this -> lnum = line; }
  • 4. bool operator == (const Token token) const { return this -> token == token; } bool operator != (const Token token) const { return this -> token == token; } Token GetToken ( ) const { return token; } String Getlexeme( ) const { return lexeme;} int GetLinenum( ) const { return lnum;} }; extern ostream &operator << (ostream &out, const Tok &tok); extern Tok getNextToken (istream&in, int & line num); #endif /*LEX_H_*/ #include 'lex.h' int main( ) { Tok toks[ ] = { TOK(PRINT, "PRINT", 3); TOK(PRINTLN, "PRINTLN",3); TOK(REPEAT, "REPEAT", 3); TOK(BEGIN, "BEGIN",3); TOK(END, "END", 3); TOK(IDENT, "foo", 3); TOK(CONST,"347",3); TOK(SCONST, "Hello world", 3); TOK(PLUS, " +" , 3); TOK(MINUS, "-", 3); TOK(STAR, "*",3); TOK(SLASH, "/",3); TOK(EQ, "=",3); TOK(LPAREN, "(",3); TOK(RPAREN, ")", 3); TOK(SC, ":",3); TOK(DONE,"DONE",3); }; for (int i = 0; toks[i]! = DONE; i++) cout<< toks[i] <<endl; return 0; } Case 1 input IDENT PROGRAM 0 IDENT $PROG1 3 IDENT INT 2
  • 5. IDENT _X1 2 COMMA , 2 IDENT @Y_1 2 COMMA , 2 IDENT Z 2 MINUS - 2 COMMA , 2 IDENT W234 2 SEMICOL ; 2 IDENT FLOAT 3 IDENT Z 3 ASSOP = 3 RCONST 0.75 3 SEMICOL ; 3 IDENT $BOOL 4 IDENT @FLAG 4 ASSOP = 4 SEMICOL ; 4 IDENT R 6 ERR $ 6 IDENT FLAG 7 ASSOP = 7 SEMICOL ; 7 IDENT IF 8 LPAREN ( 8 IDENT FLAG 8 RPAREN ) 8 IDENT THEN 8 IDENT Y_1 9 ASSOP = 9 ICONST 5 9 SEMICOL ; 9 IDENT ELSE 10 IDENT Y_1 11 ASSOP = 11 RCONST 7.5 11 SEMICOL ; 11 IDENT END 12 IDENT IF 12 IDENT PRINT 13 SCONST 8 "Value = " 13 COMMA , 13 IDENT R 13 COMMA , 13 SCONST 4 "z = " 13 COMMA , 13
  • 6. IDENT Z 13 SEMICOL ; 13 IDENT END 14 IDENT PROGRAM 14 Case 1 output IDENTIFIER: PROGRAM at Line 0 NIDENT: "$PROG1" at Line 3 IDENTIFIER: INT at Line 2 IDENTIFIER: _X1 at Line 2 COMMA: "," at Line 2 SIDENT: "@Y_1" at Line 2 COMMA: "," at Line 2 IDENTIFIER: Z at Line 2 MINUS: "-" at Line 2 COMMA: "," at Line 2 IDENTIFIER: W234 at Line 2 SEMICOL: ";" at Line 2 IDENTIFIER: FLOAT at Line 3 IDENTIFIER: Z at Line 3 ASSOP: "=" at Line 3 RCONST: (0.75) at Line 3 SEMICOL: ";" at Line 3 NIDENT: "$BOOL" at Line 4 SIDENT: "@FLAG" at Line 4 ASSOP: "=" at Line 4 SEMICOL: ";" at Line 4 IDENTIFIER: R at Line 6 Error: : "$" at Line 6 IDENTIFIER: FLAG at Line 7 ASSOP: "=" at Line 7 SEMICOL: ";" at Line 7 IF: "IF" at Line 8 LPAREN: "(" at Line 8 IDENTIFIER: FLAG at Line 8 RPAREN: ")" at Line 8 IDENTIFIER: THEN at Line 8 IDENTIFIER: Y_1 at Line 9 ASSOP: "=" at Line 9 ICONST: (5) at Line 9 SEMICOL: ";" at Line 9 ELSE: "ELSE" at Line 10 IDENTIFIER: Y_1 at Line 11 ASSOP: "=" at Line 11 RCONST: (7.5) at Line 11
  • 7. SEMICOL: ";" at Line 11 IDENTIFIER: END at Line 12 IF: "IF" at Line 12 IDENTIFIER: PRINT at Line 13 SCONST: 'Value = ' at Line 13 COMMA: "," at Line 13 IDENTIFIER: R at Line 13 COMMA: "," at Line 13 SCONST: 'z = ' at Line 13 COMMA: "," at Line 13 IDENTIFIER: Z at Line 13 SEMICOL: ";" at Line 13 IDENTIFIER: END at Line 14 IDENTIFIER: PROGRAM at Line 14