Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <vector>
- #include <sstream>
- #include <stdio.h>
- #include <string.h>
- #include <regex>
- #include <locale>
- #include <ctype.h>
- using namespace std;
- void readFile(string x);
- bool spaceCheck(string x);
- bool menmonicCheck(string testString);
- void parser2();
- void stepIncrement();
- bool validate_Label2();
- bool validate_operand(string testString);
- vector<string> myList;
- vector<string> label;
- vector<string> menmonics;
- vector<string> operand;
- vector<string> comment;
- vector<int> step;
- vector<int> syntaxError;
- vector<int> labelError;
- bool findFileFlag=true;
- vector<string> menmonicsData;
- int main () {
- readFile("L3_A");
- for(int unsigned i=0;i<myList.size();i++)
- {bool x=spaceCheck(myList.at(i));
- if(x==false){
- cout<< "syntax error"<<'\n';
- syntaxError.push_back(i);
- }
- }
- parser2();
- // for(int unsigned i=0;i<menmonics.size();i++)
- // {
- // if(i>menmonics.size()){
- // break;
- // }
- // if(menmonicCheck(menmonics.at(i))==false){
- // cout << "menmonic error"<<'\n';
- // cout<<menmonics.at(i)<<'\n' ;
- // return 0;
- // }
- //
- // }
- // for(int unsigned i=0 ; i<operand.size(); i++){
- // if(validate_operand(operand.at(i))==false){
- // cout << "operand error"<<'\n';
- // cout<<menmonics.at(i)+" "<<operand.at(i)<<i<<endl ;
- // return 0;
- // }
- //
- // }
- bool val = validate_Label2();
- //stepIncrement();
- for(int unsigned i=0;i<menmonics.size();i++)
- {
- cout<<label.at(i);
- cout<<" " ;
- cout<<menmonics.at(i);
- cout<<" " ;
- cout<<operand.at(i);
- cout<<" " ;
- cout<<comment.at(i);
- cout<<" " ;
- // cout<<step.at(i);
- cout<<" "<<endl;
- }
- //
- return 0;
- }
- //**********************************************************file reader****************************************************
- void readFile(string filename){
- string line;
- ifstream myfile (filename+".txt");
- if (myfile.is_open())
- {
- while ( getline (myfile,line) )
- {
- for(unsigned int l = 0; l < line.length(); l++)
- {
- line[l] = toupper(line[l]);
- }
- myList.push_back(line);
- cout << line << '\n';
- }
- myfile.close();
- }
- else{
- findFileFlag=false;
- cout << "Unable to open file";
- }
- // string line2;
- //
- // ifstream myfile2 ("menmonics.txt");
- // if (myfile2.is_open())
- // {
- // while ( getline (myfile2,line2) )
- // {
- // menmonicsData.push_back(line2);
- //
- // }
- // myfile2.close();
- // }
- //
- // else{
- //
- // cout << "Unable to open file";
- // }
- }
- //***********************************************************space checker***********************************
- bool spaceCheck(string testString){
- regex e("(........ ...... .{0,48})|(........ ......)|(\\.(.*))|(.*)");
- //regex e("\\s*(([A-Z](\\w*|\\d*))|\\s{8})\\s([A-Z]+)\\s{2}(\\d{1,18}|(\\d{1,16},X)|(@\\d{1,17})|(#\\d{1,17})|())");
- int length=testString.length();
- bool match=regex_match(testString,e);
- if(length>66){
- match=false;
- }
- return match;
- }
- //*************************************************************check menmonic *****************************************
- //bool menmonicCheck(string testString){
- //
- // // return menmonics;
- // //check for presence of menmonic in database
- // for(int unsigned i=0;i<menmonicsData.size();i++){
- // // cout<<menmonicsData.size();
- // if(testString.compare(menmonicsData.at(i))==0 || testString.compare(" ")==0){
- // return true ;
- // }
- //
- // }
- // return false;
- // }
- //************************************************************step increment*******************************************
- void stepIncrement(){
- string format2[] ={"ADDR","CLEAR","COMPR","DIVR","MULR","RMO","SHIFTL","SHIFTR","SUBR","TIXR"};
- for(unsigned i=0;i<menmonics.size();i++)
- {
- bool flag=false;
- //comments
- string space=" ";
- if(space.compare(menmonics.at(i))==0){
- step.push_back(0);
- flag=true;
- }
- if(flag==true){
- continue;
- }
- //reserve byte
- string resb="RESB";
- if(resb.compare(menmonics.at(i))==0){
- string n=operand.at(i);
- std::istringstream ss2(n);
- int conv;
- ss2>>conv;
- cout<<std::hex ;
- step.push_back(conv);
- flag=true;
- }
- if(flag==true){
- continue;
- }
- // reserve word
- string resw="RESW";
- if(resw.compare(menmonics.at(i))==0){
- string n=operand.at(i);
- std::istringstream ss2(n);
- int conv;
- ss2>>conv;
- conv=3*conv;
- // cout<<std::hex <<46;
- step.push_back(conv);
- flag=true;
- }
- if(flag==true){
- continue;
- }
- // BYTE directive
- string byte="BYTE";
- bool coma=false;
- bool out =false;
- int counter=0;
- if(byte.compare(menmonics.at(i))==0){
- string myByte=operand.at(i);
- for(int i=0;i<myByte.size();i++){
- if(myByte.at(i)=='\''){
- coma=true;
- }
- if(coma==true ){
- if(myByte.at(i)!='\''){
- counter++;
- out=true;
- }
- else if (out==true){
- break;
- }
- }
- }
- if(myByte.at(0)=='C'){
- ostringstream n ;
- n<<counter;
- // n.str();
- // string n=counter;
- std::istringstream ss2(n.str());
- int conv;
- ss2>>conv;
- cout<<std::hex ;
- step.push_back(conv);
- flag=true;
- }
- else if(myByte.at(0)=='X'){
- counter=counter/2;
- ostringstream n ;
- n<<counter;
- // n.str();
- // string n=counter;
- std::istringstream ss2(n.str());
- int conv;
- ss2>>conv;
- cout<<std::hex ;
- step.push_back(conv);
- flag=true;
- }
- }
- if(flag==true){
- continue;
- }
- //test 1 (format 2)
- for(unsigned j=0;j<10;j++){
- flag=false;
- string temp1=format2[j];
- if(temp1.compare(menmonics.at(i))==0){
- step.push_back(2);
- flag=true;
- break;
- }
- }
- if(flag==true){
- continue;
- }
- //test 2 (format 4);
- string str=operand.at(i);
- char myArray[str.size()+1];
- strcpy(myArray, str.c_str());
- if(myArray[0]=='+'){
- step.push_back(4);
- continue;
- }
- else{
- step.push_back(3);
- }
- }
- }
- //********************************validate operand******************************************************************************
- bool validate_operand(string testString){
- regex e("\\d{1,18}|(\\d{1,16},X)|(@\\d{1,17})|(#\\d{1,17})|([A-Z](\\w{0,17}|\\d{0,17}))|([A-Z](\\w{0,16}|\\d{0,15}),X)|(@[A-Z](\\w{0,16}|\\d{0,16}))|(#[A-Z](\\w{0,16}|\\d{0,16}))|(\\*)|(C'.{1,15}')|(X'(\\d|[A-F]){1,14}')|(\\s)|((A|X|L|B|S|T|F),(A|X|L|B|S|T|F))|([A-Z](\\w*|\\d*)-\\d+)|([A-Z](\\w*|\\d*)\\+\\d+)|(\\d+\\+([A-Z](\\w*|\\d*)))");
- bool match=regex_match(testString,e);
- //cout<< testString<<'\n' ;
- return match ;
- }
- //*************************************parser2********************************************************************************
- void parser2(){
- for(int i=0 ; i<myList.size(); i ++){
- string t =myList.at(i);
- if(t.at(0) == '.'){
- label.push_back(t);
- operand.push_back(" ");
- comment.push_back(" ");
- menmonics.push_back(" ");
- }
- else{
- string l = t.substr(0,8);
- l = regex_replace(l,regex("\\s"),string(""));
- if(l.size() == 0){
- l = " ";
- }
- label.push_back(l);
- if(t.size()<=14){
- string m = t.substr(9,t.size()-9);
- m = regex_replace(m,regex("\\s"),string(""));
- if(m.size() == 0){
- m = " ";
- }
- menmonics.push_back(m);
- operand.push_back(" ");
- comment.push_back(" ");
- }
- if(t.size() >14){
- string m = t.substr(9,6);
- m = regex_replace(m,regex("\\s"),string(""));
- if(m.size() == 0){
- m = " ";
- }
- menmonics.push_back(m);
- int des = 0 ;
- if(t.size()-17 >18){
- des = t.size()-17-(t.size()-34);
- }
- else{
- des = t.size()-17 ;
- }
- string op = t.substr(17,des);
- int t = op.find_last_of("'");
- if(t == -1){
- op = regex_replace(op,regex("\\s"),string(""));
- }
- else{
- op.resize(t+1);
- }
- operand.push_back(op);
- }
- if(t.size() > 34){
- string com = t.substr(35,t.size()-34);
- comment.push_back(com);
- }
- else{
- comment.push_back(" ");
- }
- }
- }
- }
- bool validate_Label2(){
- for(int unsigned i=0 ; i <label.size() ; i++){
- regex e("([A-Z](\\w|\\d){0,7})|((\\.)(.*))|\\s");
- bool match=regex_match(label.at(i),e);
- if(match == false){
- cout<<label.at(i)<<21<<'\n' ;
- labelError.push_back(i);
- return false ;
- }
- }
- return true ;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement