Advertisement
Guest User

Untitled

a guest
Apr 18th, 2015
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.27 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <vector>
  5. #include <sstream>
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <regex>
  9. #include <locale>
  10. #include <ctype.h>
  11.  
  12. using namespace std;
  13. void readFile(string x);
  14. bool spaceCheck(string x);
  15. bool menmonicCheck(string testString);
  16. void parser2();
  17. void stepIncrement();
  18. bool validate_Label2();
  19. bool validate_operand(string testString);
  20.  
  21. vector<string> myList;
  22. vector<string> label;
  23. vector<string> menmonics;
  24. vector<string> operand;
  25. vector<string> comment;
  26. vector<int> step;
  27. vector<int> syntaxError;
  28. vector<int> labelError;
  29. bool findFileFlag=true;
  30.  
  31.  
  32.  
  33. vector<string> menmonicsData;
  34. int main () {
  35.  
  36. readFile("L3_A");
  37.  
  38. for(int unsigned i=0;i<myList.size();i++)
  39.  
  40. {bool x=spaceCheck(myList.at(i));
  41. if(x==false){
  42. cout<< "syntax error"<<'\n';
  43. syntaxError.push_back(i);
  44. }
  45. }
  46. parser2();
  47.  
  48. // for(int unsigned i=0;i<menmonics.size();i++)
  49. // {
  50. // if(i>menmonics.size()){
  51. // break;
  52. // }
  53. // if(menmonicCheck(menmonics.at(i))==false){
  54. // cout << "menmonic error"<<'\n';
  55. // cout<<menmonics.at(i)<<'\n' ;
  56. // return 0;
  57. // }
  58. //
  59. // }
  60.  
  61. // for(int unsigned i=0 ; i<operand.size(); i++){
  62. // if(validate_operand(operand.at(i))==false){
  63. // cout << "operand error"<<'\n';
  64. // cout<<menmonics.at(i)+" "<<operand.at(i)<<i<<endl ;
  65. // return 0;
  66. // }
  67. //
  68. // }
  69.  
  70. bool val = validate_Label2();
  71.  
  72. //stepIncrement();
  73. for(int unsigned i=0;i<menmonics.size();i++)
  74. {
  75. cout<<label.at(i);
  76. cout<<" " ;
  77. cout<<menmonics.at(i);
  78. cout<<" " ;
  79. cout<<operand.at(i);
  80. cout<<" " ;
  81. cout<<comment.at(i);
  82. cout<<" " ;
  83. // cout<<step.at(i);
  84. cout<<" "<<endl;
  85.  
  86. }
  87. //
  88.  
  89.  
  90. return 0;
  91. }
  92. //**********************************************************file reader****************************************************
  93. void readFile(string filename){
  94. string line;
  95. ifstream myfile (filename+".txt");
  96. if (myfile.is_open())
  97. {
  98. while ( getline (myfile,line) )
  99. {
  100.  
  101. for(unsigned int l = 0; l < line.length(); l++)
  102. {
  103. line[l] = toupper(line[l]);
  104. }
  105. myList.push_back(line);
  106. cout << line << '\n';
  107. }
  108. myfile.close();
  109. }
  110.  
  111. else{
  112. findFileFlag=false;
  113. cout << "Unable to open file";
  114. }
  115. // string line2;
  116. //
  117. // ifstream myfile2 ("menmonics.txt");
  118. // if (myfile2.is_open())
  119. // {
  120. // while ( getline (myfile2,line2) )
  121. // {
  122. // menmonicsData.push_back(line2);
  123. //
  124. // }
  125. // myfile2.close();
  126. // }
  127. //
  128. // else{
  129. //
  130. // cout << "Unable to open file";
  131. // }
  132.  
  133. }
  134. //***********************************************************space checker***********************************
  135. bool spaceCheck(string testString){
  136. regex e("(........ ...... .{0,48})|(........ ......)|(\\.(.*))|(.*)");
  137.  
  138. //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})|())");
  139.  
  140. int length=testString.length();
  141.  
  142. bool match=regex_match(testString,e);
  143. if(length>66){
  144. match=false;
  145. }
  146.  
  147. return match;
  148.  
  149. }
  150.  
  151. //*************************************************************check menmonic *****************************************
  152.  
  153.  
  154. //bool menmonicCheck(string testString){
  155. //
  156. // // return menmonics;
  157. // //check for presence of menmonic in database
  158. // for(int unsigned i=0;i<menmonicsData.size();i++){
  159. // // cout<<menmonicsData.size();
  160. // if(testString.compare(menmonicsData.at(i))==0 || testString.compare(" ")==0){
  161. // return true ;
  162. // }
  163. //
  164. // }
  165. // return false;
  166. // }
  167.  
  168.  
  169.  
  170. //************************************************************step increment*******************************************
  171. void stepIncrement(){
  172. string format2[] ={"ADDR","CLEAR","COMPR","DIVR","MULR","RMO","SHIFTL","SHIFTR","SUBR","TIXR"};
  173. for(unsigned i=0;i<menmonics.size();i++)
  174.  
  175. {
  176. bool flag=false;
  177. //comments
  178. string space=" ";
  179. if(space.compare(menmonics.at(i))==0){
  180. step.push_back(0);
  181. flag=true;
  182. }
  183. if(flag==true){
  184. continue;
  185. }
  186.  
  187. //reserve byte
  188.  
  189. string resb="RESB";
  190. if(resb.compare(menmonics.at(i))==0){
  191. string n=operand.at(i);
  192. std::istringstream ss2(n);
  193. int conv;
  194. ss2>>conv;
  195. cout<<std::hex ;
  196.  
  197. step.push_back(conv);
  198. flag=true;
  199. }
  200. if(flag==true){
  201. continue;
  202. }
  203.  
  204. // reserve word
  205. string resw="RESW";
  206. if(resw.compare(menmonics.at(i))==0){
  207.  
  208. string n=operand.at(i);
  209. std::istringstream ss2(n);
  210. int conv;
  211. ss2>>conv;
  212. conv=3*conv;
  213. // cout<<std::hex <<46;
  214.  
  215. step.push_back(conv);
  216. flag=true;
  217. }
  218. if(flag==true){
  219. continue;
  220. }
  221.  
  222.  
  223. // BYTE directive
  224. string byte="BYTE";
  225. bool coma=false;
  226. bool out =false;
  227. int counter=0;
  228. if(byte.compare(menmonics.at(i))==0){
  229. string myByte=operand.at(i);
  230. for(int i=0;i<myByte.size();i++){
  231. if(myByte.at(i)=='\''){
  232. coma=true;
  233. }
  234.  
  235. if(coma==true ){
  236. if(myByte.at(i)!='\''){
  237. counter++;
  238. out=true;
  239. }
  240. else if (out==true){
  241. break;
  242. }
  243. }
  244. }
  245. if(myByte.at(0)=='C'){
  246. ostringstream n ;
  247. n<<counter;
  248. // n.str();
  249. // string n=counter;
  250. std::istringstream ss2(n.str());
  251. int conv;
  252. ss2>>conv;
  253. cout<<std::hex ;
  254. step.push_back(conv);
  255. flag=true;
  256. }
  257. else if(myByte.at(0)=='X'){
  258. counter=counter/2;
  259. ostringstream n ;
  260. n<<counter;
  261. // n.str();
  262. // string n=counter;
  263. std::istringstream ss2(n.str());
  264. int conv;
  265. ss2>>conv;
  266. cout<<std::hex ;
  267. step.push_back(conv);
  268. flag=true;
  269. }
  270.  
  271.  
  272. }
  273. if(flag==true){
  274. continue;
  275. }
  276.  
  277.  
  278. //test 1 (format 2)
  279. for(unsigned j=0;j<10;j++){
  280. flag=false;
  281. string temp1=format2[j];
  282. if(temp1.compare(menmonics.at(i))==0){
  283. step.push_back(2);
  284. flag=true;
  285. break;
  286. }
  287.  
  288. }
  289. if(flag==true){
  290. continue;
  291. }
  292.  
  293. //test 2 (format 4);
  294.  
  295. string str=operand.at(i);
  296. char myArray[str.size()+1];
  297. strcpy(myArray, str.c_str());
  298. if(myArray[0]=='+'){
  299. step.push_back(4);
  300. continue;
  301. }
  302.  
  303. else{
  304. step.push_back(3);
  305. }
  306. }
  307.  
  308. }
  309.  
  310. //********************************validate operand******************************************************************************
  311. bool validate_operand(string testString){
  312.  
  313. 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*)))");
  314. bool match=regex_match(testString,e);
  315. //cout<< testString<<'\n' ;
  316. return match ;
  317. }
  318. //*************************************parser2********************************************************************************
  319. void parser2(){
  320.  
  321. for(int i=0 ; i<myList.size(); i ++){
  322. string t =myList.at(i);
  323. if(t.at(0) == '.'){
  324. label.push_back(t);
  325. operand.push_back(" ");
  326. comment.push_back(" ");
  327. menmonics.push_back(" ");
  328.  
  329. }
  330. else{
  331. string l = t.substr(0,8);
  332. l = regex_replace(l,regex("\\s"),string(""));
  333. if(l.size() == 0){
  334. l = " ";
  335. }
  336. label.push_back(l);
  337.  
  338.  
  339. if(t.size()<=14){
  340. string m = t.substr(9,t.size()-9);
  341. m = regex_replace(m,regex("\\s"),string(""));
  342. if(m.size() == 0){
  343. m = " ";
  344. }
  345. menmonics.push_back(m);
  346. operand.push_back(" ");
  347. comment.push_back(" ");
  348. }
  349.  
  350. if(t.size() >14){
  351. string m = t.substr(9,6);
  352. m = regex_replace(m,regex("\\s"),string(""));
  353. if(m.size() == 0){
  354. m = " ";
  355. }
  356. menmonics.push_back(m);
  357.  
  358. int des = 0 ;
  359. if(t.size()-17 >18){
  360. des = t.size()-17-(t.size()-34);
  361. }
  362. else{
  363. des = t.size()-17 ;
  364. }
  365.  
  366. string op = t.substr(17,des);
  367. int t = op.find_last_of("'");
  368. if(t == -1){
  369. op = regex_replace(op,regex("\\s"),string(""));
  370. }
  371. else{
  372. op.resize(t+1);
  373. }
  374. operand.push_back(op);
  375. }
  376.  
  377.  
  378. if(t.size() > 34){
  379. string com = t.substr(35,t.size()-34);
  380. comment.push_back(com);
  381. }
  382. else{
  383. comment.push_back(" ");
  384. }
  385. }
  386. }
  387. }
  388. bool validate_Label2(){
  389. for(int unsigned i=0 ; i <label.size() ; i++){
  390. regex e("([A-Z](\\w|\\d){0,7})|((\\.)(.*))|\\s");
  391. bool match=regex_match(label.at(i),e);
  392. if(match == false){
  393. cout<<label.at(i)<<21<<'\n' ;
  394. labelError.push_back(i);
  395. return false ;
  396. }
  397.  
  398. }
  399. return true ;
  400. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement