Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.67 KB | None | 0 0
  1. // ConsoleApplication1.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <fstream>
  7. #include <string>
  8. #include <vector>
  9. #include <set>
  10. #include <map>
  11. #include <stack>
  12.  
  13. void prog();
  14. void expression();
  15. using namespace std;
  16.  
  17. fstream fin("test.txt", ios::in);
  18.  
  19.  
  20. struct Token {
  21. string token;
  22. int type;
  23.  
  24. Token(string tk, int tp) {
  25. token = tk;
  26. type = tp;
  27. }
  28. };
  29.  
  30. struct tcur {
  31. string type;
  32. string current;
  33. };
  34.  
  35. struct list {
  36. map<string, vector<pair <string, string>>> tid;
  37. list *up = NULL;
  38. list *down = NULL;
  39. };
  40.  
  41. list *tids = NULL;
  42. stack <string> op;
  43. stack <string> sign;
  44. bool notif = false;
  45. bool ifb = false;
  46. bool whileb = false;
  47.  
  48. set<char> operation_chrs = { '+', '*', '-', '/', '=', '<', '>', '!', ':' };
  49. set<string> operations = { "++", "--", "+=", "-=", "+", "*", "-", "/",
  50. "=", "//", "/*", "*/", "==", ">=", "<=", "!=", ":" };
  51. set<string> res_words = { "true", "false", "int", "bool", "string", "break", "continue", "switch", "if", "else", "for" };
  52.  
  53.  
  54. //POLIZ//
  55. stack <string> st;
  56. vector <string> poliz;
  57. /////////
  58.  
  59.  
  60.  
  61. Token get_token(fstream& fin) {
  62. int state = 0;
  63.  
  64. char c;
  65. string token = "";
  66.  
  67. bool long_com = false;
  68. bool short_com = false;
  69.  
  70. while (fin.get(c)) {
  71. if (state != 6 && token == "/*") {
  72. token = "";
  73. state = 0;
  74. long_com = true;
  75. short_com = false;
  76. }
  77.  
  78. if (state != 6 && long_com && token == "*/") {
  79. token = "";
  80. state = 0;
  81. long_com = false;
  82. continue;
  83. }
  84.  
  85. if (state != 6 && !long_com && token == "//") {
  86. token = "";
  87. state = 0;
  88. short_com = true;
  89. continue;
  90. }
  91.  
  92. if (state != 6 && short_com && c == '\n') {
  93. token = "";
  94. state = 0;
  95. short_com = false;
  96. continue;
  97. }
  98.  
  99. if (!long_com && !short_com && c == '"') {
  100. if (state != 6) {
  101. if (!short_com && !long_com && token.size()) {
  102. long long pos = fin.tellg();
  103. fin.seekg(pos - 1);
  104. int _state = state;
  105. state = 6;
  106.  
  107. return Token(token, _state);
  108. }
  109.  
  110. token = "";
  111. token += c;
  112. state = 6;
  113. continue;
  114. }
  115. else {
  116. token += c;
  117.  
  118. int _state = state;
  119. state = 0;
  120. return Token(token, _state);
  121. }
  122. }
  123.  
  124. if (state == 6) {
  125. token += c;
  126. continue;
  127. }
  128.  
  129.  
  130.  
  131. if (c == ' ' || c == '\n' || c == '\t') {
  132. if (!short_com && !long_com && token.size()) {
  133. long long pos = fin.tellg();
  134. fin.seekg(pos - 1);
  135. int _state = state;
  136. state = 0;
  137.  
  138. return Token(token, _state);
  139. }
  140. token = "";
  141. state = 0;
  142. continue;
  143. }
  144.  
  145. if (c == ',' || c == ';') {
  146. if (!short_com && !long_com && token.size()) {
  147. long long pos = fin.tellg();
  148. fin.seekg(pos - 1);
  149. int _state = state;
  150. state = 5;
  151.  
  152. return Token(token, _state);
  153. }
  154. token = "";
  155. token += c;
  156. state = 5;
  157. continue;
  158. }
  159.  
  160. if (c == '}' || c == '{' || c == '(' || c == ')') {
  161. if (!short_com && !long_com && token.size()) {
  162. long long pos = fin.tellg();
  163. fin.seekg(pos - 1);
  164. int _state = state;
  165. state = 3;
  166.  
  167. return Token(token, _state);
  168. }
  169. token = "";
  170. token += c;
  171. state = 3;
  172. continue;
  173. }
  174.  
  175. if (state == 7)
  176. continue;
  177.  
  178. if (operation_chrs.count(c)) {
  179. if (state != 4) {
  180. if (!short_com && !long_com && token.size()) {
  181. long long pos = fin.tellg();
  182. fin.seekg(pos - 1);
  183. int _state = state;
  184. state = 4;
  185. return Token(token, _state);
  186. }
  187. token = "";
  188. token += c;
  189. state = 4;
  190. continue;
  191. }
  192. else {
  193. if (operations.count(token + c))
  194. token += c;
  195. else {
  196. int _state = state;
  197. state = 4;
  198. long long pos = fin.tellg();
  199. fin.seekg(pos - 1);
  200.  
  201. return Token(token, _state);
  202. }
  203. continue;
  204. }
  205. }
  206.  
  207.  
  208. if (('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || c == '_') {
  209. if (state != 1) {
  210. if (!short_com && !long_com && token.size()) {
  211. long long pos = fin.tellg();
  212. fin.seekg(pos - 1);
  213. int _state = state;
  214. state = 1;
  215. return Token(token, _state);
  216. }
  217. token = "";
  218. token += c;
  219. state = 1;
  220. continue;
  221. }
  222. else {
  223. token += c;
  224. continue;
  225. }
  226. }
  227.  
  228.  
  229. if ('0' <= c && c <= '9') {
  230. if (state == 1 || state == 2) {
  231. token += c;
  232. continue;
  233. }
  234. else {
  235. if (!short_com && !long_com && token.size()) {
  236. long long pos = fin.tellg();
  237. fin.seekg(pos - 1);
  238. int _state = state;
  239. state = 2;
  240. return Token(token, _state);
  241. }
  242. token = "";
  243. token += c;
  244. state = 2;
  245. continue;
  246. }
  247. }
  248.  
  249. if (c == '.') {
  250. if (state == 2) {
  251. token += c;
  252. }
  253. else {
  254. if (!short_com && !long_com && token.size()) {
  255. long long pos = fin.tellg();
  256. fin.seekg(pos - 1);
  257. int _state = state;
  258. state = 0;
  259.  
  260. return Token(token, _state);
  261. }
  262. state = 0;
  263. return Token(".", 5);
  264. }
  265. }
  266.  
  267.  
  268. }
  269.  
  270. if (!short_com && !long_com && token.size())
  271. return Token(token, state);
  272. else
  273. return Token("", -1);
  274. }
  275.  
  276. // states
  277. // 1 - names
  278. // 2 - numbers
  279. // 3 - brackets
  280. // 4 - operaton
  281. // 5 - punkt
  282. // 6 - string
  283.  
  284.  
  285. Token lex("", -1);
  286.  
  287. string prevt = "";
  288.  
  289. void get_lexem() {
  290. prevt = lex.token;
  291. lex = get_token(fin);
  292. cout << '[' << lex.token << ']' << " - " << lex.type << endl;
  293. /*if (lex.token == ";")
  294. {
  295. while (!op.empty())
  296. {
  297. op.pop();
  298. }
  299. }*/
  300. }
  301.  
  302. // Функции семантического анализатора
  303.  
  304. void listout() // выводить все переменные из тидов
  305. {
  306. list *copy;
  307. copy = tids;
  308. bool check = false;
  309. int lvl = 1;
  310.  
  311. while (copy != NULL)
  312. {
  313. map<string, vector <pair <string, string>>> ch;
  314. ch = copy->tid;
  315. for (auto h : ch)
  316. {
  317. cout << h.first << " ";
  318. //<< " " << h.second.first << " " << h.second.current << " " << lvl << endl;
  319. for (int i = 0; i < h.second.size(); i++)
  320. {
  321. cout << h.second[i].first << " " << h.second[i].second << endl;
  322. }
  323. }
  324. copy = copy->up;
  325. lvl++;
  326. }
  327. }
  328.  
  329. void create_tid() // создает новый тид
  330. {
  331. if (!tids) tids = new list;
  332. else
  333. {
  334. tids->down = new list;
  335. tids->down->up = tids;
  336. tids = tids->down;
  337. }
  338. }
  339.  
  340. void go_up() // удаляет тид и перносит указатель
  341. {
  342. if (tids->up == NULL)
  343. {
  344. delete tids;
  345. tids = NULL;
  346. return;
  347. }
  348. else
  349. {
  350. tids = tids->up;
  351. list *tmp = tids->down;
  352. tids->down = NULL;
  353. delete tmp;
  354. }
  355.  
  356. }
  357.  
  358. void push_id(string name, string type, string cur)
  359. {
  360. list *copy = tids;
  361. if (res_words.count(name) == 0)
  362. {
  363. list *copy;
  364. copy = tids;
  365. bool check = false;
  366.  
  367. map<string, vector <pair <string, string>>> ch;
  368. ch = copy->tid;
  369. if (ch.count(name) != 0)
  370. {
  371. check = true;
  372. }
  373. if (check) throw "The name is already exist";
  374. else
  375. {
  376. tids->tid.emplace(name, make_pair(type, cur)); /// не чисто
  377. }
  378. }
  379. }
  380.  
  381. bool exist(string name) // проверяет существование имени в тидах
  382. {
  383. list *copy;
  384. copy = tids;
  385. bool check = false;
  386.  
  387. while (copy != NULL)
  388. {
  389. map<string, vector <pair <string, string>>> ch;
  390. ch = copy->tid;
  391. if (ch.count(name) != 0)
  392. {
  393. check = true;
  394. }
  395. copy = copy->up;
  396. }
  397. return check;
  398. }
  399.  
  400. void check_id(string name)
  401. {
  402. if (res_words.count(name) == 0)
  403. {
  404. //cout << name << endl;
  405. list *copy;
  406. copy = tids;
  407. bool check = false;
  408.  
  409. while (copy != NULL)
  410. {
  411. map<string, vector <pair <string, string>>> ch;
  412. ch = copy->tid;
  413. /*for (auto h : ch)
  414. {
  415. cout << h.first << " " << h.second << " ";
  416. }
  417. cout << endl;*/
  418. if (ch.count(name) != 0)
  419. {
  420. check = true;
  421. }
  422. copy = copy->up;
  423. }
  424. if (!check) throw("Out of range or the name do not exist");
  425. }
  426. else throw "You cannot use reserved words to name";
  427. }
  428.  
  429. void pushop(string type)
  430. {
  431. //cout << "TYPE OF PUSHING ELEMENT " << type << endl;
  432. op.push(type);
  433. }
  434.  
  435. void checkop()
  436. {
  437. //cout << "ENTERED " << op.size() << " " << sign.size() << " " << sign.top() << endl;
  438. stack<string> check = op;
  439. cout << endl;
  440. string n1, n2, n3;
  441. if (op.size() >= 2 && sign.size() >= 1)
  442. {
  443. n1 = op.top();
  444. op.pop();
  445. n2 = op.top();
  446. op.pop();
  447. n3 = sign.top();
  448. sign.pop();
  449. if (n1 == "int" && n2 == "int")
  450. {
  451. if (n3 != "+" && n3 != "-" && n3 != "/" && n3 != "*" && n3 != ">" && n3 != ">=" && n3 != "<" && n3 != "<=" && n3 != "==" && n3 != "!=")
  452. throw "Bad action with int";
  453. if(n3 == "+" || n3 == "-" || n3 == "*" || n3 == "/") op.push("int");
  454. else op.push("bool");
  455. }
  456. else if (n1 == "string" && n2 == "string")
  457. {
  458. if (n3 != "+" && n3 != "==" && n3 != "!=" && n3 != ">" && n3 != ">=" && n3 != "<" && n3 != "<=")
  459. throw "Bad action with string";
  460. op.push("string");
  461. }
  462.  
  463. else if (n1 == "bool" && n2 == "bool")
  464. {
  465. if (n3 != "==" && n3 != "!=" && n3 != "or" && n3 != "and")
  466. throw "Bad action with bool";
  467. op.push("bool");
  468. }
  469.  
  470. else throw("Conflict of types");
  471. }
  472. }
  473.  
  474. void check_id_2(string name, string type)
  475. {
  476. list *copy = tids;
  477. bool check = true;
  478. while (copy->up != NULL)
  479. {
  480. map<string, vector <pair <string, string>>> ch;
  481. ch = copy->tid;
  482. if (ch.count(name) != 0)
  483. {
  484. op.push(type);
  485. check = false;
  486. }
  487. copy = copy->up;
  488. }
  489. if (check) throw "Name do not exist";
  490. }
  491.  
  492. void checkbool()
  493. {
  494. if (op.top() != "bool")
  495. {
  496. throw "Error of type in brackets";
  497. }
  498. }
  499.  
  500. string find(string name) //find name in tids. returns type of name
  501. {
  502. list *copy = tids;
  503. string type = "";
  504. while (copy != NULL)
  505. {
  506. map<string, vector <pair <string, string>>> ch;
  507. ch = copy->tid;
  508. if (ch.count(name) != 0)
  509. {
  510. type = ch[name][0].first;
  511. }
  512. copy = copy->up;
  513. }
  514. return type;
  515. }
  516.  
  517. // Функции грамматики
  518. void constant() {
  519. if (lex.type == 2) {// numbers
  520. pushop("int");
  521. get_lexem();
  522. return;
  523. }
  524.  
  525. if (lex.type == 6) { // string
  526. pushop("string");
  527. get_lexem();
  528. return;
  529. }
  530.  
  531. if (lex.token == "true" || lex.token == "false") { // bool
  532. pushop("bool");
  533. if (notif) notif = false;
  534. //cout << endl << op.top() << endl;
  535. get_lexem();
  536. return;
  537. }
  538.  
  539. throw "constant was expected /1";
  540. }
  541.  
  542. bool label() {
  543. if (lex.token == "state") {
  544. get_lexem();
  545. constant();
  546. if (lex.token != ":")
  547. throw "':' was expected";
  548. get_lexem();
  549.  
  550. return true;
  551. }
  552. return false;
  553. }
  554.  
  555. bool swtch() {
  556. if (lex.token == "switch") {
  557. get_lexem();
  558.  
  559. if (lex.type != 1)
  560. throw "name was expected";
  561. get_lexem();
  562.  
  563. if (lex.token != ":")
  564. throw "':' was expected";
  565. get_lexem();
  566.  
  567. return true;
  568. }
  569. return false;
  570. }
  571.  
  572. bool brk() {
  573. if (lex.token == "break") {
  574. get_lexem();
  575. if (lex.token != ";")
  576. throw "';' was expected";
  577. get_lexem();
  578. return true;
  579. }
  580. return false;
  581. }
  582.  
  583. bool cnt() {
  584. if (lex.token == "continue") {
  585. get_lexem();
  586. if (lex.token != ";")
  587. throw "';' was expected";
  588. get_lexem();
  589. return true;
  590. }
  591. return false;
  592. }
  593.  
  594. void operand() {
  595. if (lex.type == 1) {
  596. cout << "operand: " << lex.token << endl;
  597. string name = lex.token;
  598.  
  599. if (res_words.count(name) == 0)
  600. {
  601. if (!exist(name))
  602. {
  603. throw "Name do not exist";
  604. }
  605. string type = find(name); // тут тип переменной
  606. pushop(type); // кинули в стэк тип переменной
  607. }
  608. else if (notif || ifb || whileb)
  609. {
  610. constant();
  611. return;
  612. }
  613. get_lexem();
  614. return;
  615. }
  616.  
  617. constant();
  618. }
  619.  
  620. void exp0() {
  621. if (lex.token == "(") {
  622. get_lexem();
  623. expression();
  624. if (lex.token != ")")
  625. throw "')' was expected";
  626. get_lexem();
  627. }
  628. else {
  629. operand();
  630. }
  631. }
  632.  
  633. void exp1() {
  634. exp0();
  635. if (lex.token == "*" || lex.token == "/" || lex.token == "%") {
  636. sign.push(lex.token);
  637. get_lexem();
  638. exp0();
  639. checkop();//func
  640. }
  641. }
  642.  
  643. void exp2() {
  644. exp1();
  645. if (lex.token == "+" || lex.token == "-") {
  646. sign.push(lex.token);
  647. get_lexem();
  648. exp2();
  649. checkop();
  650. }
  651. }
  652.  
  653. void exp3() {
  654. exp2();
  655. if (lex.token == "<" || lex.token == ">" || lex.token == "<=" || lex.token == ">=") {
  656. sign.push(lex.token);
  657. get_lexem();
  658. exp3();
  659. checkop();
  660. }
  661. }
  662.  
  663. void exp4() {
  664. exp3();
  665. if (lex.token == "==" || lex.token == "!=") {
  666. sign.push(lex.token);
  667. get_lexem();
  668. exp4();
  669. checkop();
  670. }
  671. }
  672.  
  673. void exp5() {
  674. exp4();
  675. if (lex.token == "and") {
  676. sign.push(lex.token);
  677. get_lexem();
  678. exp5();
  679. checkop();
  680. }
  681. }
  682.  
  683. void expression() {
  684. exp5();
  685. if (lex.token == "or") {
  686. sign.push(lex.token);
  687. get_lexem();
  688. expression();
  689. checkop();
  690. }
  691. }
  692.  
  693. bool assign() {
  694. if (lex.type == 1) {
  695. if (!exist(lex.token))
  696. throw "Out of range";
  697. get_lexem();
  698. if (lex.token == "=") {
  699. get_lexem();
  700. expression();
  701. }
  702. else if (lex.token == "++" || lex.token == "--")
  703. get_lexem();
  704. else
  705. throw "'++' or '--' or '=' was expected";
  706.  
  707. if (lex.token != ";")
  708. throw "';' was expected";
  709. get_lexem();
  710.  
  711. return true;
  712. }
  713. else {
  714. return false;
  715. }
  716. }
  717.  
  718. int hash = 0;
  719.  
  720. bool definition() {
  721. if (lex.token == "int" || lex.token == "string" || lex.token == "bool") {
  722. string type = lex.token;
  723. get_lexem();
  724.  
  725. if (lex.type != 1)
  726. throw "name was expected";
  727. string name = lex.token;
  728. cout << "operand: " << name << endl;
  729. //cout << endl << type << " " << name << endl;
  730. push_id(name, type, "");
  731. cout << endl << "NAMES IN TIDS: " << endl;
  732. listout();
  733. cout << "END" << endl << endl;
  734.  
  735. get_lexem();
  736.  
  737. if (lex.token != "=")
  738. throw "'=' was expected";
  739. get_lexem();
  740.  
  741. if (lex.token == "true" || lex.token == "false")
  742. {
  743. notif = true;
  744. }
  745.  
  746. expression();
  747. if (lex.token != ";")
  748. throw "';' was expected";
  749. if (type != op.top())
  750. throw "Conflict of types";
  751. while (!op.empty()) op.pop();
  752.  
  753. get_lexem();
  754.  
  755. cout << "___________" << endl;
  756.  
  757. return true;
  758. }
  759. else {
  760. return false;
  761. }
  762. }
  763.  
  764. void els() {
  765. if (lex.token == "else") {
  766. get_lexem();
  767. if (lex.token != "{")
  768. throw "'{' was expected";
  769.  
  770. get_lexem();
  771. prog();
  772. if (lex.token != "}")
  773. throw "'}' was expected";
  774. get_lexem();
  775. }
  776. }
  777.  
  778. bool condition() {
  779. if (lex.token == "if") {
  780. ifb = true;
  781. get_lexem();
  782. if (lex.token != "(")
  783. throw "'(' was expected";
  784. get_lexem();
  785. expression();
  786. ifb = false;
  787. if (op.top() != "bool")
  788. {
  789. throw "Error of type in if's brackets /3";
  790. }
  791. while (!op.empty()) op.pop();
  792.  
  793. if (lex.token != ")")
  794. throw "')' was expected";
  795.  
  796. get_lexem();
  797. if (lex.token != "{")
  798. throw "'{' was expected";
  799.  
  800. get_lexem();
  801. prog();
  802. if (lex.token != "}")
  803. throw "'}' was expected";
  804. get_lexem();
  805. els();
  806.  
  807. return true;
  808. }
  809. else {
  810. return false;
  811. }
  812. }
  813.  
  814. bool loop() {
  815. if (lex.token == "while") {
  816. whileb = true;
  817. get_lexem();
  818. if (lex.token != "(")
  819. throw "'(' was expected";
  820. get_lexem();
  821. expression();
  822. if (op.top() != "bool")
  823. {
  824. throw "Error of type in while's brackets /4";
  825. }
  826. whileb = false;
  827. while (!op.empty()) op.pop();
  828.  
  829. if (lex.token != ")")
  830. throw "')' was expected";
  831.  
  832. get_lexem();
  833. if (lex.token != "{")
  834. throw "'{' was expected";
  835.  
  836. get_lexem();
  837. prog();
  838. if (lex.token != "}")
  839. throw "'}' was expected";
  840. get_lexem();
  841.  
  842. return true;
  843. }
  844. else {
  845. return false;
  846. }
  847. }
  848.  
  849. void prog() {
  850. create_tid();
  851.  
  852. while (true) {
  853. if (definition())
  854. continue;
  855.  
  856. if (loop())
  857. continue;
  858.  
  859. if (condition())
  860. continue;
  861.  
  862. if (label())
  863. continue;
  864.  
  865. if (brk())
  866. continue;
  867.  
  868. if (cnt())
  869. continue;
  870.  
  871. if (swtch())
  872. continue;
  873.  
  874. if (assign())
  875. continue;
  876.  
  877. break;
  878. }
  879.  
  880. go_up();
  881. }
  882.  
  883.  
  884. // =================
  885.  
  886.  
  887. int main() {
  888. // А так мы его запускаем
  889.  
  890. get_lexem();
  891. try {
  892. prog();
  893. fin.close();
  894. cout << "SUCCESS" << endl;
  895. }
  896. catch (char const* error) {
  897. fin.close();
  898. cout << "ERROR: " << error << endl;
  899. }
  900.  
  901. return 0;
  902. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement