Advertisement
Guest User

Untitled

a guest
Apr 12th, 2016
658
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.76 KB | None | 0 0
  1. /*
  2.  * parser.cpp
  3.  *
  4.  *  Created on: Apr 6, 2016
  5.  *      Author: Abe
  6.  */
  7.  
  8. #include <iostream>
  9. #include <string>
  10. #include "p2lex.h"
  11. #include "Value.h"
  12. #include "ParseTree.h"
  13. #include <vector>
  14. #include <fstream>
  15. #include <sstream> //includes string stream
  16. #include <istream>
  17. #include <map>
  18. #include <cctype>
  19. #include <cstring>
  20.  
  21. using namespace std;
  22.  
  23.  
  24.  
  25. ParseTree *Program(istream *);
  26. ParseTree *StmtList(istream *);
  27. ParseTree *Stmt(istream *);
  28. ParseTree *Aop(istream *);
  29. ParseTree *Expr(istream *);
  30. ParseTree *Term(istream *);
  31. ParseTree *Primary(istream *);
  32.  
  33. void
  34. printError(string err)
  35. {
  36.     cerr << linenum << ": " << err;
  37. }
  38.  
  39. void postOrder(ParseTree * root)
  40. {
  41.     if(root==NULL)
  42.         return;
  43.  
  44.     postOrder(root->left);
  45.     postOrder(root->right);
  46.  
  47.  
  48.  
  49.  
  50.     // cout<< root->isAddOp() << endl;
  51.     // cout<< root->isSubOp() << endl;
  52.     // cout<< root->isAssOp() << endl;
  53.     // cout << root->isIConst() << endl;
  54.     // cout << root->isMOp() << endl;
  55.     // cout << "Var " << root->isVariable() << endl;
  56.     // cout<< "Sconst " << root->isSConst() << endl;
  57.  
  58. }
  59.  
  60.  
  61.  
  62. ParseTree *
  63. Program(istream *br)
  64. {
  65.     //cout << br->gcount << endl;
  66.     ParseTree *sl = StmtList(br);  //This is the parsetree, or should be.
  67.  
  68.     if( sl == 0 ) {
  69.         printError("empty program!");
  70.     }
  71.  
  72.     return sl;
  73. }
  74.  
  75. ParseTree *
  76. StmtList(istream *br)
  77. {
  78.     ParseTree *stmt = Stmt(br);
  79.     if( stmt != 0 ) {
  80.         return new StatementList(stmt, StmtList(br) );
  81.     }
  82.  
  83.     return stmt;
  84. }
  85.  
  86. ParseTree *
  87. Stmt(istream *br)
  88. {  istream *second=br; //Might be a problem
  89.  
  90.    Token t=getToken(br);
  91.  
  92.    if(t==Token::PRINTKW)
  93.    {
  94.        ParseTree * ex=Aop(br);
  95.  
  96.        if(ex==0)
  97.            return 0;
  98.        else if(ex!=0)
  99.        {
  100.  
  101.        if(t!=Token::SC)
  102.        {
  103.            printError("Missing semicolon at end.");
  104.  
  105.            return new PrintStatement(ex);
  106.        }
  107.  
  108.        return new PrintStatement(ex);
  109.        }
  110.  
  111.        //return new PrintStatement(ex);
  112.        }
  113.    else if(t==Token::INTKW)
  114.    {
  115.        t=getToken(br);
  116.        if(t!=Token::VAR)
  117.            return 0;
  118.        string var = t.getLexeme();
  119.        t=getToken(br);
  120.  
  121.        if(t!=Token::SC)
  122.        {
  123.            printError("Missing semicolon at end");
  124.            return new DeclStatement(INTEGER,var);
  125.        }
  126.  
  127.        return new DeclStatement(INTEGER,var);
  128.  
  129.  
  130.    }
  131.    else if(t==Token::STRKW)
  132.    {
  133.        t=getToken(br);
  134.        if(t!=Token::VAR)
  135.            return 0;
  136.        string var = t.getLexeme();
  137.  
  138.        t=getToken(br);
  139.        if(t!=Token::SC)
  140.        {
  141.            printError("Missing semicolon at end");
  142.            return new DeclStatement(STRING, var);
  143.        }
  144.  
  145.        return new DeclStatement(STRING, var);
  146.  
  147.    }
  148.    else if(t==Token::VAR)//In Aop
  149.    {
  150.     ParseTree * lhs = new Variable(t.getLexeme());
  151.     t=getToken(br);
  152.     if(t==Token::EQOP)
  153.     {
  154.     ParseTree * rhs = Aop(br);
  155.     if(rhs==0)
  156.     {
  157.        printError("Missing Proper Aop");
  158.         return 0;
  159.     }
  160.     else
  161.     {
  162.         return new AssignOp(lhs, rhs);
  163.     }
  164.     }
  165.  
  166.  
  167.    }
  168.  
  169.  
  170.  
  171.     return 0;
  172. }//Should be alright
  173.  
  174. //Completely Different Aop
  175. /* ParseTree * Aop(istream *br)
  176. {
  177.     ParseTree* element = Expr(br);
  178.  
  179.     Token t=getToken(br);
  180.     if(element!=0 && element->isVariable())
  181.     {
  182.     if(t==Token::EQOP)
  183.     {
  184.         ParseTree * rhs= Aop(br);
  185.         if(rhs==0)
  186.             return 0;
  187.         else
  188.         {
  189.             return new AssignOp(element, rhs);
  190.         }
  191.  
  192.  
  193.     }
  194.     else
  195.     {
  196.         return element;
  197.     }
  198.     }
  199.  
  200.     return 0;
  201. }*/
  202.  
  203. //Professor's AoP()
  204. ParseTree * Aop(istream *br)
  205. {
  206.  ParseTree * element = Expr(br);
  207.  if(element!=0)
  208.  {
  209.  
  210.  if(element->isVariable())
  211.  {
  212.      cout << "yeah" << endl;
  213.      Token t= getToken(br);
  214.      cout<< "t.lexeme " << t.getLexeme() << endl;
  215.      if(t==Token::EQOP)
  216.      { cout<<"No" << endl;
  217.          ParseTree * rhs = Aop(br);
  218.          if(rhs==0)
  219.              return 0;
  220.          else
  221.          {
  222.              return new AssignOp(element, rhs);
  223.          }
  224.      }
  225.      else
  226.      {
  227.  
  228.          return element;
  229.      }
  230.  }
  231.  /*if(element!=0)
  232.      if(!(element->isVariable()))
  233.              return element;*/
  234.  
  235.  }
  236.    return 0;
  237.  
  238. }//Bit iffy, but should be good
  239.  
  240. /*ParseTree * Aop(istream *br)
  241. {
  242.     cout<<"Aop accessed" << endl;
  243.     //cout<< "Stream size: " <<br->gcount() <<endl;
  244.     //bool didit=false;
  245.     ParseTree * ex=Expr(br);
  246.  
  247.     if(ex==0)
  248.         return 0;
  249.  
  250.    if((ex->isVariable()))
  251.     {
  252.        cout<< "Ever accessed? " << endl;
  253.        Token t=getToken(br);
  254.        cout<<"T.lexeme" << t.getLexeme()<< endl;
  255.  
  256.         if(t!=Token::EQOP || t!=Token::SC)
  257.         {cout<<"Ex accessed?" << endl;
  258.             return ex;
  259.         }
  260.         else{
  261.         cout<<"Aop equal " << endl;
  262.         ParseTree *rhs=Aop(br);
  263.         if(rhs==0)
  264.         {
  265.             return 0;
  266.         }
  267.         //didit=true;
  268.         cout<<"Return new assign" << endl;
  269.         return new AssignOp(ex, rhs); //He said not a parsetree but Aop, assuming he meant to use this. Now changed Aop to accomodate parsetree not expression.
  270.  
  271.         }
  272.  
  273.     }
  274.    else
  275.    {
  276.  
  277.    if(ex!=0) //For the just Expr case Maybe: if(ex!=0 && !didit)
  278.        {cout<<"Aop not a var" << endl;
  279.        return ex;
  280.        }
  281.    }
  282.    return 0;
  283.  
  284. }//Bit iffy, but should be good*/
  285.  
  286. /*ParseTree * Expr(istream *br)
  287. {
  288.     ParseTree *ex=Term(br);
  289.  
  290.     //ParseTree *ex=Expr(br); Would cause infinite loop.
  291.  
  292.     if(ex==0)
  293.     { cout<< "Expr term is zero" << endl;
  294.         return 0; //An error occured, not an expr
  295.     }
  296.  
  297.     Token t= getToken(br);
  298.  
  299.     while(t==Token::PLUSOP || t==Token::MINUSOP)
  300.     {
  301.         if(t==Token::PLUSOP)
  302.         {
  303.             ParseTree *ex2=Term(br);
  304.  
  305.             if(ex2==0)
  306.             {
  307.                 printError("Missing term to be added.");
  308.                 return 0;
  309.             }
  310.             t=getToken(br);
  311.             return new AddOp(ex,ex2);
  312.  
  313.         }
  314.  
  315.         if(t==Token::MINUSOP)
  316.         {
  317.             ParseTree *ex2 = Term(br);
  318.             if (ex2 == 0) {
  319.                 printError("Missing term to be added.");
  320.                 return 0;
  321.             }
  322.             t=getToken(br);
  323.             return new SubtractOp(ex, ex2);
  324.  
  325.         }
  326. }
  327.  
  328.     if(ex!=0) //This is a term, or the case where its just a term
  329.         return ex;
  330.  
  331.     return 0;
  332. }*/
  333.  
  334. //To go along with Professor Expr
  335.  
  336. ParseTree* Expr(istream *br)
  337. {   ParseTree * element = Term(br);
  338.     if(element!=0)
  339.     {
  340.         Token t=getToken(br);
  341.         if(t==Token::MINUSOP || t==Token::PLUSOP)
  342.         {
  343.             if(t==Token::PLUSOP)
  344.             {
  345.                 ParseTree* rhs = Expr(br);
  346.                 if(rhs==0)
  347.                     return 0;
  348.                 else
  349.                 {
  350.                   return new AddOp(element, rhs);
  351.                 }
  352.             }
  353.  
  354.             if(t==Token::MINUSOP)
  355.             {
  356.                 ParseTree* rhs = Expr(br);
  357.                 if(rhs==0)
  358.                     return 0;
  359.                 else
  360.                 {
  361.                 return new SubtractOp(element, rhs); //or switch the inputs idk
  362.                 }
  363.             }
  364.         }
  365.         else
  366.         {
  367.             return element;
  368.         }
  369.     }
  370.     return 0;
  371. }
  372.  
  373. /*ParseTree * Term(istream *br)
  374. {
  375. //cout<< "Term accessed" << endl;
  376.     string word;
  377.     if(!br->good())
  378.         cout<<"Its empty?" << endl;
  379.  
  380.  
  381.     ParseTree * ex=Primary(br);
  382.  
  383.    ;
  384.     if(ex==0)
  385.     {
  386.             cout<< "Ex term primary is zero" << endl;
  387.             return 0;
  388.      }
  389.  
  390.     Token t= getToken(br);
  391.  
  392.     if(t==Token::STAROP)
  393.     {
  394.          ParseTree *ex2 = Primary(br);
  395.          if(ex2==0)
  396.          {
  397.              printError("Missing primary term to multiply by");
  398.              return 0;
  399.          }
  400.  
  401.          return new MultiplyOp(ex,ex2);
  402.     }
  403.  
  404.     if(ex!=0) //Case of just a primary
  405.         return ex;
  406.  
  407.  
  408.  
  409.  
  410.     return 0;
  411. }
  412. */
  413. //To go along with Professor term
  414. ParseTree * Term(istream *br)
  415. {
  416.     ParseTree *element = Primary(br);
  417.  
  418.  
  419.     if(element!=0)
  420.     {
  421.  
  422.         Token t=getToken(br);
  423.     if(t==Token::STAROP)
  424.     {
  425.         ParseTree* rhs =Term(br);
  426.         if(rhs==0)
  427.             return 0;
  428.         else
  429.         {
  430.             return new MultiplyOp(element, rhs);
  431.         }
  432.     }
  433.     else if(element!=0)
  434.     {
  435.         return element;
  436.     }
  437.     }
  438.  
  439.     return 0;
  440. }
  441.  
  442. ParseTree * Primary(istream *br)
  443. {
  444.     vector<Token> v;
  445.     vector<string> l;
  446.  
  447.     Token t;
  448.     t=getToken(br);
  449.    /* v.push_back(t);
  450.  
  451.     while(br->good())
  452.     {
  453.         t=getToken(br);
  454.         v.push_back(t);
  455.     }
  456.  
  457.     for(int i=0; i<v.size(); i++)
  458.     {
  459.         cout<<"In Prime: Type: " << v.at(i).getType() << " Lex: " << v.at(i).getLexeme() << endl;
  460.     }
  461.  
  462.  
  463.     //cout<< "Primary accessed " << endl;
  464.  
  465.     while(br->good())
  466.     {
  467.         Token t=getToken(br);
  468.     }*/
  469.  
  470.     cout<< "Lex " << t.getLexeme() << endl;
  471.  
  472.  
  473.     if(t==Token::SCONST)
  474.     {
  475.         cout<< "Term: SCONST" << endl;
  476.         return new SConstant(t.getLexeme());
  477.  
  478.     }
  479.     else if(t==Token::ICONST)
  480.     {
  481.         cout<< "Term: ICONST" <<endl;
  482.         return new IConstant(t.getLexeme());
  483.  
  484.     }
  485.     else if(t==Token::VAR)
  486.     {
  487.         cout<<"Term: VAR " << t.getLexeme() << endl;
  488.         return new Variable(t.getLexeme());
  489.  
  490.     }
  491.     else if(t==Token::LPAREN)
  492.     {
  493.         cout<<"IN LPAREN" << endl;
  494.         //This is a special case, the left paren must be followed by an AOP
  495.         ParseTree *ex= Aop(br); //Of Type parse tree
  496.  
  497.         if(ex==0)
  498.         {
  499.             printError("Aop error in Prime");
  500.             return 0; //An error occured this must be an Aop
  501.         }
  502.  
  503.         t=getToken(br); //At this point t used to equal ( now it will equal right paren, since the middle Aop meat has been "eaten"
  504.  
  505.         if(t!=Token::RPAREN)
  506.         {
  507.             printError("Missing RPAREN");
  508.             return 0; //Not followed bt right paren, this is an error
  509.         }
  510.  
  511.         return ex;
  512.  
  513.  
  514.     }//By this point the passed pointer is either an SCONST, ICONST, VAR, or an LPAREN Aop RPAREN, if it gets past this an error exists
  515.  
  516.     return 0;
  517. }
  518.  
  519. int main()
  520. {
  521.  
  522.     ifstream file;
  523.     istream *input;
  524.  
  525.     file.open("test.txt");
  526.  
  527.  
  528. if(file.is_open())
  529. {
  530.     input=&file;
  531.     cout<<"Should be open m8" << endl;
  532. }
  533.  
  534.  string empty="";
  535.  //cout<<"Empty: " << endl;
  536.  //cout<< "Size of empty: " <<empty.size() << endl;
  537.  
  538.     cout << "Hello World! Its a me Mario! " << endl;
  539.  
  540.     ParseTree *t = new ParseTree();
  541.  
  542.     t=Program(input);
  543.     postOrder(t);
  544.  
  545.     cout<< t->IntegerCount() << " ICONST, " << t->StringCount() << " SCONST, " << t->VarCount() << " VARIABLES" << endl;
  546.  
  547.     cout<<"Number of Add operators: " << t->AdditionCount() << endl;
  548.     cout<<"Number of subtract operators: " << t->SubtractionCount() << endl;
  549.     cout<<"Number of multiplication operators: " << t->MultiplicationCount() << endl;
  550.     cout<<"Number of Assignment operators: "<< t->EqualCount() << endl;
  551.  
  552.  
  553.    // cout << "Var count " << t->VarCount() << endl;
  554.     //cout<< "StringConst. count " << t->StringCount() << endl;
  555.    // cout << "Equal Count" << t->EqualCount() << endl;
  556.  
  557.  
  558.  
  559.  
  560.  
  561.  
  562.  
  563. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement