A versatile syntax checker written in C++. Checks for issues with keywords and brackets.
Its a Semester Project built to correctly parse the code
file and seperate keywords and brackets and checks for any mistake in the Syntax. Its highly versatile as it also would correctly parse each word or bracket separatly even if all the code is written in a single line. If any of the below condition would be True, the compilter would throw an error:
- If Imbalanced Brackets detected .e.g
[ [ {] } ]
- Code doesn't start with the keyword
begin
- Code doesn't end with the keyword
end
end
occurs without abegin
endif
occurs without anif
endfor
occurs without anfor
endwhile
occurs without anwhile
elseif
occurs without anif
else
occurs without anif
else
occurs two times- If brackets are not opened correctly like
begin{ {if int a; }endif }end
One the main algorithms built in this project is correctly parsing all the lines given in the file. The code is given below. The following code will accept a string meaning, a line and the line number and separate each word found and the brackets and pass it to another function named validate
. All this code is available in the syntax.cpp file.
void looper(string line, int line_counter) {
string converter = "";
for (int i = 0; i < line.length(); i++) {
if (!isalpha(line[i]) || i + 1 == line.length()) {
if (i + 1 == line.length()) {
if (isalpha(line[i])) {
converter += line[i];
validate(converter, line_counter);
}
else {
validate(converter, line_counter);
converter = line[i];
validate(converter, line_counter);
}
}
else {
validate(converter, line_counter);
converter = line[i];
validate(converter, line_counter);
}
converter = "";
}
else {
converter += line[i];
}
}
}
The code was compiled and tested on Visual Studio Community Version 2019. However, it would perfectly compile on previous versions as well. Tested upto version 2012
. After compilation you can supply direct file as an argument. If you don't, the program will interactively ask to provide the file:
All code contribution are welcomed.