Advertisement
Guest User

Untitled

a guest
Nov 14th, 2018
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.44 KB | None | 0 0
  1. /*
  2. * parse.cpp
  3. */
  4.  
  5. #include "parse.h"
  6.  
  7.  
  8. // WRAPPER FOR PUSHBACK
  9. namespace Parser {
  10. bool pushed_back = false;
  11. Token pushed_token;
  12.  
  13. static Token GetNextToken(istream *in, int *line) {
  14. if( pushed_back ) {
  15. pushed_back = false;
  16. return pushed_token;
  17. }
  18. return getNextToken(in, line);
  19. }
  20.  
  21. static void PushBackToken(Token& t) {
  22. if( pushed_back ) {
  23. abort();
  24. }
  25. pushed_back = true;
  26. pushed_token = t;
  27. }
  28.  
  29. }
  30.  
  31. static int error_count = 0;
  32.  
  33. void
  34. ParseError(int line, string msg)
  35. {
  36. ++error_count;
  37. cout << line << ": " << msg << endl;
  38. }
  39.  
  40. ParseTree *Prog(istream *in, int *line)
  41. {
  42. ParseTree *sl = Slist(in, line);
  43.  
  44. if( sl == 0 )
  45. ParseError(*line, "No statements in program");
  46.  
  47. if( error_count )
  48. return 0;
  49.  
  50. return sl;
  51. }
  52.  
  53. // Slist is a Statement followed by a Statement List
  54. ParseTree *Slist(istream *in, int *line) {
  55. ParseTree *s = Stmt(in, line);
  56. if( s == 0 )
  57. return 0;
  58.  
  59. if( Parser::GetNextToken(in, line) != SC ) {
  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. ParseTree *p = Expr(in, line);
  104. if(p == 0){
  105. return 0;
  106. }
  107. Token t = Parser::GetNextToken(in, line);
  108. //ParseTree *p1 = Stmt(in, line);
  109.  
  110. if(t != THEN) {
  111. ParseError(*line, " Bad Expr");
  112. }
  113. return new IfStatement(t.GetLinenum(), p, Stmt(in, line));
  114. }
  115.  
  116. ParseTree *PrintStmt(istream *in, int *line) {
  117. // check*
  118. ParseTree *p;
  119. p= Expr(in, line);
  120. if(p==0){
  121. ParseError(*line, "Print Error");
  122. return 0;
  123. }
  124. return new PrintStatement(*line, p);
  125. }
  126. /* Token t = Parser::GetNextToken(in, line);
  127. if(t == PRINT){
  128. ParseTree *p = Expr(in, line);
  129. return new PrintStatement(t.GetLinenum(), p);
  130. }
  131. return 0;
  132. }*/
  133.  
  134. ParseTree *Expr(istream *in, int *line) {
  135. ParseTree *t1 = LogicExpr(in, line);
  136. if( t1 == 0 ) {
  137. return 0;
  138. }
  139. Token t = Parser::GetNextToken(in, line);
  140. if( t != ASSIGN ) {
  141. Parser::PushBackToken(t);
  142. return t1;
  143. }
  144. ParseTree *t2 = Expr(in, line); // right assoc
  145. if( t2 == 0 ) {
  146. ParseError(*line, "Missing expression after operator");
  147. return 0;
  148. }
  149. return new Assignment(t.GetLinenum(), t1, t2);
  150. }
  151.  
  152. ParseTree *LogicExpr(istream *in, int *line) {
  153. ParseTree *t1 = CompareExpr(in, line);
  154. if( t1 == 0 ) {
  155. return 0;
  156. }
  157. while(true) {
  158. Token t = Parser::GetNextToken(in, line);
  159. if (t != LOGICAND and t != LOGICOR) {
  160. Parser::PushBackToken(t);
  161. return t1;
  162. }
  163. ParseTree *t2 = CompareExpr(in, line);
  164. if( t2 == 0 ) {
  165. ParseError(*line, "Missing expression after operator");
  166. return 0;
  167. }
  168. if(t == LOGICAND)
  169. t1 = new LogicAndExpr(t.GetLinenum(), t1, t2);
  170. if( t == LOGICOR)
  171. t1 = new LogicOrExpr(t.GetLinenum(), t1, t2);
  172. }
  173. }
  174.  
  175. ParseTree *CompareExpr(istream *in, int *line) {
  176. ParseTree *t1 = AddExpr(in, line);
  177. if( t1 == 0 ) {
  178. return 0;
  179. }
  180. while(true){
  181. Token t = Parser::GetNextToken(in, line);
  182. if(t != EQ and t != NEQ and t != GT and t != GEQ and t != LT and t != LEQ){
  183. Parser::PushBackToken(t);
  184. return t1;
  185. }
  186. ParseTree *t2 = AddExpr(in, line);
  187. if( t2 == 0 ){
  188. ParseError(*line, "Missing expression after operator");
  189. return 0;
  190. }
  191. if(t == EQ)
  192. t1 = new EqExpr(t.GetLinenum(), t1, t2);
  193. if(t == NEQ)
  194. t1 = new NEqExpr(t.GetLinenum(), t1, t2);
  195. if(t == LT)
  196. t1 = new LtExpr(t.GetLinenum(), t1, t2);
  197. if(t == LEQ)
  198. t1 = new LEqExpr(t.GetLinenum(), t1, t2);
  199. if(t == GT)
  200. t1 = new GtExpr(t.GetLinenum(), t1, t2);
  201. if(t == GEQ)
  202. t1 = new GEqExpr(t.GetLinenum(), t1, t2);
  203. }
  204. }
  205.  
  206. ParseTree *AddExpr(istream *in, int *line) {
  207. ParseTree *t1 = MulExpr(in, line);
  208. if( t1 == 0 ) {
  209. return 0;
  210. }
  211.  
  212. while ( true ) {
  213. Token t = Parser::GetNextToken(in, line);
  214.  
  215. if( t != PLUS && t != MINUS ) {
  216. Parser::PushBackToken(t);
  217. return t1;
  218. }
  219.  
  220. ParseTree *t2 = MulExpr(in, line);
  221. if( t2 == 0 ) {
  222. ParseError(*line, "Missing expression after operator");
  223. return 0;
  224. }
  225.  
  226. if( t == PLUS )
  227. t1 = new PlusExpr(t.GetLinenum(), t1, t2);
  228. else
  229. t1 = new MinusExpr(t.GetLinenum(), t1, t2);
  230. }
  231. }
  232.  
  233. ParseTree *MulExpr(istream *in, int *line) {
  234. ParseTree *t1 = Factor(in, line);
  235. if (t1 == 0) {
  236. return 0;
  237. }
  238. while (true) {
  239. Token t = Parser::GetNextToken(in, line);
  240. if ( t != STAR and t != PLUS ){
  241. Parser::PushBackToken(t);
  242. return t1;
  243. }
  244. ParseTree *t2 = Factor(in, line);
  245. if ( t2 == 0 ){
  246. ParseError(*line, "Missing expression after operator");
  247. return 0;
  248. }
  249. if(t == SLASH){
  250. t1 = new DivideExpr(t.GetLinenum(), t1, t2);
  251. }
  252. if(t == STAR){
  253. t1 = new TimesExpr(t.GetLinenum(), t1, t2);
  254. }
  255. }
  256. }
  257.  
  258. ParseTree *Factor(istream *in, int *line) {
  259. bool neg = false;
  260. Token t = Parser::GetNextToken(in, line);
  261.  
  262. if( t == MINUS ) {
  263. neg = true;
  264. }
  265. else {
  266. Parser::PushBackToken(t);
  267. }
  268.  
  269. ParseTree *p1 = Primary(in, line);
  270. if( p1 == 0 ) {
  271. ParseError(*line, "Missing primary");
  272. return 0;
  273. }
  274.  
  275. if( neg ) {
  276. return new TimesExpr(t.GetLinenum(), new IConst(t.GetLinenum(), -1), p1);
  277. }
  278. else
  279. return p1;
  280. }
  281.  
  282. ParseTree *Primary(istream *in, int *line) {
  283. Token t = Parser::GetNextToken(in, line);
  284. if (t == ICONST) return new IConst(t);
  285. if (t == SCONST) return new SConst(t);
  286. if (t == IDENT) return new Ident(t);
  287. if (t == TRUE) return new BoolConst(t, true);
  288. if (t == FALSE) return new BoolConst(t, false);
  289. if (t == LPAREN) {
  290. ParseTree *t1 = Expr(in, line);
  291. if (t1 == 0) {
  292. ParseError(*line, "expression expected");
  293. return 0;
  294. }
  295. if (t != RPAREN){
  296. ParseError(*line, "right paren expected");
  297. return 0;
  298. }
  299. return t1;
  300. }
  301. /*ParseError(t.GetLinenum(), "primary expected");*/
  302. return 0;
  303. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement