├── Abstract-C_Compiler.pdf ├── Lexical Analyzer ├── src │ ├── a.out │ ├── compile.sh │ ├── Other RE for Reference │ ├── input.c │ ├── constantTable.txt │ ├── symbolTable.txt │ ├── scanner.l │ └── parsedTable.txt ├── screenshot │ ├── code-screenshots │ │ ├── Fig1.png │ │ ├── Fig2.png │ │ ├── Fig3.png │ │ ├── Fig4.png │ │ ├── Fig5.png │ │ ├── Fig6.png │ │ ├── Fig7.png │ │ ├── Fig8.png │ │ ├── Table1.png │ │ ├── Table2.png │ │ ├── Table3.png │ │ ├── input.png │ │ ├── SymbolTable.png │ │ └── constTable.png │ ├── output-screenshots │ │ ├── case2.png │ │ ├── case3.png │ │ ├── case4.png │ │ ├── case5.png │ │ ├── case6.png │ │ ├── case-1.png │ │ └── baseInput.png │ └── testcase-screenshots │ │ ├── Case1.png │ │ ├── Case2.png │ │ ├── Case3.png │ │ ├── Case4.png │ │ ├── Case5.png │ │ └── Case6.png └── test cases │ ├── Case1.c │ ├── Case2.c │ ├── Case3.c │ ├── Case6.c │ ├── Case4.c │ └── Case5.c ├── Project Report 4-ICG.pdf ├── Syntax Analyzer ├── src │ ├── a.out │ ├── compile.sh │ ├── input.c~ │ ├── input.c │ ├── constantTable.txt │ ├── symbolTable.txt │ ├── parsedTable.txt │ └── lexicalAnalyzer.l ├── testcases │ ├── input │ │ ├── a.out │ │ ├── compile.sh │ │ ├── case6.c │ │ ├── compile.sh~ │ │ ├── case4.c │ │ ├── case8.c │ │ ├── case5.c │ │ ├── case7.c │ │ ├── case7.c~ │ │ ├── case1.c │ │ ├── case9.c │ │ ├── constantTable.txt │ │ ├── case3.c │ │ ├── case3.c~ │ │ ├── case2.c │ │ ├── symbolTable.txt │ │ └── lexicalAnalyzer.l │ └── expected output │ │ ├── output6.txt │ │ ├── output2.txt │ │ ├── output1.txt │ │ ├── output4.txt │ │ ├── output3.txt │ │ └── output5.txt └── screenshots │ ├── run_format.png │ ├── sample_input.png │ ├── symbol_table.png │ ├── constants_table.png │ ├── CodeScreenshots │ ├── lex_1.png │ ├── lex_2.png │ ├── lex_3.png │ ├── lex_4.png │ ├── yacc_1.png │ ├── yacc_10.png │ ├── yacc_11.png │ ├── yacc_12.png │ ├── yacc_13.png │ ├── yacc_14.png │ ├── yacc_2.png │ ├── yacc_3.png │ ├── yacc_4.png │ ├── yacc_5.png │ ├── yacc_6.png │ ├── yacc_7.png │ ├── yacc_8.png │ ├── yacc_9.png │ └── yacc_1_2(missing).png │ └── TestcaseScreenshot │ ├── CodeScreenshot │ ├── case1.png │ ├── case2.png │ ├── case3.png │ ├── case4.png │ ├── case5.png │ ├── case6.png │ └── case7.png │ └── OutputScreenshot │ ├── outputCase_2.png │ ├── outputCase_3.png │ ├── outputCase_4.png │ ├── outputCase_5.png │ ├── outputCase_6.png │ ├── outputCase_7.png │ └── outputcase_1.png ├── Semantic Analyzer ├── src │ ├── a.out │ ├── compile.sh │ ├── input.c │ ├── input.c~ │ ├── constantTable.txt │ ├── symbolTable.txt │ ├── parsedTable.txt │ ├── lexicalAnalyzer.l │ ├── semantic.h │ └── syntaxChecker.y ├── testcases │ ├── a.out │ ├── compile.sh │ ├── case2.c │ ├── case1.c │ ├── case4.c │ ├── case5.c │ ├── case3.c │ ├── case6.c │ ├── constantTable.txt │ ├── symbolTable.txt │ ├── parsedTable.txt │ ├── lexicalAnalyzer.l │ └── semantic.h └── screenshots │ ├── run.png │ ├── symbolTable.png │ ├── codeScreenshots │ ├── p1.png │ ├── p10.png │ ├── p2.png │ ├── p3.png │ ├── p4.png │ ├── p5.png │ ├── p6.png │ ├── p7.png │ ├── p8.png │ ├── p9.png │ ├── lex_1.png │ ├── lex_2.png │ ├── lex_3.png │ ├── lex_4.png │ ├── sem-1.png │ ├── sem-2.png │ ├── sem-3.png │ ├── sem-4.png │ ├── sem-5.png │ └── sem-6.png │ └── testcaseScreenshots │ ├── t1.png │ ├── t2.png │ ├── t3.png │ ├── t4.png │ ├── t5.png │ └── t6.png ├── Project Report 1 - Scanner.pdf ├── Project Report 2 - Parser.pdf ├── Project Report 3-Semantic.pdf ├── References ├── lexical-analysis.pdf ├── semanticAnalyzer.pdf ├── LexAndYaccTutorial.pdf └── ThreeAddressCode_Stanford.pdf ├── Intermediate Code Generator ├── src │ ├── a.out │ ├── compile.sh │ ├── input.c │ ├── threeAddressCode.txt │ ├── symbolTable.txt │ ├── icg.h │ ├── lexicalAnalyzer.l │ └── icg.c ├── testcases │ ├── a.out │ ├── case5.c │ ├── threeAddressCode.txt │ ├── compile.sh │ ├── case1.c │ ├── case3.c │ ├── case2.c │ ├── case4.c │ ├── symbolTable.txt │ ├── icg.h │ ├── lexicalAnalyzer.l │ └── icg.c └── screenshots │ ├── run.png │ ├── input.png │ ├── symbolTable.png │ ├── testcase │ ├── case1.png │ ├── case2.png │ ├── case3.png │ ├── case4.png │ ├── case5.png │ ├── case1-icg.png │ ├── case2-icg.png │ ├── case3-icg.png │ ├── case4-icg.png │ └── case5-icg.png │ ├── sourcecode │ ├── icg1.png │ ├── icg10.png │ ├── icg11.png │ ├── icg12.png │ ├── icg2.png │ ├── icg3.png │ ├── icg4.png │ ├── icg5.png │ ├── icg6.png │ ├── icg7.png │ ├── icg8.png │ ├── icg9.png │ ├── icgh1.png │ ├── icgh2.png │ ├── lex1.png │ ├── lex2.png │ ├── yacc1.png │ ├── yacc10.png │ ├── yacc11.png │ ├── yacc12.png │ ├── yacc13.png │ ├── yacc14.png │ ├── yacc15.png │ ├── yacc16.png │ ├── yacc17.png │ ├── yacc18.png │ ├── yacc2.png │ ├── yacc3.png │ ├── yacc4.png │ ├── yacc5.png │ ├── yacc6.png │ ├── yacc7.png │ ├── yacc8.png │ └── yacc9.png │ └── threeAddressCode.png └── README.md /Abstract-C_Compiler.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Abstract-C_Compiler.pdf -------------------------------------------------------------------------------- /Lexical Analyzer/src/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Lexical Analyzer/src/a.out -------------------------------------------------------------------------------- /Project Report 4-ICG.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Project Report 4-ICG.pdf -------------------------------------------------------------------------------- /Syntax Analyzer/src/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Syntax Analyzer/src/a.out -------------------------------------------------------------------------------- /Semantic Analyzer/src/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Semantic Analyzer/src/a.out -------------------------------------------------------------------------------- /Lexical Analyzer/src/compile.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | lex scanner.l 3 | cc lex.yy.c 4 | ./a.out input.c 5 | vim table.txt 6 | -------------------------------------------------------------------------------- /Project Report 1 - Scanner.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Project Report 1 - Scanner.pdf -------------------------------------------------------------------------------- /Project Report 2 - Parser.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Project Report 2 - Parser.pdf -------------------------------------------------------------------------------- /Project Report 3-Semantic.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Project Report 3-Semantic.pdf -------------------------------------------------------------------------------- /References/lexical-analysis.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/References/lexical-analysis.pdf -------------------------------------------------------------------------------- /References/semanticAnalyzer.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/References/semanticAnalyzer.pdf -------------------------------------------------------------------------------- /Lexical Analyzer/src/Other RE for Reference: -------------------------------------------------------------------------------- 1 | "struct"{whitespace}+{identifier} insertToTable(yytext,'m'); for structure 2 | -------------------------------------------------------------------------------- /References/LexAndYaccTutorial.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/References/LexAndYaccTutorial.pdf -------------------------------------------------------------------------------- /Semantic Analyzer/testcases/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Semantic Analyzer/testcases/a.out -------------------------------------------------------------------------------- /Intermediate Code Generator/src/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Intermediate Code Generator/src/a.out -------------------------------------------------------------------------------- /Semantic Analyzer/screenshots/run.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Semantic Analyzer/screenshots/run.png -------------------------------------------------------------------------------- /Syntax Analyzer/testcases/input/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Syntax Analyzer/testcases/input/a.out -------------------------------------------------------------------------------- /References/ThreeAddressCode_Stanford.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/References/ThreeAddressCode_Stanford.pdf -------------------------------------------------------------------------------- /Intermediate Code Generator/testcases/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Intermediate Code Generator/testcases/a.out -------------------------------------------------------------------------------- /Syntax Analyzer/screenshots/run_format.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Syntax Analyzer/screenshots/run_format.png -------------------------------------------------------------------------------- /Semantic Analyzer/screenshots/symbolTable.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Semantic Analyzer/screenshots/symbolTable.png -------------------------------------------------------------------------------- /Syntax Analyzer/screenshots/sample_input.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Syntax Analyzer/screenshots/sample_input.png -------------------------------------------------------------------------------- /Syntax Analyzer/screenshots/symbol_table.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Syntax Analyzer/screenshots/symbol_table.png -------------------------------------------------------------------------------- /Intermediate Code Generator/screenshots/run.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Intermediate Code Generator/screenshots/run.png -------------------------------------------------------------------------------- /Syntax Analyzer/screenshots/constants_table.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Syntax Analyzer/screenshots/constants_table.png -------------------------------------------------------------------------------- /Intermediate Code Generator/screenshots/input.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Intermediate Code Generator/screenshots/input.png -------------------------------------------------------------------------------- /Lexical Analyzer/screenshot/code-screenshots/Fig1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Lexical Analyzer/screenshot/code-screenshots/Fig1.png -------------------------------------------------------------------------------- /Lexical Analyzer/screenshot/code-screenshots/Fig2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Lexical Analyzer/screenshot/code-screenshots/Fig2.png -------------------------------------------------------------------------------- /Lexical Analyzer/screenshot/code-screenshots/Fig3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Lexical Analyzer/screenshot/code-screenshots/Fig3.png -------------------------------------------------------------------------------- /Lexical Analyzer/screenshot/code-screenshots/Fig4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Lexical Analyzer/screenshot/code-screenshots/Fig4.png -------------------------------------------------------------------------------- /Lexical Analyzer/screenshot/code-screenshots/Fig5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Lexical Analyzer/screenshot/code-screenshots/Fig5.png -------------------------------------------------------------------------------- /Lexical Analyzer/screenshot/code-screenshots/Fig6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Lexical Analyzer/screenshot/code-screenshots/Fig6.png -------------------------------------------------------------------------------- /Lexical Analyzer/screenshot/code-screenshots/Fig7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Lexical Analyzer/screenshot/code-screenshots/Fig7.png -------------------------------------------------------------------------------- /Lexical Analyzer/screenshot/code-screenshots/Fig8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Lexical Analyzer/screenshot/code-screenshots/Fig8.png -------------------------------------------------------------------------------- /Semantic Analyzer/screenshots/codeScreenshots/p1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Semantic Analyzer/screenshots/codeScreenshots/p1.png -------------------------------------------------------------------------------- /Semantic Analyzer/screenshots/codeScreenshots/p10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Semantic Analyzer/screenshots/codeScreenshots/p10.png -------------------------------------------------------------------------------- /Semantic Analyzer/screenshots/codeScreenshots/p2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Semantic Analyzer/screenshots/codeScreenshots/p2.png -------------------------------------------------------------------------------- /Semantic Analyzer/screenshots/codeScreenshots/p3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Semantic Analyzer/screenshots/codeScreenshots/p3.png -------------------------------------------------------------------------------- /Semantic Analyzer/screenshots/codeScreenshots/p4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Semantic Analyzer/screenshots/codeScreenshots/p4.png -------------------------------------------------------------------------------- /Semantic Analyzer/screenshots/codeScreenshots/p5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Semantic Analyzer/screenshots/codeScreenshots/p5.png -------------------------------------------------------------------------------- /Semantic Analyzer/screenshots/codeScreenshots/p6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Semantic Analyzer/screenshots/codeScreenshots/p6.png -------------------------------------------------------------------------------- /Semantic Analyzer/screenshots/codeScreenshots/p7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Semantic Analyzer/screenshots/codeScreenshots/p7.png -------------------------------------------------------------------------------- /Semantic Analyzer/screenshots/codeScreenshots/p8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Semantic Analyzer/screenshots/codeScreenshots/p8.png -------------------------------------------------------------------------------- /Semantic Analyzer/screenshots/codeScreenshots/p9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Semantic Analyzer/screenshots/codeScreenshots/p9.png -------------------------------------------------------------------------------- /Syntax Analyzer/screenshots/CodeScreenshots/lex_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Syntax Analyzer/screenshots/CodeScreenshots/lex_1.png -------------------------------------------------------------------------------- /Syntax Analyzer/screenshots/CodeScreenshots/lex_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Syntax Analyzer/screenshots/CodeScreenshots/lex_2.png -------------------------------------------------------------------------------- /Syntax Analyzer/screenshots/CodeScreenshots/lex_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Syntax Analyzer/screenshots/CodeScreenshots/lex_3.png -------------------------------------------------------------------------------- /Syntax Analyzer/screenshots/CodeScreenshots/lex_4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Syntax Analyzer/screenshots/CodeScreenshots/lex_4.png -------------------------------------------------------------------------------- /Intermediate Code Generator/screenshots/symbolTable.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Intermediate Code Generator/screenshots/symbolTable.png -------------------------------------------------------------------------------- /Intermediate Code Generator/testcases/case5.c: -------------------------------------------------------------------------------- 1 | //testcase to check main function errors 2 | #include 3 | int main(); 4 | int main(){ 5 | int y; 6 | y=20; 7 | } 8 | -------------------------------------------------------------------------------- /Intermediate Code Generator/testcases/threeAddressCode.txt: -------------------------------------------------------------------------------- 1 | 2 | Intermediate Code Generated 3 | ------------------------------- 4 | 0 GOTO 1 5 | 1 y := 20 6 | -------------------------------------------------------------------------------- /Lexical Analyzer/screenshot/code-screenshots/Table1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Lexical Analyzer/screenshot/code-screenshots/Table1.png -------------------------------------------------------------------------------- /Lexical Analyzer/screenshot/code-screenshots/Table2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Lexical Analyzer/screenshot/code-screenshots/Table2.png -------------------------------------------------------------------------------- /Lexical Analyzer/screenshot/code-screenshots/Table3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Lexical Analyzer/screenshot/code-screenshots/Table3.png -------------------------------------------------------------------------------- /Lexical Analyzer/screenshot/code-screenshots/input.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Lexical Analyzer/screenshot/code-screenshots/input.png -------------------------------------------------------------------------------- /Lexical Analyzer/screenshot/output-screenshots/case2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Lexical Analyzer/screenshot/output-screenshots/case2.png -------------------------------------------------------------------------------- /Lexical Analyzer/screenshot/output-screenshots/case3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Lexical Analyzer/screenshot/output-screenshots/case3.png -------------------------------------------------------------------------------- /Lexical Analyzer/screenshot/output-screenshots/case4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Lexical Analyzer/screenshot/output-screenshots/case4.png -------------------------------------------------------------------------------- /Lexical Analyzer/screenshot/output-screenshots/case5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Lexical Analyzer/screenshot/output-screenshots/case5.png -------------------------------------------------------------------------------- /Lexical Analyzer/screenshot/output-screenshots/case6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Lexical Analyzer/screenshot/output-screenshots/case6.png -------------------------------------------------------------------------------- /Semantic Analyzer/screenshots/codeScreenshots/lex_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Semantic Analyzer/screenshots/codeScreenshots/lex_1.png -------------------------------------------------------------------------------- /Semantic Analyzer/screenshots/codeScreenshots/lex_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Semantic Analyzer/screenshots/codeScreenshots/lex_2.png -------------------------------------------------------------------------------- /Semantic Analyzer/screenshots/codeScreenshots/lex_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Semantic Analyzer/screenshots/codeScreenshots/lex_3.png -------------------------------------------------------------------------------- /Semantic Analyzer/screenshots/codeScreenshots/lex_4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Semantic Analyzer/screenshots/codeScreenshots/lex_4.png -------------------------------------------------------------------------------- /Semantic Analyzer/screenshots/codeScreenshots/sem-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Semantic Analyzer/screenshots/codeScreenshots/sem-1.png -------------------------------------------------------------------------------- /Semantic Analyzer/screenshots/codeScreenshots/sem-2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Semantic Analyzer/screenshots/codeScreenshots/sem-2.png -------------------------------------------------------------------------------- /Semantic Analyzer/screenshots/codeScreenshots/sem-3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Semantic Analyzer/screenshots/codeScreenshots/sem-3.png -------------------------------------------------------------------------------- /Semantic Analyzer/screenshots/codeScreenshots/sem-4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Semantic Analyzer/screenshots/codeScreenshots/sem-4.png -------------------------------------------------------------------------------- /Semantic Analyzer/screenshots/codeScreenshots/sem-5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Semantic Analyzer/screenshots/codeScreenshots/sem-5.png -------------------------------------------------------------------------------- /Semantic Analyzer/screenshots/codeScreenshots/sem-6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Semantic Analyzer/screenshots/codeScreenshots/sem-6.png -------------------------------------------------------------------------------- /Semantic Analyzer/screenshots/testcaseScreenshots/t1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Semantic Analyzer/screenshots/testcaseScreenshots/t1.png -------------------------------------------------------------------------------- /Semantic Analyzer/screenshots/testcaseScreenshots/t2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Semantic Analyzer/screenshots/testcaseScreenshots/t2.png -------------------------------------------------------------------------------- /Semantic Analyzer/screenshots/testcaseScreenshots/t3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Semantic Analyzer/screenshots/testcaseScreenshots/t3.png -------------------------------------------------------------------------------- /Semantic Analyzer/screenshots/testcaseScreenshots/t4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Semantic Analyzer/screenshots/testcaseScreenshots/t4.png -------------------------------------------------------------------------------- /Semantic Analyzer/screenshots/testcaseScreenshots/t5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Semantic Analyzer/screenshots/testcaseScreenshots/t5.png -------------------------------------------------------------------------------- /Semantic Analyzer/screenshots/testcaseScreenshots/t6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Semantic Analyzer/screenshots/testcaseScreenshots/t6.png -------------------------------------------------------------------------------- /Syntax Analyzer/screenshots/CodeScreenshots/yacc_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Syntax Analyzer/screenshots/CodeScreenshots/yacc_1.png -------------------------------------------------------------------------------- /Syntax Analyzer/screenshots/CodeScreenshots/yacc_10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Syntax Analyzer/screenshots/CodeScreenshots/yacc_10.png -------------------------------------------------------------------------------- /Syntax Analyzer/screenshots/CodeScreenshots/yacc_11.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Syntax Analyzer/screenshots/CodeScreenshots/yacc_11.png -------------------------------------------------------------------------------- /Syntax Analyzer/screenshots/CodeScreenshots/yacc_12.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Syntax Analyzer/screenshots/CodeScreenshots/yacc_12.png -------------------------------------------------------------------------------- /Syntax Analyzer/screenshots/CodeScreenshots/yacc_13.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Syntax Analyzer/screenshots/CodeScreenshots/yacc_13.png -------------------------------------------------------------------------------- /Syntax Analyzer/screenshots/CodeScreenshots/yacc_14.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Syntax Analyzer/screenshots/CodeScreenshots/yacc_14.png -------------------------------------------------------------------------------- /Syntax Analyzer/screenshots/CodeScreenshots/yacc_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Syntax Analyzer/screenshots/CodeScreenshots/yacc_2.png -------------------------------------------------------------------------------- /Syntax Analyzer/screenshots/CodeScreenshots/yacc_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Syntax Analyzer/screenshots/CodeScreenshots/yacc_3.png -------------------------------------------------------------------------------- /Syntax Analyzer/screenshots/CodeScreenshots/yacc_4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Syntax Analyzer/screenshots/CodeScreenshots/yacc_4.png -------------------------------------------------------------------------------- /Syntax Analyzer/screenshots/CodeScreenshots/yacc_5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Syntax Analyzer/screenshots/CodeScreenshots/yacc_5.png -------------------------------------------------------------------------------- /Syntax Analyzer/screenshots/CodeScreenshots/yacc_6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Syntax Analyzer/screenshots/CodeScreenshots/yacc_6.png -------------------------------------------------------------------------------- /Syntax Analyzer/screenshots/CodeScreenshots/yacc_7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Syntax Analyzer/screenshots/CodeScreenshots/yacc_7.png -------------------------------------------------------------------------------- /Syntax Analyzer/screenshots/CodeScreenshots/yacc_8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Syntax Analyzer/screenshots/CodeScreenshots/yacc_8.png -------------------------------------------------------------------------------- /Syntax Analyzer/screenshots/CodeScreenshots/yacc_9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Syntax Analyzer/screenshots/CodeScreenshots/yacc_9.png -------------------------------------------------------------------------------- /Intermediate Code Generator/screenshots/testcase/case1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Intermediate Code Generator/screenshots/testcase/case1.png -------------------------------------------------------------------------------- /Intermediate Code Generator/screenshots/testcase/case2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Intermediate Code Generator/screenshots/testcase/case2.png -------------------------------------------------------------------------------- /Intermediate Code Generator/screenshots/testcase/case3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Intermediate Code Generator/screenshots/testcase/case3.png -------------------------------------------------------------------------------- /Intermediate Code Generator/screenshots/testcase/case4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Intermediate Code Generator/screenshots/testcase/case4.png -------------------------------------------------------------------------------- /Intermediate Code Generator/screenshots/testcase/case5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Intermediate Code Generator/screenshots/testcase/case5.png -------------------------------------------------------------------------------- /Lexical Analyzer/screenshot/output-screenshots/case-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Lexical Analyzer/screenshot/output-screenshots/case-1.png -------------------------------------------------------------------------------- /Lexical Analyzer/screenshot/testcase-screenshots/Case1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Lexical Analyzer/screenshot/testcase-screenshots/Case1.png -------------------------------------------------------------------------------- /Lexical Analyzer/screenshot/testcase-screenshots/Case2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Lexical Analyzer/screenshot/testcase-screenshots/Case2.png -------------------------------------------------------------------------------- /Lexical Analyzer/screenshot/testcase-screenshots/Case3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Lexical Analyzer/screenshot/testcase-screenshots/Case3.png -------------------------------------------------------------------------------- /Lexical Analyzer/screenshot/testcase-screenshots/Case4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Lexical Analyzer/screenshot/testcase-screenshots/Case4.png -------------------------------------------------------------------------------- /Lexical Analyzer/screenshot/testcase-screenshots/Case5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Lexical Analyzer/screenshot/testcase-screenshots/Case5.png -------------------------------------------------------------------------------- /Lexical Analyzer/screenshot/testcase-screenshots/Case6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Lexical Analyzer/screenshot/testcase-screenshots/Case6.png -------------------------------------------------------------------------------- /Intermediate Code Generator/screenshots/sourcecode/icg1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Intermediate Code Generator/screenshots/sourcecode/icg1.png -------------------------------------------------------------------------------- /Intermediate Code Generator/screenshots/sourcecode/icg10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Intermediate Code Generator/screenshots/sourcecode/icg10.png -------------------------------------------------------------------------------- /Intermediate Code Generator/screenshots/sourcecode/icg11.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Intermediate Code Generator/screenshots/sourcecode/icg11.png -------------------------------------------------------------------------------- /Intermediate Code Generator/screenshots/sourcecode/icg12.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Intermediate Code Generator/screenshots/sourcecode/icg12.png -------------------------------------------------------------------------------- /Intermediate Code Generator/screenshots/sourcecode/icg2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Intermediate Code Generator/screenshots/sourcecode/icg2.png -------------------------------------------------------------------------------- /Intermediate Code Generator/screenshots/sourcecode/icg3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Intermediate Code Generator/screenshots/sourcecode/icg3.png -------------------------------------------------------------------------------- /Intermediate Code Generator/screenshots/sourcecode/icg4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Intermediate Code Generator/screenshots/sourcecode/icg4.png -------------------------------------------------------------------------------- /Intermediate Code Generator/screenshots/sourcecode/icg5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Intermediate Code Generator/screenshots/sourcecode/icg5.png -------------------------------------------------------------------------------- /Intermediate Code Generator/screenshots/sourcecode/icg6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Intermediate Code Generator/screenshots/sourcecode/icg6.png -------------------------------------------------------------------------------- /Intermediate Code Generator/screenshots/sourcecode/icg7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Intermediate Code Generator/screenshots/sourcecode/icg7.png -------------------------------------------------------------------------------- /Intermediate Code Generator/screenshots/sourcecode/icg8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Intermediate Code Generator/screenshots/sourcecode/icg8.png -------------------------------------------------------------------------------- /Intermediate Code Generator/screenshots/sourcecode/icg9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Intermediate Code Generator/screenshots/sourcecode/icg9.png -------------------------------------------------------------------------------- /Intermediate Code Generator/screenshots/sourcecode/icgh1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Intermediate Code Generator/screenshots/sourcecode/icgh1.png -------------------------------------------------------------------------------- /Intermediate Code Generator/screenshots/sourcecode/icgh2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Intermediate Code Generator/screenshots/sourcecode/icgh2.png -------------------------------------------------------------------------------- /Intermediate Code Generator/screenshots/sourcecode/lex1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Intermediate Code Generator/screenshots/sourcecode/lex1.png -------------------------------------------------------------------------------- /Intermediate Code Generator/screenshots/sourcecode/lex2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Intermediate Code Generator/screenshots/sourcecode/lex2.png -------------------------------------------------------------------------------- /Intermediate Code Generator/screenshots/sourcecode/yacc1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Intermediate Code Generator/screenshots/sourcecode/yacc1.png -------------------------------------------------------------------------------- /Intermediate Code Generator/screenshots/sourcecode/yacc10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Intermediate Code Generator/screenshots/sourcecode/yacc10.png -------------------------------------------------------------------------------- /Intermediate Code Generator/screenshots/sourcecode/yacc11.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Intermediate Code Generator/screenshots/sourcecode/yacc11.png -------------------------------------------------------------------------------- /Intermediate Code Generator/screenshots/sourcecode/yacc12.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Intermediate Code Generator/screenshots/sourcecode/yacc12.png -------------------------------------------------------------------------------- /Intermediate Code Generator/screenshots/sourcecode/yacc13.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Intermediate Code Generator/screenshots/sourcecode/yacc13.png -------------------------------------------------------------------------------- /Intermediate Code Generator/screenshots/sourcecode/yacc14.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Intermediate Code Generator/screenshots/sourcecode/yacc14.png -------------------------------------------------------------------------------- /Intermediate Code Generator/screenshots/sourcecode/yacc15.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Intermediate Code Generator/screenshots/sourcecode/yacc15.png -------------------------------------------------------------------------------- /Intermediate Code Generator/screenshots/sourcecode/yacc16.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Intermediate Code Generator/screenshots/sourcecode/yacc16.png -------------------------------------------------------------------------------- /Intermediate Code Generator/screenshots/sourcecode/yacc17.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Intermediate Code Generator/screenshots/sourcecode/yacc17.png -------------------------------------------------------------------------------- /Intermediate Code Generator/screenshots/sourcecode/yacc18.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Intermediate Code Generator/screenshots/sourcecode/yacc18.png -------------------------------------------------------------------------------- /Intermediate Code Generator/screenshots/sourcecode/yacc2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Intermediate Code Generator/screenshots/sourcecode/yacc2.png -------------------------------------------------------------------------------- /Intermediate Code Generator/screenshots/sourcecode/yacc3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Intermediate Code Generator/screenshots/sourcecode/yacc3.png -------------------------------------------------------------------------------- /Intermediate Code Generator/screenshots/sourcecode/yacc4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Intermediate Code Generator/screenshots/sourcecode/yacc4.png -------------------------------------------------------------------------------- /Intermediate Code Generator/screenshots/sourcecode/yacc5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Intermediate Code Generator/screenshots/sourcecode/yacc5.png -------------------------------------------------------------------------------- /Intermediate Code Generator/screenshots/sourcecode/yacc6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Intermediate Code Generator/screenshots/sourcecode/yacc6.png -------------------------------------------------------------------------------- /Intermediate Code Generator/screenshots/sourcecode/yacc7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Intermediate Code Generator/screenshots/sourcecode/yacc7.png -------------------------------------------------------------------------------- /Intermediate Code Generator/screenshots/sourcecode/yacc8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Intermediate Code Generator/screenshots/sourcecode/yacc8.png -------------------------------------------------------------------------------- /Intermediate Code Generator/screenshots/sourcecode/yacc9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Intermediate Code Generator/screenshots/sourcecode/yacc9.png -------------------------------------------------------------------------------- /Intermediate Code Generator/screenshots/threeAddressCode.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Intermediate Code Generator/screenshots/threeAddressCode.png -------------------------------------------------------------------------------- /Lexical Analyzer/screenshot/code-screenshots/SymbolTable.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Lexical Analyzer/screenshot/code-screenshots/SymbolTable.png -------------------------------------------------------------------------------- /Lexical Analyzer/screenshot/code-screenshots/constTable.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Lexical Analyzer/screenshot/code-screenshots/constTable.png -------------------------------------------------------------------------------- /Lexical Analyzer/screenshot/output-screenshots/baseInput.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Lexical Analyzer/screenshot/output-screenshots/baseInput.png -------------------------------------------------------------------------------- /Semantic Analyzer/src/compile.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | lex lexicalAnalyzer.l 3 | yacc -d syntaxChecker.y 4 | gcc lex.yy.c y.tab.c -w -g 5 | ./a.out input.c 6 | rm y.tab.c y.tab.h lex.yy.c 7 | -------------------------------------------------------------------------------- /Syntax Analyzer/src/compile.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | lex lexicalAnalyzer.l 3 | yacc -d syntaxChecker.y 4 | gcc lex.yy.c y.tab.c -w -g 5 | ./a.out input.c 6 | rm y.tab.c y.tab.h lex.yy.c 7 | -------------------------------------------------------------------------------- /Intermediate Code Generator/screenshots/testcase/case1-icg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Intermediate Code Generator/screenshots/testcase/case1-icg.png -------------------------------------------------------------------------------- /Intermediate Code Generator/screenshots/testcase/case2-icg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Intermediate Code Generator/screenshots/testcase/case2-icg.png -------------------------------------------------------------------------------- /Intermediate Code Generator/screenshots/testcase/case3-icg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Intermediate Code Generator/screenshots/testcase/case3-icg.png -------------------------------------------------------------------------------- /Intermediate Code Generator/screenshots/testcase/case4-icg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Intermediate Code Generator/screenshots/testcase/case4-icg.png -------------------------------------------------------------------------------- /Intermediate Code Generator/screenshots/testcase/case5-icg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Intermediate Code Generator/screenshots/testcase/case5-icg.png -------------------------------------------------------------------------------- /Semantic Analyzer/testcases/compile.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | lex lexicalAnalyzer.l 3 | yacc -d syntaxChecker.y 4 | gcc lex.yy.c y.tab.c -w -g 5 | ./a.out case7.c 6 | rm y.tab.c y.tab.h lex.yy.c 7 | -------------------------------------------------------------------------------- /Syntax Analyzer/screenshots/CodeScreenshots/yacc_1_2(missing).png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Syntax Analyzer/screenshots/CodeScreenshots/yacc_1_2(missing).png -------------------------------------------------------------------------------- /Syntax Analyzer/testcases/expected output/output6.txt: -------------------------------------------------------------------------------- 1 | Explanation : 2 | 3 | Unbalanced paranthesis in the end of the program 4 | 5 | Output: 6 | 7 | Unbalenced paranthesis at line number 8 -------------------------------------------------------------------------------- /Syntax Analyzer/testcases/input/compile.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | lex lexicalAnalyzer.l 3 | yacc -d syntaxChecker.y 4 | gcc lex.yy.c y.tab.c -w -g 5 | ./a.out case2.c 6 | rm y.tab.c y.tab.h lex.yy.c 7 | -------------------------------------------------------------------------------- /Syntax Analyzer/testcases/input/case6.c: -------------------------------------------------------------------------------- 1 | //Testcase to check missing semicolon ,unbalanced parenthesis 2 | #include 3 | int main(){ 4 | int a=0,b=45,c=10; 5 | a=a+b 6 | 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /Syntax Analyzer/testcases/input/compile.sh~: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | lex lexicalAnalyzer.l 3 | yacc -d syntaxChecker.y 4 | gcc lex.yy.c y.tab.c -w -g 5 | ./a.out case1.c 6 | rm y.tab.c y.tab.h lex.yy.c 7 | -------------------------------------------------------------------------------- /Intermediate Code Generator/src/compile.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | lex lexicalAnalyzer.l 3 | yacc -d syntaxChecker.y 4 | gcc lex.yy.c y.tab.c icg.c -w -g 5 | ./a.out input.c 6 | rm y.tab.c y.tab.h lex.yy.c 7 | -------------------------------------------------------------------------------- /Syntax Analyzer/screenshots/TestcaseScreenshot/CodeScreenshot/case1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Syntax Analyzer/screenshots/TestcaseScreenshot/CodeScreenshot/case1.png -------------------------------------------------------------------------------- /Syntax Analyzer/screenshots/TestcaseScreenshot/CodeScreenshot/case2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Syntax Analyzer/screenshots/TestcaseScreenshot/CodeScreenshot/case2.png -------------------------------------------------------------------------------- /Syntax Analyzer/screenshots/TestcaseScreenshot/CodeScreenshot/case3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Syntax Analyzer/screenshots/TestcaseScreenshot/CodeScreenshot/case3.png -------------------------------------------------------------------------------- /Syntax Analyzer/screenshots/TestcaseScreenshot/CodeScreenshot/case4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Syntax Analyzer/screenshots/TestcaseScreenshot/CodeScreenshot/case4.png -------------------------------------------------------------------------------- /Syntax Analyzer/screenshots/TestcaseScreenshot/CodeScreenshot/case5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Syntax Analyzer/screenshots/TestcaseScreenshot/CodeScreenshot/case5.png -------------------------------------------------------------------------------- /Syntax Analyzer/screenshots/TestcaseScreenshot/CodeScreenshot/case6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Syntax Analyzer/screenshots/TestcaseScreenshot/CodeScreenshot/case6.png -------------------------------------------------------------------------------- /Syntax Analyzer/screenshots/TestcaseScreenshot/CodeScreenshot/case7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Syntax Analyzer/screenshots/TestcaseScreenshot/CodeScreenshot/case7.png -------------------------------------------------------------------------------- /Intermediate Code Generator/testcases/compile.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | lex lexicalAnalyzer.l 3 | yacc -d syntaxChecker.y 4 | gcc lex.yy.c y.tab.c icg.c -w -g 5 | ./a.out case5.c 6 | rm y.tab.c y.tab.h lex.yy.c 7 | -------------------------------------------------------------------------------- /Intermediate Code Generator/testcases/case1.c: -------------------------------------------------------------------------------- 1 | //Testcase generate simple expresion statements 2 | #include 3 | int main(){ 4 | int a; 5 | float t; 6 | a=10; 7 | t=t*10; 8 | return 1; 9 | } 10 | -------------------------------------------------------------------------------- /Semantic Analyzer/testcases/case2.c: -------------------------------------------------------------------------------- 1 | // Testcase to check undeclared variables 2 | #include 3 | int main() 4 | { 5 | int a=10,b=50,c=10; 6 | d = a+b; 7 | printf("The sum of %d + %d = %d",a,b,d); 8 | } 9 | -------------------------------------------------------------------------------- /Syntax Analyzer/screenshots/TestcaseScreenshot/OutputScreenshot/outputCase_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Syntax Analyzer/screenshots/TestcaseScreenshot/OutputScreenshot/outputCase_2.png -------------------------------------------------------------------------------- /Syntax Analyzer/screenshots/TestcaseScreenshot/OutputScreenshot/outputCase_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Syntax Analyzer/screenshots/TestcaseScreenshot/OutputScreenshot/outputCase_3.png -------------------------------------------------------------------------------- /Syntax Analyzer/screenshots/TestcaseScreenshot/OutputScreenshot/outputCase_4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Syntax Analyzer/screenshots/TestcaseScreenshot/OutputScreenshot/outputCase_4.png -------------------------------------------------------------------------------- /Syntax Analyzer/screenshots/TestcaseScreenshot/OutputScreenshot/outputCase_5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Syntax Analyzer/screenshots/TestcaseScreenshot/OutputScreenshot/outputCase_5.png -------------------------------------------------------------------------------- /Syntax Analyzer/screenshots/TestcaseScreenshot/OutputScreenshot/outputCase_6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Syntax Analyzer/screenshots/TestcaseScreenshot/OutputScreenshot/outputCase_6.png -------------------------------------------------------------------------------- /Syntax Analyzer/screenshots/TestcaseScreenshot/OutputScreenshot/outputCase_7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Syntax Analyzer/screenshots/TestcaseScreenshot/OutputScreenshot/outputCase_7.png -------------------------------------------------------------------------------- /Syntax Analyzer/screenshots/TestcaseScreenshot/OutputScreenshot/outputcase_1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/aswanthpp/Compiler-Design/HEAD/Syntax Analyzer/screenshots/TestcaseScreenshot/OutputScreenshot/outputcase_1.png -------------------------------------------------------------------------------- /Intermediate Code Generator/testcases/case3.c: -------------------------------------------------------------------------------- 1 | //testcase to check looping statements 2 | #include 3 | int main(){ 4 | int i,s; 5 | s=0; 6 | for(i=0;i<10;i=i+2){ 7 | s=s+i; 8 | } 9 | return 2; 10 | } 11 | -------------------------------------------------------------------------------- /Semantic Analyzer/testcases/case1.c: -------------------------------------------------------------------------------- 1 | //Testcase to check multiple declaration 2 | 3 | #include 4 | int main() 5 | { 6 | int a = 5, b = 3; 7 | float x; 8 | char b; 9 | 10 | int a = b + 10; 11 | } 12 | -------------------------------------------------------------------------------- /Syntax Analyzer/testcases/expected output/output2.txt: -------------------------------------------------------------------------------- 1 | Eplanation : 2 | 3 | There is an unbalanced else in line number 12 ,ie there should be else for each if statements 4 | 5 | Output: 6 | 7 | Misplaced else in line number 12 -------------------------------------------------------------------------------- /Semantic Analyzer/testcases/case4.c: -------------------------------------------------------------------------------- 1 | //Testcase to check scope of variables 2 | #include 3 | int func(){ 4 | int a; 5 | } 6 | int main() 7 | { 8 | int a; 9 | { 10 | int a; 11 | } 12 | func(); 13 | } 14 | 15 | 16 | -------------------------------------------------------------------------------- /Intermediate Code Generator/src/input.c: -------------------------------------------------------------------------------- 1 | #include 2 | int func(int a,int b); 3 | int main (){ 4 | int a; 5 | int a; 6 | a=a+10; 7 | while(a<10){ 8 | a=a+34; 9 | printf("\nHello"); 10 | } 11 | return 0; 12 | 13 | 14 | } 15 | 16 | -------------------------------------------------------------------------------- /Intermediate Code Generator/testcases/case2.c: -------------------------------------------------------------------------------- 1 | //testcase to generate control statments 2 | #include 3 | int main(){ 4 | int a; 5 | float t; 6 | a=20; 7 | if(a<30){ 8 | t=3.0; 9 | } 10 | else{ 11 | t=4.0; 12 | } 13 | return 2; 14 | } 15 | -------------------------------------------------------------------------------- /Intermediate Code Generator/testcases/case4.c: -------------------------------------------------------------------------------- 1 | //testcase to generate different functions code 2 | #include 3 | int func(int x,int y); 4 | int func1(){ 5 | int s; 6 | s=56; 7 | } 8 | int main(){ 9 | int a; 10 | a=20; 11 | return 1; 12 | } 13 | -------------------------------------------------------------------------------- /Semantic Analyzer/testcases/case5.c: -------------------------------------------------------------------------------- 1 | // Testcase to check scope of a variable 2 | 3 | #include 4 | void main() 5 | { 6 | int a = 10, b = 5; 7 | 8 | { 9 | printf("Enter a variable"); 10 | int b; 11 | scanf("%d",&b); 12 | } 13 | } -------------------------------------------------------------------------------- /Syntax Analyzer/testcases/input/case4.c: -------------------------------------------------------------------------------- 1 | //Testcase to check function decleration and parameter passing 2 | 3 | #include 4 | int main(){ 5 | 6 | int a[]={1,2,3,4}; 7 | function1(); 8 | } 9 | void function1(){ 10 | printf("\nHello World"); 11 | } 12 | 13 | -------------------------------------------------------------------------------- /Syntax Analyzer/testcases/input/case8.c: -------------------------------------------------------------------------------- 1 | //testcase 1 for parse tree 2 | #include 3 | #define x 3 4 | void main() 5 | { 6 | int a=4,b=5; 7 | int d[2]={3,5}; 8 | if(a<10) 9 | if(a>10) 10 | a=a+1; 11 | else 12 | a=a-1; 13 | for(;i 3 | int main() 4 | { 5 | int l=10,a=0,i; 6 | float l=5.0; 7 | for(i=0;i 2 | void func(){ 3 | int a; 4 | } 5 | void main() 6 | { 7 | 8 | int a=10,b; 9 | char c; 10 | int f[100][10]; 11 | a=b; 12 | a=b+12; 13 | printf("\nHello World"); 14 | scanf(); 15 | { 16 | int y; 17 | int a; 18 | } 19 | 20 | 21 | } 22 | 23 | -------------------------------------------------------------------------------- /Intermediate Code Generator/src/threeAddressCode.txt: -------------------------------------------------------------------------------- 1 | 2 | Intermediate Code Generated 3 | ------------------------------- 4 | 0 GOTO 1 5 | 1 Ti_1 := a + 10 6 | 2 a := Ti_1 7 | 3 IF (a < 10) GOTO 5 8 | 4 GOTO 8 9 | 5 Ti_2 := a + 34 10 | 6 a := Ti_2 11 | 7 GOTO 3 12 | 8 RETURN 0 13 | -------------------------------------------------------------------------------- /Lexical Analyzer/test cases/Case1.c: -------------------------------------------------------------------------------- 1 | // Test case to check Pre Processor Directives declarations 2 | 3 | #include 4 | #include "userHeader.h" 5 | #define size 10 6 | 7 | #include< 8 | 9 | int main(){ 10 | int a=5; 11 | char c='H'; 12 | char str[]="Hello World"; 13 | return 1; 14 | 15 | } 16 | -------------------------------------------------------------------------------- /Syntax Analyzer/testcases/input/case5.c: -------------------------------------------------------------------------------- 1 | //Testcase to check printf and scanf errors 2 | #include 3 | int main(){ 4 | int a=10; 5 | 6 | printf("\nHello world"); 7 | printf("%d",a); 8 | printf("hello",); 9 | printf("%d"); 10 | 11 | scanf("%d",&a); 12 | scanf("%d",a); 13 | scanf("%d",); 14 | 15 | } 16 | -------------------------------------------------------------------------------- /Syntax Analyzer/testcases/input/case7.c: -------------------------------------------------------------------------------- 1 | //testcase to check struct and uninon operations 2 | #include 3 | struct Node { 4 | int data; 5 | }; 6 | union Node2{ 7 | int w; 8 | }; 9 | 10 | int main(){ 11 | struct Node x; 12 | printf("\nEnjter Number : "); 13 | scanf("%d",&x->data); 14 | return 1; 15 | } 16 | -------------------------------------------------------------------------------- /Syntax Analyzer/testcases/input/case7.c~: -------------------------------------------------------------------------------- 1 | //testcase to check struct and uninon operations 2 | #include 3 | struct Node { 4 | int data; 5 | }; 6 | union Node2{ 7 | int w; 8 | }; 9 | 10 | int main(){ 11 | struct Node x; 12 | printf("\nEnjter Number : "); 13 | scanf("%d",&x->data); 14 | return 1; 15 | } 16 | -------------------------------------------------------------------------------- /Semantic Analyzer/src/input.c~: -------------------------------------------------------------------------------- 1 | #include 2 | void sum(int ,int); 3 | void main() 4 | { 5 | int a=5,b; 6 | printf("\nEnter Number:"); 7 | scanf("%d",&b); 8 | sum(5,b); 9 | 10 | 11 | } 12 | void sum(int inp1,int inp2){ 13 | int s=0; 14 | s=inp1+inp2; 15 | printf("\nSum : %d",s); 16 | return ; 17 | } 18 | -------------------------------------------------------------------------------- /Syntax Analyzer/src/input.c~: -------------------------------------------------------------------------------- 1 | #include 2 | void sum(int ,int); 3 | void main() 4 | { 5 | int a=5,b; 6 | printf("\nEnter Number:"); 7 | scanf("%d",&b); 8 | sum(5,b); 9 | 10 | 11 | } 12 | void sum(int inp1,int inp2){ 13 | int s=0; 14 | s=inp1+inp2; 15 | printf("\nSum : %d",s); 16 | return ; 17 | } 18 | -------------------------------------------------------------------------------- /Syntax Analyzer/src/input.c: -------------------------------------------------------------------------------- 1 | #include 2 | void sum(int ,int); 3 | void main() 4 | { 5 | int a=5,c,d=10; 6 | char b; 7 | printf("\nEnter Number:"); 8 | scanf("%d",&b); 9 | sum(5,b); 10 | } 11 | void sum(int inp1,char inp2){ 12 | int s=0; 13 | s=inp1+inp2; 14 | printf("\nSum : %d",s); 15 | return ; 16 | } 17 | -------------------------------------------------------------------------------- /Lexical Analyzer/test cases/Case2.c: -------------------------------------------------------------------------------- 1 | // Test case to check int and char constants,string constants, 2 | // keywords, punctuators and variables 3 | 4 | #include 5 | void main(){ 6 | int a=25; 7 | char c='h'; 8 | char c='abc 9 | char arr1[10]="hello World"; 10 | char arr2[10]="hello; 11 | return 1; 12 | 13 | 14 | } 15 | -------------------------------------------------------------------------------- /Lexical Analyzer/test cases/Case3.c: -------------------------------------------------------------------------------- 1 | // Test case to check relational, logical and arithmetic operators 2 | 3 | #include 4 | void main(){ 5 | 6 | int a=1,b=2,c=3,d; 7 | a++; 8 | --b; 9 | c=a+b; 10 | d+=a; 11 | d=a||b; 12 | c=a%b; 13 | d=a++c; 14 | a=c+/c; 15 | 16 | d=a>b; 17 | 18 | 19 | } 20 | -------------------------------------------------------------------------------- /Syntax Analyzer/testcases/input/case1.c: -------------------------------------------------------------------------------- 1 | //Testcase to check arithmatic logical and relational statements 2 | 3 | #include 4 | int main(){ 5 | int a=10,c=40,b=56,d; 6 | a=b+c; 7 | a+=c; 8 | a=b+c*d; 9 | d=b*(c+d); 10 | int d[100]; 11 | d[20]=a*d[10]+c; 12 | 13 | b=a**c; 14 | c=a++c; 15 | 16 | 17 | 18 | } -------------------------------------------------------------------------------- /Syntax Analyzer/testcases/input/case9.c: -------------------------------------------------------------------------------- 1 | //testcase 2 for parse tree 2 | 3 | //dangling else error 4 | #include 5 | #define x 3 6 | int main(int argc,int *argv[]) 7 | { 8 | int a=4; 9 | if(a<10) 10 | printf("10"); 11 | else 12 | { 13 | if(a<12) 14 | printf("11"); 15 | else 16 | printf("All"); 17 | else 18 | printf("error");} 19 | } 20 | -------------------------------------------------------------------------------- /Syntax Analyzer/testcases/input/constantTable.txt: -------------------------------------------------------------------------------- 1 | 2 | Constant Table 3 | 4 | Value Line Number 5 | 6 | 10 6 13 7 | 0 6 7 -------------------------------------------------------------------------------- /Syntax Analyzer/testcases/input/case3.c: -------------------------------------------------------------------------------- 1 | // Testcase to check looping statements 2 | #include 3 | int main(){ 4 | 5 | int l=10,a=0,i; 6 | for(i=0;i 3 | int main(){ 4 | 5 | int l=10,a=0,i; 6 | for(i=0;i 4 | struct student{ 5 | int a; 6 | char c; 7 | }; 8 | union teacher{ 9 | int q; 10 | int p; 11 | } 12 | void abc(){ 13 | printf("\nHello World"); 14 | } 15 | void main(){ 16 | student *S; 17 | teacher *T; 18 | abc(); 19 | } 20 | -------------------------------------------------------------------------------- /Semantic Analyzer/testcases/case6.c: -------------------------------------------------------------------------------- 1 | // Testcase to check array dimensions 2 | 3 | #include 4 | int main() 5 | { 6 | int i, arr1[100], arr2[100]; 7 | for(i=0;i<100;i++) 8 | { 9 | scanf("%d",&arr1[i]); 10 | arr2[i] = arr1[i] * 2; 11 | } 12 | 13 | for(i=0;i<100;i++) 14 | { 15 | printf("%d",arr2[i]); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Intermediate Code Generator/testcases/symbolTable.txt: -------------------------------------------------------------------------------- 1 | 2 | Symbol Table 3 | ------------------ 4 | 5 | Name DataType ReturnType Offset Scope 6 | ------------------------------------------------------------------------ 7 | y Int None 0 main 8 | main Main Int 4 None 9 | -------------------------------------------------------------------------------- /Syntax Analyzer/testcases/expected output/output4.txt: -------------------------------------------------------------------------------- 1 | Explanation : 2 | 3 | function1 definition and invoking is also correct 4 | function2 definition and invoking with the argument are matching hence no error 5 | function3 definition and first invoking is correct 6 | 7 | function3 second invoking is incorrect because few less function parameters 8 | 9 | 10 | Output: 11 | 12 | No matching function in line number 10 -------------------------------------------------------------------------------- /Lexical Analyzer/test cases/Case4.c: -------------------------------------------------------------------------------- 1 | // test case to check single and multi line comments 2 | 3 | #include 4 | void main() 5 | { 6 | // this is a single line comment 7 | 8 | /// this is also valid comment 9 | 10 | /* this is 11 | a multi line 12 | comment */ 13 | 14 | /* this is 15 | a /* nested */ 16 | comment */ 17 | 18 | invalid comment */ 19 | 20 | /* invalid comment 21 | 22 | 23 | } 24 | -------------------------------------------------------------------------------- /Syntax Analyzer/src/constantTable.txt: -------------------------------------------------------------------------------- 1 | 2 | Constant Table 3 | 4 | Value Line Number 5 | 6 | 5 5 9 7 | 10 5 8 | 0 12 -------------------------------------------------------------------------------- /Semantic Analyzer/src/constantTable.txt: -------------------------------------------------------------------------------- 1 | 2 | Constant Table 3 | 4 | Value Line Number 5 | 6 | 10 8 10 7 | 100 10 8 | 12 12 -------------------------------------------------------------------------------- /Semantic Analyzer/testcases/constantTable.txt: -------------------------------------------------------------------------------- 1 | 2 | Constant Table 3 | 4 | Value Line Number 5 | 6 | 100 7 7 8 14 7 | 0 8 14 8 | 2 11 -------------------------------------------------------------------------------- /Syntax Analyzer/testcases/expected output/output3.txt: -------------------------------------------------------------------------------- 1 | Explanation : 2 | 3 | Third for loop is an invalid syntax because cannot create loop without the semicolon seperated expression 4 | 5 | Fourth for loop is also invalid syntax because cannot create loop without comparison segments 6 | 7 | Fifth for loop is valid because looping can be done without updation segment also 8 | 9 | Output : 10 | 11 | Invalid syntax at line 12 12 | Invalid syntax at line 16 -------------------------------------------------------------------------------- /Syntax Analyzer/testcases/input/case2.c: -------------------------------------------------------------------------------- 1 | // Testcase to check control statements 2 | 3 | #include 4 | int main(){ 5 | int a=10,b=50,c=10; 6 | if(a>b){ 7 | printf("\na is greater than b"); 8 | } 9 | else{ 10 | printf("\n a is smaller than b"); 11 | } 12 | else{ 13 | printf("\nUnbalanced Else"); 14 | } 15 | 16 | if(a>b && b 4 | void main() 5 | { 6 | int a=1,b=2,c=10; 7 | 8 | if(a>b){ 9 | printf("\nInside if"); 10 | } 11 | else{ 12 | printf("\nInside else"); 13 | } 14 | 15 | if(b>a){ 16 | if(b 2 | int main() 3 | { 4 | /* My name is Arvind. 5 | This is 6 | a sample 7 | multi-line 8 | comment*/ 9 | struct node{ 10 | int a; 11 | char name; 12 | }; 13 | 14 | float fl = 5.01; 15 | char l1 = 'a', l2 ='1'; 16 | int _a=0,b=0,c=5; 17 | int arr[50]; 18 | char *ptr; 19 | scanf("%d %d",&a,&b); 20 | int sum=0; 21 | sum=a+b; 22 | printf("\n Sum : %d \n",sum); 23 | 24 | //Enter Name 25 | char *name = "Arvind"; 26 | 27 | //End the main function 28 | return 1; 29 | } 30 | void abc() 31 | { 32 | printf("\nTest function "); 33 | } 34 | -------------------------------------------------------------------------------- /Intermediate Code Generator/src/symbolTable.txt: -------------------------------------------------------------------------------- 1 | 2 | Symbol Table 3 | ------------------ 4 | 5 | Name DataType ReturnType Offset Scope 6 | ------------------------------------------------------------------------ 7 | func Proto Int 0 None 8 | a Int None 4 func 9 | b Int None 4 func 10 | a Int None 0 main 11 | a Int None 4 main 12 | Ti_1 Int None 8 main 13 | Ti_2 Int None 12 main 14 | main Main Int 16 None 15 | -------------------------------------------------------------------------------- /Lexical Analyzer/src/constantTable.txt: -------------------------------------------------------------------------------- 1 | 2 | Constants Table: 3 | Lexeme Type Attribute Value Line Number 4 | 5 | 5.01 float 14 14 6 | a char 6 15 7 | 1 char 18 15 8 | 0 int 20 16 9 | 0 int 20 16 10 | 5 int 23 16 11 | 0 int 20 20 12 | 1 int 18 28 -------------------------------------------------------------------------------- /Syntax Analyzer/src/symbolTable.txt: -------------------------------------------------------------------------------- 1 | 2 | SymbolTable 3 | 4 | Token Type Line Number 5 | 6 | sum VOID 2 9 11 7 | main VOID 3 8 | a INT 5 9 | c INT 5 10 | d INT 5 11 | b CHAR 6 8 9 12 | printf 7 14 13 | scanf 8 14 | inp1 INT 11 13 15 | inp2 CHAR 11 13 16 | s INT 12 13 14 -------------------------------------------------------------------------------- /Semantic Analyzer/src/symbolTable.txt: -------------------------------------------------------------------------------- 1 | 2 | SymbolTable 3 | 4 | Token Type Line Number Scope Function Number 5 | 6 | func VOID 2 GLOBAL 0 1 7 | a INT 3 NESTING 1 1 8 | main VOID 5 GLOBAL 0 2 9 | a INT 8 11 12 NESTING 1 2 10 | b INT 8 11 12 NESTING 1 2 11 | c CHAR 9 NESTING 1 2 12 | f INT 10 NESTING 1 2 13 | printf 13 NESTING 1 2 14 | scanf 14 NESTING 1 2 15 | y INT 16 NESTING 2 2 16 | a INT 17 NESTING 2 2 -------------------------------------------------------------------------------- /Semantic Analyzer/testcases/symbolTable.txt: -------------------------------------------------------------------------------- 1 | 2 | SymbolTable 3 | 4 | Token Type Line Number Scope Function Number 5 | 6 | main INT 5 GLOBAL 0 1 7 | i INT 7 8 8 8 NESTING 1 1 8 | arr1 INT 7 NESTING 1 1 9 | arr2 INT 7 NESTING 1 1 10 | scanf 10 NESTING 2 1 11 | arr1 10 11 NESTING 2 1 12 | i 10 11 11 NESTING 2 1 13 | arr2 11 NESTING 2 1 14 | i 14 14 14 NESTING 1 2 15 | printf 16 NESTING 2 2 16 | arr2 16 NESTING 2 2 17 | i 16 NESTING 2 2 -------------------------------------------------------------------------------- /Semantic Analyzer/src/parsedTable.txt: -------------------------------------------------------------------------------- 1 | 2 | Parsed Table 3 | 4 | Token Type Line Number 5 | 6 | void Keyword 2 5 7 | ( Punctuator 2 5 13 14 8 | ) Punctuator 2 5 13 14 9 | { Punctuator 2 6 15 10 | int Keyword 3 8 10 16 17 11 | ; Punctuator 3 8 9 10 11 12 13 14 16 17 12 | } Punctuator 4 18 21 13 | = Operator 8 11 12 14 | , Punctuator 8 15 | char Keyword 9 16 | [ Punctuator 10 10 17 | ] Punctuator 10 10 18 | + Operator 12 19 | "\nHello World" String Literal 13 -------------------------------------------------------------------------------- /Semantic Analyzer/testcases/parsedTable.txt: -------------------------------------------------------------------------------- 1 | 2 | Parsed Table 3 | 4 | Token Type Line Number 5 | 6 | int Keyword 5 7 7 | ( Punctuator 5 10 12 16 17 8 | ) Punctuator 5 10 12 16 17 9 | { Punctuator 6 9 15 10 | [ Punctuator 7 7 10 11 11 16 11 | ] Punctuator 7 7 10 11 11 16 12 | , Punctuator 7 7 10 16 13 | ; Punctuator 7 8 8 10 11 14 14 16 14 | = Operator 8 11 14 15 | i Operator 8 14 16 | "%d" String Literal 10 16 17 | & Operator 10 18 | * Operator 11 19 | } Punctuator 12 17 18 20 | for Keyword 12 17 -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Compiler-Design 2 | 3 |

Aim : Design a C Compiler

4 | 5 |

Course : Compiler Design Lab (CO351)

6 |

Team :

7 |
1. Arvind Ramachandran - 15CO111
8 | 2. Aswanth P. P. - 15CO112
9 | 10 |

Module

11 |

1. Lexical Analyzer

Lexical Analysis is the first phase of compiler also known as scanner. It converts the input program into a sequence of Tokens.It can be implemented with the Deterministic finite Automata. 12 | 13 |

2. Syntax Analyzer

Syntax Analysis or Parsing is the second phase,i.e. after lexical analysis. It checks the syntactical structure of the given input,i.e. whether the given input is in the correct syntax (of the language in which the input has been written) or not.It does so by building a data structure, called a Parse tree or Syntax tree.The parse tree is constructed by using the pre-defined Grammar of the language and the input string.If the given input string can be produced with the help of the syntax tree (in the derivation process),the input string is found to be in the correct syntax. 14 | 15 |

3. Semantic Analyzer

Semantic analysis is the task of ensuring that the declarations and statements of a program are semantically correct, i.e,that their meaning is clear and consistent with the way in which control structures and data types are supposed to be used. 16 | 17 |

4. Intermediate Code Generator

The front end of a compiler translates a source program into an independent intermediate code, then the back end of the compiler uses this intermediate code to generate the target code.Intermediate code can be either language specific (e.g., Byte Code for Java) or language independent (three-address code). 18 |
Three-Address Code
19 | Intermediate code generator receives input from its predecessor phase, semantic analyzer, in the form of an annotated syntax tree. That syntax tree then can be converted into a linear representation, e.g., postfix notation. Intermediate code tends to be machine independent code. Therefore, code generator assumes to have unlimited number of memory storage (register) to generate code. 20 | -------------------------------------------------------------------------------- /Syntax Analyzer/testcases/input/symbolTable.txt: -------------------------------------------------------------------------------- 1 | 2 | SymbolTable 3 | 4 | Token Type LineNumber 5 | 6 | int Keyword 4 6 7 | main Identifier 4 8 | ( Punctuator 4 8 9 11 12 14 15 9 | ) Punctuator 4 8 9 11 12 14 15 10 | { Punctuator 4 7 10 13 11 | l Identifier 6 7 10 12 | = Operator 6 6 7 13 | a Identifier 6 10 10 10 14 | , Punctuator 6 6 15 | i Identifier 6 7 7 7 7 13 16 | ; Punctuator 6 7 7 8 10 10 11 14 17 | printf Identifier 8 11 14 18 | "\nHello World" String Literal 8 19 | } Punctuator 9 12 15 17 20 | for Keyword 9 12 21 | "\nInvalid Syntax" String Literal 11 22 | "\nCompiler Design" String Literal 14 23 | while Keyword 15 -------------------------------------------------------------------------------- /Intermediate Code Generator/src/icg.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #define PARENT_NONE NULL 7 | 8 | typedef enum 9 | { 10 | BOOL_type, 11 | INT_type, 12 | FLOAT_type, 13 | FUNC, 14 | MAIN, 15 | PROTO, 16 | } tokenType; 17 | 18 | typedef enum 19 | { 20 | Return_VOID, 21 | Return_INT, 22 | Return_FLOAT 23 | 24 | } tokenReturnType; 25 | 26 | typedef enum 27 | { 28 | CONST_type, 29 | VAR_type, 30 | NONE_type 31 | 32 | } tokenConstType; 33 | 34 | 35 | 36 | struct tokenList 37 | { 38 | char *name; 39 | tokenType type; 40 | tokenReturnType returnType; 41 | long size; 42 | char *scope; 43 | long line; 44 | long parameter; 45 | 46 | struct tokenList *next; 47 | 48 | }; 49 | typedef struct tokenList tokenList; 50 | 51 | 52 | struct threeAddressCode 53 | { 54 | char *code; 55 | int gotoLine; 56 | 57 | struct threeAddressCode *next; 58 | 59 | }; 60 | typedef struct threeAddressCode threeAddressCode; 61 | 62 | struct backPatchList 63 | { 64 | threeAddressCode *entry; 65 | struct backPatchList *next; 66 | 67 | }; 68 | typedef struct backPatchList backPatchList ; 69 | 70 | 71 | threeAddressCode* appendCode(char *code); 72 | void backpatch(backPatchList * list, int gotoL); 73 | backPatchList* mergelists(backPatchList * a, backPatchList * b); 74 | backPatchList* appendToBackPatch(backPatchList * list, threeAddressCode * entry); 75 | tokenList* appendToSymbolTable(char *name,tokenType type,tokenReturnType returnType,long size,long line,char *scope,long parameter); 76 | void writeCode(FILE *icgOut); 77 | void writeSymbolTable(FILE *symOut); 78 | 79 | tokenList * addSymbolToParameterQueue(tokenList * queue, char *name, tokenType type); 80 | 81 | 82 | int checkAndGenerateParams(tokenList * queue, char* name ,int parameterCount); 83 | 84 | int getFunctionType(char *name); 85 | 86 | int getSymbolType(char *name); 87 | 88 | char* nextFloatVar(); 89 | 90 | char* nextIntVar(); 91 | 92 | char* nextBoolVar(); 93 | 94 | int nextquad(); 95 | 96 | void addFunction(char *name, unsigned int parameter_count, tokenReturnType ret_type, int line); 97 | 98 | tokenList * lookup(char *name); 99 | 100 | extern tokenList *symbolTable; 101 | -------------------------------------------------------------------------------- /Intermediate Code Generator/testcases/icg.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #define PARENT_NONE NULL 7 | 8 | typedef enum 9 | { 10 | BOOL_type, 11 | INT_type, 12 | FLOAT_type, 13 | FUNC, 14 | MAIN, 15 | PROTO, 16 | } tokenType; 17 | 18 | typedef enum 19 | { 20 | Return_VOID, 21 | Return_INT, 22 | Return_FLOAT 23 | 24 | } tokenReturnType; 25 | 26 | typedef enum 27 | { 28 | CONST_type, 29 | VAR_type, 30 | NONE_type 31 | 32 | } tokenConstType; 33 | 34 | 35 | 36 | struct tokenList 37 | { 38 | char *name; 39 | tokenType type; 40 | tokenReturnType returnType; 41 | long size; 42 | char *scope; 43 | long line; 44 | long parameter; 45 | 46 | struct tokenList *next; 47 | 48 | }; 49 | typedef struct tokenList tokenList; 50 | 51 | 52 | struct threeAddressCode 53 | { 54 | char *code; 55 | int gotoLine; 56 | 57 | struct threeAddressCode *next; 58 | 59 | }; 60 | typedef struct threeAddressCode threeAddressCode; 61 | 62 | struct backPatchList 63 | { 64 | threeAddressCode *entry; 65 | struct backPatchList *next; 66 | 67 | }; 68 | typedef struct backPatchList backPatchList ; 69 | 70 | 71 | threeAddressCode* appendCode(char *code); 72 | void backpatch(backPatchList * list, int gotoL); 73 | backPatchList* mergelists(backPatchList * a, backPatchList * b); 74 | backPatchList* appendToBackPatch(backPatchList * list, threeAddressCode * entry); 75 | tokenList* appendToSymbolTable(char *name,tokenType type,tokenReturnType returnType,long size,long line,char *scope,long parameter); 76 | void writeCode(FILE *icgOut); 77 | void writeSymbolTable(FILE *symOut); 78 | 79 | tokenList * addSymbolToParameterQueue(tokenList * queue, char *name, tokenType type); 80 | 81 | 82 | int checkAndGenerateParams(tokenList * queue, char* name ,int parameterCount); 83 | 84 | int getFunctionType(char *name); 85 | 86 | int getSymbolType(char *name); 87 | 88 | char* nextFloatVar(); 89 | 90 | char* nextIntVar(); 91 | 92 | char* nextBoolVar(); 93 | 94 | int nextquad(); 95 | 96 | void addFunction(char *name, unsigned int parameter_count, tokenReturnType ret_type, int line); 97 | 98 | tokenList * lookup(char *name); 99 | 100 | extern tokenList *symbolTable; 101 | -------------------------------------------------------------------------------- /Intermediate Code Generator/src/lexicalAnalyzer.l: -------------------------------------------------------------------------------- 1 | %option noyywrap 2 | %option nomain 3 | %option yylineno 4 | 5 | 6 | %{ 7 | #include "icg.h" 8 | #include "y.tab.h" 9 | 10 | // unary plus and minus can only appear at line beginnings and after opening paranthesises or *,/,% 11 | int f=0; 12 | %} 13 | 14 | NEWLINE (\n|\n\l|\r\n) 15 | WHITESPACE (" "|\t) 16 | SINGLELINE_COMMENT ("//".*{NEWLINE}) 17 | MULTILINE_COMMENT ("/*".*"*/") 18 | PREPROCESSOR ("#".*{NEWLINE}) 19 | INTEGER (0|[1-9][0-9]*) 20 | FLOAT (0\.0|[1-9][0-9]*\.[0-9]+) 21 | IDENTIFIER ([a-zA-Z_][a-zA-Z0-9_]*) 22 | LIBFUNC "printf"|"scanf" 23 | 24 | %% 25 | 26 | ({NEWLINE}+|{WHITESPACE}+) {} 27 | ({SINGLELINE_COMMENT}|{MULTILINE_COMMENT}) {} 28 | {PREPROCESSOR} {} 29 | 30 | ^("+"|"-") { 31 | if('+' == yytext[0]) { 32 | return(U_PLUS); 33 | } else { 34 | return(U_MINUS); 35 | } 36 | f = 0; 37 | } 38 | ("("|")"|"{"|"}"|"<"|">"|"="|";"|","|"!") {f = 1;return(yytext[0]);} 39 | 40 | ("/"|"*"|"%") {f = 1;return(yytext[0]);} 41 | ("+"|"-") { 42 | if(f==1) { 43 | if('+' == yytext[0]) { 44 | return(U_PLUS); 45 | } 46 | else { 47 | return(U_MINUS); 48 | } 49 | f = 0; 50 | } else { 51 | return(yytext[0]); 52 | } 53 | } 54 | 55 | "void" {return(VOID);} 56 | "int" {return(INT);} 57 | "float" {return(FLOAT);} 58 | "constant" {return(CONSTANT);} 59 | "if" {return(IF);} 60 | "else" {return(ELSE);} 61 | "return" {return(RETURN);} 62 | "do" {return(DO);} 63 | "while" {return(WHILE);} 64 | "for" {return(FOR);} 65 | "++" {return(INC_OP);} 66 | "--" {return(DEC_OP);} 67 | "==" {return(EQUAL);} 68 | "!=" {return(NOT_EQUAL);} 69 | ">=" {return(GREATER_OR_EQUAL);} 70 | "<=" {return(LESS_OR_EQUAL);} 71 | "<<" {return(SHIFTLEFT);} 72 | "&&" {return(LOG_AND);} 73 | "||" {return(LOG_OR);} 74 | {LIBFUNC}\(.*\)\; {//printf("LEXER: found Library function %s\n",yytext); 75 | //printf("\nLEXER : excluding meaning given during linking\n"); 76 | } 77 | 78 | {INTEGER} { 79 | yylval.expr.value = strdup(yytext); 80 | yylval.expr.cType = CONST_type; 81 | yylval.expr.type = INT_type; 82 | f = 0; 83 | return(CONSTANT); 84 | } 85 | {FLOAT} { 86 | yylval.expr.value = strdup(yytext); 87 | yylval.expr.cType = CONST_type; 88 | yylval.expr.type = FLOAT_type; 89 | f = 0; 90 | return(CONSTANT); 91 | } 92 | {IDENTIFIER} { 93 | yylval.str = malloc(strlen(yytext)*sizeof(char)+1); 94 | strcpy(yylval.str, yytext); 95 | f = 0; 96 | return(IDENTIFIER); 97 | } 98 | 99 | %% 100 | -------------------------------------------------------------------------------- /Intermediate Code Generator/testcases/lexicalAnalyzer.l: -------------------------------------------------------------------------------- 1 | %option noyywrap 2 | %option nomain 3 | %option yylineno 4 | 5 | 6 | %{ 7 | #include "icg.h" 8 | #include "y.tab.h" 9 | 10 | // unary plus and minus can only appear at line beginnings and after opening paranthesises or *,/,% 11 | int f=0; 12 | %} 13 | 14 | NEWLINE (\n|\n\l|\r\n) 15 | WHITESPACE (" "|\t) 16 | SINGLELINE_COMMENT ("//".*{NEWLINE}) 17 | MULTILINE_COMMENT ("/*".*"*/") 18 | PREPROCESSOR ("#".*{NEWLINE}) 19 | INTEGER (0|[1-9][0-9]*) 20 | FLOAT (0\.0|[1-9][0-9]*\.[0-9]+) 21 | IDENTIFIER ([a-zA-Z_][a-zA-Z0-9_]*) 22 | LIBFUNC "printf"|"scanf" 23 | 24 | %% 25 | 26 | ({NEWLINE}+|{WHITESPACE}+) {} 27 | ({SINGLELINE_COMMENT}|{MULTILINE_COMMENT}) {} 28 | {PREPROCESSOR} {} 29 | 30 | ^("+"|"-") { 31 | if('+' == yytext[0]) { 32 | return(U_PLUS); 33 | } else { 34 | return(U_MINUS); 35 | } 36 | f = 0; 37 | } 38 | ("("|")"|"{"|"}"|"<"|">"|"="|";"|","|"!") {f = 1;return(yytext[0]);} 39 | 40 | ("/"|"*"|"%") {f = 1;return(yytext[0]);} 41 | ("+"|"-") { 42 | if(f==1) { 43 | if('+' == yytext[0]) { 44 | return(U_PLUS); 45 | } 46 | else { 47 | return(U_MINUS); 48 | } 49 | f = 0; 50 | } else { 51 | return(yytext[0]); 52 | } 53 | } 54 | 55 | "void" {return(VOID);} 56 | "int" {return(INT);} 57 | "float" {return(FLOAT);} 58 | "constant" {return(CONSTANT);} 59 | "if" {return(IF);} 60 | "else" {return(ELSE);} 61 | "return" {return(RETURN);} 62 | "do" {return(DO);} 63 | "while" {return(WHILE);} 64 | "for" {return(FOR);} 65 | "++" {return(INC_OP);} 66 | "--" {return(DEC_OP);} 67 | "==" {return(EQUAL);} 68 | "!=" {return(NOT_EQUAL);} 69 | ">=" {return(GREATER_OR_EQUAL);} 70 | "<=" {return(LESS_OR_EQUAL);} 71 | "<<" {return(SHIFTLEFT);} 72 | "&&" {return(LOG_AND);} 73 | "||" {return(LOG_OR);} 74 | {LIBFUNC}\(.*\)\; {//printf("LEXER: found Library function %s\n",yytext); 75 | //printf("\nLEXER : excluding meaning given during linking\n"); 76 | } 77 | 78 | {INTEGER} { 79 | yylval.expr.value = strdup(yytext); 80 | yylval.expr.cType = CONST_type; 81 | yylval.expr.type = INT_type; 82 | f = 0; 83 | return(CONSTANT); 84 | } 85 | {FLOAT} { 86 | yylval.expr.value = strdup(yytext); 87 | yylval.expr.cType = CONST_type; 88 | yylval.expr.type = FLOAT_type; 89 | f = 0; 90 | return(CONSTANT); 91 | } 92 | {IDENTIFIER} { 93 | yylval.str = malloc(strlen(yytext)*sizeof(char)+1); 94 | strcpy(yylval.str, yytext); 95 | f = 0; 96 | return(IDENTIFIER); 97 | } 98 | 99 | %% 100 | -------------------------------------------------------------------------------- /Lexical Analyzer/src/symbolTable.txt: -------------------------------------------------------------------------------- 1 | 2 | Symbol Table: 3 | Lexeme Type Attribute Value Line Number 4 | 5 | main Procedure 2 2 6 | node Identifier 5 9 7 | a Identifier 6 10 8 | name Identifier 9 11 9 | fl Identifier 12 14 10 | l1 Identifier 15 15 11 | l2 Identifier 17 15 12 | _a Identifier 19 16 13 | b Identifier 21 16 14 | c Identifier 22 16 15 | arr Array 24 17 16 | ptr Pointer 25 18 17 | scanf Identifier 26 19 18 | a Identifier 6 19 19 | b Identifier 21 19 20 | sum Identifier 31 20 21 | sum Identifier 31 21 22 | a Identifier 6 21 23 | b Identifier 21 21 24 | printf Identifier 33 22 25 | sum Identifier 31 22 26 | name Pointer 9 25 27 | abc Procedure 38 30 28 | printf Identifier 33 32 -------------------------------------------------------------------------------- /Syntax Analyzer/src/parsedTable.txt: -------------------------------------------------------------------------------- 1 | 2 | Parsed Table 3 | 4 | Token Type LineNumber 5 | 6 | void Keyword 2 3 11 7 | sum Identifier 2 9 11 8 | int Keyword 2 2 5 11 12 9 | , Punctuator 2 5 5 8 9 11 14 10 | ( Punctuator 2 3 7 8 9 11 14 11 | ) Punctuator 2 3 7 8 9 11 14 12 | ; Punctuator 2 5 6 7 8 9 12 13 14 15 13 | main Identifier 3 14 | { Punctuator 4 11 15 | a Identifier 5 16 | 5 Constant 5 9 17 | = Operator 5 5 12 13 18 | c Identifier 5 19 | d Identifier 5 20 | 10 Constant 5 21 | char Keyword 6 11 22 | b Identifier 6 8 9 23 | printf Identifier 7 14 24 | "\nEnter Number:" String Literal 7 25 | scanf Identifier 8 26 | "%d" String Literal 8 27 | & Operator 8 28 | } Punctuator 10 16 29 | inp1 Identifier 11 13 30 | inp2 Identifier 11 13 31 | s Identifier 12 13 14 32 | 0 Constant 12 33 | + Operator 13 34 | "\nSum : %d" String Literal 14 35 | return Keyword 15 -------------------------------------------------------------------------------- /Syntax Analyzer/src/lexicalAnalyzer.l: -------------------------------------------------------------------------------- 1 | %{ 2 | #include 3 | #include "y.tab.h" 4 | int lineCount=1; 5 | int nestedCommentCount=0; 6 | int commentFlag=0; 7 | char *tablePtr; 8 | void addToken(char*); 9 | %} 10 | 11 | digit [0-9] 12 | letter [a-zA-Z_] 13 | hex [a-fA-F0-9] 14 | E [Ee][+-]?{digit}+ 15 | FS (f|F|l|letter) 16 | IS (u|U|l|letter)* 17 | 18 | singlelineComment (\/\/.*) 19 | multilineCommentStart (\/\*) 20 | multilineCommentEnd (\*\/) 21 | %x DETECT_COMMENT 22 | 23 | %% 24 | 25 | 26 | {singlelineComment} { lineCount++; } 27 | 28 | {multilineCommentStart} { BEGIN(DETECT_COMMENT); 29 | nestedCommentCount++; 30 | } 31 | 32 | {multilineCommentStart} { nestedCommentCount++; 33 | if(nestedCommentCount>1) 34 | commentFlag = 1; 35 | } 36 | 37 | {multilineCommentEnd} { BEGIN(INITIAL); lineCount++; 38 | if(nestedCommentCount>0) 39 | nestedCommentCount--; 40 | if(nestedCommentCount==0) 41 | BEGIN(INITIAL); 42 | } 43 | 44 | \n {lineCount++;} 45 | . {} 46 | 47 | 48 | 49 | 50 | "auto" { return(AUTO); } 51 | "break" { return(BREAK); } 52 | "case" { return(CASE); } 53 | "char" { return(CHAR); } 54 | "const" { return(CONST); } 55 | "continue" { return(CONTINUE); } 56 | "default" { return(DEFAULT); } 57 | "do" { return(DO); } 58 | "double" { return(DOUBLE); } 59 | "else" { return(ELSE); } 60 | "enum" { return(ENUM); } 61 | "extern" { return(EXTERN); } 62 | "float" { return(FLOAT); } 63 | "for" { return(FOR); } 64 | "goto" { return(GOTO); } 65 | "if" { return(IF); } 66 | "int" { return(INT); } 67 | "long" { return(LONG); } 68 | "register" { return(REGISTER); } 69 | "return" { return(RETURN); } 70 | "short" { return(SHORT); } 71 | "signed" { return(SIGNED); } 72 | "sizeof" { return(SIZEOF); } 73 | "static" { return(STATIC); } 74 | "struct" { return(STRUCT); } 75 | "switch" { return(SWITCH); } 76 | "typedef" { return(TYPEDEF); } 77 | "union" { return(UNION); } 78 | "unsigned" { return(UNSIGNED); } 79 | "void" { return(VOID); } 80 | "volatile" { return(VOLATILE); } 81 | "while" { return(WHILE); } 82 | 83 | {letter}({letter}|{digit})* { addToken(yytext); return(IDENTIFIER); } 84 | {letter}?\"(\\.|[^\\"])*\" { addToken(yytext); return(STRING_LITERAL); } 85 | 86 | 0[xX]{hex}+{IS}? { addToken(yytext); return(CONSTANT); } 87 | 0{digit}+{IS}? { addToken(yytext); return(CONSTANT); } 88 | {hex}+{IS}? { addToken(yytext); return(CONSTANT); } 89 | {letter}?'(\\.|[^\\'])+' { addToken(yytext); return(CONSTANT); } 90 | {digit}+{E}{FS}? { addToken(yytext); return(CONSTANT); } 91 | {digit}*"."{digit}+({E})?{FS}? { addToken(yytext); return(CONSTANT); } 92 | {digit}+"."{digit}*({E})?{FS}? { addToken(yytext); return(CONSTANT); } 93 | 94 | "..." { return(ELLIPSIS); } 95 | ">>=" { return(RIGHT_ASSIGN); } 96 | "<<=" { return(LEFT_ASSIGN); } 97 | "+=" { return(ADD_ASSIGN); } 98 | "-=" { return(SUB_ASSIGN); } 99 | "*=" { return(MUL_ASSIGN); } 100 | "/=" { return(DIV_ASSIGN); } 101 | "%=" { return(MOD_ASSIGN); } 102 | "&=" { return(AND_ASSIGN); } 103 | "^=" { return(XOR_ASSIGN); } 104 | "|=" { return(OR_ASSIGN); } 105 | ">>" { return(RIGHT_OP); } 106 | "<<" { return(LEFT_OP); } 107 | "++" { return(INC_OP); } 108 | "--" { return(DEC_OP); } 109 | "->" { return(PTR_OP); } 110 | "&&" { return(AND_OP); } 111 | "||" { return(OR_OP); } 112 | "<=" { return(LE_OP); } 113 | ">=" { return(GE_OP); } 114 | "==" { return(EQ_OP); } 115 | "!=" { return(NE_OP); } 116 | ";" { return(';'); } 117 | ("{"|"<%") { makeList("{",'p',lineCount); return('{'); } 118 | ("}"|"%>") { makeList("}",'p',lineCount); return('}'); } 119 | "," { return(','); } 120 | ":" { return(':'); } 121 | "=" { return('='); } 122 | "(" { return('('); } 123 | ")" { return(')'); } 124 | ("["|"<:") { return('['); } 125 | ("]"|":>") { return(']'); } 126 | "." { return('.'); } 127 | "&" { return('&'); } 128 | "!" { return('!'); } 129 | "~" { return('~'); } 130 | "-" { return('-'); } 131 | "+" { return('+'); } 132 | "*" { return('*'); } 133 | "/" { return('/'); } 134 | "%" { return('%'); } 135 | "<" { return('<'); } 136 | ">" { return('>'); } 137 | "^" { return('^'); } 138 | "|" { return('|'); } 139 | "?" { return('?'); } 140 | 141 | 142 | "#include"(.)*"\n" { lineCount++; } 143 | "#define"(.)*"\n" { lineCount++; } 144 | 145 | [ ] {} 146 | [\t\v\f] {} 147 | [\n] {lineCount++;} 148 | . { } 149 | 150 | %% 151 | yywrap() 152 | { 153 | return(1); 154 | } 155 | 156 | void addToken(char *yytext) 157 | { 158 | int len = strlen(yytext); 159 | tablePtr = (char*)malloc((len+1)*sizeof(char)); 160 | strcpy(tablePtr, yytext); 161 | } 162 | 163 | -------------------------------------------------------------------------------- /Syntax Analyzer/testcases/input/lexicalAnalyzer.l: -------------------------------------------------------------------------------- 1 | %{ 2 | #include 3 | #include "y.tab.h" 4 | int lineCount=1; 5 | int nestedCommentCount=0; 6 | int commentFlag=0; 7 | char *tablePtr; 8 | void addToken(char*); 9 | %} 10 | 11 | digit [0-9] 12 | letter [a-zA-Z_] 13 | hex [a-fA-F0-9] 14 | E [Ee][+-]?{digit}+ 15 | FS (f|F|l|letter) 16 | IS (u|U|l|letter)* 17 | 18 | singlelineComment (\/\/.*) 19 | multilineCommentStart (\/\*) 20 | multilineCommentEnd (\*\/) 21 | %x DETECT_COMMENT 22 | 23 | %% 24 | 25 | 26 | {singlelineComment} { lineCount++; } 27 | 28 | {multilineCommentStart} { BEGIN(DETECT_COMMENT); 29 | nestedCommentCount++; 30 | } 31 | 32 | {multilineCommentStart} { nestedCommentCount++; 33 | if(nestedCommentCount>1) 34 | commentFlag = 1; 35 | } 36 | 37 | {multilineCommentEnd} { BEGIN(INITIAL); lineCount++; 38 | if(nestedCommentCount>0) 39 | nestedCommentCount--; 40 | if(nestedCommentCount==0) 41 | BEGIN(INITIAL); 42 | } 43 | 44 | \n {lineCount++;} 45 | . {} 46 | 47 | 48 | 49 | 50 | "auto" { return(AUTO); } 51 | "break" { return(BREAK); } 52 | "case" { return(CASE); } 53 | "char" { return(CHAR); } 54 | "const" { return(CONST); } 55 | "continue" { return(CONTINUE); } 56 | "default" { return(DEFAULT); } 57 | "do" { return(DO); } 58 | "double" { return(DOUBLE); } 59 | "else" { return(ELSE); } 60 | "enum" { return(ENUM); } 61 | "extern" { return(EXTERN); } 62 | "float" { return(FLOAT); } 63 | "for" { return(FOR); } 64 | "goto" { return(GOTO); } 65 | "if" { return(IF); } 66 | "int" { return(INT); } 67 | "long" { return(LONG); } 68 | "register" { return(REGISTER); } 69 | "return" { return(RETURN); } 70 | "short" { return(SHORT); } 71 | "signed" { return(SIGNED); } 72 | "sizeof" { return(SIZEOF); } 73 | "static" { return(STATIC); } 74 | "struct" { return(STRUCT); } 75 | "switch" { return(SWITCH); } 76 | "typedef" { return(TYPEDEF); } 77 | "union" { return(UNION); } 78 | "unsigned" { return(UNSIGNED); } 79 | "void" { return(VOID); } 80 | "volatile" { return(VOLATILE); } 81 | "while" { return(WHILE); } 82 | 83 | {letter}({letter}|{digit})* { addToken(yytext); return(IDENTIFIER); } 84 | {letter}?\"(\\.|[^\\"])*\" { addToken(yytext); return(STRING_LITERAL); } 85 | 86 | 0[xX]{hex}+{IS}? { addToken(yytext); return(CONSTANT); } 87 | 0{digit}+{IS}? { addToken(yytext); return(CONSTANT); } 88 | {hex}+{IS}? { addToken(yytext); return(CONSTANT); } 89 | {letter}?'(\\.|[^\\'])+' { addToken(yytext); return(CONSTANT); } 90 | {digit}+{E}{FS}? { addToken(yytext); return(CONSTANT); } 91 | {digit}*"."{digit}+({E})?{FS}? { addToken(yytext); return(CONSTANT); } 92 | {digit}+"."{digit}*({E})?{FS}? { addToken(yytext); return(CONSTANT); } 93 | 94 | "..." { return(ELLIPSIS); } 95 | ">>=" { return(RIGHT_ASSIGN); } 96 | "<<=" { return(LEFT_ASSIGN); } 97 | "+=" { return(ADD_ASSIGN); } 98 | "-=" { return(SUB_ASSIGN); } 99 | "*=" { return(MUL_ASSIGN); } 100 | "/=" { return(DIV_ASSIGN); } 101 | "%=" { return(MOD_ASSIGN); } 102 | "&=" { return(AND_ASSIGN); } 103 | "^=" { return(XOR_ASSIGN); } 104 | "|=" { return(OR_ASSIGN); } 105 | ">>" { return(RIGHT_OP); } 106 | "<<" { return(LEFT_OP); } 107 | "++" { return(INC_OP); } 108 | "--" { return(DEC_OP); } 109 | "->" { return(PTR_OP); } 110 | "&&" { return(AND_OP); } 111 | "||" { return(OR_OP); } 112 | "<=" { return(LE_OP); } 113 | ">=" { return(GE_OP); } 114 | "==" { return(EQ_OP); } 115 | "!=" { return(NE_OP); } 116 | ";" { return(';'); } 117 | ("{"|"<%") { makeList("{",'p',lineCount); return('{'); } 118 | ("}"|"%>") { makeList("}",'p',lineCount); return('}'); } 119 | "," { return(','); } 120 | ":" { return(':'); } 121 | "=" { return('='); } 122 | "(" { return('('); } 123 | ")" { return(')'); } 124 | ("["|"<:") { return('['); } 125 | ("]"|":>") { return(']'); } 126 | "." { return('.'); } 127 | "&" { return('&'); } 128 | "!" { return('!'); } 129 | "~" { return('~'); } 130 | "-" { return('-'); } 131 | "+" { return('+'); } 132 | "*" { return('*'); } 133 | "/" { return('/'); } 134 | "%" { return('%'); } 135 | "<" { return('<'); } 136 | ">" { return('>'); } 137 | "^" { return('^'); } 138 | "|" { return('|'); } 139 | "?" { return('?'); } 140 | 141 | 142 | "#include"(.)*"\n" { lineCount++; } 143 | "#define"(.)*"\n" { lineCount++; } 144 | 145 | [ ] {} 146 | [\t\v\f] {} 147 | [\n] {lineCount++;} 148 | . { } 149 | 150 | %% 151 | yywrap() 152 | { 153 | return(1); 154 | } 155 | 156 | void addToken(char *yytext) 157 | { 158 | int len = strlen(yytext); 159 | tablePtr = (char*)malloc((len+1)*sizeof(char)); 160 | strcpy(tablePtr, yytext); 161 | } 162 | 163 | -------------------------------------------------------------------------------- /Semantic Analyzer/src/lexicalAnalyzer.l: -------------------------------------------------------------------------------- 1 | %{ 2 | #include 3 | #include "y.tab.h" 4 | int arrayIndexErr=0; 5 | int lineCount=1; 6 | int nestedCommentCount=0; 7 | int scopeCount=0; 8 | int commentFlag=0; 9 | int functionCount; 10 | char *tablePtr; 11 | void addToken(char*); 12 | %} 13 | 14 | digit [0-9] 15 | letter [a-zA-Z_] 16 | hex [a-fA-F0-9] 17 | E [Ee][+-]?{digit}+ 18 | FS (f|F|l|letter) 19 | IS (u|U|l|letter)* 20 | 21 | singlelineComment (\/\/.*) 22 | multilineCommentStart (\/\*) 23 | multilineCommentEnd (\*\/) 24 | %x DETECT_COMMENT 25 | 26 | %% 27 | 28 | 29 | {singlelineComment} { lineCount++; } 30 | 31 | {multilineCommentStart} { BEGIN(DETECT_COMMENT); 32 | nestedCommentCount++; 33 | } 34 | 35 | {multilineCommentStart} { nestedCommentCount++; 36 | if(nestedCommentCount>1) 37 | commentFlag = 1; 38 | } 39 | 40 | {multilineCommentEnd} { BEGIN(INITIAL); lineCount++; 41 | if(nestedCommentCount>0) 42 | nestedCommentCount--; 43 | if(nestedCommentCount==0) 44 | BEGIN(INITIAL); 45 | } 46 | 47 | \n {lineCount++;} 48 | . {} 49 | 50 | 51 | 52 | 53 | "auto" { return(AUTO); } 54 | "break" { return(BREAK); } 55 | "case" { return(CASE); } 56 | "char" { return(CHAR); } 57 | "const" { return(CONST); } 58 | "continue" { return(CONTINUE); } 59 | "default" { return(DEFAULT); } 60 | "do" { return(DO); } 61 | "double" { return(DOUBLE); } 62 | "else" { return(ELSE); } 63 | "enum" { return(ENUM); } 64 | "extern" { return(EXTERN); } 65 | "float" { return(FLOAT); } 66 | "for" { return(FOR); } 67 | "goto" { return(GOTO); } 68 | "if" { return(IF); } 69 | "int" { return(INT); } 70 | "long" { return(LONG); } 71 | "register" { return(REGISTER); } 72 | "return" { return(RETURN); } 73 | "short" { return(SHORT); } 74 | "signed" { return(SIGNED); } 75 | "sizeof" { return(SIZEOF); } 76 | "static" { return(STATIC); } 77 | "struct" { return(STRUCT); } 78 | "switch" { return(SWITCH); } 79 | "typedef" { return(TYPEDEF); } 80 | "union" { return(UNION); } 81 | "unsigned" { return(UNSIGNED); } 82 | "void" { return(VOID); } 83 | "volatile" { return(VOLATILE); } 84 | "while" { return(WHILE); } 85 | "["[-]{digit}+"]" {arrayIndexErr=1;printf("\n Invalid array Index %d",lineCount);} 86 | "["{digit}*"."{digit}+({E})?{FS}?"]" {arrayIndexErr=1;printf("\nInvalid array Index %d",lineCount);} 87 | 88 | {letter}({letter}|{digit})* { addToken(yytext); return(IDENTIFIER); } 89 | {letter}?\"(\\.|[^\\"])*\" { addToken(yytext); return(STRING_LITERAL); } 90 | 91 | 0[xX]{hex}+{IS}? { addToken(yytext); return(CONSTANT); } 92 | 0{digit}+{IS}? { addToken(yytext); return(CONSTANT); } 93 | {hex}+{IS}? { addToken(yytext); return(CONSTANT); } 94 | {letter}?'(\\.|[^\\'])+' { addToken(yytext); return(CONSTANT); } 95 | 96 | 97 | {digit}+{E}{FS}? { addToken(yytext); return(FLCONSTANT);} 98 | {digit}*"."{digit}+({E})?{FS}? { addToken(yytext); return(FLCONSTANT); } 99 | {digit}+"."{digit}*({E})?{FS}? { addToken(yytext); return(FLCONSTANT); } 100 | 101 | 102 | "..." { return(ELLIPSIS); } 103 | ">>=" { return(RIGHT_ASSIGN); } 104 | "<<=" { return(LEFT_ASSIGN); } 105 | "+=" { return(ADD_ASSIGN); } 106 | "-=" { return(SUB_ASSIGN); } 107 | "*=" { return(MUL_ASSIGN); } 108 | "/=" { return(DIV_ASSIGN); } 109 | "%=" { return(MOD_ASSIGN); } 110 | "&=" { return(AND_ASSIGN); } 111 | "^=" { return(XOR_ASSIGN); } 112 | "|=" { return(OR_ASSIGN); } 113 | ">>" { return(RIGHT_OP); } 114 | "<<" { return(LEFT_OP); } 115 | "++" { return(INC_OP); } 116 | "--" { return(DEC_OP); } 117 | "->" { return(PTR_OP); } 118 | "&&" { return(AND_OP); } 119 | "||" { return(OR_OP); } 120 | "<=" { return(LE_OP); } 121 | ">=" { return(GE_OP); } 122 | "==" { return(EQ_OP); } 123 | "!=" { return(NE_OP); } 124 | ";" { return(';'); } 125 | ("{"|"<%") { scopeCount++;makeList("{",'p',lineCount); return('{'); } 126 | ("}"|"%>") { scopeCount--;functionCount++;makeList("}",'p',lineCount); return('}'); } 127 | "," { return(','); } 128 | ":" { return(':'); } 129 | "=" { return('='); } 130 | "(" { return('('); } 131 | ")" { return(')'); } 132 | ("["|"<:") { return('['); } 133 | ("]"|":>") { return(']'); } 134 | "." { return('.'); } 135 | "&" { return('&'); } 136 | "!" { return('!'); } 137 | "~" { return('~'); } 138 | "-" { return('-'); } 139 | "+" { return('+'); } 140 | "*" { return('*'); } 141 | "/" { return('/'); } 142 | "%" { return('%'); } 143 | "<" { return('<'); } 144 | ">" { return('>'); } 145 | "^" { return('^'); } 146 | "|" { return('|'); } 147 | "?" { return('?'); } 148 | 149 | 150 | "#include"(.)*"\n" { lineCount++; } 151 | "#define"(.)*"\n" { lineCount++; } 152 | 153 | [ ] {} 154 | [\t\v\f] {} 155 | [\n] {lineCount++;} 156 | . { } 157 | 158 | %% 159 | yywrap() 160 | { 161 | return(1); 162 | } 163 | 164 | void addToken(char *yytext) 165 | { 166 | int len = strlen(yytext); 167 | tablePtr = (char*)malloc((len+1)*sizeof(char)); 168 | strcpy(tablePtr, yytext); 169 | } 170 | 171 | -------------------------------------------------------------------------------- /Semantic Analyzer/testcases/lexicalAnalyzer.l: -------------------------------------------------------------------------------- 1 | %{ 2 | #include 3 | #include "y.tab.h" 4 | int arrayIndexErr=0; 5 | int lineCount=1; 6 | int nestedCommentCount=0; 7 | int scopeCount=0; 8 | int commentFlag=0; 9 | int functionCount; 10 | char *tablePtr; 11 | void addToken(char*); 12 | %} 13 | 14 | digit [0-9] 15 | letter [a-zA-Z_] 16 | hex [a-fA-F0-9] 17 | E [Ee][+-]?{digit}+ 18 | FS (f|F|l|letter) 19 | IS (u|U|l|letter)* 20 | 21 | singlelineComment (\/\/.*) 22 | multilineCommentStart (\/\*) 23 | multilineCommentEnd (\*\/) 24 | %x DETECT_COMMENT 25 | 26 | %% 27 | 28 | 29 | {singlelineComment} { lineCount++; } 30 | 31 | {multilineCommentStart} { BEGIN(DETECT_COMMENT); 32 | nestedCommentCount++; 33 | } 34 | 35 | {multilineCommentStart} { nestedCommentCount++; 36 | if(nestedCommentCount>1) 37 | commentFlag = 1; 38 | } 39 | 40 | {multilineCommentEnd} { BEGIN(INITIAL); lineCount++; 41 | if(nestedCommentCount>0) 42 | nestedCommentCount--; 43 | if(nestedCommentCount==0) 44 | BEGIN(INITIAL); 45 | } 46 | 47 | \n {lineCount++;} 48 | . {} 49 | 50 | 51 | 52 | 53 | "auto" { return(AUTO); } 54 | "break" { return(BREAK); } 55 | "case" { return(CASE); } 56 | "char" { return(CHAR); } 57 | "const" { return(CONST); } 58 | "continue" { return(CONTINUE); } 59 | "default" { return(DEFAULT); } 60 | "do" { return(DO); } 61 | "double" { return(DOUBLE); } 62 | "else" { return(ELSE); } 63 | "enum" { return(ENUM); } 64 | "extern" { return(EXTERN); } 65 | "float" { return(FLOAT); } 66 | "for" { return(FOR); } 67 | "goto" { return(GOTO); } 68 | "if" { return(IF); } 69 | "int" { return(INT); } 70 | "long" { return(LONG); } 71 | "register" { return(REGISTER); } 72 | "return" { return(RETURN); } 73 | "short" { return(SHORT); } 74 | "signed" { return(SIGNED); } 75 | "sizeof" { return(SIZEOF); } 76 | "static" { return(STATIC); } 77 | "struct" { return(STRUCT); } 78 | "switch" { return(SWITCH); } 79 | "typedef" { return(TYPEDEF); } 80 | "union" { return(UNION); } 81 | "unsigned" { return(UNSIGNED); } 82 | "void" { return(VOID); } 83 | "volatile" { return(VOLATILE); } 84 | "while" { return(WHILE); } 85 | "["[-]{digit}+"]" {arrayIndexErr=1;printf("\n Invalid array Index %d",lineCount);} 86 | "["{digit}*"."{digit}+({E})?{FS}?"]" {arrayIndexErr=1;printf("\nInvalid array Index %d",lineCount);} 87 | 88 | {letter}({letter}|{digit})* { addToken(yytext); return(IDENTIFIER); } 89 | {letter}?\"(\\.|[^\\"])*\" { addToken(yytext); return(STRING_LITERAL); } 90 | 91 | 0[xX]{hex}+{IS}? { addToken(yytext); return(CONSTANT); } 92 | 0{digit}+{IS}? { addToken(yytext); return(CONSTANT); } 93 | {hex}+{IS}? { addToken(yytext); return(CONSTANT); } 94 | {letter}?'(\\.|[^\\'])+' { addToken(yytext); return(CONSTANT); } 95 | 96 | 97 | {digit}+{E}{FS}? { addToken(yytext); return(FLCONSTANT);} 98 | {digit}*"."{digit}+({E})?{FS}? { addToken(yytext); return(FLCONSTANT); } 99 | {digit}+"."{digit}*({E})?{FS}? { addToken(yytext); return(FLCONSTANT); } 100 | 101 | 102 | "..." { return(ELLIPSIS); } 103 | ">>=" { return(RIGHT_ASSIGN); } 104 | "<<=" { return(LEFT_ASSIGN); } 105 | "+=" { return(ADD_ASSIGN); } 106 | "-=" { return(SUB_ASSIGN); } 107 | "*=" { return(MUL_ASSIGN); } 108 | "/=" { return(DIV_ASSIGN); } 109 | "%=" { return(MOD_ASSIGN); } 110 | "&=" { return(AND_ASSIGN); } 111 | "^=" { return(XOR_ASSIGN); } 112 | "|=" { return(OR_ASSIGN); } 113 | ">>" { return(RIGHT_OP); } 114 | "<<" { return(LEFT_OP); } 115 | "++" { return(INC_OP); } 116 | "--" { return(DEC_OP); } 117 | "->" { return(PTR_OP); } 118 | "&&" { return(AND_OP); } 119 | "||" { return(OR_OP); } 120 | "<=" { return(LE_OP); } 121 | ">=" { return(GE_OP); } 122 | "==" { return(EQ_OP); } 123 | "!=" { return(NE_OP); } 124 | ";" { return(';'); } 125 | ("{"|"<%") { scopeCount++;makeList("{",'p',lineCount); return('{'); } 126 | ("}"|"%>") { scopeCount--;functionCount++;makeList("}",'p',lineCount); return('}'); } 127 | "," { return(','); } 128 | ":" { return(':'); } 129 | "=" { return('='); } 130 | "(" { return('('); } 131 | ")" { return(')'); } 132 | ("["|"<:") { return('['); } 133 | ("]"|":>") { return(']'); } 134 | "." { return('.'); } 135 | "&" { return('&'); } 136 | "!" { return('!'); } 137 | "~" { return('~'); } 138 | "-" { return('-'); } 139 | "+" { return('+'); } 140 | "*" { return('*'); } 141 | "/" { return('/'); } 142 | "%" { return('%'); } 143 | "<" { return('<'); } 144 | ">" { return('>'); } 145 | "^" { return('^'); } 146 | "|" { return('|'); } 147 | "?" { return('?'); } 148 | 149 | 150 | "#include"(.)*"\n" { lineCount++; } 151 | "#define"(.)*"\n" { lineCount++; } 152 | 153 | [ ] {} 154 | [\t\v\f] {} 155 | [\n] {lineCount++;} 156 | . { } 157 | 158 | %% 159 | yywrap() 160 | { 161 | return(1); 162 | } 163 | 164 | void addToken(char *yytext) 165 | { 166 | int len = strlen(yytext); 167 | tablePtr = (char*)malloc((len+1)*sizeof(char)); 168 | strcpy(tablePtr, yytext); 169 | } 170 | 171 | -------------------------------------------------------------------------------- /Semantic Analyzer/src/semantic.h: -------------------------------------------------------------------------------- 1 | struct tokenList 2 | { 3 | char *token,type[20],line[100]; 4 | char *scope[20]; 5 | int scopeValue; 6 | int funcCount; 7 | struct tokenList *next; 8 | }; 9 | typedef struct tokenList tokenList; 10 | struct funcNode{ 11 | char funcName[30]; 12 | int line; 13 | char funcReturn[20]; 14 | struct funcNode *next; 15 | }; 16 | typedef struct funcNode funcNode; 17 | 18 | 19 | tokenList *symbolPtr = NULL; 20 | tokenList *constantPtr = NULL; 21 | tokenList *parsedPtr =NULL; 22 | extern int functionCount; 23 | extern int scopeCount; 24 | char typeBuffer=' '; 25 | char *sourceCode; 26 | int tempCheckType=3; 27 | 28 | int semanticErr=0,lineSemanticCount; 29 | int checkScope(char *tempToken,int lineCount) 30 | { tokenList *temp=NULL; 31 | char type[20]; 32 | int flag=0,tempFlag=0; 33 | for(tokenList *p=symbolPtr;p!=NULL;p=p->next){ 34 | if(strcmp(tempToken,"printf")==0 || strcmp(tempToken,"scanf")==0){ 35 | tempFlag=1; 36 | } 37 | else{ 38 | if(strcmp(tempToken,p->token)==0){ 39 | strcpy(type,p->type); 40 | flag=1; 41 | break; 42 | } 43 | } 44 | 45 | } 46 | if (flag == 0 && tempFlag ==0 ) 47 | { 48 | printf("\n%s : %d :Undeclared variable \n",sourceCode,lineCount-1); 49 | semanticErr=1; 50 | } 51 | else 52 | { 53 | addSymbol(tempToken,lineCount); 54 | if(strcmp(type,"VOID")==0) 55 | return(1); 56 | if(strcmp(type,"CHAR")==0) 57 | return(2); 58 | if(strcmp(type,"INT")==0) 59 | return(3); 60 | if(strcmp(type,"FLOAT")==0) 61 | return(4); 62 | } 63 | 64 | } 65 | 66 | void checkType(int value1,int value2,int lineCount) 67 | { lineSemanticCount=lineCount; 68 | if(value2 == 0) 69 | value2 = tempCheckType; 70 | if(value1!=value2) 71 | { 72 | printf("\n%s : %d :Type Mismatch error \n",sourceCode,lineSemanticCount-1); 73 | semanticErr=1; 74 | } 75 | tempCheckType=3; 76 | } 77 | void checkDeclaration(char *tokenName,int tokenLine,int scopeVal){ 78 | char type[20]; 79 | char line[39],lineBuffer[19]; 80 | snprintf(lineBuffer, 19, "%d", tokenLine); 81 | strcpy(line," "); 82 | strcat(line,lineBuffer); 83 | switch(typeBuffer){ 84 | case 'i': strcpy(type,"INT"); break; 85 | case 'f': strcpy(type,"FLOAT");break; 86 | case 'v': strcpy(type,"VOID");break; 87 | case 'c': strcpy(type,"CHAR");break; 88 | 89 | } 90 | for(tokenList *p=symbolPtr;p!=NULL;p=p->next){ 91 | if(strcmp(p->token,tokenName)==0 && p->scopeValue == scopeCount && p->funcCount == functionCount){ 92 | semanticErr=1; 93 | if(strcmp(p->type,type)==0){ 94 | printf("\n%s : %d :Multiple Declaration \n",sourceCode,tokenLine); 95 | return; 96 | } 97 | else{ 98 | printf("\n%s : %d :Multiple Declration with Different Type \n",sourceCode,tokenLine); 99 | return; 100 | } 101 | } 102 | } 103 | addSymbol(tokenName,tokenLine,scopeCount); 104 | 105 | } 106 | void checkArray(int val,int lineCount){ 107 | if(val<0){ 108 | semanticErr=1; 109 | printf("\n%s : %d :Array Index error\n",sourceCode,lineCount-1); 110 | } 111 | } 112 | void addSymbol(char *tokenName,int tokenLine,int scopeVal){ 113 | char line[39],lineBuffer[19]; 114 | snprintf(lineBuffer, 19, "%d", tokenLine); 115 | strcpy(line," "); 116 | strcat(line,lineBuffer); 117 | char type[20]; 118 | for(tokenList *p=symbolPtr;p!=NULL;p=p->next) 119 | if(strcmp(p->token,tokenName)==0 && p->scopeValue == scopeCount && p->funcCount ==functionCount ){ 120 | strcat(p->line,line); 121 | return; 122 | } 123 | tokenList *temp=(tokenList *)malloc(sizeof(tokenList)); 124 | temp->token=(char *)malloc(strlen(tokenName)+1); 125 | strcpy(temp->token,tokenName); 126 | switch(typeBuffer){ 127 | case 'i': strcpy(temp->type,"INT"); break; 128 | case 'f': strcpy(temp->type,"FLOAT");break; 129 | case 'v': strcpy(temp->type,"VOID");break; 130 | case 'c': strcpy(temp->type,"CHAR");break; 131 | } 132 | temp->funcCount=functionCount; 133 | if(scopeCount==0){ 134 | strcpy(temp->scope,"GLOBAL"); 135 | temp->scopeValue=scopeCount; 136 | 137 | } 138 | else{ 139 | strcpy(temp->scope,"NESTING"); 140 | temp->scopeValue=scopeCount; 141 | } 142 | strcpy(temp->line,line); 143 | temp->next=NULL; 144 | tokenList *p=symbolPtr; 145 | if(p==NULL){ 146 | symbolPtr=temp; 147 | } 148 | else{ 149 | while(p->next!=NULL){ 150 | p=p->next; 151 | } 152 | p->next=temp; 153 | } 154 | 155 | 156 | } 157 | void addConstant(char *tokenName,int tokenLine){ 158 | char line[39],lineBuffer[19]; 159 | snprintf(lineBuffer, 19, "%d", tokenLine); 160 | strcpy(line," "); 161 | strcat(line,lineBuffer); 162 | for(tokenList *p=constantPtr;p!=NULL;p=p->next) 163 | if(strcmp(p->token,tokenName)==0){ 164 | strcat(p->line,line); 165 | return; 166 | } 167 | tokenList *temp=(tokenList *)malloc(sizeof(tokenList)); 168 | temp->token=(char *)malloc(strlen(tokenName)+1); 169 | strcpy(temp->token,tokenName); 170 | strcpy(temp->line,line); 171 | temp->next=NULL; 172 | 173 | tokenList *p=constantPtr; 174 | if(p==NULL){ 175 | constantPtr=temp; 176 | } 177 | else{ 178 | while(p->next!=NULL){ 179 | p=p->next; 180 | } 181 | p->next=temp; 182 | } 183 | 184 | 185 | } 186 | void makeList(char *tokenName,char tokenType, int tokenLine) 187 | { 188 | char line[39],lineBuffer[19]; 189 | 190 | snprintf(lineBuffer, 19, "%d", tokenLine); 191 | strcpy(line," "); 192 | strcat(line,lineBuffer); 193 | char type[20]; 194 | switch(tokenType) 195 | { 196 | case 'c': 197 | strcpy(type,"Constant"); 198 | break; 199 | case 'v': 200 | strcpy(type,"Identifier"); 201 | break; 202 | case 'p': 203 | strcpy(type,"Punctuator"); 204 | break; 205 | case 'o': 206 | strcpy(type,"Operator"); 207 | break; 208 | case 'k': 209 | strcpy(type,"Keyword"); 210 | break; 211 | case 's': 212 | strcpy(type,"String Literal"); 213 | break; 214 | case 'd': 215 | strcpy(type,"Preprocessor Statement"); 216 | break; 217 | } 218 | 219 | 220 | for(tokenList *p=parsedPtr;p!=NULL;p=p->next) 221 | if(strcmp(p->token,tokenName)==0){ 222 | strcat(p->line,line); 223 | return; 224 | } 225 | tokenList *temp=(tokenList *)malloc(sizeof(tokenList)); 226 | temp->token=(char *)malloc(strlen(tokenName)+1); 227 | strcpy(temp->token,tokenName); 228 | strcpy(temp->type,type); 229 | strcpy(temp->line,line); 230 | temp->next=NULL; 231 | 232 | tokenList *p=parsedPtr; 233 | if(p==NULL){ 234 | parsedPtr=temp; 235 | } 236 | else{ 237 | while(p->next!=NULL){ 238 | p=p->next; 239 | } 240 | p->next=temp; 241 | } 242 | 243 | /*if(tokenType == 'c') 244 | { 245 | 246 | for(tokenList *p=constantPtr;p!=NULL;p=p->next) 247 | if(strcmp(p->token,tokenName)==0){ 248 | strcat(p->line,line); 249 | return; 250 | } 251 | tokenList *temp=(tokenList *)malloc(sizeof(tokenList)); 252 | temp->token=(char *)malloc(strlen(tokenName)+1); 253 | strcpy(temp->token,tokenName); 254 | strcpy(temp->type,type); 255 | strcpy(temp->line,line); 256 | temp->next=NULL; 257 | 258 | tokenList *p=constantPtr; 259 | if(p==NULL){ 260 | constantPtr=temp; 261 | } 262 | else{ 263 | while(p->next!=NULL){ 264 | p=p->next; 265 | } 266 | p->next=temp; 267 | } 268 | 269 | 270 | } 271 | if(tokenType=='v') 272 | { 273 | for(tokenList *p=symbolPtr;p!=NULL;p=p->next) 274 | if(strcmp(p->token,tokenName)==0){ 275 | strcat(p->line,line); 276 | return; 277 | } 278 | tokenList *temp=(tokenList *)malloc(sizeof(tokenList)); 279 | temp->token=(char *)malloc(strlen(tokenName)+1); 280 | strcpy(temp->token,tokenName); 281 | switch(typeBuffer){ 282 | case 'i': strcpy(temp->type,"INT"); break; 283 | case 'f': strcpy(temp->type,"FLOAT");break; 284 | case 'v' :strcpy(temp->type,"VOID");break; 285 | case 'c': strcpy(temp->type,"CHAR");break; 286 | 287 | } 288 | 289 | strcpy(temp->line,line); 290 | temp->next=NULL; 291 | tokenList *p=symbolPtr; 292 | if(p==NULL){ 293 | 294 | symbolPtr=temp; 295 | } 296 | else{ 297 | while(p->next!=NULL){ 298 | p=p->next; 299 | } 300 | p->next=temp; 301 | } 302 | }*/ 303 | } 304 | -------------------------------------------------------------------------------- /Semantic Analyzer/testcases/semantic.h: -------------------------------------------------------------------------------- 1 | struct tokenList 2 | { 3 | char *token,type[20],line[100]; 4 | char *scope[20]; 5 | int scopeValue; 6 | int funcCount; 7 | struct tokenList *next; 8 | }; 9 | typedef struct tokenList tokenList; 10 | struct funcNode{ 11 | char funcName[30]; 12 | int line; 13 | char funcReturn[20]; 14 | struct funcNode *next; 15 | }; 16 | typedef struct funcNode funcNode; 17 | 18 | 19 | tokenList *symbolPtr = NULL; 20 | tokenList *constantPtr = NULL; 21 | tokenList *parsedPtr =NULL; 22 | extern int functionCount; 23 | extern int scopeCount; 24 | char typeBuffer=' '; 25 | char *sourceCode; 26 | int tempCheckType=3; 27 | 28 | int semanticErr=0,lineSemanticCount; 29 | int checkScope(char *tempToken,int lineCount) 30 | { tokenList *temp=NULL; 31 | char type[20]; 32 | int flag=0,tempFlag=0; 33 | for(tokenList *p=symbolPtr;p!=NULL;p=p->next){ 34 | if(strcmp(tempToken,"printf")==0 || strcmp(tempToken,"scanf")==0){ 35 | tempFlag=1; 36 | } 37 | else{ 38 | if(strcmp(tempToken,p->token)==0){ 39 | strcpy(type,p->type); 40 | flag=1; 41 | break; 42 | } 43 | } 44 | 45 | } 46 | if (flag == 0 && tempFlag ==0 ) 47 | { 48 | printf("\n%s : %d :Undeclared variable \n",sourceCode,lineCount-1); 49 | semanticErr=1; 50 | } 51 | else 52 | { 53 | addSymbol(tempToken,lineCount); 54 | if(strcmp(type,"VOID")==0) 55 | return(1); 56 | if(strcmp(type,"CHAR")==0) 57 | return(2); 58 | if(strcmp(type,"INT")==0) 59 | return(3); 60 | if(strcmp(type,"FLOAT")==0) 61 | return(4); 62 | } 63 | 64 | } 65 | 66 | void checkType(int value1,int value2,int lineCount) 67 | { lineSemanticCount=lineCount; 68 | if(value2 == 0) 69 | value2 = tempCheckType; 70 | if(value1!=value2) 71 | { 72 | printf("\n%s : %d :Type Mismatch error \n",sourceCode,lineSemanticCount-1); 73 | semanticErr=1; 74 | } 75 | tempCheckType=3; 76 | } 77 | void checkDeclaration(char *tokenName,int tokenLine,int scopeVal){ 78 | char type[20]; 79 | char line[39],lineBuffer[19]; 80 | snprintf(lineBuffer, 19, "%d", tokenLine); 81 | strcpy(line," "); 82 | strcat(line,lineBuffer); 83 | switch(typeBuffer){ 84 | case 'i': strcpy(type,"INT"); break; 85 | case 'f': strcpy(type,"FLOAT");break; 86 | case 'v': strcpy(type,"VOID");break; 87 | case 'c': strcpy(type,"CHAR");break; 88 | 89 | } 90 | for(tokenList *p=symbolPtr;p!=NULL;p=p->next){ 91 | if(strcmp(p->token,tokenName)==0 && p->scopeValue == scopeCount && p->funcCount == functionCount){ 92 | semanticErr=1; 93 | if(strcmp(p->type,type)==0){ 94 | printf("\n%s : %d :Multiple Declaration \n",sourceCode,tokenLine); 95 | return; 96 | } 97 | else{ 98 | printf("\n%s : %d :Multiple Declration with Different Type \n",sourceCode,tokenLine); 99 | return; 100 | } 101 | } 102 | } 103 | addSymbol(tokenName,tokenLine,scopeCount); 104 | 105 | } 106 | void checkArray(int val,int lineCount){ 107 | if(val<0){ 108 | semanticErr=1; 109 | printf("\n%s : %d :Array Index error\n",sourceCode,lineCount-1); 110 | } 111 | } 112 | void addSymbol(char *tokenName,int tokenLine,int scopeVal){ 113 | char line[39],lineBuffer[19]; 114 | snprintf(lineBuffer, 19, "%d", tokenLine); 115 | strcpy(line," "); 116 | strcat(line,lineBuffer); 117 | char type[20]; 118 | for(tokenList *p=symbolPtr;p!=NULL;p=p->next) 119 | if(strcmp(p->token,tokenName)==0 && p->scopeValue == scopeCount && p->funcCount ==functionCount ){ 120 | strcat(p->line,line); 121 | return; 122 | } 123 | tokenList *temp=(tokenList *)malloc(sizeof(tokenList)); 124 | temp->token=(char *)malloc(strlen(tokenName)+1); 125 | strcpy(temp->token,tokenName); 126 | switch(typeBuffer){ 127 | case 'i': strcpy(temp->type,"INT"); break; 128 | case 'f': strcpy(temp->type,"FLOAT");break; 129 | case 'v': strcpy(temp->type,"VOID");break; 130 | case 'c': strcpy(temp->type,"CHAR");break; 131 | } 132 | temp->funcCount=functionCount; 133 | if(scopeCount==0){ 134 | strcpy(temp->scope,"GLOBAL"); 135 | temp->scopeValue=scopeCount; 136 | 137 | } 138 | else{ 139 | strcpy(temp->scope,"NESTING"); 140 | temp->scopeValue=scopeCount; 141 | } 142 | strcpy(temp->line,line); 143 | temp->next=NULL; 144 | tokenList *p=symbolPtr; 145 | if(p==NULL){ 146 | symbolPtr=temp; 147 | } 148 | else{ 149 | while(p->next!=NULL){ 150 | p=p->next; 151 | } 152 | p->next=temp; 153 | } 154 | 155 | 156 | } 157 | void addConstant(char *tokenName,int tokenLine){ 158 | char line[39],lineBuffer[19]; 159 | snprintf(lineBuffer, 19, "%d", tokenLine); 160 | strcpy(line," "); 161 | strcat(line,lineBuffer); 162 | for(tokenList *p=constantPtr;p!=NULL;p=p->next) 163 | if(strcmp(p->token,tokenName)==0){ 164 | strcat(p->line,line); 165 | return; 166 | } 167 | tokenList *temp=(tokenList *)malloc(sizeof(tokenList)); 168 | temp->token=(char *)malloc(strlen(tokenName)+1); 169 | strcpy(temp->token,tokenName); 170 | strcpy(temp->line,line); 171 | temp->next=NULL; 172 | 173 | tokenList *p=constantPtr; 174 | if(p==NULL){ 175 | constantPtr=temp; 176 | } 177 | else{ 178 | while(p->next!=NULL){ 179 | p=p->next; 180 | } 181 | p->next=temp; 182 | } 183 | 184 | 185 | } 186 | void makeList(char *tokenName,char tokenType, int tokenLine) 187 | { 188 | char line[39],lineBuffer[19]; 189 | 190 | snprintf(lineBuffer, 19, "%d", tokenLine); 191 | strcpy(line," "); 192 | strcat(line,lineBuffer); 193 | char type[20]; 194 | switch(tokenType) 195 | { 196 | case 'c': 197 | strcpy(type,"Constant"); 198 | break; 199 | case 'v': 200 | strcpy(type,"Identifier"); 201 | break; 202 | case 'p': 203 | strcpy(type,"Punctuator"); 204 | break; 205 | case 'o': 206 | strcpy(type,"Operator"); 207 | break; 208 | case 'k': 209 | strcpy(type,"Keyword"); 210 | break; 211 | case 's': 212 | strcpy(type,"String Literal"); 213 | break; 214 | case 'd': 215 | strcpy(type,"Preprocessor Statement"); 216 | break; 217 | } 218 | 219 | 220 | for(tokenList *p=parsedPtr;p!=NULL;p=p->next) 221 | if(strcmp(p->token,tokenName)==0){ 222 | strcat(p->line,line); 223 | return; 224 | } 225 | tokenList *temp=(tokenList *)malloc(sizeof(tokenList)); 226 | temp->token=(char *)malloc(strlen(tokenName)+1); 227 | strcpy(temp->token,tokenName); 228 | strcpy(temp->type,type); 229 | strcpy(temp->line,line); 230 | temp->next=NULL; 231 | 232 | tokenList *p=parsedPtr; 233 | if(p==NULL){ 234 | parsedPtr=temp; 235 | } 236 | else{ 237 | while(p->next!=NULL){ 238 | p=p->next; 239 | } 240 | p->next=temp; 241 | } 242 | 243 | /*if(tokenType == 'c') 244 | { 245 | 246 | for(tokenList *p=constantPtr;p!=NULL;p=p->next) 247 | if(strcmp(p->token,tokenName)==0){ 248 | strcat(p->line,line); 249 | return; 250 | } 251 | tokenList *temp=(tokenList *)malloc(sizeof(tokenList)); 252 | temp->token=(char *)malloc(strlen(tokenName)+1); 253 | strcpy(temp->token,tokenName); 254 | strcpy(temp->type,type); 255 | strcpy(temp->line,line); 256 | temp->next=NULL; 257 | 258 | tokenList *p=constantPtr; 259 | if(p==NULL){ 260 | constantPtr=temp; 261 | } 262 | else{ 263 | while(p->next!=NULL){ 264 | p=p->next; 265 | } 266 | p->next=temp; 267 | } 268 | 269 | 270 | } 271 | if(tokenType=='v') 272 | { 273 | for(tokenList *p=symbolPtr;p!=NULL;p=p->next) 274 | if(strcmp(p->token,tokenName)==0){ 275 | strcat(p->line,line); 276 | return; 277 | } 278 | tokenList *temp=(tokenList *)malloc(sizeof(tokenList)); 279 | temp->token=(char *)malloc(strlen(tokenName)+1); 280 | strcpy(temp->token,tokenName); 281 | switch(typeBuffer){ 282 | case 'i': strcpy(temp->type,"INT"); break; 283 | case 'f': strcpy(temp->type,"FLOAT");break; 284 | case 'v' :strcpy(temp->type,"VOID");break; 285 | case 'c': strcpy(temp->type,"CHAR");break; 286 | 287 | } 288 | 289 | strcpy(temp->line,line); 290 | temp->next=NULL; 291 | tokenList *p=symbolPtr; 292 | if(p==NULL){ 293 | 294 | symbolPtr=temp; 295 | } 296 | else{ 297 | while(p->next!=NULL){ 298 | p=p->next; 299 | } 300 | p->next=temp; 301 | } 302 | }*/ 303 | } 304 | -------------------------------------------------------------------------------- /Lexical Analyzer/src/scanner.l: -------------------------------------------------------------------------------- 1 | %{ 2 | #include 3 | #include 4 | #include 5 | #include 6 | int var=0,i,nc=0,cLine=0,flag=0; 7 | int lineNumber=1; 8 | int cBrac=0; 9 | 10 | FILE *symbol,*constants; 11 | char *comment,*inputFile, s_comment[1000]; 12 | 13 | void insertToTable(char *yytext,char type); 14 | void displayComment(char *yytext); 15 | void storeSingleLineComment(char *yytext); 16 | 17 | struct Node { 18 | char *tname; 19 | int av; 20 | struct Node *next; 21 | }*head=NULL; 22 | 23 | %} 24 | 25 | digit [0-9] 26 | letter [a-zA-Z] 27 | keyword "auto"|"break"|"case"|"char"|"const"|"continue"|"default"|"do"|"double"|"else"|"enum"|"extern"|"float"|"for"|"goto"|"if"|"int"|"long"|"register"|"return"|"short"|"signed"|"sizeof"|"static"|"struct"|"switch"|"typedef"|"union"|"unsigned"|"void"|"volatile"|"while" 28 | datatype "int"|"char"|"void" 29 | sign "signed"|"unsigned" 30 | modifiers "long"|"short" 31 | relational >|<|<=|>=|!=|== 32 | logical \&|\^|\~ 33 | arithmatic \+|\-|\*|\/|\% 34 | puncuator \(|\)|\[|\]|\;|\,|\:|\. 35 | assignment = 36 | quote \'|\"|\\ 37 | whitespace [ \t]+ 38 | newline "\n" 39 | singlelinecomment (\/\/.*) 40 | multilinecommentstart (\/\*) 41 | multilinecommentend (\*\/) 42 | identifier ({letter}({letter}|{digit})*)|"_"({letter}|{digit})+ 43 | 44 | %x DETECT_COMMENT 45 | 46 | %% 47 | 48 | ^#([-a-zA-Z0-9.]|{relational}|{whitespace})* insertToTable(yytext,'d'); //preprocessor directive rule 49 | 50 | {keyword} insertToTable(yytext,'k'); 51 | {sign}?{whitespace}{modifiers}?{whitespace}{datatype} insertToTable(yytext,'k'); //keyword rule 52 | 53 | ^{datatype}{whitespace}*{identifier}\(.*\) { int i,j=0;char temp1[50]={'\0'}, temp2[50]={'\0'}; 54 | for(i=0;yytext[i]!=' ';i++) 55 | { 56 | temp1[i] = yytext[i]; 57 | } 58 | 59 | insertToTable(temp1,'k'); 60 | for(;yytext[i]!='(';i++){ 61 | temp2[j]=yytext[i]; 62 | j++; 63 | } 64 | 65 | insertToTable(temp2,'j'); //procedure rule 66 | } 67 | 68 | {identifier}\[{digit}*\] { int i,j=0;char temp[50]={'\0'}; 69 | 70 | for(i=0;yytext[i]!='[';i++) 71 | { 72 | temp[j] = yytext[i]; 73 | j++; 74 | } 75 | 76 | insertToTable(temp,'a'); // array rule 77 | } 78 | 79 | \*{identifier} { int i,j=0;char temp[50]={'\0'}; 80 | for(i=1;yytext[i]!='\0';i++) 81 | { 82 | temp[j++] = yytext[i]; 83 | } 84 | insertToTable(temp,'q'); // pointer rule 85 | } 86 | 87 | {identifier} insertToTable(yytext,'i'); // variable rule 88 | {digit}+({letter}|{digit})+|"_" { printf("%s : %d : Invalid Identifier\n",inputFile,lineNumber); } // invalid identifier 89 | 90 | 91 | {relational} insertToTable(yytext,'r'); //operator rules 92 | {logical} insertToTable(yytext,'l'); 93 | {arithmatic} insertToTable(yytext,'o'); 94 | {assignment} insertToTable(yytext,'e'); 95 | {puncuator} insertToTable(yytext,'p'); 96 | 97 | {digit}+ insertToTable(yytext,'c'); //integer constants rule 98 | \"(.)*\" insertToTable(yytext,'s'); //string constants rule 99 | L?\"(\\.|[^\\"])* { 100 | if(nc<=0) //invalid String 101 | printf("%s : %d : String does not End\n",inputFile,lineNumber); 102 | } 103 | [-+]?{digit}*\.?{digit}+([eE][-+]?{digit}+)? insertToTable(yytext,'f'); // float constant rule 104 | \'({letter}|{digit})\' { char temp[50]={'\0'}; 105 | temp[0] = yytext[1]; 106 | insertToTable(temp,'z'); // character constant rule 107 | } 108 | 109 | {quote} ; 110 | {whitespace} ; 111 | {newline} lineNumber++; 112 | 113 | "{" { cBrac++; 114 | insertToTable(yytext,'p'); 115 | } 116 | 117 | "}" { cBrac--; 118 | insertToTable(yytext,'p'); 119 | } 120 | 121 | {singlelinecomment} {storeSingleLineComment(yytext);} 122 | 123 | {multilinecommentstart} { 124 | BEGIN(DETECT_COMMENT); 125 | nc++; 126 | cLine++; 127 | displayComment("\n\t"); 128 | } 129 | 130 | {multilinecommentstart} { 131 | nc++; 132 | if(nc>1) 133 | { 134 | printf("%s : %d : Nested Comment\n",inputFile,lineNumber); 135 | flag = 1; 136 | } 137 | } 138 | 139 | {multilinecommentend} { 140 | if(nc>0) 141 | nc--; 142 | else 143 | printf("%s : %d : */ found before /*\n",inputFile,lineNumber); 144 | 145 | if(nc==0) 146 | BEGIN(INITIAL); 147 | } 148 | 149 | \n { 150 | cLine++; 151 | lineNumber++; 152 | displayComment("\n"); 153 | } 154 | 155 | . {displayComment(yytext);} 156 | 157 | 158 | %% 159 | 160 | int main(int argc,char **argv) 161 | { 162 | comment = (char*)malloc(100*sizeof(char)); 163 | yyin=fopen(argv[1],"r"); 164 | inputFile=argv[1]; 165 | 166 | symbol=fopen("symbolTable.txt","w"); //File to write symbol table 167 | fprintf(symbol,"\n Symbol Table:\n \t\tLexeme\t\t\t\tType\t\t\tAttribute Value\t\t\tLine Number\n"); 168 | 169 | constants=fopen("constantTable.txt","w"); // File to write constant table 170 | fprintf(constants,"\n Constants Table:\n \t\tLexeme\t\t\tType\t\t\tAttribute Value\t\t\tLine Number\n"); 171 | 172 | yyout=fopen("parsedTable.txt","w"); // File to write all token in source program 173 | fprintf(yyout,"\n Table:\n \t\tLexeme\t\t\tToken\t\t\t\tAttribute Value\t\t\tLine Number\n"); 174 | 175 | yylex(); 176 | 177 | if(nc!=0) 178 | printf("%s : %d : Comment Does Not End\n",inputFile,lineNumber); 179 | 180 | if(cBrac!=0) 181 | printf("%s : %d : Unbalanced Parenthesis\n",inputFile,lineNumber); 182 | 183 | fprintf(yyout,"\n"); 184 | if(flag==1) 185 | { 186 | cLine = 0; 187 | fprintf(yyout,"\n\nComment (%d lines):\n",cLine); 188 | printf("%s : %d : Nested Comment\n",inputFile,lineNumber); 189 | } 190 | else 191 | { 192 | int i; 193 | fprintf(yyout,"\n\nMultiLineComment (%d lines):",cLine); 194 | fputs(comment,yyout); 195 | fprintf(yyout,"\n\nSingleLineComment :\n"); 196 | fputs(s_comment,yyout); 197 | } 198 | 199 | fclose(yyout); 200 | fclose(symbol); 201 | fclose(constants); 202 | } 203 | 204 | void storeSingleLineComment(char *yytext) 205 | { 206 | int len = strlen(yytext); 207 | int i, j=0; 208 | char *temp; 209 | temp = (char*)malloc((len+1)*sizeof(char)); 210 | for(i=2;yytext[i]!='\0';i++) 211 | { 212 | temp[j++] = yytext[i]; 213 | } 214 | strcat(temp,"\n"); 215 | strcat(s_comment,temp); 216 | } 217 | void displayComment(char *yytext) 218 | { 219 | int l1, l2; 220 | char *temp; 221 | 222 | l1 = strlen(comment); 223 | l2 = strlen(yytext); 224 | temp = (char*)malloc((l1+1)*sizeof(char)); 225 | strcpy(temp,comment); 226 | comment = (char*)malloc((l1+l2+1)*sizeof(char)); 227 | strcat(temp,yytext); 228 | strcpy(comment,temp); 229 | } 230 | void insertToTable(char *yytext,char type) 231 | { 232 | int l1 = strlen(yytext), i; 233 | 234 | 235 | char token[30]; 236 | struct Node *current = NULL, *temp = NULL; 237 | 238 | switch(type) 239 | { 240 | case 'd': strcpy(token,"Preprocessor Statement");break; 241 | 242 | case 'k': strcpy(token,"Keyword");break; 243 | 244 | case 'j': strcpy(token,"Procedure");break; 245 | 246 | case 'a': strcpy(token,"Array");break; 247 | 248 | case 'q' : strcpy(token,"Pointer");break; 249 | 250 | case 'i': strcpy(token,"Identifier");break; 251 | 252 | case 'r': strcpy(token,"Relational Op");break; 253 | 254 | case 'p': strcpy(token,"Punctuator");break; 255 | 256 | case 'o': strcpy(token,"Arithmetic Op");break; 257 | 258 | case 'c': strcpy(token,"Integer Constant");break; 259 | 260 | case 'f': strcpy(token,"Float Constant");break; 261 | 262 | case 'z': strcpy(token,"Character Constant");break; 263 | 264 | case 'e': strcpy(token,"Assignment Op");break; 265 | 266 | case 'l': strcpy(token,"Logical Op");break; 267 | 268 | case 's': strcpy(token,"String Literal");break; 269 | } 270 | 271 | if(nc<=0) 272 | { 273 | current = head; 274 | for(i=0;itname,yytext)==0) 277 | { 278 | break; 279 | } 280 | current = current->next; 281 | } 282 | 283 | if(i==var) 284 | { 285 | temp = (struct Node *)malloc(sizeof(struct Node)); 286 | temp->av = i; 287 | temp->tname = (char *)malloc(sizeof(char)*(l1+1)); 288 | strcpy(temp->tname,yytext); 289 | temp->next = NULL; 290 | 291 | if(head==NULL) 292 | { 293 | head = temp; 294 | } 295 | else 296 | { 297 | current = head; 298 | while(current->next!=NULL) 299 | { 300 | current = current->next; 301 | } 302 | current->next = temp; 303 | } 304 | 305 | var++; 306 | } 307 | } 308 | 309 | if(type =='i' || type == 'a' || type == 'q' || type=='j') 310 | { 311 | fprintf(symbol,"\n%20s%30s%30d%35d",yytext,token,i,lineNumber); 312 | } 313 | switch(type) 314 | { 315 | case 'c' : fprintf(constants,"\n%20s%20s%30d%35d",yytext,"int",i,lineNumber); 316 | break; 317 | 318 | case 'f' : fprintf(constants,"\n%20s%20s%30d%35d",yytext,"float",i,lineNumber); 319 | break; 320 | 321 | case 'z' : fprintf(constants,"\n%20s%20s%30d%35d",yytext,"char",i,lineNumber); 322 | break; 323 | } 324 | 325 | fprintf(yyout,"\n%20s%30s%30d%35d",yytext,token,i,lineNumber); 326 | } 327 | 328 | int yywrap() 329 | { 330 | return(1); 331 | } 332 | -------------------------------------------------------------------------------- /Lexical Analyzer/src/parsedTable.txt: -------------------------------------------------------------------------------- 1 | 2 | Table: 3 | Lexeme Token Attribute Value Line Number 4 | 5 | #include Preprocessor Statement 0 1 6 | int Keyword 1 2 7 | main Procedure 2 2 8 | { Punctuator 3 3 9 | struct Keyword 4 9 10 | node Identifier 5 9 11 | { Punctuator 3 9 12 | int Keyword 1 10 13 | a Identifier 6 10 14 | ; Punctuator 7 10 15 | char Keyword 8 11 16 | name Identifier 9 11 17 | ; Punctuator 7 11 18 | } Punctuator 10 12 19 | ; Punctuator 7 12 20 | float Keyword 11 14 21 | fl Identifier 12 14 22 | = Assignment Op 13 14 23 | 5.01 Float Constant 14 14 24 | ; Punctuator 7 14 25 | char Keyword 8 15 26 | l1 Identifier 15 15 27 | = Assignment Op 13 15 28 | a Character Constant 6 15 29 | , Punctuator 16 15 30 | l2 Identifier 17 15 31 | = Assignment Op 13 15 32 | 1 Character Constant 18 15 33 | ; Punctuator 7 15 34 | int Keyword 1 16 35 | _a Identifier 19 16 36 | = Assignment Op 13 16 37 | 0 Integer Constant 20 16 38 | , Punctuator 16 16 39 | b Identifier 21 16 40 | = Assignment Op 13 16 41 | 0 Integer Constant 20 16 42 | , Punctuator 16 16 43 | c Identifier 22 16 44 | = Assignment Op 13 16 45 | 5 Integer Constant 23 16 46 | ; Punctuator 7 16 47 | int Keyword 1 17 48 | arr Array 24 17 49 | ; Punctuator 7 17 50 | char Keyword 8 18 51 | ptr Pointer 25 18 52 | ; Punctuator 7 18 53 | scanf Identifier 26 19 54 | ( Punctuator 27 19 55 | "%d %d" String Literal 28 19 56 | , Punctuator 16 19 57 | & Logical Op 29 19 58 | a Identifier 6 19 59 | , Punctuator 16 19 60 | & Logical Op 29 19 61 | b Identifier 21 19 62 | ) Punctuator 30 19 63 | ; Punctuator 7 19 64 | int Keyword 1 20 65 | sum Identifier 31 20 66 | = Assignment Op 13 20 67 | 0 Integer Constant 20 20 68 | ; Punctuator 7 20 69 | sum Identifier 31 21 70 | = Assignment Op 13 21 71 | a Identifier 6 21 72 | + Arithmetic Op 32 21 73 | b Identifier 21 21 74 | ; Punctuator 7 21 75 | printf Identifier 33 22 76 | ( Punctuator 27 22 77 | "\n Sum : %d \n" String Literal 34 22 78 | , Punctuator 16 22 79 | sum Identifier 31 22 80 | ) Punctuator 30 22 81 | ; Punctuator 7 22 82 | char Keyword 8 25 83 | name Pointer 9 25 84 | = Assignment Op 13 25 85 | "Arvind" String Literal 35 25 86 | ; Punctuator 7 25 87 | return Keyword 36 28 88 | 1 Integer Constant 18 28 89 | ; Punctuator 7 28 90 | } Punctuator 10 29 91 | void Keyword 37 30 92 | abc Procedure 38 30 93 | { Punctuator 3 31 94 | printf Identifier 33 32 95 | ( Punctuator 27 32 96 | "\nTest function " String Literal 39 32 97 | ) Punctuator 30 32 98 | ; Punctuator 7 32 99 | } Punctuator 10 33 100 | 101 | 102 | MultiLineComment (5 lines): 103 | My name is Arvind. 104 | This is 105 | a sample 106 | multi-line 107 | comment 108 | 109 | SingleLineComment : 110 | Enter Name 111 | End the main function 112 | -------------------------------------------------------------------------------- /Intermediate Code Generator/src/icg.c: -------------------------------------------------------------------------------- 1 | #include "icg.h" 2 | #include "y.tab.h" 3 | 4 | tokenList *symPtr = NULL; 5 | tokenList *bufferPtr = NULL; 6 | 7 | threeAddressCode *codePtr = NULL; 8 | 9 | extern int yylineno; 10 | extern int yyin; 11 | 12 | int globalOffset = 0, tempIntCounter = 0,tempFloatCounter= 0,currentLine = -1; 13 | 14 | void yyerror() { 15 | printf("ERROR: %d : Syntactical Error\n",yylineno); 16 | exit(1); 17 | } 18 | 19 | int main(int argc, char **argv) { 20 | yyin = fopen(argv[1],"r"); 21 | yyparse(); 22 | /*tokenList *p1=symPtr,*p2=symPtr; 23 | if(p1!=NULL && p2!=NULL){ 24 | while(p1!=NULL){ 25 | while(p2!=NULL){ 26 | if(strcmp(p1->name,p2->name)==0 && p1->type==p2->type && strcmp(p1->scope,p2->scope)==0){ 27 | printf("ERROR: %d : Multiple Declarations of %s in %s scope\n",p1->name,p1->scope); 28 | exit(1); 29 | } 30 | p2=p2->next; 31 | } 32 | p1=p1->next; 33 | } 34 | }*/ 35 | printf("\n\n\t Parsing Completed\n"); 36 | printf("Three Address Code and Symbol table are generated\n\n"); 37 | FILE *icgPtr= fopen("threeAddressCode.txt","w"); 38 | writeCode(icgPtr); 39 | FILE *symPtr = fopen("symbolTable.txt", "w"); 40 | writeSymbolTable(symPtr); 41 | fclose(icgPtr); 42 | fclose(symPtr); 43 | printf("\n"); 44 | return(0); 45 | } 46 | 47 | static const char* returnTypeToString(tokenReturnType type) 48 | { 49 | switch(type) 50 | { 51 | case Return_VOID: 52 | return "None"; 53 | 54 | case Return_INT: 55 | return "Int"; 56 | 57 | case Return_FLOAT: 58 | return "Real"; 59 | } 60 | } 61 | 62 | 63 | static const char* dataTypeToString(tokenType type) 64 | { 65 | switch(type) 66 | { 67 | 68 | case INT_type: 69 | return "Int"; 70 | 71 | case FLOAT_type: 72 | return "Real"; 73 | 74 | case BOOL_type: 75 | return "Bool"; 76 | 77 | case FUNC: 78 | return "Func"; 79 | case PROTO: 80 | return "Proto"; 81 | 82 | case MAIN: 83 | return "Main"; 84 | } 85 | } 86 | 87 | threeAddressCode* appendCode(char *code){ 88 | 89 | threeAddressCode *temp = malloc(sizeof(threeAddressCode )); 90 | 91 | temp->code=(char *)malloc(sizeof(char)*strlen(code)); 92 | strcpy(temp->code,code); 93 | temp->gotoLine = -1; 94 | temp->next = NULL; 95 | 96 | 97 | if(codePtr == NULL){ 98 | codePtr=temp; 99 | } 100 | else{ 101 | threeAddressCode *p=codePtr; 102 | while(p->next!=NULL){ 103 | p=p->next; 104 | } 105 | p->next=temp; 106 | } 107 | currentLine++; 108 | return temp; 109 | } 110 | 111 | void backpatch(backPatchList* list, int gotoL){ 112 | if(list == NULL){ 113 | return; 114 | } else{ 115 | backPatchList* temp; 116 | while(list){ 117 | if(list->entry != NULL){ 118 | list->entry->gotoLine = gotoL; 119 | } 120 | //printf("backpatching: %s",list->entry->code); 121 | temp = list; 122 | list = list->next; 123 | free(temp); 124 | } 125 | } 126 | } 127 | 128 | backPatchList* mergelists(backPatchList* a, backPatchList* b){ 129 | if(a != NULL && b == NULL){ 130 | return a; 131 | } 132 | else if(a == NULL && b != NULL){ 133 | return b; 134 | } 135 | else if(a == NULL && b == NULL){ 136 | return NULL; 137 | } 138 | else{ 139 | backPatchList* temp = a; 140 | while(a->next){ 141 | a = a->next; 142 | } 143 | a->next = b; 144 | return temp; 145 | } 146 | } 147 | 148 | backPatchList* appendToBackPatch(backPatchList* p, threeAddressCode* newCode){ 149 | 150 | if(newCode == NULL){ 151 | return p; 152 | } 153 | else if(p == NULL){ 154 | backPatchList* temp = malloc(sizeof(backPatchList)); 155 | temp->entry = newCode; 156 | temp->next = NULL; 157 | return temp; 158 | } 159 | else{ 160 | backPatchList* temp = malloc(sizeof(backPatchList)); 161 | temp->entry = newCode; 162 | temp->next=NULL; 163 | while(p->next!=NULL){ 164 | p=p->next; 165 | } 166 | p->next = temp; 167 | return temp; 168 | } 169 | } 170 | 171 | tokenList* appendToSymbolTable(char *name,tokenType type,tokenReturnType returnType,long size,long line,char *scope,long parameter) 172 | { 173 | 174 | tokenList *temp = malloc(sizeof(tokenList )); 175 | 176 | temp->name = strdup(name); 177 | temp->type = type; 178 | temp->returnType = returnType; 179 | temp->size = size; 180 | temp->line = line; 181 | temp->scope = (scope == PARENT_NONE ? NULL : strdup(scope)); 182 | temp->parameter = parameter; 183 | temp->next = NULL; 184 | 185 | if(symPtr==NULL){ 186 | symPtr=temp; 187 | } 188 | else{ 189 | tokenList *p = symPtr; 190 | while(p->next!=NULL) 191 | p=p->next; 192 | p->next=temp; 193 | } 194 | return temp; 195 | } 196 | 197 | 198 | void writeCode(FILE *icgOut) 199 | { 200 | long icgLineCount = 0; 201 | fprintf(icgOut, "\n\t\t\t Intermediate Code Generated"); 202 | fprintf(icgOut, "\n\t\t\t-------------------------------\n"); 203 | 204 | if(codePtr==NULL){ 205 | printf("\nNo code Generated\n"); 206 | } 207 | else{ 208 | threeAddressCode *p = codePtr; 209 | while(p) 210 | { 211 | if(p->gotoLine == -1){ 212 | fprintf(icgOut,"%-4lu %s\n",icgLineCount,p->code); 213 | } 214 | else{ 215 | fprintf(icgOut,"%-4lu %s %d\n",icgLineCount,p->code,p->gotoLine); 216 | } 217 | p = p->next; 218 | icgLineCount++; 219 | } 220 | } 221 | } 222 | 223 | 224 | 225 | void writeSymbolTable(FILE *symOut) 226 | { 227 | fprintf(symOut, "\n\t\t\t Symbol Table"); 228 | fprintf(symOut, "\n\t\t\t------------------\n\n"); 229 | 230 | if(symPtr == NULL) 231 | { 232 | printf("\nSymbol table is empty"); 233 | } 234 | else{ 235 | fprintf(symOut," Name DataType ReturnType Offset Scope"); 236 | fprintf(symOut,"\n------------------------------------------------------------------------\n"); 237 | 238 | tokenList *p= symPtr; 239 | while(p!=NULL) 240 | { 241 | fprintf(symOut,"%-20s %-14s %-14s %-12lu %-12s\n", 242 | 243 | p->name, 244 | dataTypeToString(p->type), 245 | returnTypeToString(p->returnType), 246 | p->size, 247 | p->scope == NULL ? "None" : p->scope 248 | ); 249 | p=p->next; 250 | } 251 | } 252 | } 253 | void addSymbolToQueue(char *name, tokenType type, unsigned long param_no) { 254 | tokenList *temp = malloc(sizeof(tokenList )); 255 | temp->name = strdup(name); 256 | temp->type = type; 257 | switch(type){ 258 | case INT_type: temp->size = 4;break; 259 | case FLOAT_type: temp->size = 8;break; 260 | case BOOL_type: temp->size = 1;break; 261 | default: temp->size = 4; 262 | } 263 | temp->parameter = param_no; 264 | temp->next=NULL; 265 | 266 | if (bufferPtr == NULL) { 267 | bufferPtr = temp; 268 | } else { 269 | tokenList *p = bufferPtr; 270 | while (p->next!=NULL) 271 | p=p->next; 272 | p->next=temp; 273 | } 274 | } 275 | void clearQueue() { 276 | tokenList *temp; 277 | while(bufferPtr != NULL) { 278 | temp = bufferPtr; 279 | bufferPtr = bufferPtr->next; 280 | free(temp); 281 | } 282 | } 283 | void addFunctionPrototype(char *name, unsigned int paramCount, tokenReturnType ret_type){ 284 | 285 | if(strcmp(name, "main") == 0){ 286 | printf("ERROR: %d : main func cannot declare as prototype\n",yylineno); 287 | exit(1); 288 | } 289 | tokenList *temp = symPtr; 290 | while(temp!=NULL){ 291 | if((strcmp(name,temp->name)==0)&&(temp->type==PROTO||temp->type==FUNC)){ 292 | printf("ERROR: %d : Multiple function declaration of %s\n",yylineno,name); 293 | exit(1); 294 | } 295 | temp=temp->next; 296 | } 297 | appendToSymbolTable(name,PROTO,ret_type,0,0,NULL,paramCount); 298 | 299 | if(paramCount!=0 && bufferPtr==NULL){ 300 | printf("ERROR: %d : Parameter mismatch of function %s\n",yylineno,name); 301 | exit(1); 302 | } 303 | else{ 304 | int cnt=1; 305 | if(bufferPtr!=NULL){ 306 | tokenList *p=bufferPtr; 307 | while(p!=NULL){ 308 | p->scope=name; 309 | p->parameter=cnt++; 310 | appendToSymbolTable(p->name,p->type,p->returnType,p->size,p->line,p->scope,p->parameter); 311 | p=p->next; 312 | 313 | } 314 | clearQueue(); 315 | } 316 | 317 | } 318 | } 319 | tokenList * addSymbolToParameterQueue(tokenList * queue, char *name, tokenType type) { 320 | tokenList *symbol = malloc(sizeof(tokenList )); 321 | symbol->name = strdup(name); 322 | symbol->type = type; 323 | symbol->next = false; 324 | if (queue == NULL) { 325 | return symbol; 326 | } else { 327 | tokenList *entry = queue; 328 | while (entry->next) { 329 | entry = entry->next; 330 | } 331 | entry->next = symbol; 332 | return queue; 333 | } 334 | } 335 | void addFunction(char *name,unsigned int paramCount,tokenReturnType ret_type,int line) { 336 | tokenList *symTable = symPtr,*prototype = NULL; 337 | unsigned int localOffset = 0; 338 | if(symTable != NULL){ 339 | while(symTable){ 340 | if( symTable->type == PROTO && 0 == strcasecmp(name,symTable->name)){ 341 | prototype = symTable; 342 | break; 343 | } 344 | symTable = symTable->next; 345 | } 346 | } 347 | if(strcmp(name, "main") == 0){ 348 | if(paramCount != 0){ 349 | printf("ERROR: %d : main function should not have parameters\n",yylineno); 350 | exit(1); 351 | } 352 | else if(ret_type != Return_INT){ 353 | printf("ERROR: %d : main function should return integer\n",yylineno); 354 | exit(1); 355 | } 356 | else{ 357 | 358 | tokenList *symbol = bufferPtr; 359 | int s = 0; 360 | while(symbol){ 361 | int size = symbol->size; 362 | symbol->size = localOffset; 363 | localOffset+=size; 364 | globalOffset+=size; 365 | s+=size; 366 | 367 | symbol->scope = name; 368 | symbol->parameter = 0; 369 | appendToSymbolTable(symbol->name,symbol->type,symbol->returnType,symbol->size,symbol->line,symbol->scope,symbol->parameter); 370 | symbol=symbol->next; 371 | } 372 | tokenList *newEntry= appendToSymbolTable(name,MAIN,ret_type,s,line,NULL,0); 373 | clearQueue(); 374 | 375 | } 376 | 377 | } 378 | else if(!prototype){ 379 | 380 | if(bufferPtr == NULL && paramCount != 0){ 381 | printf("ERROR: %d : Parameter mismatch of function %s\n",yylineno,name); 382 | exit(1); 383 | } 384 | else{ 385 | int cnt = 1; 386 | int s = 0; 387 | tokenList *symbol = bufferPtr; 388 | while(symbol!=NULL){ 389 | int size = symbol->size; 390 | symbol->size = localOffset; 391 | localOffset+=size; 392 | globalOffset+=size; 393 | s += size; 394 | 395 | symbol->scope = name; 396 | if(paramCount != 0){ 397 | symbol->parameter=cnt++; 398 | } 399 | else{ 400 | symbol->parameter=0; 401 | } 402 | appendToSymbolTable(symbol->name,symbol->type,symbol->returnType,symbol->size,symbol->line,symbol->scope,symbol->parameter); 403 | symbol = symbol->next; 404 | } 405 | tokenList *newEntry = appendToSymbolTable(name,FUNC,ret_type,s,line,NULL,paramCount); 406 | clearQueue(); 407 | } 408 | } 409 | else{ 410 | prototype->type = FUNC; 411 | prototype->line = line; 412 | if(bufferPtr == NULL && paramCount != 0){ 413 | printf("ERROR: %d : Parameter mismatch of function %s\n",yylineno,name); 414 | exit(1); 415 | } 416 | else{ 417 | if(prototype->parameter != paramCount){ 418 | printf("ERROR: %d : Parameters are not matching with prototype %s\n",yylineno,name); 419 | exit(1); 420 | } 421 | int s = 0; 422 | tokenList *symbol = symPtr; 423 | for(int i = 0;iscope == NULL){ 425 | symbol=symbol->next; 426 | if(strcmp(symbol->scope,name)==0&&symbol->scope != NULL){ 427 | break; 428 | } 429 | } 430 | if(symbol->type != bufferPtr->type){ 431 | printf("ERROR: %d : Parameter type mismatch in function %s\n",yylineno,name); 432 | exit(1); 433 | } 434 | int size = symbol->size; 435 | symbol->size = localOffset; 436 | localOffset+=size; 437 | globalOffset+=size; 438 | s+=size; 439 | 440 | tokenList *temp = bufferPtr; 441 | bufferPtr = bufferPtr->next; 442 | free(temp); 443 | 444 | symbol=symbol->next; 445 | } 446 | tokenList *queuSymbol = bufferPtr; 447 | while(queuSymbol!=NULL){ 448 | int size = queuSymbol->size; 449 | queuSymbol->size = localOffset; 450 | localOffset+=size; 451 | globalOffset+=size; 452 | s+=size; 453 | 454 | queuSymbol->scope = name; 455 | queuSymbol->parameter = 0; 456 | appendToSymbolTable(queuSymbol->name,queuSymbol->type,queuSymbol->returnType,queuSymbol->size,queuSymbol->line, 457 | queuSymbol->scope,queuSymbol->parameter); 458 | queuSymbol = queuSymbol->next; 459 | } 460 | prototype->size = s; 461 | clearQueue(); 462 | 463 | } 464 | } 465 | 466 | } 467 | char* nextFloatVar(){ 468 | char buffer[10]; 469 | sprintf(buffer,"Tf_%d",++tempFloatCounter); 470 | addSymbolToQueue(buffer, FLOAT_type, 0); 471 | return strdup(buffer); 472 | } 473 | 474 | char* nextIntVar(){ 475 | char buffer[10]; 476 | sprintf(buffer,"Ti_%d",++tempIntCounter); 477 | addSymbolToQueue(buffer, INT_type, 0); 478 | return strdup(buffer); 479 | } 480 | int nextquad(){ 481 | return currentLine + 1; 482 | } 483 | tokenList * getSymbol(char *token){ 484 | tokenList *p = symPtr; 485 | while(p!=NULL){ 486 | if(p->name!=NULL && strcmp(token, p->name)==0){ 487 | return p; 488 | } 489 | p=p->next; 490 | } 491 | } 492 | int getSymbolType(char *token) { 493 | tokenList *p=bufferPtr; 494 | while(p!=NULL) { 495 | if(strcmp(token, p->name)==0) 496 | return(p->type); 497 | 498 | p=p->next; 499 | } 500 | return 0; 501 | } 502 | int getFunctionType(char *token){ 503 | tokenList *p=symPtr; 504 | while(p!=NULL){ 505 | if(strcmp(token,p->name)==0 && (p->type==FUNC||p->type==PROTO)){ 506 | return p->returnType; 507 | } 508 | } 509 | 510 | return 0; 511 | } 512 | int checkAndGenerateParams(tokenList * queue, char* name ,int parameterCount){ 513 | char buffer[50]; 514 | tokenList *cur = symPtr; 515 | while(cur != NULL){ 516 | if((cur->type == FUNC || cur->type == PROTO) && 0 == strcmp(name,cur->name)){ 517 | break; 518 | } 519 | } 520 | if(cur == NULL) 521 | return -1; 522 | int foundParams = 0; 523 | cur = symPtr; 524 | do{ 525 | while(cur != NULL){ 526 | if(cur->scope != NULL && 0 == strcmp(name,cur->scope) && cur->parameter != 0){ 527 | break; 528 | } 529 | cur = cur->next; 530 | } 531 | if(cur == NULL || queue == NULL){ 532 | if(parameterCount == 0 && foundParams == 0) 533 | return 0; 534 | else 535 | return -2; 536 | } 537 | else if(cur->type != queue->type){ 538 | return -3; 539 | } 540 | foundParams++; 541 | sprintf(buffer,"PARAM %s",queue->name); 542 | appendCode(buffer); 543 | cur = cur->next; 544 | tokenList *temp = queue; 545 | queue = queue->next; 546 | free(temp); 547 | }while(foundParams != parameterCount); 548 | return 0; 549 | } 550 | 551 | 552 | -------------------------------------------------------------------------------- /Intermediate Code Generator/testcases/icg.c: -------------------------------------------------------------------------------- 1 | #include "icg.h" 2 | #include "y.tab.h" 3 | 4 | tokenList *symPtr = NULL; 5 | tokenList *bufferPtr = NULL; 6 | 7 | threeAddressCode *codePtr = NULL; 8 | 9 | extern int yylineno; 10 | extern int yyin; 11 | 12 | int globalOffset = 0, tempIntCounter = 0,tempFloatCounter= 0,currentLine = -1; 13 | 14 | void yyerror() { 15 | printf("ERROR: %d : Syntactical Error\n",yylineno); 16 | exit(1); 17 | } 18 | 19 | int main(int argc, char **argv) { 20 | yyin = fopen(argv[1],"r"); 21 | yyparse(); 22 | /*tokenList *p1=symPtr,*p2=symPtr; 23 | if(p1!=NULL && p2!=NULL){ 24 | while(p1!=NULL){ 25 | while(p2!=NULL){ 26 | if(strcmp(p1->name,p2->name)==0 && p1->type==p2->type && strcmp(p1->scope,p2->scope)==0){ 27 | printf("ERROR: %d : Multiple Declarations of %s in %s scope\n",p1->name,p1->scope); 28 | exit(1); 29 | } 30 | p2=p2->next; 31 | } 32 | p1=p1->next; 33 | } 34 | }*/ 35 | printf("\n\n\t Parsing Completed\n"); 36 | printf("Three Address Code and Symbol table are generated\n\n"); 37 | FILE *icgPtr= fopen("threeAddressCode.txt","w"); 38 | writeCode(icgPtr); 39 | FILE *symPtr = fopen("symbolTable.txt", "w"); 40 | writeSymbolTable(symPtr); 41 | fclose(icgPtr); 42 | fclose(symPtr); 43 | printf("\n"); 44 | return(0); 45 | } 46 | 47 | static const char* returnTypeToString(tokenReturnType type) 48 | { 49 | switch(type) 50 | { 51 | case Return_VOID: 52 | return "None"; 53 | 54 | case Return_INT: 55 | return "Int"; 56 | 57 | case Return_FLOAT: 58 | return "Real"; 59 | } 60 | } 61 | 62 | 63 | static const char* dataTypeToString(tokenType type) 64 | { 65 | switch(type) 66 | { 67 | 68 | case INT_type: 69 | return "Int"; 70 | 71 | case FLOAT_type: 72 | return "Real"; 73 | 74 | case BOOL_type: 75 | return "Bool"; 76 | 77 | case FUNC: 78 | return "Func"; 79 | case PROTO: 80 | return "Proto"; 81 | 82 | case MAIN: 83 | return "Main"; 84 | } 85 | } 86 | 87 | threeAddressCode* appendCode(char *code){ 88 | 89 | threeAddressCode *temp = malloc(sizeof(threeAddressCode )); 90 | 91 | temp->code=(char *)malloc(sizeof(char)*strlen(code)); 92 | strcpy(temp->code,code); 93 | temp->gotoLine = -1; 94 | temp->next = NULL; 95 | 96 | 97 | if(codePtr == NULL){ 98 | codePtr=temp; 99 | } 100 | else{ 101 | threeAddressCode *p=codePtr; 102 | while(p->next!=NULL){ 103 | p=p->next; 104 | } 105 | p->next=temp; 106 | } 107 | currentLine++; 108 | return temp; 109 | } 110 | 111 | void backpatch(backPatchList* list, int gotoL){ 112 | if(list == NULL){ 113 | return; 114 | } else{ 115 | backPatchList* temp; 116 | while(list){ 117 | if(list->entry != NULL){ 118 | list->entry->gotoLine = gotoL; 119 | } 120 | //printf("backpatching: %s",list->entry->code); 121 | temp = list; 122 | list = list->next; 123 | free(temp); 124 | } 125 | } 126 | } 127 | 128 | backPatchList* mergelists(backPatchList* a, backPatchList* b){ 129 | if(a != NULL && b == NULL){ 130 | return a; 131 | } 132 | else if(a == NULL && b != NULL){ 133 | return b; 134 | } 135 | else if(a == NULL && b == NULL){ 136 | return NULL; 137 | } 138 | else{ 139 | backPatchList* temp = a; 140 | while(a->next){ 141 | a = a->next; 142 | } 143 | a->next = b; 144 | return temp; 145 | } 146 | } 147 | 148 | backPatchList* appendToBackPatch(backPatchList* p, threeAddressCode* newCode){ 149 | 150 | if(newCode == NULL){ 151 | return p; 152 | } 153 | else if(p == NULL){ 154 | backPatchList* temp = malloc(sizeof(backPatchList)); 155 | temp->entry = newCode; 156 | temp->next = NULL; 157 | return temp; 158 | } 159 | else{ 160 | backPatchList* temp = malloc(sizeof(backPatchList)); 161 | temp->entry = newCode; 162 | temp->next=NULL; 163 | while(p->next!=NULL){ 164 | p=p->next; 165 | } 166 | p->next = temp; 167 | return temp; 168 | } 169 | } 170 | 171 | tokenList* appendToSymbolTable(char *name,tokenType type,tokenReturnType returnType,long size,long line,char *scope,long parameter) 172 | { 173 | 174 | tokenList *temp = malloc(sizeof(tokenList )); 175 | 176 | temp->name = strdup(name); 177 | temp->type = type; 178 | temp->returnType = returnType; 179 | temp->size = size; 180 | temp->line = line; 181 | temp->scope = (scope == PARENT_NONE ? NULL : strdup(scope)); 182 | temp->parameter = parameter; 183 | temp->next = NULL; 184 | 185 | if(symPtr==NULL){ 186 | symPtr=temp; 187 | } 188 | else{ 189 | tokenList *p = symPtr; 190 | while(p->next!=NULL) 191 | p=p->next; 192 | p->next=temp; 193 | } 194 | return temp; 195 | } 196 | 197 | 198 | void writeCode(FILE *icgOut) 199 | { 200 | long icgLineCount = 0; 201 | fprintf(icgOut, "\n\t\t\t Intermediate Code Generated"); 202 | fprintf(icgOut, "\n\t\t\t-------------------------------\n"); 203 | 204 | if(codePtr==NULL){ 205 | printf("\nNo code Generated\n"); 206 | } 207 | else{ 208 | threeAddressCode *p = codePtr; 209 | while(p) 210 | { 211 | if(p->gotoLine == -1){ 212 | fprintf(icgOut,"%-4lu %s\n",icgLineCount,p->code); 213 | } 214 | else{ 215 | fprintf(icgOut,"%-4lu %s %d\n",icgLineCount,p->code,p->gotoLine); 216 | } 217 | p = p->next; 218 | icgLineCount++; 219 | } 220 | } 221 | } 222 | 223 | 224 | 225 | void writeSymbolTable(FILE *symOut) 226 | { 227 | fprintf(symOut, "\n\t\t\t Symbol Table"); 228 | fprintf(symOut, "\n\t\t\t------------------\n\n"); 229 | 230 | if(symPtr == NULL) 231 | { 232 | printf("\nSymbol table is empty"); 233 | } 234 | else{ 235 | fprintf(symOut," Name DataType ReturnType Offset Scope"); 236 | fprintf(symOut,"\n------------------------------------------------------------------------\n"); 237 | 238 | tokenList *p= symPtr; 239 | while(p!=NULL) 240 | { 241 | fprintf(symOut,"%-20s %-14s %-14s %-12lu %-12s\n", 242 | 243 | p->name, 244 | dataTypeToString(p->type), 245 | returnTypeToString(p->returnType), 246 | p->size, 247 | p->scope == NULL ? "None" : p->scope 248 | ); 249 | p=p->next; 250 | } 251 | } 252 | } 253 | void addSymbolToQueue(char *name, tokenType type, unsigned long param_no) { 254 | tokenList *temp = malloc(sizeof(tokenList )); 255 | temp->name = strdup(name); 256 | temp->type = type; 257 | switch(type){ 258 | case INT_type: temp->size = 4;break; 259 | case FLOAT_type: temp->size = 8;break; 260 | case BOOL_type: temp->size = 1;break; 261 | default: temp->size = 4; 262 | } 263 | temp->parameter = param_no; 264 | temp->next=NULL; 265 | 266 | if (bufferPtr == NULL) { 267 | bufferPtr = temp; 268 | } else { 269 | tokenList *p = bufferPtr; 270 | while (p->next!=NULL) 271 | p=p->next; 272 | p->next=temp; 273 | } 274 | } 275 | void clearQueue() { 276 | tokenList *temp; 277 | while(bufferPtr != NULL) { 278 | temp = bufferPtr; 279 | bufferPtr = bufferPtr->next; 280 | free(temp); 281 | } 282 | } 283 | void addFunctionPrototype(char *name, unsigned int paramCount, tokenReturnType ret_type){ 284 | 285 | if(strcmp(name, "main") == 0){ 286 | printf("ERROR: %d : main func cannot declare as prototype\n",yylineno); 287 | exit(1); 288 | } 289 | tokenList *temp = symPtr; 290 | while(temp!=NULL){ 291 | if((strcmp(name,temp->name)==0)&&(temp->type==PROTO||temp->type==FUNC)){ 292 | printf("ERROR: %d : Multiple function declaration of %s\n",yylineno,name); 293 | exit(1); 294 | } 295 | temp=temp->next; 296 | } 297 | appendToSymbolTable(name,PROTO,ret_type,0,0,NULL,paramCount); 298 | 299 | if(paramCount!=0 && bufferPtr==NULL){ 300 | printf("ERROR: %d : Parameter mismatch of function %s\n",yylineno,name); 301 | exit(1); 302 | } 303 | else{ 304 | int cnt=1; 305 | if(bufferPtr!=NULL){ 306 | tokenList *p=bufferPtr; 307 | while(p!=NULL){ 308 | p->scope=name; 309 | p->parameter=cnt++; 310 | appendToSymbolTable(p->name,p->type,p->returnType,p->size,p->line,p->scope,p->parameter); 311 | p=p->next; 312 | 313 | } 314 | clearQueue(); 315 | } 316 | 317 | } 318 | } 319 | tokenList * addSymbolToParameterQueue(tokenList * queue, char *name, tokenType type) { 320 | tokenList *symbol = malloc(sizeof(tokenList )); 321 | symbol->name = strdup(name); 322 | symbol->type = type; 323 | symbol->next = false; 324 | if (queue == NULL) { 325 | return symbol; 326 | } else { 327 | tokenList *entry = queue; 328 | while (entry->next) { 329 | entry = entry->next; 330 | } 331 | entry->next = symbol; 332 | return queue; 333 | } 334 | } 335 | void addFunction(char *name,unsigned int paramCount,tokenReturnType ret_type,int line) { 336 | tokenList *symTable = symPtr,*prototype = NULL; 337 | unsigned int localOffset = 0; 338 | if(symTable != NULL){ 339 | while(symTable){ 340 | if( symTable->type == PROTO && 0 == strcasecmp(name,symTable->name)){ 341 | prototype = symTable; 342 | break; 343 | } 344 | symTable = symTable->next; 345 | } 346 | } 347 | if(strcmp(name, "main") == 0){ 348 | if(paramCount != 0){ 349 | printf("ERROR: %d : main function should not have parameters\n",yylineno); 350 | exit(1); 351 | } 352 | else if(ret_type != Return_INT){ 353 | printf("ERROR: %d : main function should return integer\n",yylineno); 354 | exit(1); 355 | } 356 | else{ 357 | 358 | tokenList *symbol = bufferPtr; 359 | int s = 0; 360 | while(symbol){ 361 | int size = symbol->size; 362 | symbol->size = localOffset; 363 | localOffset+=size; 364 | globalOffset+=size; 365 | s+=size; 366 | 367 | symbol->scope = name; 368 | symbol->parameter = 0; 369 | appendToSymbolTable(symbol->name,symbol->type,symbol->returnType,symbol->size,symbol->line,symbol->scope,symbol->parameter); 370 | symbol=symbol->next; 371 | } 372 | tokenList *newEntry= appendToSymbolTable(name,MAIN,ret_type,s,line,NULL,0); 373 | clearQueue(); 374 | 375 | } 376 | 377 | } 378 | else if(!prototype){ 379 | 380 | if(bufferPtr == NULL && paramCount != 0){ 381 | printf("ERROR: %d : Parameter mismatch of function %s\n",yylineno,name); 382 | exit(1); 383 | } 384 | else{ 385 | int cnt = 1; 386 | int s = 0; 387 | tokenList *symbol = bufferPtr; 388 | while(symbol!=NULL){ 389 | int size = symbol->size; 390 | symbol->size = localOffset; 391 | localOffset+=size; 392 | globalOffset+=size; 393 | s += size; 394 | 395 | symbol->scope = name; 396 | if(paramCount != 0){ 397 | symbol->parameter=cnt++; 398 | } 399 | else{ 400 | symbol->parameter=0; 401 | } 402 | appendToSymbolTable(symbol->name,symbol->type,symbol->returnType,symbol->size,symbol->line,symbol->scope,symbol->parameter); 403 | symbol = symbol->next; 404 | } 405 | tokenList *newEntry = appendToSymbolTable(name,FUNC,ret_type,s,line,NULL,paramCount); 406 | clearQueue(); 407 | } 408 | } 409 | else{ 410 | prototype->type = FUNC; 411 | prototype->line = line; 412 | if(bufferPtr == NULL && paramCount != 0){ 413 | printf("ERROR: %d : Parameter mismatch of function %s\n",yylineno,name); 414 | exit(1); 415 | } 416 | else{ 417 | if(prototype->parameter != paramCount){ 418 | printf("ERROR: %d : Parameters are not matching with prototype %s\n",yylineno,name); 419 | exit(1); 420 | } 421 | int s = 0; 422 | tokenList *symbol = symPtr; 423 | for(int i = 0;iscope == NULL){ 425 | symbol=symbol->next; 426 | if(strcmp(symbol->scope,name)==0&&symbol->scope != NULL){ 427 | break; 428 | } 429 | } 430 | if(symbol->type != bufferPtr->type){ 431 | printf("ERROR: %d : Parameter type mismatch in function %s\n",yylineno,name); 432 | exit(1); 433 | } 434 | int size = symbol->size; 435 | symbol->size = localOffset; 436 | localOffset+=size; 437 | globalOffset+=size; 438 | s+=size; 439 | 440 | tokenList *temp = bufferPtr; 441 | bufferPtr = bufferPtr->next; 442 | free(temp); 443 | 444 | symbol=symbol->next; 445 | } 446 | tokenList *queuSymbol = bufferPtr; 447 | while(queuSymbol!=NULL){ 448 | int size = queuSymbol->size; 449 | queuSymbol->size = localOffset; 450 | localOffset+=size; 451 | globalOffset+=size; 452 | s+=size; 453 | 454 | queuSymbol->scope = name; 455 | queuSymbol->parameter = 0; 456 | appendToSymbolTable(queuSymbol->name,queuSymbol->type,queuSymbol->returnType,queuSymbol->size,queuSymbol->line, 457 | queuSymbol->scope,queuSymbol->parameter); 458 | queuSymbol = queuSymbol->next; 459 | } 460 | prototype->size = s; 461 | clearQueue(); 462 | 463 | } 464 | } 465 | 466 | } 467 | char* nextFloatVar(){ 468 | char buffer[10]; 469 | sprintf(buffer,"Tf_%d",++tempFloatCounter); 470 | addSymbolToQueue(buffer, FLOAT_type, 0); 471 | return strdup(buffer); 472 | } 473 | 474 | char* nextIntVar(){ 475 | char buffer[10]; 476 | sprintf(buffer,"Ti_%d",++tempIntCounter); 477 | addSymbolToQueue(buffer, INT_type, 0); 478 | return strdup(buffer); 479 | } 480 | int nextquad(){ 481 | return currentLine + 1; 482 | } 483 | tokenList * getSymbol(char *token){ 484 | tokenList *p = symPtr; 485 | while(p!=NULL){ 486 | if(p->name!=NULL && strcmp(token, p->name)==0){ 487 | return p; 488 | } 489 | p=p->next; 490 | } 491 | } 492 | int getSymbolType(char *token) { 493 | tokenList *p=bufferPtr; 494 | while(p!=NULL) { 495 | if(strcmp(token, p->name)==0) 496 | return(p->type); 497 | 498 | p=p->next; 499 | } 500 | return 0; 501 | } 502 | int getFunctionType(char *token){ 503 | tokenList *p=symPtr; 504 | while(p!=NULL){ 505 | if(strcmp(token,p->name)==0 && (p->type==FUNC||p->type==PROTO)){ 506 | return p->returnType; 507 | } 508 | } 509 | 510 | return 0; 511 | } 512 | int checkAndGenerateParams(tokenList * queue, char* name ,int parameterCount){ 513 | char buffer[50]; 514 | tokenList *cur = symPtr; 515 | while(cur != NULL){ 516 | if((cur->type == FUNC || cur->type == PROTO) && 0 == strcmp(name,cur->name)){ 517 | break; 518 | } 519 | } 520 | if(cur == NULL) 521 | return -1; 522 | int foundParams = 0; 523 | cur = symPtr; 524 | do{ 525 | while(cur != NULL){ 526 | if(cur->scope != NULL && 0 == strcmp(name,cur->scope) && cur->parameter != 0){ 527 | break; 528 | } 529 | cur = cur->next; 530 | } 531 | if(cur == NULL || queue == NULL){ 532 | if(parameterCount == 0 && foundParams == 0) 533 | return 0; 534 | else 535 | return -2; 536 | } 537 | else if(cur->type != queue->type){ 538 | return -3; 539 | } 540 | foundParams++; 541 | sprintf(buffer,"PARAM %s",queue->name); 542 | appendCode(buffer); 543 | cur = cur->next; 544 | tokenList *temp = queue; 545 | queue = queue->next; 546 | free(temp); 547 | }while(foundParams != parameterCount); 548 | return 0; 549 | } 550 | 551 | 552 | -------------------------------------------------------------------------------- /Semantic Analyzer/src/syntaxChecker.y: -------------------------------------------------------------------------------- 1 | %{ 2 | #include 3 | #include 4 | #include 5 | #include "y.tab.h" 6 | #include "semantic.h" 7 | 8 | 9 | 10 | extern FILE *yyin; 11 | extern int lineCount; 12 | extern char *tablePtr; 13 | extern char *tablePtr; 14 | extern int nestedCommentCount; 15 | extern int commentFlag; 16 | extern int arrayIndexErr; 17 | 18 | 19 | 20 | 21 | char *sourceCode=NULL; 22 | int errorFlag=0; 23 | void makeList(char *,char,int); 24 | %} 25 | 26 | %token AUTO BREAK CASE CHAR CONST CONTINUE DEFAULT DO DOUBLE ELSE ENUM 27 | %token EXTERN FLOAT FOR GOTO IF INT LONG REGISTER RETURN SHORT SIGNED 28 | 29 | %token SIZEOF STATIC STRUCT SWITCH TYPEDEF UNION UNSIGNED VOID VOLATILE WHILE 30 | 31 | 32 | 33 | %token IDENTIFIER 34 | 35 | %token CONSTANT FLCONSTANT STRING_LITERAL 36 | 37 | %token ELLIPSIS 38 | 39 | %token PTR_OP INC_OP DEC_OP LEFT_OP RIGHT_OP LE_OP GE_OP EQ_OP NE_OP 40 | %token AND_OP OR_OP MUL_ASSIGN DIV_ASSIGN MOD_ASSIGN ADD_ASSIGN 41 | %token SUB_ASSIGN LEFT_ASSIGN RIGHT_ASSIGN AND_ASSIGN 42 | %token XOR_ASSIGN OR_ASSIGN TYPE_NAME 43 | 44 | %nonassoc LOWER_THAN_ELSE 45 | %nonassoc ELSE 46 | 47 | %start translation_unit 48 | 49 | %% 50 | 51 | primary_expression 52 | : IDENTIFIER { $$=checkScope(tablePtr,lineCount); } 53 | | FLCONSTANT {tempCheckType=4;} 54 | | CONSTANT { addConstant(tablePtr, lineCount);} 55 | | STRING_LITERAL { makeList(tablePtr, 's', lineCount);} 56 | | '(' expression ')' { makeList("(", 'p', lineCount); makeList(")", 'p', lineCount); $$=$2; } 57 | ; 58 | 59 | postfix_expression 60 | : primary_expression {$$=$1;} 61 | | postfix_expression '[' expression ']' { makeList("[", 'p', lineCount); makeList("]", 'p', lineCount); } 62 | | postfix_expression '(' ')' { makeList("(", 'p', lineCount); makeList(")", 'p', lineCount); } 63 | | postfix_expression '(' argument_expression_list ')' { makeList("(", 'p', lineCount); makeList(")", 'p', lineCount); } 64 | | postfix_expression '.' IDENTIFIER { makeList(tablePtr, 'v', lineCount);} 65 | | postfix_expression PTR_OP IDENTIFIER { makeList(tablePtr, 'v', lineCount);} 66 | | postfix_expression INC_OP { makeList(tablePtr, 'o', lineCount);} 67 | | postfix_expression DEC_OP { makeList(tablePtr, 'o', lineCount);} 68 | ; 69 | 70 | argument_expression_list 71 | : assignment_expression {$$=$1;} 72 | | argument_expression_list ',' assignment_expression { makeList(",",'p', lineCount); } 73 | ; 74 | 75 | unary_expression 76 | : postfix_expression {$$=$1;} 77 | | INC_OP unary_expression { makeList("++",'o', lineCount); } 78 | | DEC_OP unary_expression { makeList("--",'o', lineCount); } 79 | | unary_operator cast_expression 80 | | SIZEOF unary_expression { makeList("sizeof",'o', lineCount); } 81 | | SIZEOF '(' type_name ')' { makeList("sizeof",'o', lineCount); } 82 | { makeList("(", 'p', lineCount); makeList(")", 'p', lineCount); } 83 | ; 84 | 85 | unary_operator 86 | : '&' { makeList("&",'o', lineCount); } 87 | | '*' { makeList("*",'o', lineCount); } 88 | | '+' { makeList("+",'o', lineCount); } 89 | | '-' { makeList("-",'o', lineCount); } 90 | | '~' { makeList("~",'o', lineCount); } 91 | | '!' { makeList("!",'o', lineCount); } 92 | ; 93 | 94 | cast_expression 95 | : unary_expression {$$=$1;} 96 | | '(' type_name ')' cast_expression { makeList("(", 'p', lineCount); makeList(")", 'p', lineCount); } 97 | ; 98 | 99 | multiplicative_expression 100 | : cast_expression {$$=$1;} 101 | | multiplicative_expression '*' cast_expression { makeList("*",'o', lineCount); } 102 | | multiplicative_expression '/' cast_expression { makeList("/",'o', lineCount); } 103 | | multiplicative_expression '%' cast_expression { makeList("%",'o', lineCount); } 104 | ; 105 | 106 | additive_expression 107 | : multiplicative_expression {$$=$1;} 108 | | additive_expression '+' multiplicative_expression { makeList("+",'o', lineCount); } 109 | | additive_expression '-' multiplicative_expression { makeList("-",'o', lineCount); } 110 | ; 111 | 112 | shift_expression 113 | : additive_expression {$$=$1;} 114 | | shift_expression LEFT_OP additive_expression { makeList("<<",'o', lineCount); } 115 | | shift_expression RIGHT_OP additive_expression { makeList(">>",'o', lineCount); } 116 | ; 117 | 118 | relational_expression 119 | : shift_expression {$$=$1;} 120 | | relational_expression '<' shift_expression 121 | | relational_expression '>' shift_expression 122 | | relational_expression LE_OP shift_expression { makeList("<=",'o', lineCount); } 123 | | relational_expression GE_OP shift_expression { makeList(">=",'o', lineCount); } 124 | ; 125 | 126 | equality_expression 127 | : relational_expression {$$=$1;} 128 | | equality_expression EQ_OP relational_expression { makeList("==",'o', lineCount); } 129 | | equality_expression NE_OP relational_expression { makeList("!=",'o', lineCount); } 130 | ; 131 | 132 | and_expression 133 | : equality_expression {$$=$1;} 134 | | and_expression '&' equality_expression { makeList("&", 'o', lineCount);} 135 | ; 136 | 137 | exclusive_or_expression 138 | : and_expression {$$=$1;} 139 | | exclusive_or_expression '^' and_expression { makeList("^", 'o', lineCount); } 140 | ; 141 | 142 | inclusive_or_expression 143 | : exclusive_or_expression {$$=$1;} 144 | | inclusive_or_expression '|' exclusive_or_expression { makeList("|", 'o', lineCount); } 145 | ; 146 | 147 | logical_and_expression 148 | : inclusive_or_expression {$$=$1;} 149 | | logical_and_expression AND_OP inclusive_or_expression { makeList("&&", 'o', lineCount); } 150 | ; 151 | 152 | logical_or_expression 153 | : logical_and_expression {$$=$1;} 154 | | logical_or_expression OR_OP logical_and_expression { makeList("||", 'o', lineCount); } 155 | ; 156 | 157 | conditional_expression 158 | : logical_or_expression {$$=$1;} 159 | | logical_or_expression '?' expression ':' conditional_expression { makeList("?:",'o', lineCount); } 160 | ; 161 | 162 | assignment_expression 163 | : conditional_expression {$$=$1;} 164 | | unary_expression assignment_operator assignment_expression {$$=$3; checkType($1,$3,lineCount);} 165 | ; 166 | 167 | assignment_operator 168 | : '=' { makeList("=",'o', lineCount); } 169 | | MUL_ASSIGN { makeList("*=",'o', lineCount); } 170 | | DIV_ASSIGN { makeList("/=",'o', lineCount); } 171 | | MOD_ASSIGN { makeList("%=",'o', lineCount); } 172 | | ADD_ASSIGN { makeList("+=",'o', lineCount); } 173 | | SUB_ASSIGN { makeList("-=",'o', lineCount); } 174 | | LEFT_ASSIGN { makeList("<<=",'o', lineCount); } 175 | | RIGHT_ASSIGN { makeList(">==",'o', lineCount); } 176 | | AND_ASSIGN { makeList("&=",'o', lineCount); } 177 | | XOR_ASSIGN { makeList("^=",'o', lineCount); } 178 | | OR_ASSIGN { makeList("|=",'o', lineCount); } 179 | ; 180 | 181 | expression 182 | : assignment_expression {$$=$1;} 183 | | expression ',' assignment_expression { makeList(",", 'p', lineCount); } 184 | ; 185 | 186 | constant_expression 187 | : conditional_expression 188 | ; 189 | 190 | declaration 191 | : declaration_specifiers ';' { makeList(";", 'p', lineCount);typeBuffer=' '; } 192 | | declaration_specifiers init_declarator_list ';' { makeList(";", 'p', lineCount); typeBuffer=' ';} 193 | ; 194 | 195 | declaration_specifiers 196 | : storage_class_specifier 197 | | storage_class_specifier declaration_specifiers 198 | | type_specifier 199 | | type_specifier declaration_specifiers 200 | | type_qualifier 201 | | type_qualifier declaration_specifiers 202 | ; 203 | 204 | init_declarator_list 205 | : init_declarator 206 | | init_declarator_list ',' init_declarator { makeList(",", 'p', lineCount); } 207 | ; 208 | 209 | init_declarator 210 | : declarator 211 | | declarator '=' initializer { makeList("=", 'o', lineCount); } 212 | ; 213 | 214 | storage_class_specifier 215 | : TYPEDEF { makeList("typedef", 'k', lineCount);} 216 | | EXTERN { makeList("extern", 'k', lineCount);} 217 | | STATIC { makeList("static", 'k', lineCount);} 218 | | AUTO { makeList("auto", 'k', lineCount);} 219 | | REGISTER { makeList("register", 'k', lineCount);} 220 | ; 221 | 222 | type_specifier 223 | : VOID { makeList("void", 'k', lineCount);typeBuffer='v';} 224 | | CHAR { makeList("char", 'k', lineCount); typeBuffer='c';} 225 | | SHORT { makeList("short", 'k', lineCount);} 226 | | INT { makeList("int", 'k', lineCount); typeBuffer='i';} 227 | | LONG { makeList("long", 'k', lineCount);} 228 | | FLOAT { makeList("float", 'k', lineCount); typeBuffer='f';} 229 | | DOUBLE { makeList("double", 'k', lineCount);} 230 | | SIGNED { makeList("signed", 'k', lineCount);} 231 | | UNSIGNED { makeList("unsigned", 'k', lineCount);} 232 | | struct_or_union_specifier 233 | | enum_specifier 234 | | TYPE_NAME 235 | ; 236 | 237 | struct_or_union_specifier 238 | : struct_or_union IDENTIFIER '{' struct_declaration_list '}' 239 | | struct_or_union '{' struct_declaration_list '}' 240 | | struct_or_union IDENTIFIER 241 | ; 242 | 243 | struct_or_union 244 | : STRUCT { makeList("struct", 'k', lineCount);} 245 | | UNION { makeList("union", 'k', lineCount);} 246 | ; 247 | 248 | struct_declaration_list 249 | : struct_declaration 250 | | struct_declaration_list struct_declaration 251 | ; 252 | 253 | struct_declaration 254 | : specifier_qualifier_list struct_declarator_list ';' { makeList(";", 'p', lineCount); } 255 | ; 256 | 257 | specifier_qualifier_list 258 | : type_specifier specifier_qualifier_list 259 | | type_specifier 260 | | type_qualifier specifier_qualifier_list 261 | | type_qualifier 262 | ; 263 | 264 | struct_declarator_list 265 | : struct_declarator 266 | | struct_declarator_list ',' struct_declarator { makeList(",", 'p', lineCount); } 267 | ; 268 | 269 | struct_declarator 270 | : declarator 271 | | ':' constant_expression { makeList(":", 'p', lineCount); } 272 | | declarator ':' constant_expression { makeList(":", 'p', lineCount); } 273 | ; 274 | 275 | enum_specifier 276 | : ENUM '{' enumerator_list '}' { makeList("enum", 'k', lineCount);} 277 | | ENUM IDENTIFIER '{' enumerator_list '}' { makeList("enum", 'k', lineCount); makeList(tablePtr, 'v', lineCount); } 278 | | ENUM IDENTIFIER { makeList("enum", 'k', lineCount); makeList(tablePtr, 'v', lineCount); } 279 | ; 280 | 281 | enumerator_list 282 | : enumerator 283 | | enumerator_list ',' enumerator { makeList(",", 'p', lineCount); } 284 | ; 285 | 286 | enumerator 287 | : IDENTIFIER { makeList(tablePtr, 'v', lineCount); } 288 | | IDENTIFIER '=' constant_expression { makeList("=", 'o', lineCount); makeList("tablePtr", 'v', lineCount); } 289 | ; 290 | 291 | type_qualifier 292 | : CONST { makeList("const", 'k', lineCount); } 293 | | VOLATILE { makeList("volatile", 'k', lineCount); } 294 | ; 295 | 296 | declarator 297 | : pointer direct_declarator 298 | | direct_declarator 299 | ; 300 | 301 | direct_declarator 302 | : IDENTIFIER { checkDeclaration(tablePtr,lineCount,scopeCount);} 303 | | '(' declarator ')' { makeList("(", 'p', lineCount); makeList(")", 'p', lineCount); } 304 | | direct_declarator '[' constant_expression ']' { makeList("[", 'p', lineCount); makeList("]", 'p', lineCount); } 305 | | direct_declarator '[' ']' { makeList("[", 'p', lineCount); makeList("]", 'p', lineCount); } 306 | | direct_declarator '(' parameter_type_list ')' { makeList("(", 'p', lineCount); makeList(")", 'p', lineCount); } 307 | | direct_declarator '(' identifier_list ')' { makeList("(", 'p', lineCount); makeList(")", 'p', lineCount); } 308 | | direct_declarator '(' ')' { makeList("(", 'p', lineCount); makeList(")", 'p', lineCount); } 309 | ; 310 | 311 | pointer 312 | : '*' { makeList("*", 'o', lineCount); } 313 | | '*' type_qualifier_list { makeList("*", 'o', lineCount); } 314 | | '*' pointer { makeList("*", 'o', lineCount); } 315 | | '*' type_qualifier_list pointer { makeList("*", 'o', lineCount); } 316 | ; 317 | 318 | type_qualifier_list 319 | : type_qualifier 320 | | type_qualifier_list type_qualifier 321 | ; 322 | 323 | 324 | parameter_type_list 325 | : parameter_list 326 | | parameter_list ',' ELLIPSIS { makeList(",", 'p', lineCount); makeList("::", 'o', lineCount); } 327 | ; 328 | 329 | parameter_list 330 | : parameter_declaration 331 | | parameter_list ',' parameter_declaration { makeList(",", 'p', lineCount); } 332 | ; 333 | 334 | parameter_declaration 335 | : declaration_specifiers declarator 336 | | declaration_specifiers abstract_declarator 337 | | declaration_specifiers 338 | ; 339 | 340 | identifier_list 341 | : IDENTIFIER { checkDeclaration(tablePtr,lineCount,scopeCount);} 342 | | identifier_list ',' IDENTIFIER { checkDeclaration(tablePtr,lineCount,scopeCount);makeList(",", 'p', lineCount); } 343 | ; 344 | 345 | type_name 346 | : specifier_qualifier_list 347 | | specifier_qualifier_list abstract_declarator 348 | ; 349 | 350 | abstract_declarator 351 | : pointer 352 | | direct_abstract_declarator 353 | | pointer direct_abstract_declarator 354 | ; 355 | 356 | direct_abstract_declarator 357 | : '(' abstract_declarator ')' { makeList("(", 'p', lineCount); makeList(")", 'p', lineCount); } 358 | | '[' ']' { makeList("[", 'p', lineCount); makeList("]", 'p', lineCount); } 359 | | '[' constant_expression ']' { makeList("[", 'p', lineCount); makeList("]", 'p', lineCount); } 360 | | direct_abstract_declarator '[' ']' { makeList("[", 'p', lineCount); makeList("]", 'p', lineCount); } 361 | | direct_abstract_declarator '[' constant_expression ']' { makeList("[", 'p', lineCount); makeList("]", 'p', lineCount); } 362 | | '(' ')' { makeList("(", 'p', lineCount); makeList(")", 'p', lineCount); } 363 | | '(' parameter_type_list ')' { makeList("(", 'p', lineCount); makeList(")", 'p', lineCount); } 364 | | direct_abstract_declarator '(' ')' { makeList("(", 'p', lineCount); makeList(")", 'p', lineCount); } 365 | | direct_abstract_declarator '(' parameter_type_list ')' { makeList("(", 'p', lineCount); makeList(")", 'p', lineCount); } 366 | ; 367 | 368 | initializer 369 | : assignment_expression {$$=$1;} 370 | | '{' initializer_list '}' 371 | | '{' initializer_list ',' '}' 372 | ; 373 | 374 | initializer_list 375 | : initializer 376 | | initializer_list ',' initializer { makeList(",", 'p', lineCount); } 377 | ; 378 | 379 | statement 380 | : labeled_statement 381 | | compound_statement 382 | | expression_statement 383 | | selection_statement 384 | | iteration_statement 385 | | jump_statement 386 | ; 387 | 388 | labeled_statement 389 | : IDENTIFIER ':' statement { makeList(tablePtr, 'v', lineCount); } 390 | | CASE constant_expression ':' statement { makeList(":", 'p', lineCount); makeList("case", 'k', lineCount);} 391 | | DEFAULT ':' statement { makeList(":", 'p', lineCount); makeList("default", 'k', lineCount); } 392 | ; 393 | 394 | compound_statement 395 | : '{' '}' 396 | | '{' statement_list '}' 397 | | '{' declaration_list '}' 398 | | '{' declaration_list statement_list '}' 399 | ; 400 | 401 | declaration_list 402 | : declaration 403 | | declaration_list declaration 404 | ; 405 | 406 | statement_list 407 | : statement 408 | | statement_list statement 409 | ; 410 | 411 | expression_statement 412 | : ';' { makeList(";", 'p', lineCount); } 413 | | expression ';' { makeList(";", 'p', lineCount); } 414 | ; 415 | 416 | selection_statement 417 | : IF '(' expression ')' statement %prec LOWER_THAN_ELSE 418 | { makeList("if", 'k', lineCount); makeList("(", 'p', lineCount); makeList(")", 'p', lineCount);} 419 | | IF '(' expression ')' statement ELSE statement 420 | { makeList("if", 'k', lineCount); makeList("else", 'k', lineCount); makeList("(", 'p', lineCount); makeList(")", 'p', lineCount); 421 | } 422 | | SWITCH '(' expression ')' statement 423 | { makeList("switch", 'k', lineCount); makeList("(", 'p', lineCount); makeList(")", 'p', lineCount); } 424 | ; 425 | 426 | iteration_statement 427 | : WHILE '(' expression ')' statement 428 | { makeList("while", 'k', lineCount); makeList("(", 'p', lineCount); makeList(")", 'p', lineCount); } 429 | | DO statement WHILE '(' expression ')' ';' 430 | { makeList("do", 'k', lineCount); makeList("while", 'k', lineCount); makeList("(", 'p', lineCount); makeList(")", 'p', lineCount); makeList(";", 'p', lineCount); 431 | } 432 | | FOR '(' expression_statement expression_statement ')' statement 433 | { makeList("for", 'k', lineCount); makeList("(", 'p', lineCount); makeList(")", 'p', lineCount); } 434 | | FOR '(' expression_statement expression_statement expression ')' statement 435 | { makeList("for", 'k', lineCount); makeList("(", 'p', lineCount); makeList(")", 'p', lineCount); } 436 | ; 437 | 438 | jump_statement 439 | : GOTO IDENTIFIER ';' { makeList("goto", 'k', lineCount); makeList(";", 'p', lineCount); makeList(tablePtr, 'v', lineCount);} 440 | | CONTINUE ';' { makeList("continue", 'k', lineCount); makeList(";", 'p', lineCount); } 441 | | BREAK ';' { makeList("break", 'k', lineCount); makeList(";", 'p', lineCount);} 442 | | RETURN ';' { makeList("return", 'k', lineCount); makeList(";", 'p', lineCount);} 443 | | RETURN expression ';' { makeList("return", 'k', lineCount); makeList(";", 'p', lineCount);} 444 | ; 445 | 446 | translation_unit 447 | : external_declaration 448 | | translation_unit external_declaration 449 | ; 450 | 451 | external_declaration 452 | : function_definition 453 | | declaration 454 | ; 455 | 456 | function_definition 457 | : declaration_specifiers declarator declaration_list compound_statement 458 | | declaration_specifiers declarator compound_statement 459 | | declarator declaration_list compound_statement 460 | | declarator compound_statement 461 | ; 462 | 463 | %% 464 | void yyerror() 465 | { 466 | errorFlag=1; 467 | fflush(stdout); 468 | printf("\n%s : %d :Syntax error \n",sourceCode,lineCount); 469 | } 470 | void main(int argc,char **argv){ 471 | if(argc<=1){ 472 | 473 | printf("Invalid ,Expected Format : ./a.out <\"sourceCode\"> \n"); 474 | return 0; 475 | } 476 | 477 | yyin=fopen(argv[1],"r"); 478 | sourceCode=(char *)malloc(strlen(argv[1])*sizeof(char)); 479 | sourceCode=argv[1]; 480 | yyparse(); 481 | 482 | if(nestedCommentCount!=0){ 483 | errorFlag=1; 484 | printf("%s : %d : Comment Does Not End\n",sourceCode,lineCount); 485 | 486 | } 487 | if(commentFlag==1){ 488 | errorFlag=1; 489 | printf("%s : %d : Nested Comment\n",sourceCode,lineCount); 490 | } 491 | 492 | 493 | 494 | if(!errorFlag && !semanticErr && arrayIndexErr!=1){ 495 | 496 | printf("\n\n\t\t%s Parsing Completed\n\n",sourceCode); 497 | 498 | 499 | 500 | FILE *writeParsed=fopen("parsedTable.txt","w"); 501 | fprintf(writeParsed,"\n\t\t\t\tParsed Table\n\n\t\tToken\t\t\t\t\t\tType\t\t\t\t\t\t\tLine Number\n"); 502 | for(tokenList *ptr=parsedPtr;ptr!=NULL;ptr=ptr->next){ 503 | fprintf(writeParsed,"\n%20s%30.30s%60s",ptr->token,ptr->type,ptr->line); 504 | } 505 | FILE *writeSymbol=fopen("symbolTable.txt","w"); 506 | fprintf(writeSymbol,"\n\t\t\t\tSymbolTable\n\n\t\tToken\t\t\t\tType\t\tLine Number\t\t\t\tScope\t\tFunction Number\n"); 507 | for(tokenList *ptr=symbolPtr;ptr!=NULL;ptr=ptr->next){ 508 | fprintf(writeSymbol,"\n%20s%30.30s%30s%30s\t%d\t%d",ptr->token,ptr->type,ptr->line,ptr->scope,ptr->scopeValue,ptr->funcCount+1); 509 | } 510 | 511 | FILE *writeConstant=fopen("constantTable.txt","w"); 512 | fprintf(writeConstant,"\n \t\t\t\t\t\t\t\tConstant Table \n\n \t\t\t\t\t\tValue\t\t\t\t\t\t\tLine Number\n"); 513 | for(tokenList *ptr=constantPtr;ptr!=NULL;ptr=ptr->next) 514 | fprintf(writeConstant,"\n%50s%60s",ptr->token,ptr->line); 515 | 516 | 517 | fclose(writeSymbol); 518 | fclose(writeConstant); 519 | fclose(writeParsed); 520 | } 521 | printf("\n\n"); 522 | } 523 | 524 | 525 | --------------------------------------------------------------------------------