Add another example

This commit is contained in:
Robin Jadoul 2016-05-28 14:40:52 +02:00
parent 733dfffe75
commit 27540745fe
2 changed files with 43 additions and 0 deletions

View File

@ -0,0 +1,6 @@
MODULE = (M|m)odule
END = [Ee]nd
IF = if|If
IDENT = [a-zA-Z_][a-zA-Z_0-9]*
ignore = \n| |\t
UNKNOWN = .

View File

@ -0,0 +1,37 @@
#include "KeywordsLexer.h"
#include <iostream>
int main() {
KeywordsLexer lex(std::cin);
try {
while (true) {
KeywordsLexer::Token tok = lex.nextToken();
switch (tok.type) {
case KeywordsLexer::MODULE:
std::cout << "MODULE: ";
break;
case KeywordsLexer::END:
std::cout << "END: ";
break;
case KeywordsLexer::IF:
std::cout << "IF: ";
break;
case KeywordsLexer::IDENT:
std::cout << "IDENT: ";
break;
case KeywordsLexer::UNKNOWN:
std::cout << "UNKNOWN: ";
break;
case KeywordsLexer::nonmatching: case KeywordsLexer::ignore:
break; //These can never occur, just to satisfy the compiler...
}
std::cout << "\"" << tok.content << "\"" << std::endl;
}
}
catch (KeywordsLexer::NoMatch& err) {
std::cout << "'" << lex.peek() << "'" << " did not match" << std::endl;
}
catch (KeywordsLexer::NoMoreTokens& err) {
std::cout << "DONE" << std::endl;
}
}