fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #include <stdlib.h>
  5.  
  6. #define MAX_SOURCE 5000
  7. #define MAX_TOKENS 500
  8. #define MAX_TEXT 50
  9. #define MAX_SYMBOLS 100
  10. #define MAX_ERRORS 100
  11. #define MAX_TAC 500
  12.  
  13. #define TOKEN_INT 1
  14. #define TOKEN_IDENTIFIER 2
  15. #define TOKEN_NUMBER 3
  16. #define TOKEN_IF 4
  17. #define TOKEN_ELSE 5
  18. #define TOKEN_WHILE 6
  19. #define TOKEN_OPERATOR 7
  20. #define TOKEN_SYMBOL 8
  21. #define TOKEN_UNKNOWN 9
  22.  
  23. char source[MAX_SOURCE];
  24. int sourceLength = 0;
  25.  
  26. char tokenText[MAX_TOKENS][MAX_TEXT];
  27. int tokenType[MAX_TOKENS];
  28. int tokenLine[MAX_TOKENS];
  29. int tokenCount = 0;
  30.  
  31. char symbolName[MAX_SYMBOLS][MAX_TEXT];
  32. char symbolType[MAX_SYMBOLS][20];
  33. int symbolValue[MAX_SYMBOLS];
  34. int symbolCount = 0;
  35.  
  36. char semanticErrors[MAX_ERRORS][100];
  37. int semanticErrorCount = 0;
  38.  
  39. char tac[MAX_TAC][100];
  40. int tacCount = 0;
  41.  
  42. int currentToken = 0;
  43. int syntaxError = 0;
  44. char syntaxErrorMessage[120];
  45.  
  46. int tempCount = 1;
  47. int labelCount = 1;
  48. int assignmentAffectsSymbol = 1;
  49.  
  50. char lastIdentifier[MAX_TEXT];
  51. char lastExpressionText[100];
  52. char lastExprLeft[MAX_TEXT];
  53. char lastExprOp[MAX_TEXT];
  54. char lastExprRight[MAX_TEXT];
  55. int lastExprHasOperator = 0;
  56. int lastExprValue = 0;
  57. int lastExprValueKnown = 0;
  58. int lastExprSemanticOk = 1;
  59.  
  60. char conditionLeft[MAX_TEXT];
  61. char conditionOp[MAX_TEXT];
  62. char conditionRight[MAX_TEXT];
  63.  
  64. char tempLine[150];
  65.  
  66. void readSource();
  67. void tokenize();
  68. void printSource();
  69. void printTokens();
  70. void parseProgram();
  71. void parseDeclaration();
  72. void parseAssignment();
  73. void parseIfStatement();
  74. void parseWhileStatement();
  75. void parseCondition();
  76. void parseExpression();
  77. void readOperand(char outputText[]);
  78. int findSymbolIndex(char name[]);
  79. void addSymbol(char name[]);
  80. void updateSymbolValue(char name[], int value);
  81. void addSemanticError(char message[]);
  82. void addTAC(char line[]);
  83. void printSyntaxResult();
  84. void printSymbolTable();
  85. void printSemanticErrors();
  86. void printTAC();
  87. void addToken(char text[], int type, int line);
  88. int isOperatorToken(char text[]);
  89. int isSymbolToken(char text[]);
  90. int tokenIs(char text[]);
  91. int tokenKindIs(int type);
  92. void setSyntaxError(char message[]);
  93. void expectText(char text[]);
  94. void copyCurrentToken(char outputText[]);
  95. int operandValue(char text[], int ok);
  96. int newLabel();
  97.  
  98. int main()
  99. {
  100. readSource();
  101. tokenize();
  102.  
  103. printf("========== Source Program ==========\n");
  104. printSource();
  105.  
  106. printf("\n========== 1. Tokens ==========\n");
  107. printTokens();
  108.  
  109. parseProgram();
  110.  
  111. printf("\n========== 2. Syntax Analysis Output ==========\n");
  112. printSyntaxResult();
  113.  
  114. printf("\n========== 3. Symbol Table ==========\n");
  115. printSymbolTable();
  116.  
  117. printf("\n========== 4. Semantic Analysis Output ==========\n");
  118. printSemanticErrors();
  119.  
  120. printf("\n========== 5. Three Address Code ==========\n");
  121. printTAC();
  122.  
  123. return 0;
  124. }
  125.  
  126. void readSource()
  127. {
  128. int ch;
  129. int i;
  130.  
  131. i = 0;
  132.  
  133. if (freopen("input.txt", "r", stdin) == NULL)
  134. {
  135. printf("Error: input.txt file not found.\n");
  136. source[0] = '\0';
  137. sourceLength = 0;
  138. return;
  139. }
  140.  
  141. while ((ch = getchar()) != EOF && i < MAX_SOURCE - 1)
  142. {
  143. source[i] = ch;
  144. i++;
  145. }
  146.  
  147. source[i] = '\0';
  148. sourceLength = i;
  149. }
  150.  
  151. void printSource()
  152. {
  153. if (sourceLength == 0)
  154. {
  155. printf("No source code loaded.\n");
  156. }
  157. else
  158. {
  159. printf("%s", source);
  160. if (source[sourceLength - 1] != '\n')
  161. {
  162. printf("\n");
  163. }
  164. }
  165. }
  166.  
  167. void addToken(char text[], int type, int line)
  168. {
  169. if (tokenCount >= MAX_TOKENS)
  170. {
  171. return;
  172. }
  173.  
  174. strcpy(tokenText[tokenCount], text);
  175. tokenType[tokenCount] = type;
  176. tokenLine[tokenCount] = line;
  177. tokenCount++;
  178. }
  179.  
  180. int isOperatorToken(char text[])
  181. {
  182. if (strcmp(text, "=") == 0)
  183. {
  184. return 1;
  185. }
  186. if (strcmp(text, "+") == 0)
  187. {
  188. return 1;
  189. }
  190. if (strcmp(text, "-") == 0)
  191. {
  192. return 1;
  193. }
  194. if (strcmp(text, "<") == 0)
  195. {
  196. return 1;
  197. }
  198. if (strcmp(text, ">") == 0)
  199. {
  200. return 1;
  201. }
  202. return 0;
  203. }
  204.  
  205. int isSymbolToken(char text[])
  206. {
  207. if (strcmp(text, ";") == 0)
  208. {
  209. return 1;
  210. }
  211. if (strcmp(text, "(") == 0)
  212. {
  213. return 1;
  214. }
  215. if (strcmp(text, ")") == 0)
  216. {
  217. return 1;
  218. }
  219. return 0;
  220. }
  221.  
  222. void tokenize()
  223. {
  224. int i;
  225. int j;
  226. int line;
  227. char word[MAX_TEXT];
  228. char single[2];
  229.  
  230. i = 0;
  231. line = 1;
  232.  
  233. while (source[i] != '\0')
  234. {
  235. if (source[i] == '\n')
  236. {
  237. line++;
  238. i++;
  239. }
  240. else if (isspace(source[i]))
  241. {
  242. i++;
  243. }
  244. else if (isalpha(source[i]) || source[i] == '_')
  245. {
  246. j = 0;
  247. while ((isalnum(source[i]) || source[i] == '_') && j < MAX_TEXT - 1)
  248. {
  249. word[j] = source[i];
  250. j++;
  251. i++;
  252. }
  253. word[j] = '\0';
  254.  
  255. if (strcmp(word, "int") == 0)
  256. {
  257. addToken(word, TOKEN_INT, line);
  258. }
  259. else if (strcmp(word, "if") == 0)
  260. {
  261. addToken(word, TOKEN_IF, line);
  262. }
  263. else if (strcmp(word, "else") == 0)
  264. {
  265. addToken(word, TOKEN_ELSE, line);
  266. }
  267. else if (strcmp(word, "while") == 0)
  268. {
  269. addToken(word, TOKEN_WHILE, line);
  270. }
  271. else
  272. {
  273. addToken(word, TOKEN_IDENTIFIER, line);
  274. }
  275. }
  276. else if (isdigit(source[i]))
  277. {
  278. j = 0;
  279. while (isdigit(source[i]) && j < MAX_TEXT - 1)
  280. {
  281. word[j] = source[i];
  282. j++;
  283. i++;
  284. }
  285. word[j] = '\0';
  286. addToken(word, TOKEN_NUMBER, line);
  287. }
  288. else
  289. {
  290. single[0] = source[i];
  291. single[1] = '\0';
  292.  
  293. if (isOperatorToken(single))
  294. {
  295. addToken(single, TOKEN_OPERATOR, line);
  296. }
  297. else if (isSymbolToken(single))
  298. {
  299. addToken(single, TOKEN_SYMBOL, line);
  300. }
  301. else
  302. {
  303. addToken(single, TOKEN_UNKNOWN, line);
  304. }
  305.  
  306. i++;
  307. }
  308. }
  309. }
  310.  
  311. void printTokens()
  312. {
  313. int i;
  314. int previousLine;
  315.  
  316. previousLine = -1;
  317.  
  318. for (i = 0; i < tokenCount; i++)
  319. {
  320. if (previousLine != -1 && tokenLine[i] != previousLine)
  321. {
  322. printf("\n");
  323. }
  324.  
  325. if (tokenType[i] == TOKEN_INT)
  326. {
  327. printf("INT");
  328. }
  329. else if (tokenType[i] == TOKEN_IDENTIFIER)
  330. {
  331. printf("IDENTIFIER(%s)", tokenText[i]);
  332. }
  333. else if (tokenType[i] == TOKEN_NUMBER)
  334. {
  335. printf("NUMBER(%s)", tokenText[i]);
  336. }
  337. else if (tokenType[i] == TOKEN_IF)
  338. {
  339. printf("IF");
  340. }
  341. else if (tokenType[i] == TOKEN_ELSE)
  342. {
  343. printf("ELSE");
  344. }
  345. else if (tokenType[i] == TOKEN_WHILE)
  346. {
  347. printf("WHILE");
  348. }
  349. else
  350. {
  351. printf("%s", tokenText[i]);
  352. }
  353.  
  354. if (i + 1 < tokenCount && tokenLine[i + 1] == tokenLine[i])
  355. {
  356. printf(" ");
  357. }
  358.  
  359. previousLine = tokenLine[i];
  360. }
  361.  
  362. printf("\n");
  363. }
  364.  
  365. int tokenIs(char text[])
  366. {
  367. if (currentToken < tokenCount && strcmp(tokenText[currentToken], text) == 0)
  368. {
  369. return 1;
  370. }
  371. return 0;
  372. }
  373.  
  374. int tokenKindIs(int type)
  375. {
  376. if (currentToken < tokenCount && tokenType[currentToken] == type)
  377. {
  378. return 1;
  379. }
  380. return 0;
  381. }
  382.  
  383. void setSyntaxError(char message[])
  384. {
  385. if (syntaxError == 0)
  386. {
  387. syntaxError = 1;
  388. strcpy(syntaxErrorMessage, message);
  389. }
  390. }
  391.  
  392. void expectText(char text[])
  393. {
  394. if (syntaxError)
  395. {
  396. return;
  397. }
  398.  
  399. if (tokenIs(text))
  400. {
  401. currentToken++;
  402. }
  403. else
  404. {
  405. sprintf(tempLine, "Expected '%s'", text);
  406. setSyntaxError(tempLine);
  407. }
  408. }
  409.  
  410. void copyCurrentToken(char outputText[])
  411. {
  412. if (currentToken < tokenCount)
  413. {
  414. strcpy(outputText, tokenText[currentToken]);
  415. }
  416. else
  417. {
  418. outputText[0] = '\0';
  419. }
  420. }
  421.  
  422. void parseProgram()
  423. {
  424. currentToken = 0;
  425. assignmentAffectsSymbol = 1;
  426.  
  427. while (currentToken < tokenCount && syntaxError == 0)
  428. {
  429. if (tokenKindIs(TOKEN_INT))
  430. {
  431. parseDeclaration();
  432. }
  433. else if (tokenKindIs(TOKEN_IDENTIFIER))
  434. {
  435. assignmentAffectsSymbol = 1;
  436. parseAssignment();
  437. }
  438. else if (tokenKindIs(TOKEN_IF))
  439. {
  440. parseIfStatement();
  441. }
  442. else if (tokenKindIs(TOKEN_WHILE))
  443. {
  444. parseWhileStatement();
  445. }
  446. else
  447. {
  448. sprintf(tempLine, "Unexpected token '%s'", tokenText[currentToken]);
  449. setSyntaxError(tempLine);
  450. }
  451. }
  452. }
  453.  
  454. void parseDeclaration()
  455. {
  456. char name[MAX_TEXT];
  457.  
  458. expectText("int");
  459.  
  460. if (syntaxError)
  461. {
  462. return;
  463. }
  464.  
  465. if (tokenKindIs(TOKEN_IDENTIFIER))
  466. {
  467. copyCurrentToken(name);
  468. currentToken++;
  469. }
  470. else
  471. {
  472. setSyntaxError("Expected identifier after int");
  473. return;
  474. }
  475.  
  476. expectText(";");
  477.  
  478. if (syntaxError == 0)
  479. {
  480. addSymbol(name);
  481. }
  482. }
  483.  
  484. void parseAssignment()
  485. {
  486. char target[MAX_TEXT];
  487. int targetIndex;
  488. int canUpdate;
  489.  
  490. canUpdate = 1;
  491.  
  492. if (tokenKindIs(TOKEN_IDENTIFIER))
  493. {
  494. copyCurrentToken(target);
  495. strcpy(lastIdentifier, target);
  496. currentToken++;
  497. }
  498. else
  499. {
  500. setSyntaxError("Expected identifier at start of assignment");
  501. return;
  502. }
  503.  
  504. targetIndex = findSymbolIndex(target);
  505. if (targetIndex == -1)
  506. {
  507. sprintf(tempLine, "Variable '%s' used without declaration", target);
  508. addSemanticError(tempLine);
  509. canUpdate = 0;
  510. }
  511.  
  512. expectText("=");
  513. parseExpression();
  514. expectText(";");
  515.  
  516. if (syntaxError)
  517. {
  518. return;
  519. }
  520.  
  521. if (lastExprSemanticOk == 0)
  522. {
  523. canUpdate = 0;
  524. }
  525.  
  526. if (lastExprHasOperator)
  527. {
  528. sprintf(tempLine, "t%d = %s %s %s", tempCount, lastExprLeft, lastExprOp, lastExprRight);
  529. addTAC(tempLine);
  530. sprintf(tempLine, "%s = t%d", target, tempCount);
  531. addTAC(tempLine);
  532. tempCount++;
  533. }
  534. else
  535. {
  536. sprintf(tempLine, "%s = %s", target, lastExprLeft);
  537. addTAC(tempLine);
  538. }
  539.  
  540. if (assignmentAffectsSymbol && canUpdate && lastExprValueKnown)
  541. {
  542. updateSymbolValue(target, lastExprValue);
  543. }
  544. }
  545.  
  546. void parseExpression()
  547. {
  548. int leftOk;
  549. int rightOk;
  550. int leftValue;
  551. int rightValue;
  552.  
  553. lastExprHasOperator = 0;
  554. lastExprValueKnown = 0;
  555. lastExprSemanticOk = 1;
  556. lastExprLeft[0] = '\0';
  557. lastExprOp[0] = '\0';
  558. lastExprRight[0] = '\0';
  559. lastExpressionText[0] = '\0';
  560.  
  561. readOperand(lastExprLeft);
  562. if (syntaxError)
  563. {
  564. return;
  565. }
  566.  
  567. leftOk = lastExprSemanticOk;
  568. leftValue = operandValue(lastExprLeft, leftOk);
  569.  
  570. if (tokenIs("+") || tokenIs("-"))
  571. {
  572. lastExprHasOperator = 1;
  573. copyCurrentToken(lastExprOp);
  574. currentToken++;
  575.  
  576. readOperand(lastExprRight);
  577. if (syntaxError)
  578. {
  579. return;
  580. }
  581.  
  582. rightOk = lastExprSemanticOk;
  583. rightValue = operandValue(lastExprRight, rightOk);
  584.  
  585. sprintf(lastExpressionText, "%s %s %s", lastExprLeft, lastExprOp, lastExprRight);
  586.  
  587. if (leftOk && rightOk)
  588. {
  589. lastExprValueKnown = 1;
  590. if (strcmp(lastExprOp, "+") == 0)
  591. {
  592. lastExprValue = leftValue + rightValue;
  593. }
  594. else
  595. {
  596. lastExprValue = leftValue - rightValue;
  597. }
  598. }
  599. }
  600. else
  601. {
  602. strcpy(lastExpressionText, lastExprLeft);
  603. if (leftOk)
  604. {
  605. lastExprValueKnown = 1;
  606. lastExprValue = leftValue;
  607. }
  608. }
  609. }
  610.  
  611. void readOperand(char outputText[])
  612. {
  613. int symbolIndex;
  614.  
  615. if (tokenKindIs(TOKEN_IDENTIFIER))
  616. {
  617. copyCurrentToken(outputText);
  618. symbolIndex = findSymbolIndex(outputText);
  619.  
  620. if (symbolIndex == -1)
  621. {
  622. sprintf(tempLine, "Variable '%s' used without declaration", outputText);
  623. addSemanticError(tempLine);
  624. lastExprSemanticOk = 0;
  625. }
  626.  
  627. currentToken++;
  628. }
  629. else if (tokenKindIs(TOKEN_NUMBER))
  630. {
  631. copyCurrentToken(outputText);
  632. currentToken++;
  633. }
  634. else
  635. {
  636. setSyntaxError("Expected identifier or number in expression");
  637. }
  638. }
  639.  
  640. int operandValue(char text[], int ok)
  641. {
  642. int i;
  643.  
  644. if (ok == 0)
  645. {
  646. return 0;
  647. }
  648.  
  649. if (isdigit(text[0]))
  650. {
  651. return atoi(text);
  652. }
  653.  
  654. i = findSymbolIndex(text);
  655. if (i != -1)
  656. {
  657. return symbolValue[i];
  658. }
  659.  
  660. return 0;
  661. }
  662.  
  663. void parseIfStatement()
  664. {
  665. int trueLabel;
  666. int falseLabel;
  667. int endLabel;
  668.  
  669. expectText("if");
  670. parseCondition();
  671.  
  672. if (syntaxError)
  673. {
  674. return;
  675. }
  676.  
  677. trueLabel = newLabel();
  678. falseLabel = newLabel();
  679. endLabel = newLabel();
  680.  
  681. sprintf(tempLine, "if %s %s %s goto L%d", conditionLeft, conditionOp, conditionRight, trueLabel);
  682. addTAC(tempLine);
  683. sprintf(tempLine, "goto L%d", falseLabel);
  684. addTAC(tempLine);
  685. sprintf(tempLine, "L%d:", trueLabel);
  686. addTAC(tempLine);
  687.  
  688. assignmentAffectsSymbol = 0;
  689.  
  690. if (tokenKindIs(TOKEN_IDENTIFIER))
  691. {
  692. parseAssignment();
  693. }
  694. else
  695. {
  696. setSyntaxError("Expected assignment after if condition");
  697. return;
  698. }
  699.  
  700. sprintf(tempLine, "goto L%d", endLabel);
  701. addTAC(tempLine);
  702. sprintf(tempLine, "L%d:", falseLabel);
  703. addTAC(tempLine);
  704.  
  705. if (tokenKindIs(TOKEN_ELSE))
  706. {
  707. expectText("else");
  708. }
  709. else
  710. {
  711. setSyntaxError("Expected else after if statement");
  712. return;
  713. }
  714.  
  715. if (tokenKindIs(TOKEN_IDENTIFIER))
  716. {
  717. parseAssignment();
  718. }
  719. else
  720. {
  721. setSyntaxError("Expected assignment after else");
  722. return;
  723. }
  724.  
  725. sprintf(tempLine, "L%d:", endLabel);
  726. addTAC(tempLine);
  727. assignmentAffectsSymbol = 1;
  728. }
  729.  
  730. void parseWhileStatement()
  731. {
  732. int startLabel;
  733. int bodyLabel;
  734. int endLabel;
  735.  
  736. expectText("while");
  737.  
  738. startLabel = newLabel();
  739. bodyLabel = newLabel();
  740. endLabel = newLabel();
  741.  
  742. sprintf(tempLine, "L%d:", startLabel);
  743. addTAC(tempLine);
  744.  
  745. parseCondition();
  746.  
  747. if (syntaxError)
  748. {
  749. return;
  750. }
  751.  
  752. sprintf(tempLine, "if %s %s %s goto L%d", conditionLeft, conditionOp, conditionRight, bodyLabel);
  753. addTAC(tempLine);
  754. sprintf(tempLine, "goto L%d", endLabel);
  755. addTAC(tempLine);
  756. sprintf(tempLine, "L%d:", bodyLabel);
  757. addTAC(tempLine);
  758.  
  759. assignmentAffectsSymbol = 0;
  760.  
  761. if (tokenKindIs(TOKEN_IDENTIFIER))
  762. {
  763. parseAssignment();
  764. }
  765. else
  766. {
  767. setSyntaxError("Expected assignment after while condition");
  768. return;
  769. }
  770.  
  771. sprintf(tempLine, "goto L%d", startLabel);
  772. addTAC(tempLine);
  773. sprintf(tempLine, "L%d:", endLabel);
  774. addTAC(tempLine);
  775.  
  776. assignmentAffectsSymbol = 1;
  777. }
  778.  
  779. void parseCondition()
  780. {
  781. int symbolIndex;
  782.  
  783. expectText("(");
  784.  
  785. if (tokenKindIs(TOKEN_IDENTIFIER) || tokenKindIs(TOKEN_NUMBER))
  786. {
  787. copyCurrentToken(conditionLeft);
  788.  
  789. if (tokenKindIs(TOKEN_IDENTIFIER))
  790. {
  791. symbolIndex = findSymbolIndex(conditionLeft);
  792. if (symbolIndex == -1)
  793. {
  794. sprintf(tempLine, "Variable '%s' used without declaration", conditionLeft);
  795. addSemanticError(tempLine);
  796. }
  797. }
  798.  
  799. currentToken++;
  800. }
  801. else
  802. {
  803. setSyntaxError("Expected left operand in condition");
  804. return;
  805. }
  806.  
  807. if (tokenIs("<") || tokenIs(">"))
  808. {
  809. copyCurrentToken(conditionOp);
  810. currentToken++;
  811. }
  812. else
  813. {
  814. setSyntaxError("Expected relational operator in condition");
  815. return;
  816. }
  817.  
  818. if (tokenKindIs(TOKEN_IDENTIFIER) || tokenKindIs(TOKEN_NUMBER))
  819. {
  820. copyCurrentToken(conditionRight);
  821.  
  822. if (tokenKindIs(TOKEN_IDENTIFIER))
  823. {
  824. symbolIndex = findSymbolIndex(conditionRight);
  825. if (symbolIndex == -1)
  826. {
  827. sprintf(tempLine, "Variable '%s' used without declaration", conditionRight);
  828. addSemanticError(tempLine);
  829. }
  830. }
  831.  
  832. currentToken++;
  833. }
  834. else
  835. {
  836. setSyntaxError("Expected right operand in condition");
  837. return;
  838. }
  839.  
  840. expectText(")");
  841. }
  842.  
  843. int findSymbolIndex(char name[])
  844. {
  845. int i;
  846.  
  847. for (i = 0; i < symbolCount; i++)
  848. {
  849. if (strcmp(symbolName[i], name) == 0)
  850. {
  851. return i;
  852. }
  853. }
  854.  
  855. return -1;
  856. }
  857.  
  858. void addSymbol(char name[])
  859. {
  860. if (findSymbolIndex(name) != -1)
  861. {
  862. sprintf(tempLine, "Duplicate declaration of variable '%s'", name);
  863. addSemanticError(tempLine);
  864. return;
  865. }
  866.  
  867. if (symbolCount >= MAX_SYMBOLS)
  868. {
  869. addSemanticError("Symbol table is full");
  870. return;
  871. }
  872.  
  873. strcpy(symbolName[symbolCount], name);
  874. strcpy(symbolType[symbolCount], "int");
  875. symbolValue[symbolCount] = 0;
  876. symbolCount++;
  877. }
  878.  
  879. void updateSymbolValue(char name[], int value)
  880. {
  881. int index;
  882.  
  883. index = findSymbolIndex(name);
  884. if (index != -1)
  885. {
  886. symbolValue[index] = value;
  887. }
  888. }
  889.  
  890. void addSemanticError(char message[])
  891. {
  892. if (semanticErrorCount >= MAX_ERRORS)
  893. {
  894. return;
  895. }
  896.  
  897. strcpy(semanticErrors[semanticErrorCount], message);
  898. semanticErrorCount++;
  899. }
  900.  
  901. void addTAC(char line[])
  902. {
  903. if (tacCount >= MAX_TAC)
  904. {
  905. return;
  906. }
  907.  
  908. strcpy(tac[tacCount], line);
  909. tacCount++;
  910. }
  911.  
  912. int newLabel()
  913. {
  914. int label;
  915.  
  916. label = labelCount;
  917. labelCount++;
  918. return label;
  919. }
  920.  
  921. void printSyntaxResult()
  922. {
  923. if (syntaxError)
  924. {
  925. printf("Parsing Failed\n");
  926. printf("Syntax Error: %s\n", syntaxErrorMessage);
  927. }
  928. else
  929. {
  930. printf("Parsing Successful\n");
  931. printf("No Syntax Error Found\n");
  932. }
  933. }
  934.  
  935. void printSymbolTable()
  936. {
  937. int i;
  938.  
  939. printf("Variable\tType\tValue\n");
  940. printf("-----------------------------\n");
  941.  
  942. if (symbolCount == 0)
  943. {
  944. printf("No symbols found\n");
  945. return;
  946. }
  947.  
  948. for (i = 0; i < symbolCount; i++)
  949. {
  950. printf("%s\t\t%s\t%d\n", symbolName[i], symbolType[i], symbolValue[i]);
  951. }
  952. }
  953.  
  954. void printSemanticErrors()
  955. {
  956. int i;
  957.  
  958. if (semanticErrorCount == 0)
  959. {
  960. printf("No Semantic Error Found\n");
  961. return;
  962. }
  963.  
  964. for (i = 0; i < semanticErrorCount; i++)
  965. {
  966. printf("Semantic Error: %s\n", semanticErrors[i]);
  967. }
  968. }
  969.  
  970. void printTAC()
  971. {
  972. int i;
  973.  
  974. if (tacCount == 0)
  975. {
  976. printf("No TAC generated\n");
  977. return;
  978. }
  979.  
  980. for (i = 0; i < tacCount; i++)
  981. {
  982. printf("%s\n", tac[i]);
  983. }
  984. }
  985.  
Success #stdin #stdout 0s 5316KB
stdin
int a;
int b;
a = 5;
b = 10;

if(a < b)
    a = a + 1;
else
    a = a - 1;

while(a < 10)
    a = a + 1;
stdout
Error: input.txt file not found.
========== Source Program ==========
No source code loaded.

========== 1. Tokens ==========


========== 2. Syntax Analysis Output ==========
Parsing Successful
No Syntax Error Found

========== 3. Symbol Table ==========
Variable	Type	Value
-----------------------------
No symbols found

========== 4. Semantic Analysis Output ==========
No Semantic Error Found

========== 5. Three Address Code ==========
No TAC generated