-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSyntax.cpp
More file actions
68 lines (58 loc) · 2.09 KB
/
Copy pathSyntax.cpp
File metadata and controls
68 lines (58 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//koby Yoshida
#include <iostream>
#include <fstream>
#include "Syntax.h"
#include "syntaxGenStack.h"
#include "fileio.h"
using namespace std;
Syntax::Syntax(){
}
Syntax::~Syntax(){
//delete(stack);
}
bool Syntax::checking(ifstream& file, syntaxGenStack<char>& stack){
string line;
currentLine = 0;
while(getline(file,line)){//iterates thru file line by line
currentLine++;
for(int i = 0; i < line.size(); i++){//iterates thru the line char by char
if(line[i]=='/'&& line[i+1]=='/'){
break; //continue to the next line
}
if(line[i] == '[' || line[i]=='(' || line[i]=='{'){//if there is a beginning delimiter, add it to the stack
stack.push(line[i]);
}
else if(line[i] == ']' || line[i]==')' || line[i]=='}'){//when there is a closing delimiter, check the stack for the beginning delimiter
if(stack.isEmpty()){
cout << "Error on line " << currentLine << "\nFound an extra "<< line[i] << endl; //closing delimiter with no opening delimiter
return false;
}
if(stack.peek()=='('){
if(line[i] != ')'){
cout << "Error on line " << currentLine << "\nExpected ')' but found "<< line[i]<< endl;
return false;
}
}
if(stack.peek()=='['){//looking for beginning delimiters with no end
if(line[i] != ']'){
cout << "Error on line " << currentLine << "\nExpected ']' but found "<< line[i]<<endl;
return false;
}
}
if(stack.peek()=='{'){
if(line[i] != '}'){
cout << "Error on line " << currentLine << "\nExpected '}' but found "<< line[i]<< endl;
return false;
}
}
stack.pop();
}
}
}//end read file loop
bool end = stack.isEmpty();
while(!stack.isEmpty()){ // when we have read through the whole file, if there is anything left in the stack
// it will print the leftover delimiters
cout << "Unmatched " << stack.pop() << endl;
}
return end; // will return true if it passes all the checks for syntax errors
}