Skip to content

Instantly share code, notes, and snippets.

@HarshVardhanKumar
Created September 10, 2018 19:04
Show Gist options
  • Save HarshVardhanKumar/5c9f67d5cab04fee902a19082b5c1487 to your computer and use it in GitHub Desktop.
Save HarshVardhanKumar/5c9f67d5cab04fee902a19082b5c1487 to your computer and use it in GitHub Desktop.
//
// Created by Harsh Vardhan Kumar on 10-09-2018.
//
#include <iostream>
#include <unordered_map>
#include <string>
using namespace std ;
unordered_map <string, int> keywords ;
unordered_map <string, string> output ;
string finalReplacedSequenceString = "" ;
void initializeSetOfKeywords() {
keywords["if"] = 1 ;
keywords["If"] = 1 ;
keywords["else"] = 1 ;
keywords["Else"] = 1 ;
keywords["then"] = 1 ;
keywords["Then"] = 1 ;
keywords["return"] = 1 ;
keywords["break"] = 1 ;
keywords["while"] = 1 ;
keywords["using"] = 1 ;
keywords["void"] = 1 ;
keywords["switch"] = 1 ;
keywords["for"] = 1 ;
}
bool isUnderscore(char c) {
return c == '_' ;
}
bool isLetter(char c) {
return isUnderscore(c) || (c>='A' && c<='z' && c!=' ' && c!='\t' && c!='\n') ;
}
bool isWhiteSpace(char c) {
return c==' ' || c=='\t' || c== '\n' ;
}
bool isDigit(char c) {
return c>='0' && c<='9' ;
}
bool isKeyword(string s) {
return (bool) keywords[s] ;
}
bool isOperator (char c) {
return (c=='=' || c=='<' || c=='>' || c=='+' || c=='-' || c=='*' || c=='/' || c== '(' || c==')') ;
}
void printResults() {
for(pair<string, string> p : output) {
printf("%-15s %10s\n", (p.first).c_str(), p.second.c_str()) ;
}
cout<<endl<<finalReplacedSequenceString<<endl<<endl;
finalReplacedSequenceString = "" ;
}
int main() {
initializeSetOfKeywords() ;
int state = 0 ; // start state
string st = "" ;
char c ;
while(1) {
if(c == '~') { printResults() ;break;}
switch(state) {
case 0:
c = getchar() ;
if(isLetter(c)) {
st += string(1,c) ;
state = 1 ;
}
else if (isOperator(c) || isOperator(st.front())) {
state = 3 ;
st += string(1,c) ;
}
break ;
case 1:
c = getchar() ;
if(isLetter(c) || isDigit(c)) {
st += string(1,c);
state = 1 ;
}
else state = 2 ;
break;
case 2:
// identifier is identified. Check for keywords.
if(isKeyword(st)) {
output[st] = "Keyword" ;
finalReplacedSequenceString += st+" " ;
}
else {
output[st] = "ID" ;
finalReplacedSequenceString += "ID " ;
}
if(isOperator(c)) {state = 3 ; st = string(1,c) ;}
else {state = 0 ; st = "" ;}
break;
case 3:
c = getchar() ;
if(!isOperator(c)) {
// right now, st is an operator.
output[st] = "Operator" ;
finalReplacedSequenceString += st+" " ;
if(c == ' ' || c== '\t' || c=='\n') {
state = 0 ;
st = "" ;
}
else {state = 1; st = string(1,c) ;}
}
else if (c == '=') {
state = 6 ;
st += string(1,c) ;
}
else if (c == ')' && st == "(") {state = 5; st += string(1,c);}
else if (c=='+') {state = 7; st += string(1,c);}
else if (c=='-') {state = 8; st += string(1,c);}
break ;
case 5:
// accepted "()"
c = getchar() ;
if(isOperator(c)) {
// certainly, this is an error. It is not an operator sequence.
st += string(1,c) ;
state = 1 ;
}
else {
finalReplacedSequenceString += "() " ;
output[st] = "Operator" ;
st = "" ;
st += string(1,c) ;
state = 1 ;
}
break ;
case 6:
c = getchar() ;
// valid st values are *= == /= += -= >= <=
if(isOperator(c)) {
// certainly, this is an error. It is not an operator sequence.
}
else {
if(st == "*=" || st == "==" || st == "/=" || st == "+=" || st == "-=" || st == ">=" || st == "<=") {
output[st] = "Operator";
finalReplacedSequenceString += st +" " ;
st = "" ;
}
}
st += string(1,c) ;
state = 1 ;
break ;
case 7:
c = getchar() ;
// valid st value is ++
if(isOperator(c)) {
// certainly, this is an error. It is not an operator sequence.
}
else {
if (st == "++") {
output[st] = "Operator";
finalReplacedSequenceString += "++ " ;
st = "" ;
}
}
st += string (1,c) ;
state = 1 ;
break ;
case 8:
c = getchar() ;
// valid st value is --
if(isOperator(c)) {
// certainly, this is an error. It is not an operator sequence.
}
else {
if (st == "--") {
output[st] = "Operator";
finalReplacedSequenceString += "-- " ;
st = "" ;
}
}
st += string (1,c) ;
state = 1 ;
break ;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment