Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * parser.cpp
- *
- * Created on: Apr 6, 2016
- * Author: Abe
- */
- #include <iostream>
- #include <string>
- #include "p2lex.h"
- #include "Value.h"
- #include "ParseTree.h"
- #include <vector>
- #include <fstream>
- #include <sstream> //includes string stream
- #include <istream>
- #include <map>
- #include <cctype>
- #include <cstring>
- using namespace std;
- ParseTree *Program(istream *);
- ParseTree *StmtList(istream *);
- ParseTree *Stmt(istream *);
- ParseTree *Aop(istream *);
- ParseTree *Expr(istream *);
- ParseTree *Term(istream *);
- ParseTree *Primary(istream *);
- void
- printError(string err)
- {
- cerr << linenum << ": " << err;
- }
- void postOrder(ParseTree * root)
- {
- if(root==NULL)
- return;
- postOrder(root->left);
- postOrder(root->right);
- // cout<< root->isAddOp() << endl;
- // cout<< root->isSubOp() << endl;
- // cout<< root->isAssOp() << endl;
- // cout << root->isIConst() << endl;
- // cout << root->isMOp() << endl;
- // cout << "Var " << root->isVariable() << endl;
- // cout<< "Sconst " << root->isSConst() << endl;
- }
- ParseTree *
- Program(istream *br)
- {
- //cout << br->gcount << endl;
- ParseTree *sl = StmtList(br); //This is the parsetree, or should be.
- if( sl == 0 ) {
- printError("empty program!");
- }
- return sl;
- }
- ParseTree *
- StmtList(istream *br)
- {
- ParseTree *stmt = Stmt(br);
- if( stmt != 0 ) {
- return new StatementList(stmt, StmtList(br) );
- }
- return stmt;
- }
- ParseTree *
- Stmt(istream *br)
- { istream *second=br; //Might be a problem
- Token t=getToken(br);
- if(t==Token::PRINTKW)
- {
- ParseTree * ex=Aop(br);
- if(ex==0)
- return 0;
- else if(ex!=0)
- {
- if(t!=Token::SC)
- {
- printError("Missing semicolon at end.");
- return new PrintStatement(ex);
- }
- return new PrintStatement(ex);
- }
- //return new PrintStatement(ex);
- }
- else if(t==Token::INTKW)
- {
- t=getToken(br);
- if(t!=Token::VAR)
- return 0;
- string var = t.getLexeme();
- t=getToken(br);
- if(t!=Token::SC)
- {
- printError("Missing semicolon at end");
- return new DeclStatement(INTEGER,var);
- }
- return new DeclStatement(INTEGER,var);
- }
- else if(t==Token::STRKW)
- {
- t=getToken(br);
- if(t!=Token::VAR)
- return 0;
- string var = t.getLexeme();
- t=getToken(br);
- if(t!=Token::SC)
- {
- printError("Missing semicolon at end");
- return new DeclStatement(STRING, var);
- }
- return new DeclStatement(STRING, var);
- }
- else if(t==Token::VAR)//In Aop
- {
- ParseTree * lhs = new Variable(t.getLexeme());
- t=getToken(br);
- if(t==Token::EQOP)
- {
- ParseTree * rhs = Aop(br);
- if(rhs==0)
- {
- printError("Missing Proper Aop");
- return 0;
- }
- else
- {
- return new AssignOp(lhs, rhs);
- }
- }
- }
- return 0;
- }//Should be alright
- //Completely Different Aop
- /* ParseTree * Aop(istream *br)
- {
- ParseTree* element = Expr(br);
- Token t=getToken(br);
- if(element!=0 && element->isVariable())
- {
- if(t==Token::EQOP)
- {
- ParseTree * rhs= Aop(br);
- if(rhs==0)
- return 0;
- else
- {
- return new AssignOp(element, rhs);
- }
- }
- else
- {
- return element;
- }
- }
- return 0;
- }*/
- //Professor's AoP()
- ParseTree * Aop(istream *br)
- {
- ParseTree * element = Expr(br);
- if(element!=0)
- {
- if(element->isVariable())
- {
- cout << "yeah" << endl;
- Token t= getToken(br);
- cout<< "t.lexeme " << t.getLexeme() << endl;
- if(t==Token::EQOP)
- { cout<<"No" << endl;
- ParseTree * rhs = Aop(br);
- if(rhs==0)
- return 0;
- else
- {
- return new AssignOp(element, rhs);
- }
- }
- else
- {
- return element;
- }
- }
- /*if(element!=0)
- if(!(element->isVariable()))
- return element;*/
- }
- return 0;
- }//Bit iffy, but should be good
- /*ParseTree * Aop(istream *br)
- {
- cout<<"Aop accessed" << endl;
- //cout<< "Stream size: " <<br->gcount() <<endl;
- //bool didit=false;
- ParseTree * ex=Expr(br);
- if(ex==0)
- return 0;
- if((ex->isVariable()))
- {
- cout<< "Ever accessed? " << endl;
- Token t=getToken(br);
- cout<<"T.lexeme" << t.getLexeme()<< endl;
- if(t!=Token::EQOP || t!=Token::SC)
- {cout<<"Ex accessed?" << endl;
- return ex;
- }
- else{
- cout<<"Aop equal " << endl;
- ParseTree *rhs=Aop(br);
- if(rhs==0)
- {
- return 0;
- }
- //didit=true;
- cout<<"Return new assign" << endl;
- 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.
- }
- }
- else
- {
- if(ex!=0) //For the just Expr case Maybe: if(ex!=0 && !didit)
- {cout<<"Aop not a var" << endl;
- return ex;
- }
- }
- return 0;
- }//Bit iffy, but should be good*/
- /*ParseTree * Expr(istream *br)
- {
- ParseTree *ex=Term(br);
- //ParseTree *ex=Expr(br); Would cause infinite loop.
- if(ex==0)
- { cout<< "Expr term is zero" << endl;
- return 0; //An error occured, not an expr
- }
- Token t= getToken(br);
- while(t==Token::PLUSOP || t==Token::MINUSOP)
- {
- if(t==Token::PLUSOP)
- {
- ParseTree *ex2=Term(br);
- if(ex2==0)
- {
- printError("Missing term to be added.");
- return 0;
- }
- t=getToken(br);
- return new AddOp(ex,ex2);
- }
- if(t==Token::MINUSOP)
- {
- ParseTree *ex2 = Term(br);
- if (ex2 == 0) {
- printError("Missing term to be added.");
- return 0;
- }
- t=getToken(br);
- return new SubtractOp(ex, ex2);
- }
- }
- if(ex!=0) //This is a term, or the case where its just a term
- return ex;
- return 0;
- }*/
- //To go along with Professor Expr
- ParseTree* Expr(istream *br)
- { ParseTree * element = Term(br);
- if(element!=0)
- {
- Token t=getToken(br);
- if(t==Token::MINUSOP || t==Token::PLUSOP)
- {
- if(t==Token::PLUSOP)
- {
- ParseTree* rhs = Expr(br);
- if(rhs==0)
- return 0;
- else
- {
- return new AddOp(element, rhs);
- }
- }
- if(t==Token::MINUSOP)
- {
- ParseTree* rhs = Expr(br);
- if(rhs==0)
- return 0;
- else
- {
- return new SubtractOp(element, rhs); //or switch the inputs idk
- }
- }
- }
- else
- {
- return element;
- }
- }
- return 0;
- }
- /*ParseTree * Term(istream *br)
- {
- //cout<< "Term accessed" << endl;
- string word;
- if(!br->good())
- cout<<"Its empty?" << endl;
- ParseTree * ex=Primary(br);
- ;
- if(ex==0)
- {
- cout<< "Ex term primary is zero" << endl;
- return 0;
- }
- Token t= getToken(br);
- if(t==Token::STAROP)
- {
- ParseTree *ex2 = Primary(br);
- if(ex2==0)
- {
- printError("Missing primary term to multiply by");
- return 0;
- }
- return new MultiplyOp(ex,ex2);
- }
- if(ex!=0) //Case of just a primary
- return ex;
- return 0;
- }
- */
- //To go along with Professor term
- ParseTree * Term(istream *br)
- {
- ParseTree *element = Primary(br);
- if(element!=0)
- {
- Token t=getToken(br);
- if(t==Token::STAROP)
- {
- ParseTree* rhs =Term(br);
- if(rhs==0)
- return 0;
- else
- {
- return new MultiplyOp(element, rhs);
- }
- }
- else if(element!=0)
- {
- return element;
- }
- }
- return 0;
- }
- ParseTree * Primary(istream *br)
- {
- vector<Token> v;
- vector<string> l;
- Token t;
- t=getToken(br);
- /* v.push_back(t);
- while(br->good())
- {
- t=getToken(br);
- v.push_back(t);
- }
- for(int i=0; i<v.size(); i++)
- {
- cout<<"In Prime: Type: " << v.at(i).getType() << " Lex: " << v.at(i).getLexeme() << endl;
- }
- //cout<< "Primary accessed " << endl;
- while(br->good())
- {
- Token t=getToken(br);
- }*/
- cout<< "Lex " << t.getLexeme() << endl;
- if(t==Token::SCONST)
- {
- cout<< "Term: SCONST" << endl;
- return new SConstant(t.getLexeme());
- }
- else if(t==Token::ICONST)
- {
- cout<< "Term: ICONST" <<endl;
- return new IConstant(t.getLexeme());
- }
- else if(t==Token::VAR)
- {
- cout<<"Term: VAR " << t.getLexeme() << endl;
- return new Variable(t.getLexeme());
- }
- else if(t==Token::LPAREN)
- {
- cout<<"IN LPAREN" << endl;
- //This is a special case, the left paren must be followed by an AOP
- ParseTree *ex= Aop(br); //Of Type parse tree
- if(ex==0)
- {
- printError("Aop error in Prime");
- return 0; //An error occured this must be an Aop
- }
- t=getToken(br); //At this point t used to equal ( now it will equal right paren, since the middle Aop meat has been "eaten"
- if(t!=Token::RPAREN)
- {
- printError("Missing RPAREN");
- return 0; //Not followed bt right paren, this is an error
- }
- return ex;
- }//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
- return 0;
- }
- int main()
- {
- ifstream file;
- istream *input;
- file.open("test.txt");
- if(file.is_open())
- {
- input=&file;
- cout<<"Should be open m8" << endl;
- }
- string empty="";
- //cout<<"Empty: " << endl;
- //cout<< "Size of empty: " <<empty.size() << endl;
- cout << "Hello World! Its a me Mario! " << endl;
- ParseTree *t = new ParseTree();
- t=Program(input);
- postOrder(t);
- cout<< t->IntegerCount() << " ICONST, " << t->StringCount() << " SCONST, " << t->VarCount() << " VARIABLES" << endl;
- cout<<"Number of Add operators: " << t->AdditionCount() << endl;
- cout<<"Number of subtract operators: " << t->SubtractionCount() << endl;
- cout<<"Number of multiplication operators: " << t->MultiplicationCount() << endl;
- cout<<"Number of Assignment operators: "<< t->EqualCount() << endl;
- // cout << "Var count " << t->VarCount() << endl;
- //cout<< "StringConst. count " << t->StringCount() << endl;
- // cout << "Equal Count" << t->EqualCount() << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement