Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "SyntaxAnalysis.h"
- #include <iostream>
- using namespace std;
- SyntaxAnalysis::SyntaxAnalysis(LexicalAnalysis& lex) : lexicalAnalysis(lex), errorFound(false)
- {
- tokenIterator = lexicalAnalysis.getTokenList().begin();
- vars = list<Variable>();
- }
- bool SyntaxAnalysis::Do()
- {
- currentToken = getNextToken();
- q();
- // check if anything has been analyzed:
- if (--tokenIterator == lexicalAnalysis.getTokenList().begin())
- return false;
- else
- return !errorFound;
- }
- void SyntaxAnalysis::printSyntaxError(Token& token)
- {
- cout << "Syntax error! Token: " << token.getValue() << " unexpected" << endl;
- }
- void SyntaxAnalysis::eat(TokenType t)
- {
- if (currentToken.getType() == t)
- {
- if (t == T_ID) {
- bool found = false;
- Variable temp = Variable(currentToken.getValue());
- for (Variable v : vars) {
- if (v.getName() == temp.getName()) {
- found = true;
- break;
- }
- }
- if (!found) {
- printSyntaxError(currentToken);
- errorFound = true;
- }
- }
- currentToken.printTokenValue();
- if (t != T_END_OF_FILE)
- currentToken = getNextToken();
- }
- else
- {
- printSyntaxError(currentToken);
- errorFound = true;
- }
- }
- void SyntaxAnalysis::eatAndSave(TokenType t)
- {
- if (currentToken.getType() == t)
- {
- currentToken.printTokenValue();
- bool duplicate = false;
- Variable temp = Variable(currentToken.getValue());
- for (Variable v : vars) {
- if (v.getName() == temp.getName()) {
- duplicate = true;
- break;
- }
- }
- if (duplicate) {
- printSyntaxError(currentToken);
- errorFound = true;
- }
- else {
- vars.push_back(temp);
- }
- if (t != T_END_OF_FILE)
- currentToken = getNextToken();
- }
- else
- {
- printSyntaxError(currentToken);
- errorFound = true;
- }
- }
- void SyntaxAnalysis::printVars()
- {
- cout << "Variables: " << endl;
- for (Variable v : vars) {
- cout << v.getName() << endl;
- }
- }
- Token SyntaxAnalysis::getNextToken()
- {
- if (tokenIterator == lexicalAnalysis.getTokenList().end())
- throw runtime_error("End of input file reached");
- return *tokenIterator++;
- }
- void SyntaxAnalysis::q()
- {
- s();
- l();
- }
- void SyntaxAnalysis::s()
- {
- if(currentToken.getType() == T_DEC)
- {
- eat(T_DEC);
- eatAndSave(T_ID);
- eat(T_SEMI);
- }
- else if(currentToken.getType() == T_IF)
- {
- eat(T_IF);
- c();
- eat(T_THEN);
- e();
- eat(T_SEMI);
- }
- else
- {
- e();
- eat(T_SEMI);
- }
- }
- void SyntaxAnalysis::l()
- {
- if(currentToken.getType() == T_END_OF_FILE)
- {
- eat(T_END_OF_FILE);
- }
- else
- {
- q();
- }
- }
- void SyntaxAnalysis::e()
- {
- if(currentToken.getType() == T_ID) {
- eat(T_ID);
- eat(T_EQ);
- if(currentToken.getType() == T_NUM)
- {
- eat(T_NUM);
- }
- else
- {
- eat(T_ID);
- eat(T_PLUS);
- if(currentToken.getType() == T_NUM)
- {
- eat(T_NUM);
- }
- else
- {
- eat(T_ID);
- }
- }
- }
- }
- void SyntaxAnalysis::c()
- {
- if(currentToken.getType() == T_ID) {
- eat(T_ID);
- eat(T_EQEQ);
- eat(T_NUM);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment