Website Summary Files Commits Branches @commit @diff @stats
path: root / homework / 81568 / c2html / lexcer.h
diff options
context:
space:
mode:
Commit06811744a002f4ab332d94d510475231afcd45a1 (patch)
Author
 Mincho Paskalev <minchopaskal@gmail.com>
2018-11-03 13:36:57 +0200
Committer
 Martin Dobrev <martindobrev0@gmail.com>
2018-11-03 13:36:57 +0200
Files91c41776fbdf899dcb61a8d5df3ac1316c24f3c8 /homework/81568/c2html/lexcer.h
IPL-06811744a002f4ab332d94d510475231afcd45a1.tar
IPL-06811744a002f4ab332d94d510475231afcd45a1.tar.gz
IPL-06811744a002f4ab332d94d510475231afcd45a1.zip
Parent26e5f891d537b5d4fc2890c9cdbeb73d5748d568 (diff)
First HW (#9)
* Add 1st hw * --amend * moving readme for 1st hw to designated folder * renamed TokenType enum to TokenClass * fixed character constant lexing. added sampe output file * add new line at end-of-file. added gitignore * a bit of refactoring
1 files changed, 84 insertions, 0 deletions
-rw-r--r--homework/81568/c2html/lexcer.h84
diff --git a/homework/81568/c2html/lexcer.h b/homework/81568/c2html/lexcer.h
new file mode 100644
index 0000000..e329a90
--- /dev/null
+++ b/homework/81568/c2html/lexcer.h
@@ -0,0 +1,84 @@
+#ifndef FMI_IPL_LEXCER_H
+#define FMI_IPL_LEXCER_H
+
+#include <unordered_set>
+#include <unordered_map>
+#include <string>
+
+#include <cstdio>
+
+namespace LexCer {
+
+enum class TokenClass : short {
+ Operator,
+ Keyword,
+ PreprocessorDirective,
+ Typename,
+ Identifier,
+ String,
+ Comment,
+ Number,
+ LeftParen,
+ RightParen,
+
+ Skip,
+ Eof,
+ Invalid
+};
+
+struct Token {
+ TokenClass type;
+ unsigned line;
+ std::string lexeme;
+ double number;
+};
+
+
+class Tokenizer {
+private:
+ enum num_t {
+ BIN = 2,
+ OCT = 8,
+ DEC = 10,
+ HEX = 16
+ };
+
+public:
+ Tokenizer(FILE *in, FILE *out);
+
+ unsigned lineNumber() const { return line; }
+ Token currentToken() const { return token; }
+
+ TokenClass nextToken(std::string *lexeme, double *number);
+ void makeToken(TokenClass type, unsigned line,
+ std::string lexeme = std::string(), double number = 0.0);
+ bool writeToOutput();
+
+private:
+ std::string escapeHTML(const std::string &lexeme);
+ bool readNextDirective(std::string *lexeme);
+ bool readNextString(std::string *lexeme, char oldc);
+ bool readNextIdentifier(std::string *lexeme);
+ bool readNextNumber(char beg, double *number, std::string *numberRepr);
+ bool isValidDigit(char c, num_t numType) const ;
+ bool readLineComment(std::string *lexeme);
+ bool readMultilineComment(std::string *lexeme);
+ bool readInclude(std::string *lexeme);
+ bool readUntilBlank(std::string *lexeme);
+
+ double atod(const std::string &number) const { return 0.0; }
+private:
+ FILE *in;
+ FILE *out;
+ Token token; // current token
+ unsigned line = 1;
+ std::unordered_set<std::string> typenames;
+};
+
+
+std::string getOutputFileName(const std::string &name);
+void lexCit(FILE *in, FILE *out);
+
+} /* Namespace LexCer */
+
+#endif