SHARE
TWEET

main.cpp

a guest Feb 16th, 2020 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.   Simple integer arithmetic calculator according to the following BNF
  3.   exps      --> exp | exp NEWLINE exps
  4.   exp       --> term {addop term}
  5.   addop     --> + | -
  6.   term      --> factor {mulop factor}
  7.   mulop     --> * | /
  8.   factor    --> ( exp ) | INT
  9. */
  10.  
  11. #include <iostream>
  12. #include <iomanip>
  13. #include <fstream>
  14. #include <sstream>
  15. #include <string>
  16. #include "tokens.h"
  17. #include "FlexLexer.h"
  18.  
  19. using namespace std;
  20.  
  21. string toknames[] = {
  22. "INT", "LPAREN", "RPAREN", "PLUS", "MINUS", "TIMES", "DIVIDE", "NEWLINE"
  23. };
  24.  
  25. string tokname(int tok) {
  26.   return tok<257 || tok>264 ? "BAD_TOKEN" : toknames[tok-257];
  27. }
  28.  
  29. yyFlexLexer         lexer;
  30. YYSTYPE             yylval;
  31.  
  32. int     nextToken;  //global variable stores the token to be processed
  33.  
  34. void readNextToken( void ); //read the next token into the variable nextToken
  35.  
  36. void exps( void );  //process all expressions in the input
  37. int  exp( void );   //returns the integer value of an expression
  38. int term ( void );  //returns the integer value of an term
  39. int factor( void ); //returns the integer value of an factor
  40.  
  41. //If the next token matches expectedToken, read the next token and return true
  42. //otherwise, print an error message and return false
  43. bool match( int expectedToken );
  44.  
  45. //print the error message
  46. void error( string errorMsg );
  47.  
  48. //skip the rest of the line
  49. void skipline( void );
  50.  
  51. int main(int argc, char **argv) {
  52.     ifstream    ifs;
  53.    
  54.     if (argc!=2)
  55.     {
  56.         cerr << "usage: " << argv[0] << " filename" << endl;
  57.         return 1;  
  58.     }
  59.     ifs.open( argv[1] );
  60.     if( !ifs )
  61.     {
  62.         cerr << "Input file cannot be opened.\n";
  63.         return 0;
  64.     }
  65.     cout << "Lexcial Analysis of the file " << argv[1] << endl;
  66.    
  67.     lexer.switch_streams(&ifs, NULL);
  68.  
  69.     readNextToken();
  70.  
  71.     exps();
  72.  
  73.     return 0;
  74. }
  75.  
  76. //print the error message, and
  77. //terminate the program
  78. void error( string errorMsg )
  79. {
  80.     cout << errorMsg << endl;
  81.     exit(1);
  82. }
  83.  
  84. //skip the rest of the line
  85. void skipline( void )
  86. {
  87.     while( nextToken != NEWLINE && nextToken != 0 )
  88.         readNextToken();
  89. }
  90.  
  91.  
  92. //read the next token into the variable nextToken
  93. void readNextToken( void )
  94. {
  95.     nextToken = lexer.yylex();
  96. }
  97.  
  98. //If the next token is expectedToken, call readNextToken and return true,
  99. //otherwise, print an error message and return false
  100. bool match( int expectedToken )
  101. {
  102.     if ( expectedToken == nextToken )
  103.     {
  104.         readNextToken();
  105.         return true;
  106.     }
  107.     else
  108.         return false;
  109. }
  110.  
  111. void exps( void )
  112. {
  113.     int     count = 1;
  114.     int     value;
  115.  
  116.     do
  117.     {
  118.         try {
  119.             value = exp();
  120.             cout << "expression " << count << " : " << value << endl;
  121.         } catch(runtime_error e) {
  122.             skipline();
  123.             cout << "expression " << count << " :    wrong syntax -- " << e.what() << endl;
  124.         }
  125.         count ++;
  126.     } while ( match(NEWLINE) );
  127. }
  128.  
  129. //returns the integer value of an expression
  130. int exp( void )
  131. {
  132.     string token;
  133.     token = tokname(nextToken);
  134.     readNextToken();
  135.     int temp = term();
  136.     while ((token == "PLUS") || (token == "MINUS"))
  137.     {
  138.         if (token == "PLUS")
  139.         {
  140.             temp += term();
  141.         }
  142.         else if (token == "MINUS")
  143.         {
  144.             temp -= term();
  145.         }
  146.  
  147.     }
  148.     return (temp);
  149.     //PUT YOUR IMPLEMENTATION HERE
  150. }
  151. //returns int value of a term
  152. int term ( void )
  153. {
  154.     string token;
  155.     token = tokname(nextToken);
  156.     readNextToken();
  157.     int temp = factor();
  158.     while ((token == "TIMES") || (token == "DIVIDE"))
  159.     {
  160.         if (token == "TIMES")
  161.         {
  162.             temp *= factor();
  163.         }
  164.         else if (token == "DIVIDE")
  165.         {
  166.             temp = temp / factor();
  167.         }
  168.  
  169.     }
  170.     return(temp);
  171.     //PUT YOUR IMPLEMENTATION HERE
  172. }
  173. //returns int value of factor
  174. int factor( void )
  175. {
  176.     string token;
  177.     token = tokname(nextToken);
  178.     readNextToken();
  179.     int temp;
  180.  
  181.     if (token == "INT")
  182.     {
  183.         temp = yylval.ival;
  184.         return(temp);
  185.     }
  186.     else if (token == "LPAREN")
  187.     {
  188.         readNextToken();
  189.        
  190.         temp = exp();
  191.  
  192.         readNextToken();
  193.  
  194.     }
  195.     else
  196.     {
  197.         throw runtime_error("Syntax error");
  198.     }
  199.  
  200.  
  201. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top