SlideShare uma empresa Scribd logo
1 de 7
Baixar para ler offline
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

Mais conteúdo relacionado

Semelhante a Lex-h -- - lex-h - - CS280 - Spring 2023 -- #ifndef LEX_H_ #define L.pdf

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
wkyra78
 
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
amarnathmahajansport
 
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
albert20021
 
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
abdulkadar1977
 
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
rahulfancycorner21
 
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
adityaenterprise32
 
C++ Advanced
C++ AdvancedC++ Advanced
C++ Advanced
Vivek Das
 
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
MARRY7
 
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
RAHUL126667
 
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
fazalenterprises
 

Semelhante a Lex-h -- - lex-h - - CS280 - Spring 2023 -- #ifndef LEX_H_ #define L.pdf (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
 

Mais de PierschdJohnstonb

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
PierschdJohnstonb
 
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
PierschdJohnstonb
 
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
PierschdJohnstonb
 

Mais 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

Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 

Último (20)

Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
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
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptx
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 

Lex-h -- - lex-h - - CS280 - Spring 2023 -- #ifndef LEX_H_ #define L.pdf

  • 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