Mua Lexical Analyzer
#include <iostream>
#include <vector>
using namespace std;
class Error{
string error;
public:
Error(string _e="Unknown Error") : error(_e) {}
void print() { cout<<"Error: "<<error<<endl; }
};
enum ETokenType{
RESERVED, NUMBER, STRING, SYMBOL, NAME, EOL
};
class CToken{
public:
ETokenType type;
string token;
CToken(ETokenType _type=EOL,string _token="") : type(_type), token(_token) {}
};
vector<CToken> token_list;
//and,break,do,else,elseif,end,false,for,function,if,in,local,nil,not,or,repeat,return,then,true,until,while
const string reserved_word_list[]={
"and", "break", "do", "else", "elseif", "end", "false",
"for", "function", "if", "in", "local", "nil", "not",
"or", "repeat", "return", "then", "true", "until", "while"
};
const int reserved_word_list_cnt=21;
bool IsReservedWord(string s)
{
for(int i=0;i<reserved_word_list_cnt;i++){
if(s==reserved_word_list[i]) return true;
}
return false;
}
//+ - * / % ^ # == >= <= > < ~= ( ) { } [ ] ; : , . .. ... =
const string symbol_list[]={
"+", "-", "*", "/", "%", "^", "#", "==", ">=", "<=",
">", "<", "~=", "(", ")", "{", "}", "[", "]", ";",
":", ",", ".", "..", "...", "=", "--"
};
const int symbol_list_cnt=27;
bool IsSymbol(string s)
{
for(int i=0;i<symbol_list_cnt;i++){
if(s==symbol_list[i]) return true;
}
return false;
}
void ParseInputFile()
{
token_list.clear();
string str;
while(getline(cin,str)){
int p=0;
while(p<str.size()){
string s="";
int cnt=0;
while(str[p]==' '){
p++;
}
if( isdigit(str[p]) || ( str[p]=='.' && p<str.size()-1 && isdigit(str[p+1]) ) ){ //str[p] is a number
s+=str[p];
p++;
cnt++;
bool has_dot=false;
bool has_e=false;
bool has_x=false;
if(s[0]=='.'){
has_dot=true;
}
while(p<str.size()){
char ch=str[p];
if(ch=='.'){
if(has_dot){
throw Error("two dots for a number");
}
has_dot=true;
}
else if(ch=='e'){
if(has_e){
throw Error("two e's for a number");
}
has_e=true;
}
else if(ch=='x'){
if(has_x){
throw Error("two x's for a number");
}
if(has_dot){
throw Error("floating point for hex numbers is not allowed");
}
if(cnt!=1 || s[0]!='0'){
throw Error("illegal x in number");
}
has_x=true;
}
else if(ch=='+' || ch=='-'){
if(s[cnt-1]!='e') break;
}
else if(isdigit(ch)){
;
}
else if( ('a'<=ch && ch<='f') || ('A'<=ch && ch<='F') ){
if(!has_x){
throw Error("illegal letter in un-hex number");
}
}
else{
break;
}
s+=ch;
p++;
cnt++;
}
token_list.push_back(CToken(NUMBER,s));
}
else if(isalpha(str[p])){
s+=str[p];
p++;
cnt++;
while(p<str.size()){
char ch=str[p];
if(isalpha(ch)||isdigit(ch)||ch=='_'){
;
}
else{
break;
}
s+=ch;
p++;
cnt++;
}
if(IsReservedWord(s)){
token_list.push_back(CToken(RESERVED,s));
}
else{
token_list.push_back(CToken(NAME,s));
}
}
else if(str[p]=='\'' || str[p]=='\"'){
char st=str[p];
bool ended=false;
s+=str[p];
p++;
cnt++;
bool slash=false;
while(p<str.size()){
char ch=str[p];
s+=ch;
p++;
cnt++;
if(ch==st && !slash){
ended=true;
break;
}
else if(ch=='\\'){
slash=true;
}
else{
slash=false;
}
}
if(!ended){
throw Error("unended string");
}
token_list.push_back(CToken(STRING,s));
}
else{
s+=str[p];
p++;
cnt++;
while(p<str.size()){
char ch=str[p];
if(IsSymbol(s+ch)){
s+=ch;
p++;
cnt++;
}
else{
break;
}
}
if(s!="--"){
token_list.push_back(CToken(SYMBOL,s));
}
else{
break;
}
}
}
token_list.push_back(CToken(EOL,""));
}
}
int main()
{
try{
ParseInputFile();
}
catch(Error error){
error.print();
// throw;
}
for(int i=0;i<token_list.size();i++){
CToken t=token_list[i];
if(t.type==RESERVED){
printf("[RESERVED] %s\n",t.token.c_str());
}
else if(t.type==NUMBER){
printf("[NUMBER] %s\n",t.token.c_str());
}
else if(t.type==STRING){
printf("[STRING] %s\n",t.token.c_str());
}
else if(t.type==SYMBOL){
printf("[SYMBOL] %s\n",t.token.c_str());
}
else if(t.type==NAME){
printf("[NAME] %s\n",t.token.c_str());
}
else{
printf("[EOL]\n");
}
}
return 0;
}