1 / 18

Our first C++ Class: a Lexer

Our first C++ Class: a Lexer. Constructors struct Function pointers map. Agenda. A toy program and its design Lexical analysis Lexer: interface and implementation Function pointers and map. Parsing command lines Thinking about the main body. A toy program and its design. A toy program.

jasonklein
Download Presentation

Our first C++ Class: a Lexer

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Our first C++ Class: a Lexer Constructors struct Function pointers map

  2. Agenda • A toy program and its design • Lexical analysis • Lexer: interface and implementation • Function pointers and map CSE 250, Fall 2012, SUNY Buffalo, (C) Hung Q. Ngo

  3. Parsing command lines Thinking about the main body A toy program and its design CSE 250, Fall 2012, SUNY Buffalo, (C) Hung Q. Ngo

  4. A toy program > exit > quit > bye > foreground [red|green|blue] “Print this string” > fc <file1> <file2> … <filek> How do we design such a program? CSE 250, Fall 2012, SUNY Buffalo, (C) Hung Q. Ngo

  5. A “Good” Design • The skeleton should be clean, easy to read • Easy to add codes to accommodate changes in requirements • Add “yellow”, “cyan” to the color list • Add bf, mp … to the command list CSE 250, Fall 2012, SUNY Buffalo, (C) Hung Q. Ngo

  6. A Skeleton of a “Good” Design • While (there is more to read) • Read an input line • Break the line into tokens> foregroundgreen “Print this string in green”> wc “blowing in the wind.txt” • Use an “array” indexed by strings to call functions • If the command name is not found, say “Command not found” CSE 250, Fall 2012, SUNY Buffalo, (C) Hung Q. Ngo

  7. Main questions • Is there a systematic way to break a string into tokens? • Lexical analysis • How do we define/use an “array” indexed by strings? • map • How do we define/use functions as data types? • function pointers CSE 250, Fall 2012, SUNY Buffalo, (C) Hung Q. Ngo

  8. Tokens Token scanning Lexer Lexical analysis CSE 250, Fall 2012, SUNY Buffalo, (C) Hung Q. Ngo

  9. Tokens bf“pattern here” file.txt STRING IDENTIFIER IDENTIFIER DELIMITER DELIMITER CSE 250, Fall 2012, SUNY Buffalo, (C) Hung Q. Ngo

  10. A class has public and private components The constructor method is called when an object of the class is created A struct is a class whose members are public by default Initialization list is a more efficient way to do assignment in the constructor Our first C++ class: Lexer CSE 250, Fall 2012, SUNY Buffalo, (C) Hung Q. Ngo

  11. How a Lexer class might work? // lextest.cpp: a simple driver for the the Lexer class#include <iostream>#include "Lexer.h"usingnamespacestd;int main(){Lexerlexer; Token tok;string line;while(cin){cout<<"> ";getline(cin, line);lexer.set_input(line);while(lexer.has_more_token()){tok=lexer.next_token();if(tok.type==IDENT)cout<<"IDENT: "<<tok.value<<endl;if(tok.type==STRING)cout<<"STR:   "<<tok.value<<endl;if(tok.type==ERRTOK)cout<<"Don't drink and type"<<endl;}}return0;} CSE 250, Fall 2012, SUNY Buffalo, (C) Hung Q. Ngo

  12. Defining the Lexer’s interface: Lexer.h classLexer { public: // constructor Lexer(std::string str="") : input_str(str), cur_pos(0), in_err(false), separators(" \t\n\r") { } // a couple of modifiers void set_input(std::string); // set a new input, void restart(); // move cursor to the beginning, restart Token next_token(); // returns the next token bool has_more_token(); // are there more token(s)? private: std::string input_str; // the input string to be scanned size_t cur_pos; // current position in the input string boolin_err; // are we in the error state? std::string separators; // set of separators; *not* the best option! }; CSE 250, Fall 2012, SUNY Buffalo, (C) Hung Q. Ngo

  13. Constructor • Called when an object is created • string str; • // the “default constructor” is called • string str(“David Blaine”); • // another constructor • Lexerlexer(“String to be parsed”); CSE 250, Fall 2012, SUNY Buffalo, (C) Hung Q. Ngo

  14. Other types in Lexer.h enumtoken_types_t { IDENT, // a sequence of non-space characters, 1st != ‘”’ STRING, // sequence of characters between " ", no escape ENDTOK, // end of string/file, no more token ERRTOK // unrecognized token }; struct Token { token_types_t type; std::string value; // constructor for Token Token(token_types_ttt=ENDTOK, std::string val="") : type(tt), value(val) {} }; Struct is a class all of whose members are public by default CSE 250, Fall 2012, SUNY Buffalo, (C) Hung Q. Ngo

  15. Organizing Source Files Lexer.h Term_control.h #include #include #include program.cpp Lexer.cpp Term_control.cpp CSE 250, Fall 2012, SUNY Buffalo, (C) Hung Q. Ngo

  16. Defining Function pointers Map from string to function pointers Function pointers and map CSE 250, Fall 2012, SUNY Buffalo, (C) Hung Q. Ngo

  17. Defining function pointers #include <iostream> using std::cout; using std::endl; int add(int x, int y) { return x+y; } int sub(int x, int y) { return x-y; } int main() { // define fp to be a pointer to a function which takes // two int parameters and returns an int int (*fp)(int, int); fp = &add; cout << fp(6,3) << endl; // get 9 fp = &sub; cout << fp(6,3) << endl; // get 3 return0; } CSE 250, Fall 2012, SUNY Buffalo, (C) Hung Q. Ngo

  18. Mapping strings to function pointers typedefvoid (*cmd_handler_t)(Lexer); voidprint_color(Lexer); void bye(Lexer); . . . map<string, cmd_handler_t> cmd_map; // simply add all commands to the map cmd_map["foreground"] = &color_print; cmd_map["exit"] = &bye; cmd_map["bye"] = &bye; cmd_map["quit"] = &bye; CSE 250, Fall 2012, SUNY Buffalo, (C) Hung Q. Ngo

More Related