Skip to content

Commit

Permalink
Remove sout from parser
Browse files Browse the repository at this point in the history
  • Loading branch information
quazi-irfan committed May 5, 2017
1 parent 6249176 commit 0f509a4
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 24 deletions.
11 changes: 9 additions & 2 deletions Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,24 @@ public static void main(String[] args) throws Exception {
}

String adaFileName = args[0];
String tacFileName = adaFileName.substring(0, adaFileName.length()-4).concat(".tac");
String asmFileName = adaFileName.substring(0, adaFileName.length()-4).concat(".asm");

Parser parser = new Parser(adaFileName);
if(!parser.isParsingSuccessful()){
System.out.println("Parsing successful. Output at " + tacFileName);;
} else {
System.out.println("Parsing " + adaFileName + " failed.");
System.exit(1);
}

SymbolTable symbolTable = parser.getSymbolTable();
String tacFileName = adaFileName.substring(0, adaFileName.length()-4).concat(".tac");
x86Translator x86Translator = new x86Translator(tacFileName, symbolTable);
if(x86Translator.isSuccessfullyTranslated()){
System.out.println("Sucessfully created ASM file.");
System.out.println("TAC to x86 translation sucessful. Output at " + asmFileName);
} else {
System.out.println("TAC to x86 translation failed.");
System.exit(1);
}
}
}
42 changes: 21 additions & 21 deletions ParserPkg/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public class Parser {
public Parser(String fileName) throws IOException {
String tacFileName = fileName.substring(0, fileName.length()-4).concat(".tac");
tacWriter = new PrintWriter(tacFileName);
System.out.println("Writing output to " + tacFileName);
// System.out.println("Writing output to " + tacFileName);

// initialize symbol table before parsing
_symbolTable = new SymbolTable();
Expand All @@ -96,7 +96,7 @@ public Parser(String fileName) throws IOException {

// initialize parsing
Prog();
System.out.println(formattedString(new String[]{"START", "PROC" , _startProcedure}));
// System.out.println(formattedString(new String[]{"START", "PROC" , _startProcedure}));
tacWriter.println(formattedString(new String[]{"START", "PROC" , _startProcedure}));

// print the symbol table of global space
Expand Down Expand Up @@ -136,10 +136,10 @@ private void Prog(){
DeclarativePart(_currentProcedureName);
Procedures();
match(currentToken, TokenType.BEGIN);
System.out.println(formattedString(new String[]{"PROC", _currentProcedureName}));
// System.out.println(formattedString(new String[]{"PROC", _currentProcedureName}));
tacWriter.println(formattedString(new String[]{"PROC", _currentProcedureName}));
SeqOfStatements();
System.out.println(formattedString(new String[]{"ENDP" , _currentProcedureName}));
// System.out.println(formattedString(new String[]{"ENDP" , _currentProcedureName}));
tacWriter.println(formattedString(new String[]{"ENDP" , _currentProcedureName}));

match(currentToken, TokenType.END);
Expand Down Expand Up @@ -457,7 +457,7 @@ private void AssignStat(String identifier_) {
match(currentToken, TokenType.assignop);
String synthesizedAttributeofExpe = Expr();

System.out.println(formattedString(new String[]{variable1, "=", synthesizedAttributeofExpe}));
// System.out.println(formattedString(new String[]{variable1, "=", synthesizedAttributeofExpe}));
tacWriter.println(formattedString(new String[]{variable1, "=", synthesizedAttributeofExpe}));

} else {
Expand All @@ -472,7 +472,7 @@ private void ProcCall(String procedureName_) {
Params(procedureName_);
match(currentToken, TokenType.rparen);

System.out.println(formattedString(new String[]{"call" , procedureName_}));
// System.out.println(formattedString(new String[]{"call" , procedureName_}));
tacWriter.println(formattedString(new String[]{"call" , procedureName_}));
_currentIndexOfFunctionParameter = 0;
}
Expand All @@ -485,10 +485,10 @@ private void Params(String procedureName_) {

Symbol functionSymbol = _symbolTable.lookup(procedureName_, ESymbolType.function);
if(functionSymbol.functionAttributes.parameterModeList.get(_currentIndexOfFunctionParameter) != EParameterModeType.in){
System.out.println(formattedString(new String[]{"push" , "@".concat(currentToken.getLexeme())}));
// System.out.println(formattedString(new String[]{"push" , "@".concat(currentToken.getLexeme())}));
tacWriter.println(formattedString(new String[]{"push" , "@".concat(currentToken.getLexeme())}));
} else {
System.out.println(formattedString(new String[]{"push" , currentToken.getLexeme()}));
// System.out.println(formattedString(new String[]{"push" , currentToken.getLexeme()}));
tacWriter.println(formattedString(new String[]{"push" , currentToken.getLexeme()}));
}

Expand All @@ -497,7 +497,7 @@ private void Params(String procedureName_) {
ParamsTail(procedureName_);

} else if (currentToken.getTokenType() == TokenType.num) {
System.out.println(formattedString(new String[]{"push" , currentToken.getLexeme()}));
// System.out.println(formattedString(new String[]{"push" , currentToken.getLexeme()}));
tacWriter.println(formattedString(new String[]{"push" , currentToken.getLexeme()}));
match(currentToken, TokenType.id);
_currentIndexOfFunctionParameter++;
Expand All @@ -520,10 +520,10 @@ private void ParamsTail(String procedureName_) {

Symbol functionSymbol = _symbolTable.lookup(procedureName_, ESymbolType.function);
if(functionSymbol.functionAttributes.parameterModeList.get(_currentIndexOfFunctionParameter) != EParameterModeType.in){
System.out.println(formattedString(new String[]{"push" , "@".concat(currentToken.getLexeme())}));
// System.out.println(formattedString(new String[]{"push" , "@".concat(currentToken.getLexeme())}));
tacWriter.println(formattedString(new String[]{"push" , "@".concat(currentToken.getLexeme())}));
} else {
System.out.println(formattedString(new String[]{"push" , currentToken.getLexeme()}));
// System.out.println(formattedString(new String[]{"push" , currentToken.getLexeme()}));
tacWriter.println(formattedString(new String[]{"push" , currentToken.getLexeme()}));
}

Expand All @@ -532,7 +532,7 @@ private void ParamsTail(String procedureName_) {
ParamsTail(procedureName_);

} else if(currentToken.getTokenType() == TokenType.num) {
System.out.println(formattedString(new String[]{"push" , currentToken.getLexeme()}));
// System.out.println(formattedString(new String[]{"push" , currentToken.getLexeme()}));
tacWriter.println(formattedString(new String[]{"push" , currentToken.getLexeme()}));

match(currentToken, TokenType.num);
Expand Down Expand Up @@ -571,7 +571,7 @@ private void OutStat() {
match(currentToken, TokenType.rparen);

// putln prints a new line at the end
System.out.println(formattedString(new String[]{"wrln"}));
// System.out.println(formattedString(new String[]{"wrln"}));
tacWriter.println(formattedString(new String[]{"wrln"}));
}
}
Expand All @@ -597,11 +597,11 @@ private void WriteToken() {
if(currentToken.getTokenType() == TokenType.id || currentToken.getTokenType() == TokenType.num || currentToken.getTokenType() == TokenType.string){ // todo check if id was defined before
if(currentToken.getTokenType() == TokenType.id){
Symbol tempSymbol = _symbolTable.lookup(currentToken.getLexeme());
System.out.println(formattedString(new String[]{"wri", getSymbolLexemeOrOffset(tempSymbol)}));
// System.out.println(formattedString(new String[]{"wri", getSymbolLexemeOrOffset(tempSymbol)}));
tacWriter.println(formattedString(new String[]{"wri", getSymbolLexemeOrOffset(tempSymbol)}));
} else if(currentToken.getTokenType() == TokenType.string){
Symbol tempSymbol = _symbolTable.lookup(currentToken.getLexeme());
System.out.println(formattedString(new String[]{"wrs", tempSymbol.lexeme}));
// System.out.println(formattedString(new String[]{"wrs", tempSymbol.lexeme}));
tacWriter.println(formattedString(new String[]{"wrs", tempSymbol.lexeme}));
}
currentToken = tokenizer.getNextToken();
Expand All @@ -625,7 +625,7 @@ private void InStat() {
private void IdList() {
if(currentToken.getTokenType() == TokenType.id){
Symbol tempSymbol = isDefinedIdentifier(currentToken.getLexeme());
System.out.println(formattedString(new String[]{"rdi", getSymbolLexemeOrOffset(tempSymbol)}));
// System.out.println(formattedString(new String[]{"rdi", getSymbolLexemeOrOffset(tempSymbol)}));
tacWriter.println(formattedString(new String[]{"rdi", getSymbolLexemeOrOffset(tempSymbol)}));

match(currentToken, TokenType.id);
Expand All @@ -643,7 +643,7 @@ private void IdListTail() {
isDefinedIdentifier(currentToken.getLexeme());

Symbol tempSymbol = isDefinedIdentifier(currentToken.getLexeme());
System.out.println(formattedString(new String[]{"rdi", getSymbolLexemeOrOffset(tempSymbol)}));
// System.out.println(formattedString(new String[]{"rdi", getSymbolLexemeOrOffset(tempSymbol)}));
tacWriter.println(formattedString(new String[]{"rdi", getSymbolLexemeOrOffset(tempSymbol)}));

match(currentToken, TokenType.id);
Expand Down Expand Up @@ -676,7 +676,7 @@ private String MoreTerm(String _inheritedAttrib) {
String operator = currentToken.getLexeme();
match(currentToken, TokenType.addop);
String synthesizedAttributeofTerm = Term();
System.out.println(formattedString(new String[]{variable1, "=" , _inheritedAttrib, operator, synthesizedAttributeofTerm}));
// System.out.println(formattedString(new String[]{variable1, "=" , _inheritedAttrib, operator, synthesizedAttributeofTerm}));
tacWriter.println(formattedString(new String[]{variable1, "=" , _inheritedAttrib, operator, synthesizedAttributeofTerm}));
return MoreTerm(getSymbolLexemeOrOffset(tempSymbol));
}
Expand All @@ -699,7 +699,7 @@ private String MoreFactor(String _inheritedAttrib) {
String operator = currentToken.getLexeme();
match(currentToken, TokenType.mulop);
String synthesizedAttributeofFactor = Factor();
System.out.println(formattedString(new String[]{variable1, "=" , _inheritedAttrib, operator, synthesizedAttributeofFactor}));
// System.out.println(formattedString(new String[]{variable1, "=" , _inheritedAttrib, operator, synthesizedAttributeofFactor}));
tacWriter.println(formattedString(new String[]{variable1, "=" , _inheritedAttrib, operator, synthesizedAttributeofFactor}));
return MoreFactor(getSymbolLexemeOrOffset(tempSymbol));
}
Expand All @@ -719,7 +719,7 @@ private String Factor() {
} else if(currentToken.getTokenType() == TokenType.num){

Symbol tempSymbol = tempVariable(_currentProcedureName);
System.out.println(formattedString(new String[]{getSymbolLexemeOrOffset(tempSymbol), "=", currentToken.getLexeme()}));
// System.out.println(formattedString(new String[]{getSymbolLexemeOrOffset(tempSymbol), "=", currentToken.getLexeme()}));
tacWriter.println(formattedString(new String[]{getSymbolLexemeOrOffset(tempSymbol), "=", currentToken.getLexeme()}));

match(currentToken, TokenType.num);
Expand All @@ -736,7 +736,7 @@ private String Factor() {
Symbol tempSymbol = tempVariable(_currentProcedureName);
String variable1 = getSymbolLexemeOrOffset(tempSymbol);
String synthesizedAttributeofFactor = Factor();
System.out.println(formattedString(new String[]{variable1, "=", "-".concat(synthesizedAttributeofFactor)}));
// System.out.println(formattedString(new String[]{variable1, "=", "-".concat(synthesizedAttributeofFactor)}));
tacWriter.println(formattedString(new String[]{variable1, "=", "-".concat(synthesizedAttributeofFactor)}));
return getSymbolLexemeOrOffset(tempSymbol);
}
Expand Down
2 changes: 1 addition & 1 deletion TACx86Pkg/x86Translator.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ else if(statement.length() > 22 && (statement.charAt(16) == '-')){
} while (statement != null);

isSuccessfullyTranslated = true;

asmWriter.close();
}

Expand Down

0 comments on commit 0f509a4

Please sign in to comment.