Advertisement
Guest User

prog 3 parse 1

a guest
Nov 14th, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.57 KB | None | 0 0
  1. /*
  2. * parse.cpp
  3. */
  4.  
  5. #include "parse.h"
  6.  
  7. // WRAPPER FOR PUSHBACK
  8. namespace Parser {
  9. bool pushed_back = false;
  10. Token pushed_token;
  11.  
  12. static Token GetNextToken(istream *in, int *line) {
  13. if( pushed_back ) {
  14. pushed_back = false;
  15. return pushed_token;
  16. }
  17. return getNextToken(in, line);
  18. }
  19.  
  20. static void PushBackToken(Token& t) {
  21. if( pushed_back ) {
  22. abort();
  23. }
  24. pushed_back = true;
  25. pushed_token = t;
  26. }
  27.  
  28. }
  29.  
  30. static int error_count = 0;
  31.  
  32. void
  33. ParseError(int line, string msg)
  34. {
  35. ++error_count;
  36. cout << line << ": " << msg << endl;
  37. }
  38.  
  39. ParseTree *Prog(istream *in, int *line)
  40. {
  41. ParseTree *sl = Slist(in, line);
  42.  
  43. if( sl == 0 )
  44. ParseError(*line, "No statements in program");
  45.  
  46. if( error_count )
  47. return 0;
  48.  
  49. return sl;
  50. }
  51.  
  52. // Slist is a Statement followed by a Statement List
  53. ParseTree *Slist(istream *in, int *line) {
  54. ParseTree *s = Stmt(in, line);
  55. if( s == 0 )
  56. return 0;
  57.  
  58. if( Parser::GetNextToken(in, line) != SC ) {
  59. //cout << s << endl;
  60. ParseError(*line, "Missing semicolon");
  61. return 0;
  62. }
  63.  
  64. return new StmtList(s, Slist(in,line));
  65. }
  66.  
  67. ParseTree *Stmt(istream *in, int *line) {
  68. ParseTree *s;
  69.  
  70. Token t = Parser::GetNextToken(in, line);
  71. switch( t.GetTokenType() ) {
  72. case IF:
  73. s = IfStmt(in, line);
  74. break;
  75.  
  76. case PRINT:
  77. s = PrintStmt(in, line);
  78. break;
  79.  
  80. case DONE:
  81. return 0;
  82.  
  83. case ERR:
  84. ParseError(*line, "Invalid token");
  85. return 0;
  86.  
  87. default:
  88. // put back the token and then see if it's an Expr
  89. Parser::PushBackToken(t);
  90. s = Expr(in, line);
  91. if( s == 0 ) {
  92. ParseError(*line, "Invalid Statement");
  93. return 0;
  94. }
  95. break;
  96. }
  97.  
  98.  
  99. return s;
  100. }
  101.  
  102. ParseTree *IfStmt(istream *in, int *line) {
  103.  
  104.  
  105. ParseTree *ex = Expr(in, line);
  106. if( ex == 0 )
  107. {
  108. ParseError(*line, "rr");
  109. return 0;
  110. }
  111.  
  112. Token t = Parser::GetNextToken(in, line);
  113.  
  114. if(t != THEN)
  115. {
  116. Parser::PushBackToken(t);
  117. ParseError(*line, "helo there");
  118. return 0;
  119. }
  120.  
  121. ParseTree *stmt = Stmt(in, line);
  122.  
  123. if(stmt == 0)
  124. {
  125. ParseError(*line, "hello");
  126. return 0;
  127. }
  128.  
  129.  
  130. //return ex;1
  131.  
  132. return new IfStatement(t.GetLinenum(), ex, stmt);
  133. }
  134.  
  135. ParseTree *PrintStmt(istream *in, int *line) {
  136. Token t = Parser::GetNextToken(in, line);
  137. //if(t != PRINT)
  138. {
  139. //Parser::PushBackToken(t);
  140. //ParseError(*line, "hello");
  141. //return 0;
  142. }
  143.  
  144. ParseTree *ex = Expr(in, line);
  145. if( ex == 0 )
  146. {
  147. ParseError(*line, "Invalid statement");
  148. return 0;
  149. }
  150.  
  151. //return 0;
  152.  
  153. return new PrintStatement(t.GetLinenum(), ex);
  154. }
  155.  
  156. ParseTree *Expr(istream *in, int *line) {
  157. ParseTree *t1 = LogicExpr(in, line);
  158. if( t1 == 0 ) {
  159. return 0;
  160. }
  161.  
  162. Token t = Parser::GetNextToken(in, line);
  163.  
  164. if( t != ASSIGN ) {
  165. Parser::PushBackToken(t);
  166. return t1;
  167. }
  168.  
  169. ParseTree *t2 = Expr(in, line); // right assoc
  170. if( t2 == 0 ) {
  171. ParseError(*line, "Missing expression after operator");
  172. return 0;
  173. }
  174.  
  175. return new Assignment(t.GetLinenum(), t1, t2);
  176. }
  177.  
  178. ParseTree *LogicExpr(istream *in, int *line) {
  179. ParseTree *t1 = CompareExpr(in, line);
  180. if( t1 == 0 ) {
  181. return 0;
  182. }
  183.  
  184. Token t = Parser::GetNextToken(in, line);
  185.  
  186. if( t != LOGICAND && t != LOGICOR ) {
  187. Parser::PushBackToken(t);
  188. return t1;
  189. //cout << "Hello" << endl;
  190. }
  191.  
  192. ParseTree *t2 = LogicExpr(in, line);
  193.  
  194. if( t2 == 0 ) {
  195. ParseError(*line, "Missing expression after operator");
  196. return 0;
  197. }
  198.  
  199. //return 0;
  200. if(t == LOGICAND)
  201. {
  202. t1 = new LogicAndExpr(t.GetLinenum(), t1, t2);
  203. }
  204. else
  205. {
  206. t1 = new LogicOrExpr(t.GetLinenum(), t1, t2);
  207. }
  208. return t1;
  209. }
  210.  
  211. ParseTree *CompareExpr(istream *in, int *line) {
  212. ParseTree *t1 = AddExpr(in, line);
  213. if( t1 == 0 ) {
  214. return 0;
  215. }
  216.  
  217. Token t = Parser::GetNextToken(in, line);
  218.  
  219. if( t != EQ && t != NEQ && t != GT && t != GEQ && t != LT && t != LEQ ) {
  220. Parser::PushBackToken(t);
  221. return t1;
  222. }
  223.  
  224. ParseTree *t2 = CompareExpr(in, line);
  225.  
  226. if( t2 == 0 ) {
  227. ParseError(*line, "Missing expression after operator");
  228. return 0;
  229. }
  230.  
  231. if(t == EQ)
  232. {
  233. t1 = new EqExpr(t.GetLinenum(), t1, t2);
  234. }
  235. else if (t == NEQ)
  236. {
  237. t1 = new NEqExpr(t.GetLinenum(), t1, t2);
  238. }
  239. else if (t == LT)
  240. {
  241. t1 = new LtExpr(t.GetLinenum(), t1, t2);
  242. }
  243. else if (t == LEQ)
  244. {
  245. t1 = new LEqExpr(t.GetLinenum(), t1, t2);
  246. }
  247. else if (t == GT)
  248. {
  249. t1 = new GtExpr(t.GetLinenum(), t1, t2);
  250. }
  251. else
  252. {
  253. t1 = new GEqExpr(t.GetLinenum(), t1, t2);
  254. }
  255.  
  256. return t1;
  257.  
  258. }
  259.  
  260. ParseTree *AddExpr(istream *in, int *line) {
  261. ParseTree *t1 = MulExpr(in, line);
  262. if( t1 == 0 ) {
  263. return 0;
  264. }
  265.  
  266. while ( true ) {
  267. Token t = Parser::GetNextToken(in, line);
  268.  
  269. if( t != PLUS && t != MINUS ) {
  270. Parser::PushBackToken(t);
  271. return t1;
  272. }
  273.  
  274. ParseTree *t2 = MulExpr(in, line);
  275. if( t2 == 0 ) {
  276. ParseError(*line, "Missing expression after operator");
  277. return 0;
  278. }
  279.  
  280. if( t == PLUS )
  281. t1 = new PlusExpr(t.GetLinenum(), t1, t2);
  282. else
  283. t1 = new MinusExpr(t.GetLinenum(), t1, t2);
  284. }
  285. return t1;
  286. }
  287.  
  288. ParseTree *MulExpr(istream *in, int *line) {
  289. ParseTree *t1 = Factor(in, line);
  290. if( t1 == 0 ) {
  291. return 0;
  292. }
  293.  
  294. while ( true ) {
  295. Token t = Parser::GetNextToken(in, line);
  296.  
  297. if( t != STAR && t != SLASH ) {
  298. Parser::PushBackToken(t);
  299. return t1;
  300. }
  301.  
  302. ParseTree *t2 = Factor(in, line);
  303. if( t2 == 0 ) {
  304. ParseError(*line, "Missing expression after operator");
  305. return 0;
  306. }
  307.  
  308. if( t == STAR )
  309. t1 = new TimesExpr(t.GetLinenum(), t1, t2);
  310. else
  311. t1 = new DivideExpr(t.GetLinenum(), t1, t2);
  312. }
  313. return t1;
  314. // HANDLE OP
  315. //return 0;
  316. }
  317.  
  318. ParseTree *Factor(istream *in, int *line) {
  319. bool neg = false;
  320. Token t = Parser::GetNextToken(in, line);
  321.  
  322. if( t == MINUS ) {
  323. neg = true;
  324. }
  325. else {
  326. Parser::PushBackToken(t);
  327. }
  328.  
  329. ParseTree *p1 = Primary(in, line);
  330. if( p1 == 0 ) {
  331. //cout << p1 << endl;
  332. ParseError(*line, "Missing primary");
  333. return 0;
  334. }
  335.  
  336. if( neg ) {
  337. // handle as -1 * Primary
  338. return new TimesExpr(t.GetLinenum(), new IConst(t.GetLinenum(), -1), p1);
  339. }
  340. else
  341. return p1;
  342. }
  343.  
  344. ParseTree *Primary(istream *in, int *line) {
  345. ParseTree *tk;
  346. Token t = Parser::GetNextToken(in, line);
  347.  
  348. if(t != IDENT && t != ICONST && t != SCONST && t != TRUE && t != FALSE && t != LPAREN && t != SC)
  349. {
  350. cout << t << endl;
  351. Parser::PushBackToken(t);
  352. ParseError(*line, "hello");
  353. return 0;
  354. }
  355.  
  356. if(t == LPAREN)
  357. {
  358. ParseTree *ex = Expr(in, line);
  359.  
  360. if( ex == 0 )
  361. {
  362. ParseError(*line, "Missing expression after operator");
  363. return 0;
  364. }
  365.  
  366. if(t != RPAREN)
  367. {
  368. Parser::PushBackToken(t);
  369. ParseError(*line, "Invaled Token");
  370. return 0;
  371. }
  372. return ex;
  373. }
  374.  
  375.  
  376.  
  377. if(t == ICONST)
  378. {
  379. tk = new IConst(t);
  380. }
  381.  
  382. else if(t == SCONST)
  383. {
  384. tk = new SConst(t);
  385. }
  386. else if(t == IDENT)
  387. {
  388. tk = new Ident(t);
  389. }
  390.  
  391. else
  392. {
  393. if (t == TRUE)
  394. {
  395. tk = new BoolConst(t, true);
  396. }
  397. else if (t == FALSE)
  398. {
  399. tk = new BoolConst(t, false);
  400. }
  401. else
  402. {
  403. //return tk;
  404. ;
  405. }
  406. }
  407.  
  408. return tk;
  409.  
  410. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement