├── phase-3 ├── jllvm.jar ├── Decaf Compiler.pdf ├── build.sh ├── build.sh~ ├── src │ ├── NameTypePair.java │ ├── ParserVal.java │ ├── Visitor.java │ ├── decaf.l │ ├── parser.y │ ├── Ast.java │ ├── Yylex.java │ └── VisitorImpl.java ├── test-cases │ ├── factorial.decaf │ ├── fibonacci.decaf │ ├── checkprime.decaf │ ├── arrayDotProd.decaf │ ├── findmin.decaf │ ├── findmax.decaf │ ├── arrayAddition.decaf │ ├── bubbleSort.decaf │ ├── binarySearch.decaf │ ├── arithDemo.decaf │ ├── mergesort.decaf │ └── test-cases ├── readme ├── run.sh ├── jLLVMInstallation.txt └── classhierarchy.txt ├── phase-1 ├── C Impl │ ├── readme │ ├── parser.y │ └── decaf.l └── Java Impl │ ├── parser.y │ └── decaf.l └── phase-2 ├── readme ├── Visitor.java ├── decaf.l ├── VisitorImpl_bak.java ├── VisitorImpl.java ├── parser.y └── Ast.java /phase-3/jllvm.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vspandan/DecafCompiler/master/phase-3/jllvm.jar -------------------------------------------------------------------------------- /phase-3/Decaf Compiler.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vspandan/DecafCompiler/master/phase-3/Decaf Compiler.pdf -------------------------------------------------------------------------------- /phase-1/C Impl/readme: -------------------------------------------------------------------------------- 1 | flex lexer.l 2 | bison -d -v parser.y 3 | cc -o decaf parser.tab.c lex.yy.c -ly -ll 4 | 5 | -------------------------------------------------------------------------------- /phase-3/build.sh: -------------------------------------------------------------------------------- 1 | dirr=bin 2 | if [ ! -d $dirr ]; then 3 | mkdir bin 4 | fi 5 | javac -cp jllvm.jar -d bin/ src/*.java 6 | -------------------------------------------------------------------------------- /phase-3/build.sh~: -------------------------------------------------------------------------------- 1 | dir=bin 2 | if [ ! -d "/bin"]; then 3 | mkdir bin 4 | else 5 | javac -cp jllvm.jar -d bin/ src/*.java 6 | fi 7 | -------------------------------------------------------------------------------- /phase-2/readme: -------------------------------------------------------------------------------- 1 | Implemented "visitor design pattern" 2 | 3 | Require: jflex and baccj (Java Implementation) 4 | 5 | Steps to run: 6 | 7 | jflex decaf.l 8 | byaccj -J parser.y 9 | javac Ast.java 10 | javac Parser.java 11 | java Parser 12 | -------------------------------------------------------------------------------- /phase-3/src/NameTypePair.java: -------------------------------------------------------------------------------- 1 | import org.jllvm.LLVMType; 2 | 3 | public class NameTypePair{ 4 | String name=null; 5 | LLVMType type=null; 6 | public NameTypePair(String name, LLVMType type) { 7 | this.name=name; 8 | this.type=type; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /phase-3/test-cases/factorial.decaf: -------------------------------------------------------------------------------- 1 | class factorial{ 2 | int fact(int n){ 3 | int f; 4 | f=1; 5 | for i=0, n { 6 | f=f*(i+1); 7 | } 8 | return f; 9 | } 10 | int main(){ 11 | callout("printf","%d", fact(5)); 12 | return 0; 13 | } 14 | } 15 | 16 | 17 | -------------------------------------------------------------------------------- /phase-3/test-cases/fibonacci.decaf: -------------------------------------------------------------------------------- 1 | class p{ 2 | 3 | int main() 4 | { 5 | int n,a,b,c; 6 | a = 0; 7 | b = 1; 8 | n = 10; 9 | for i=0,n { 10 | if(i<=1) { 11 | c = i; 12 | } 13 | else { 14 | c = a + b ; 15 | a = b; 16 | b = c; 17 | } 18 | callout ("printf", " -%d ",c); 19 | } 20 | return 0; 21 | } 22 | } 23 | 24 | -------------------------------------------------------------------------------- /phase-3/test-cases/checkprime.decaf: -------------------------------------------------------------------------------- 1 | class p { 2 | int main() 3 | { 4 | int n, i, ind; 5 | ind=0; 6 | n=29; 7 | for i=3,(n/2+1){ 8 | if((n%i)==0) 9 | { 10 | ind=1; 11 | break; 12 | } 13 | } 14 | if (ind==0) { 15 | callout("printf","%d is prime",n); 16 | } 17 | else { 18 | callout("printf","%d is not prime",n); 19 | } 20 | return 0; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /phase-3/test-cases/arrayDotProd.decaf: -------------------------------------------------------------------------------- 1 | class arrayDotProd{ 2 | int a[5],b[5]; 3 | int main(){ 4 | int n,sum; 5 | n=5; 6 | 7 | a[0]=1; 8 | a[1]=2; 9 | a[2]=3; 10 | a[3]=4; 11 | a[4]=5; 12 | b[0]=6; 13 | b[1]=7; 14 | b[2]=8; 15 | b[3]=9; 16 | b[4]=10; 17 | 18 | for i=0,n{ 19 | sum += a[i]*b[i]; 20 | } 21 | 22 | callout("printf","Dot Product %d",sum); 23 | return 0; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /phase-3/test-cases/findmin.decaf: -------------------------------------------------------------------------------- 1 | class findmin { 2 | 3 | 4 | int b[5]; 5 | int main() 6 | { 7 | int min, size; 8 | size=5; 9 | b[0]=60; 10 | b[1]=97; 11 | b[2]=88; 12 | b[3]=9; 13 | b[4]=10; 14 | 15 | 16 | min = b[0]; 17 | 18 | for c = 1, size 19 | { 20 | 21 | if (min > b[c]) 22 | { 23 | min = b[c]; 24 | 25 | 26 | } 27 | } 28 | 29 | callout("printf","Minimum element %d.\n",min); 30 | return 0; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /phase-3/test-cases/findmax.decaf: -------------------------------------------------------------------------------- 1 | class findmax { 2 | 3 | 4 | int b[5]; 5 | int main() 6 | { 7 | int maximum, size; 8 | size=5; 9 | b[0]=60; 10 | b[1]=97; 11 | b[2]=88; 12 | b[3]=90; 13 | b[4]=10; 14 | 15 | 16 | maximum = b[0]; 17 | 18 | for c = 1, size 19 | { 20 | 21 | if (b[c] > maximum) 22 | { 23 | maximum = b[c]; 24 | 25 | 26 | } 27 | } 28 | 29 | callout("printf","Maximum element %d.\n",maximum); 30 | return 0; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /phase-3/test-cases/arrayAddition.decaf: -------------------------------------------------------------------------------- 1 | class arrayAddition{ 2 | int a[5],b[5],c[5]; 3 | int main(){ 4 | int n; 5 | n=5; 6 | 7 | a[0]=1; 8 | a[1]=2; 9 | a[2]=3; 10 | a[3]=4; 11 | a[4]=5; 12 | b[0]=6; 13 | b[1]=7; 14 | b[2]=8; 15 | b[3]=9; 16 | b[4]=10; 17 | 18 | for i=0,(n-1) { 19 | c[i] = a[i]+b[i]; 20 | } 21 | 22 | for i=0,(n-1){ 23 | callout ("printf", " The sum of %d & %d is %d ",a[i],b[i],c[i]); 24 | } 25 | return 0; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /phase-3/test-cases/bubbleSort.decaf: -------------------------------------------------------------------------------- 1 | class bubblesort{ 2 | int arr[30]; 3 | int main() { 4 | int num, temp; 5 | num=10; 6 | arr[0]=90; 7 | arr[1]=87; 8 | arr[2]=109; 9 | arr[3]=73; 10 | arr[4]=38; 11 | arr[5]=92; 12 | arr[6]=73; 13 | arr[7]=98; 14 | arr[8]=100; 15 | arr[9]=10; 16 | 17 | 18 | for i = 1, num{ 19 | for j = 0,( num - 1){ 20 | if (arr[j] > arr[j + 1]) { 21 | temp = arr[j]; 22 | arr[j] = arr[j + 1]; 23 | arr[j + 1] = temp; 24 | } 25 | } 26 | 27 | } 28 | 29 | for d=0,num{ 30 | callout ("printf", " -%d- ",arr[d]); 31 | } 32 | return 0; 33 | 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /phase-3/readme: -------------------------------------------------------------------------------- 1 | Implemented "visitor design pattern" 2 | 3 | Require: jflex,baccj,jllvm,LLVM3.2 (Java Implementation) 4 | Read jLLVMInstallation.txt for installation steps 5 | 6 | Generate scanner and parser output files 7 | 8 | Flex: jflex decaf.l 9 | YACC: byaccj -J parser.y 10 | 11 | Steps to Compile programs: 12 | 13 | build.sh --- compiles the source 14 | run.sh [-llvm-ir] [] 15 | 16 | To build and compile manually: 17 | 18 | javac -cp "../jllvm.jar" -d ../bin *.java 19 | java -cp ".:../jllvm.jar" Parser [] 20 | llvm-dis -f decaf.output -o 21 | lli 22 | 23 | To use clang for generating LLVM-IR: 24 | clang s.c -S -emit-llvm 25 | -------------------------------------------------------------------------------- /phase-3/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | llvmir=0 3 | option=-llvm-ir 4 | output1=decaf.out 5 | output2=decaf.s 6 | 7 | cd bin 8 | 9 | if [ -s $output2 ] 10 | then 11 | rm $output2 12 | fi 13 | 14 | if [ "$#" -ne 0 ] 15 | then 16 | if [ $1 = $option ] 17 | then 18 | llvmir=1 19 | if [ "$#" -eq 2 ] 20 | then 21 | java -cp ".:../jllvm.jar" Parser ../"$2" 22 | else 23 | java -cp ".:../jllvm.jar" Parser 24 | fi 25 | else 26 | java -cp ".:../jllvm.jar" Parser ../"$1" 27 | fi 28 | else 29 | java -cp ".:../jllvm.jar" Parser 30 | fi 31 | 32 | if [ -s $output1 ] 33 | then 34 | llvm-dis -f decaf.out -o decaf.s 35 | 36 | if [ $llvmir -eq 1 ]; 37 | then 38 | cat decaf.s 39 | echo "Press any key to continue ... " 40 | read tmp 41 | fi 42 | lli decaf.s 43 | fi 44 | cd .. 45 | -------------------------------------------------------------------------------- /phase-3/test-cases/binarySearch.decaf: -------------------------------------------------------------------------------- 1 | class binarySearch{ 2 | int a[7]; 3 | int main(){ 4 | int last, middle, n, search,first; 5 | n=7; 6 | 7 | a[0]=100; 8 | a[1]=24; 9 | a[2]=30; 10 | a[3]=40; 11 | a[4]=25; 12 | a[5]=85; 13 | a[6]=75; 14 | search = 85; 15 | 16 | 17 | last = n - 1; 18 | middle = (first+last)/2; 19 | 20 | for first =0, (last+1 ) 21 | { 22 | if ( a[middle] < search ){ 23 | first = middle + 1; 24 | } 25 | 26 | if ( a[middle] == search ) 27 | { 28 | callout ("printf","%d found at location %d.\n", search, middle+1); 29 | break; 30 | } 31 | if(a[middle] > search){ 32 | last = middle - 1; 33 | } 34 | middle = (first + last)/2; 35 | } 36 | 37 | 38 | return 0; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /phase-3/jLLVMInstallation.txt: -------------------------------------------------------------------------------- 1 | 2 | Download Following packages 3 | 4 | llvm-3.2 5 | clang-3.2(optional) 6 | compiler-rt-3.2(optional) 7 | jllvm3.2 8 | (http://code.google.com/p/jllvm/) 9 | cmake(if not installed before) 10 | 11 | 12 | Move Clang and Compiler-RT in following relative locations 13 | 14 | clang-> llvm-3.2/tools/ 15 | compiler-rt ->/llvm-3.2/projects/ 16 | 17 | Configure LLVM 18 | ./configure 19 | 20 | Compile LLVM source files using make 21 | makei 22 | 23 | Add llvm bin path to env variables 24 | gedit ~/.bashrc 25 | export PATH=$PATH:~/llvm/build/Release+Asserts/bin 26 | 27 | Add LLVM libraries 28 | gedit /etc/ld.so.conf.d/llvm.conf 29 | /home//llvm/build/Release+Asserts/lib 30 | 31 | Build JLLVM 32 | build packages using ant 33 | ant build 34 | 35 | Copy libjllvm.so to system library locations 36 | one of the following location 37 | /usr/lib 38 | /us/local/lib 39 | 40 | Use jllvm.jar for building compiler source files 41 | -------------------------------------------------------------------------------- /phase-3/test-cases/arithDemo.decaf: -------------------------------------------------------------------------------- 1 | class arithDemo 2 | { 3 | int main () 4 | { 5 | int a, b, c; 6 | 7 | a = 10 + 20; 8 | callout("printf", "10 + 20 is %d (30)\n", a); 9 | 10 | a = 10 - 20; 11 | callout("printf", "10 - 20 is %d (-10)\n", a); 12 | 13 | a = 10 * 20; 14 | callout("printf", "10 * 20 is %d (200)\n", a); 15 | 16 | a = 1; 17 | b = 2; 18 | c = 2; 19 | 20 | if (a < b) { callout("printf", "a < b is correct\n"); } 21 | if (a <= b) { callout("printf", "a <= b is correct\n"); } 22 | if (a > b) { callout("printf", "a > b is incorrect\n"); } 23 | if (a >= b) { callout("printf", "a >= b is incorrect\n"); } 24 | 25 | if (c < b) { callout("printf", "c < b is incorrect\n"); } 26 | if (c <= b) { callout("printf", "c <= b is correct\n"); } 27 | if (c > b) { callout("printf", "c > b is incorrect\n"); } 28 | if (c >= b) { callout("printf", "c >= b is correct\n"); } 29 | 30 | 31 | 32 | 33 | return 0; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /phase-3/test-cases/mergesort.decaf: -------------------------------------------------------------------------------- 1 | class merget{ 2 | int arr[10]; int temp[10]; 3 | 4 | void mergeSort(int low,int mid,int high){ 5 | 6 | int i,m,k,l; 7 | 8 | l=low; 9 | i=low; 10 | m=mid+1; 11 | 12 | for tmp=0,999{ 13 | if((l<=mid)&&(m<=high)){ 14 | 15 | if(arr[l]<=arr[m]){ 16 | temp[i]=arr[l]; 17 | l+=1; 18 | } 19 | else{ 20 | temp[i]=arr[m]; 21 | m+=1; 22 | } 23 | i+=1; 24 | } 25 | } 26 | 27 | if(l>mid){ 28 | k=m; 29 | for tmp1=0,999{ 30 | if(k<=high){ 31 | temp[i]=arr[k]; 32 | i+=1; 33 | k+=1; 34 | } 35 | } 36 | } 37 | else{ 38 | k=l; 39 | for tmp1=0,999{ 40 | if(k<=mid){ 41 | temp[i]=arr[k]; 42 | i+=1; 43 | k+=1; 44 | } 45 | } 46 | } 47 | 48 | for tmp2=low,(high+1){ 49 | arr[tmp2]=temp[tmp2]; 50 | } 51 | } 52 | void partition(int low,int high){ 53 | 54 | int mid; 55 | 56 | if(low Decaf_Class 3 | ------> DeclarationsIntf(A) 4 | ------> FieldDeclIntf(A) 5 | ------> FieldDecl 6 | ------> MethodDeclarations(A) 7 | ------> MethodDeclarations1 8 | ------> MethodDeclarations2 9 | 10 | Var_Decl 11 | 12 | Fields(A) 13 | ------> Field 14 | ------> Fields1 15 | 16 | Arguement(A) 17 | ------> Arguement1 18 | ------> Arguement2 19 | 20 | Type 21 | ------> IntegerType 22 | ------> BooleanType 23 | ------> Void 24 | 25 | ExpressionIntf(A) 26 | ------> StatementIntf(A) 27 | -----> Statement1 28 | -----> ReturnStatemen1 29 | -----> ReturnStatement2 30 | -----> BreakStatement 31 | -----> ContinueStatement 32 | -----> IfStatement 33 | -----> ElseStatement 34 | -----> ForStatement 35 | -----> Block(A) 36 | ------> Block1 37 | ------> BLock2 38 | ------> Block3 39 | ------> Block4 40 | -----> MethodStatement1 41 | -----> MethodStatement2 42 | -----> MethodStatementCallOut1 43 | -----> MethodStatementCallOut2 44 | 45 | -----> callout_args(A) 46 | ------> callout_arg 47 | ------> callout_arg1 48 | -----> Location(A) 49 | ------> Location1 50 | ------> Location2 51 | -----> Expression1 52 | -----> Expression2 53 | -----> Expression3 54 | -----> Literal 55 | ------> IntegerLiteral 56 | ------> BooleanLiteral 57 | ------> StringLiteral 58 | 59 | 60 | 61 | 62 | Class 63 | Identifier 64 | IF 65 | Else 66 | Return 67 | Break 68 | Continue 69 | For 70 | Callout 71 | 72 | -------------------------------------------------------------------------------- /phase-3/src/ParserVal.java: -------------------------------------------------------------------------------- 1 | //############################################# 2 | //## file: Parser.java 3 | //## Generated by Byacc/j 4 | //############################################# 5 | /** 6 | * BYACC/J Semantic Value for parser: Parser 7 | * This class provides some of the functionality 8 | * of the yacc/C 'union' directive 9 | */ 10 | public class ParserVal 11 | { 12 | /** 13 | * integer value of this 'union' 14 | */ 15 | public int ival; 16 | 17 | /** 18 | * double value of this 'union' 19 | */ 20 | public double dval; 21 | 22 | /** 23 | * string value of this 'union' 24 | */ 25 | public String sval; 26 | 27 | /** 28 | * object value of this 'union' 29 | */ 30 | public Object obj; 31 | 32 | //############################################# 33 | //## C O N S T R U C T O R S 34 | //############################################# 35 | /** 36 | * Initialize me without a value 37 | */ 38 | public ParserVal() 39 | { 40 | } 41 | /** 42 | * Initialize me as an int 43 | */ 44 | public ParserVal(int val) 45 | { 46 | ival=val; 47 | } 48 | 49 | /** 50 | * Initialize me as a double 51 | */ 52 | public ParserVal(double val) 53 | { 54 | dval=val; 55 | } 56 | 57 | /** 58 | * Initialize me as a string 59 | */ 60 | public ParserVal(String val) 61 | { 62 | sval=val; 63 | } 64 | 65 | /** 66 | * Initialize me as an Object 67 | */ 68 | public ParserVal(Object val) 69 | { 70 | obj=val; 71 | } 72 | }//end class 73 | 74 | //############################################# 75 | //## E N D O F F I L E 76 | //############################################# 77 | -------------------------------------------------------------------------------- /phase-2/Visitor.java: -------------------------------------------------------------------------------- 1 | 2 | public interface Visitor { 3 | 4 | void visit(Decaf_Class decaf_Class); 5 | 6 | void visit(Declarations1 declarations1); 7 | 8 | void visit(FieldDecl fieldDecl); 9 | 10 | void visit(Fields1 fields1); 11 | 12 | void visit(Identifier identifier); 13 | 14 | void visit(Field field); 15 | 16 | void visit(MethodDeclaration1 methodDeclaration1); 17 | 18 | void visit(MethodDeclaration2 methodDeclaration2); 19 | 20 | void visit(Arguement1 arguement1); 21 | 22 | void visit(Var_Decl var_Decl); 23 | 24 | void visit(Statement1 statement1); 25 | 26 | void visit(ReturnStatement returnStatement); 27 | 28 | void visit(ReturnStatement1 returnStatement1); 29 | 30 | void visit(BreakStatement breakStatement); 31 | 32 | void visit(ContinueStatement continueStatement); 33 | 34 | void visit(IfStatement ifStatement); 35 | 36 | void visit(IfElseStatement ifElseStatement); 37 | 38 | void visit(ForStatement forStatement); 39 | 40 | void visit(IntegerType integerType); 41 | 42 | void visit(BooleanType booleanType); 43 | 44 | void visit(Void void1); 45 | 46 | void visit(callout_arg callout_arg); 47 | 48 | void visit(MethodStatement1 methodStatement1); 49 | 50 | void visit(MethodStatement2 methodStatement2); 51 | 52 | void visit(MethodStatementCallOut1 methodStatementCallOut1); 53 | 54 | void visit(MethodStatementCallOut2 methodStatementCallOut2); 55 | 56 | void visit(Expression1 expression1); 57 | 58 | void visit(Expression2 expression2); 59 | 60 | void visit(Expression3 expression3); 61 | 62 | void visit(IntegerLiteral integerLiteral); 63 | 64 | void visit(BooleanLiteral booleanLiteral); 65 | 66 | void visit(Return return1); 67 | 68 | void visit(Break break1); 69 | 70 | void visit(Continue continue1); 71 | 72 | void visit(Class class1); 73 | 74 | void visit(ELSE else1); 75 | 76 | void visit(IF if1); 77 | 78 | void visit(For for1); 79 | 80 | void visit(StringLiteral stringLiteral); 81 | 82 | void visit(CallOut callOut); 83 | 84 | void visit(Location2 location2); 85 | 86 | void visit(Location1 location1); 87 | 88 | void visit(Field1 field1); 89 | 90 | void visit(Block1 block); 91 | 92 | void visit(Block2 block2); 93 | 94 | void visit(Block3 block3); 95 | 96 | } 97 | 98 | -------------------------------------------------------------------------------- /phase-3/src/Visitor.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.HashMap; 3 | 4 | import org.jllvm.LLVMContext; 5 | import org.jllvm.LLVMFunction; 6 | import org.jllvm.LLVMInstructionBuilder; 7 | import org.jllvm.LLVMIntegerType; 8 | import org.jllvm.LLVMModule; 9 | import org.jllvm.LLVMValue; 10 | import org.jllvm.LLVMVoidType; 11 | 12 | 13 | public interface Visitor { 14 | 15 | public static final LLVMContext context = LLVMContext.getGlobalContext(); 16 | 17 | public static final LLVMModule module=new LLVMModule("Compilers Proj",context); 18 | 19 | public static final HashMap namedValues = new HashMap(); 20 | 21 | public static final ArrayList localVar = new ArrayList(); 22 | 23 | public static final LLVMInstructionBuilder builder = new LLVMInstructionBuilder(); 24 | 25 | LLVMValue visit(Decaf_Class decaf_Class); 26 | 27 | LLVMValue visit(Declarations1 declarations1); 28 | 29 | LLVMValue visit(FieldDecl fieldDecl); 30 | 31 | void visit(Fields1 fields1); 32 | 33 | String visit(Identifier identifier); 34 | 35 | void visit(Field field); 36 | 37 | LLVMFunction visit(MethodDeclaration1 methodDeclaration1); 38 | 39 | LLVMFunction visit(MethodDeclaration2 methodDeclaration2); 40 | 41 | NameTypePair visit(Arguement1 arguement1); 42 | 43 | void visit(Var_Decl var_Decl); 44 | 45 | LLVMValue visit(Statement1 statement1); 46 | 47 | LLVMValue visit(ReturnStatement returnStatement); 48 | 49 | LLVMValue visit(ReturnStatement1 returnStatement1); 50 | 51 | LLVMIntegerType visit(IntegerType integerType); 52 | 53 | LLVMIntegerType visit(BooleanType booleanType); 54 | 55 | LLVMVoidType visit(Void void1); 56 | 57 | LLVMValue visit(callout_arg callout_arg); 58 | 59 | LLVMValue visit(callout_arg1 callout_arg1); 60 | 61 | LLVMValue visit(IntegerLiteral integerLiteral); 62 | 63 | LLVMValue visit(BooleanLiteral booleanLiteral); 64 | 65 | String visit(Return return1); 66 | 67 | String visit(Break break1); 68 | 69 | String visit(Continue continue1); 70 | 71 | String visit(Class class1); 72 | 73 | String visit(ELSE else1); 74 | 75 | String visit(IF if1); 76 | 77 | String visit(For for1); 78 | 79 | String visit(StringLiteral stringLiteral); 80 | 81 | String visit(CallOut callOut); 82 | 83 | void visit(Field1 field1); 84 | 85 | LLVMValue visit(Block1 block); 86 | 87 | LLVMValue visit(Block2 block2); 88 | 89 | LLVMValue visit(Block3 block3); 90 | 91 | LLVMValue visit(IfStatement ifStatement); 92 | 93 | LLVMValue visit(IfElseStatement ifElseStatement); 94 | 95 | LLVMValue visit(Location1 location); 96 | 97 | LLVMValue visit(Expression1 expression1); 98 | 99 | LLVMValue visit(Expression2 expression2); 100 | 101 | LLVMValue visit(Expression3 expression3); 102 | 103 | LLVMValue visit(BreakStatement breakStatement); 104 | 105 | LLVMValue visit(ContinueStatement continueStatement); 106 | 107 | LLVMValue visit(Location2 location); 108 | 109 | LLVMValue visit(ForStatement forStatement); 110 | 111 | LLVMValue visit(MethodStatement1 methodStatement1); 112 | 113 | LLVMValue visit(MethodStatement2 methodStatement2); 114 | 115 | LLVMValue visit(MethodStatementCallOut1 methodStatementCallOut1); 116 | 117 | LLVMValue visit(MethodStatementCallOut2 methodStatementCallOut2); 118 | 119 | 120 | 121 | } 122 | 123 | -------------------------------------------------------------------------------- /phase-1/C Impl/parser.y: -------------------------------------------------------------------------------- 1 | %{ 2 | 3 | import java.lang.Math; 4 | import java.io.*; 5 | import java.util.*; 6 | 7 | %} 8 | 9 | %token INT BOOLEAN IF ELSE FOR RETURN BREAK CONTINUE CLASS VOID CALLOUT TRUE FALSE 10 | 11 | %token ASSIGN_OP ID STRING_LITERAL ARITH_OP REL_OP EQ_OP COND_OP INT_LITERAL E_ASSIGN_OP MINUS Program 12 | 13 | %left '-' '!' ARITH_OP REL_OP EQ_OP COND_OP 14 | 15 | %% 16 | 17 | program : CLASS ID '{' declarations '}' 18 | 19 | 20 | declarations : type fields ';' declarations 21 | | method_decl; 22 | ; 23 | 24 | 25 | 26 | fields : field 27 | | field ',' fields 28 | ; 29 | 30 | field : ID 31 | | ID '[' INT_LITERAL ']' 32 | ; 33 | 34 | method_decl : type ID '(' args_decl ')' block method_decl 35 | | VOID ID '(' args_decl ')' block method_decl 36 | | 37 | ; 38 | 39 | args_decl :arg ',' args_decl 40 | | arg 41 | | 42 | ; 43 | 44 | arg : type ID ; 45 | ; 46 | 47 | vars : ID ';' 48 | | ID ',' vars 49 | ; 50 | 51 | var_decl : type vars 52 | ; 53 | 54 | var_decls : var_decl var_decls 55 | | var_decl 56 | ; 57 | 58 | block : '{' block_body '}' 59 | ; 60 | 61 | statements : statement 62 | | statement statements 63 | 64 | block_body : var_decls statements 65 | | var_decls 66 | | statements 67 | | 68 | ; 69 | 70 | type : INT 71 | | BOOLEAN 72 | ; 73 | 74 | statement: location ASSGN_OP expr ';' 75 | | method_call ';' 76 | | IF '(' expr ')' block ELSE block 77 | | IF '(' expr ')' block 78 | | FOR ID E_ASSIGN_OP expr ',' expr block 79 | | RETURN ';' 80 | | RETURN expr ';' 81 | | BREAK ';' 82 | | CONTINUE ';' 83 | | block 84 | 85 | ASSGN_OP : ASSIGN_OP 86 | | E_ASSIGN_OP 87 | ; 88 | 89 | exprs : expr 90 | | expr ',' exprs 91 | ; 92 | 93 | callout_args : callout_arg 94 | | callout_arg ',' callout_args 95 | ; 96 | 97 | method_call : method_name '(' ')' 98 | | method_name '(' exprs ')' 99 | | CALLOUT '(' STRING_LITERAL ',' callout_args ')' 100 | | CALLOUT '(' STRING_LITERAL ')' 101 | ; 102 | 103 | method_name : ID ; 104 | 105 | location : ID 106 | | ID '[' expr ']' 107 | ; 108 | 109 | ARTH_OP : ARITH_OP 110 | | MINUS 111 | ; 112 | 113 | expr : expr ARTH_OP term1 114 | | term1 115 | ; 116 | 117 | term1 : term1 REL_OP term2 118 | | term2 119 | ; 120 | 121 | 122 | term2 : term2 EQ_OP term3 123 | | term3 124 | ; 125 | 126 | 127 | term3 : term3 COND_OP term4 128 | | term4 129 | ; 130 | 131 | term4 : location 132 | | method_call 133 | | literal 134 | | MINUS term4 135 | | '!' term4 136 | | '(' expr ')' 137 | ; 138 | 139 | callout_arg : expr 140 | | STRING_LITERAL 141 | ; 142 | 143 | bool_literal : TRUE 144 | | FALSE 145 | ; 146 | 147 | literal : INT_LITERAL 148 | // | CHAR_LITERAL 149 | | bool_literal 150 | ; 151 | 152 | %% 153 | 154 | 155 | 156 | private Yylex lexer; 157 | 158 | 159 | private int yylex () { 160 | int yyl_return = -1; 161 | try { 162 | yyl_return = lexer.yylex(); 163 | } 164 | catch (IOException e) { 165 | System.err.println("IO error :"+e); 166 | } 167 | return yyl_return; 168 | } 169 | 170 | 171 | public void yyerror (String error) { 172 | System.err.println ("Error: " + error); 173 | } 174 | 175 | 176 | public Parser(Reader r) { 177 | lexer = new Yylex(r, this); 178 | } 179 | 180 | 181 | public static void main(String args[]) throws IOException { 182 | 183 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 184 | Parser yyparser = new Parser(br); 185 | yyparser.yyparse(); 186 | 187 | } 188 | -------------------------------------------------------------------------------- /phase-1/Java Impl/parser.y: -------------------------------------------------------------------------------- 1 | %{ 2 | 3 | import java.lang.Math; 4 | import java.io.*; 5 | import java.util.*; 6 | 7 | %} 8 | 9 | %token INT BOOLEAN IF ELSE FOR RETURN BREAK CONTINUE CLASS VOID CALLOUT TRUE FALSE 10 | 11 | %token ASSIGN_OP ID STRING_LITERAL ARITH_OP REL_OP EQ_OP COND_OP INT_LITERAL E_ASSIGN_OP MINUS Program 12 | 13 | %left '-' '!' ARITH_OP REL_OP EQ_OP COND_OP 14 | 15 | %% 16 | 17 | program : CLASS ID '{' declarations '}' 18 | 19 | 20 | declarations : type fields ';' declarations 21 | | method_decl; 22 | ; 23 | 24 | 25 | 26 | fields : field 27 | | field ',' fields 28 | ; 29 | 30 | field : ID 31 | | ID '[' INT_LITERAL ']' 32 | ; 33 | 34 | method_decl : type ID '(' args_decl ')' block method_decl 35 | | VOID ID '(' args_decl ')' block method_decl 36 | | 37 | ; 38 | 39 | args_decl :arg ',' args_decl 40 | | arg 41 | | 42 | ; 43 | 44 | arg : type ID ; 45 | ; 46 | 47 | vars : ID ';' 48 | | ID ',' vars 49 | ; 50 | 51 | var_decl : type vars 52 | ; 53 | 54 | var_decls : var_decl var_decls 55 | | var_decl 56 | ; 57 | 58 | block : '{' block_body '}' 59 | ; 60 | 61 | statements : statement 62 | | statement statements 63 | 64 | block_body : var_decls statements 65 | | var_decls 66 | | statements 67 | | 68 | ; 69 | 70 | type : INT 71 | | BOOLEAN 72 | ; 73 | 74 | statement: location ASSGN_OP expr ';' 75 | | method_call ';' 76 | | IF '(' expr ')' block ELSE block 77 | | IF '(' expr ')' block 78 | | FOR ID E_ASSIGN_OP expr ',' expr block 79 | | RETURN ';' 80 | | RETURN expr ';' 81 | | BREAK ';' 82 | | CONTINUE ';' 83 | | block 84 | 85 | ASSGN_OP : ASSIGN_OP 86 | | E_ASSIGN_OP 87 | ; 88 | 89 | exprs : expr 90 | | expr ',' exprs 91 | ; 92 | 93 | callout_args : callout_arg 94 | | callout_arg ',' callout_args 95 | ; 96 | 97 | method_call : method_name '(' ')' 98 | | method_name '(' exprs ')' 99 | | CALLOUT '(' STRING_LITERAL ',' callout_args ')' 100 | | CALLOUT '(' STRING_LITERAL ')' 101 | ; 102 | 103 | method_name : ID ; 104 | 105 | location : ID 106 | | ID '[' expr ']' 107 | ; 108 | 109 | ARTH_OP : ARITH_OP 110 | | MINUS 111 | ; 112 | 113 | expr : expr ARTH_OP term1 114 | | term1 115 | ; 116 | 117 | term1 : term1 REL_OP term2 118 | | term2 119 | ; 120 | 121 | 122 | term2 : term2 EQ_OP term3 123 | | term3 124 | ; 125 | 126 | 127 | term3 : term3 COND_OP term4 128 | | term4 129 | ; 130 | 131 | term4 : location 132 | | method_call 133 | | literal 134 | | MINUS term4 135 | | '!' term4 136 | | '(' expr ')' 137 | ; 138 | 139 | callout_arg : expr 140 | | STRING_LITERAL 141 | ; 142 | 143 | bool_literal : TRUE 144 | | FALSE 145 | ; 146 | 147 | literal : INT_LITERAL 148 | // | CHAR_LITERAL 149 | | bool_literal 150 | ; 151 | 152 | %% 153 | 154 | 155 | 156 | private Yylex lexer; 157 | 158 | 159 | private int yylex () { 160 | int yyl_return = -1; 161 | try { 162 | yyl_return = lexer.yylex(); 163 | } 164 | catch (IOException e) { 165 | System.err.println("IO error :"+e); 166 | } 167 | return yyl_return; 168 | } 169 | 170 | 171 | public void yyerror (String error) { 172 | System.err.println ("Error: " + error); 173 | } 174 | 175 | 176 | public Parser(Reader r) { 177 | lexer = new Yylex(r, this); 178 | } 179 | 180 | 181 | public static void main(String args[]) throws IOException { 182 | 183 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 184 | Parser yyparser = new Parser(br); 185 | yyparser.yyparse(); 186 | 187 | } 188 | -------------------------------------------------------------------------------- /phase-1/C Impl/decaf.l: -------------------------------------------------------------------------------- 1 | %% 2 | 3 | %byaccj 4 | 5 | %{ 6 | /* store a reference to the parser object */ 7 | Parser yyparser; 8 | 9 | /* constructor taking an additional parser object */ 10 | public Yylex(java.io.Reader r, Parser yyparser) { 11 | this(r); 12 | this.yyparser = yyparser; 13 | } 14 | %} 15 | 16 | 17 | DIGIT = [0-9] 18 | HEX_DIGIT = [0-9a-fA-F] 19 | HEX_INTEGER = 0[Xx]{HEX_DIGIT}+ 20 | INTEGER = {DIGIT}+ 21 | EXPONENT = [Ee][-+]?{INTEGER} 22 | DOUBLE = {INTEGER}"."{DIGIT}*{EXPONENT}? 23 | STRING = "\".*?\"" 24 | IDENTIFIER = [a-zA-Z][a-zA-Z_0-9]* 25 | ARITH_OP = ([+/*%]) 26 | REL_OP = [<|>] 27 | WHITE_SPACE_CHAR=[\ \n\r\t\f] 28 | 29 | %% 30 | "boolean" { yyparser.yylval=new ParserVal(yytext());return yyparser.BOOLEAN; } 31 | "break" { yyparser.yylval=new ParserVal(yytext()); return yyparser.BREAK; } 32 | "callout" { yyparser.yylval=new ParserVal(yytext()); return yyparser.CALLOUT; } 33 | "class" { yyparser.yylval=new ParserVal(yytext()); return yyparser.CLASS; } 34 | "true" { yyparser.yylval=new ParserVal(yytext()); return yyparser.TRUE; } 35 | "false" { yyparser.yylval=new ParserVal(yytext()); return yyparser.FALSE; } 36 | "for" { yyparser.yylval=new ParserVal(yytext()); return yyparser.FOR; } 37 | "if" { yyparser.yylval=new ParserVal(yytext()); return yyparser.IF; } 38 | "else" { yyparser.yylval=new ParserVal(yytext()); return yyparser.ELSE; } 39 | "return" { yyparser.yylval=new ParserVal(yytext()); return yyparser.RETURN; } 40 | "void" { yyparser.yylval=new ParserVal(yytext()); return yyparser.VOID; } 41 | "int" { yyparser.yylval=new ParserVal(yytext()); return yyparser.INT; } 42 | "continue" { yyparser.yylval=new ParserVal(yytext()); return yyparser.CONTINUE; } 43 | //"Program" { yyparser.yylval=new ParserVal(yytext()); return yyparser.Program; } 44 | {STRING} { yyparser.yylval=new ParserVal(yytext()); return yyparser.STRING_LITERAL; } 45 | {INTEGER} { yyparser.yylval=new ParserVal(Integer.parseInt(yytext())); return yyparser.INT_LITERAL; } 46 | {DOUBLE} { yyparser.yylval=new ParserVal(Double.parseDouble(yytext())); return yyparser.INT_LITERAL; } 47 | {HEX_INTEGER} { yyparser.yylval=new ParserVal(Integer.parseInt(yytext(),16)); return yyparser.INT_LITERAL; } 48 | {IDENTIFIER} { yyparser.yylval=new ParserVal(yytext()); return yyparser.ID; } 49 | "=" { yyparser.yylval=new ParserVal(yytext()); return yyparser.E_ASSIGN_OP; } 50 | "-=" { yyparser.yylval=new ParserVal(yytext()); return yyparser.ASSIGN_OP; } 51 | "+=" { yyparser.yylval=new ParserVal(yytext()); return yyparser.ASSIGN_OP; } 52 | "<=" { yyparser.yylval=new ParserVal(yytext()); return yyparser.EQ_OP; } 53 | ">=" { yyparser.yylval=new ParserVal(yytext()); return yyparser.EQ_OP;} 54 | "==" { yyparser.yylval=new ParserVal(yytext()); return yyparser.EQ_OP; } 55 | "!=" { yyparser.yylval=new ParserVal(yytext()); return yyparser.EQ_OP; } 56 | "&&" { yyparser.yylval=new ParserVal(yytext()); return yyparser.COND_OP; } 57 | "||" { yyparser.yylval=new ParserVal(yytext()); return yyparser.COND_OP; } 58 | {REL_OP} { yyparser.yylval=new ParserVal(yytext()); return yyparser.REL_OP; } 59 | {ARITH_OP} { yyparser.yylval=new ParserVal(yytext()); return yyparser.ARITH_OP; } 60 | "-" { yyparser.yylval=new ParserVal(yytext()); return yyparser.MINUS ; } 61 | "{" { yyparser.yylval=new ParserVal(yytext());return '{' ;} 62 | "}" { yyparser.yylval=new ParserVal(yytext()); return '}' ; } 63 | ";" { yyparser.yylval=new ParserVal(yytext()); return ';' ;} 64 | "[" { yyparser.yylval=new ParserVal(yytext()); return '[';} 65 | "]" { yyparser.yylval=new ParserVal(yytext()); return ']';} 66 | "(" { yyparser.yylval=new ParserVal(yytext()); return '(';} 67 | ")" { yyparser.yylval=new ParserVal(yytext()); return ')';} 68 | "," { yyparser.yylval=new ParserVal(yytext()); return ',';} 69 | "!" { yyparser.yylval=new ParserVal(yytext()); return '!';} 70 | {WHITE_SPACE_CHAR} { } -------------------------------------------------------------------------------- /phase-2/decaf.l: -------------------------------------------------------------------------------- 1 | %% 2 | 3 | %byaccj 4 | 5 | %{ 6 | /* store a reference to the parser object */ 7 | Parser yyparser; 8 | 9 | /* constructor taking an additional parser object */ 10 | public Yylex(java.io.Reader r, Parser yyparser) { 11 | this(r); 12 | this.yyparser = yyparser; 13 | } 14 | %} 15 | 16 | 17 | DIGIT = [0-9] 18 | HEX_DIGIT = [0-9a-fA-F] 19 | HEX_INTEGER = 0[Xx]{HEX_DIGIT}+ 20 | INTEGER = {DIGIT}+ 21 | EXPONENT = [Ee][-+]?{INTEGER} 22 | DOUBLE = {INTEGER}"."{DIGIT}*{EXPONENT}? 23 | STRING = \"[^\n\"]*\" 24 | IDENTIFIER = [a-zA-Z][a-zA-Z_0-9]* 25 | ARITH_OP = ([+/*%]) 26 | REL_OP = [<|>] 27 | WHITE_SPACE_CHAR=[\ \n\r\t\f] 28 | 29 | %% 30 | "boolean" { yyparser.yylval=new ParserVal(yytext());return yyparser.BOOLEAN; } 31 | "break" { yyparser.yylval=new ParserVal(yytext()); return yyparser.BREAK; } 32 | "callout" { yyparser.yylval=new ParserVal(yytext()); return yyparser.CALLOUT; } 33 | "class" { yyparser.yylval=new ParserVal(yytext()); return yyparser.CLASS; } 34 | "true" { yyparser.yylval=new ParserVal(yytext()); return yyparser.TRUE; } 35 | "false" { yyparser.yylval=new ParserVal(yytext()); return yyparser.FALSE; } 36 | "for" { yyparser.yylval=new ParserVal(yytext()); return yyparser.FOR; } 37 | "if" { yyparser.yylval=new ParserVal(yytext()); return yyparser.IF; } 38 | "else" { yyparser.yylval=new ParserVal(yytext()); return yyparser.ELSE; } 39 | "return" { yyparser.yylval=new ParserVal(yytext()); return yyparser.RETURN; } 40 | "void" { yyparser.yylval=new ParserVal(yytext()); return yyparser.VOID; } 41 | "int" { yyparser.yylval=new ParserVal(yytext()); return yyparser.INT; } 42 | "continue" { yyparser.yylval=new ParserVal(yytext()); return yyparser.CONTINUE; } 43 | //"Program" { yyparser.yylval=new ParserVal(yytext()); return yyparser.Program; } 44 | {STRING} { yyparser.yylval=new ParserVal(yytext()); return yyparser.STRING_LITERAL; } 45 | {INTEGER} { yyparser.yylval=new ParserVal(Integer.parseInt(yytext())); return yyparser.INT_LITERAL; } 46 | {DOUBLE} { yyparser.yylval=new ParserVal(Double.parseDouble(yytext())); return yyparser.INT_LITERAL; } 47 | {HEX_INTEGER} { yyparser.yylval=new ParserVal(Integer.parseInt(yytext(),16)); return yyparser.INT_LITERAL; } 48 | {IDENTIFIER} { yyparser.yylval=new ParserVal(yytext()); return yyparser.ID; } 49 | "=" { yyparser.yylval=new ParserVal(yytext()); return yyparser.E_ASSIGN_OP; } 50 | "-=" { yyparser.yylval=new ParserVal(yytext()); return yyparser.ASSIGN_OP; } 51 | "+=" { yyparser.yylval=new ParserVal(yytext()); return yyparser.ASSIGN_OP; } 52 | "<=" { yyparser.yylval=new ParserVal(yytext()); return yyparser.EQ_OP; } 53 | ">=" { yyparser.yylval=new ParserVal(yytext()); return yyparser.EQ_OP;} 54 | "==" { yyparser.yylval=new ParserVal(yytext()); return yyparser.EQ_OP; } 55 | "!=" { yyparser.yylval=new ParserVal(yytext()); return yyparser.EQ_OP; } 56 | "&&" { yyparser.yylval=new ParserVal(yytext()); return yyparser.COND_OP; } 57 | "||" { yyparser.yylval=new ParserVal(yytext()); return yyparser.COND_OP; } 58 | {REL_OP} { yyparser.yylval=new ParserVal(yytext()); return yyparser.REL_OP; } 59 | {ARITH_OP} { yyparser.yylval=new ParserVal(yytext()); return yyparser.ARITH_OP; } 60 | "-" { yyparser.yylval=new ParserVal(yytext()); return yyparser.MINUS ; } 61 | "{" { yyparser.yylval=new ParserVal(yytext());return '{' ;} 62 | "}" { yyparser.yylval=new ParserVal(yytext()); return '}' ; } 63 | ";" { yyparser.yylval=new ParserVal(yytext()); return ';' ;} 64 | "[" { yyparser.yylval=new ParserVal(yytext()); return '[';} 65 | "]" { yyparser.yylval=new ParserVal(yytext()); return ']';} 66 | "(" { yyparser.yylval=new ParserVal(yytext()); return '(';} 67 | ")" { yyparser.yylval=new ParserVal(yytext()); return ')';} 68 | "," { yyparser.yylval=new ParserVal(yytext()); return ',';} 69 | "!" { yyparser.yylval=new ParserVal(yytext()); return '!';} 70 | {WHITE_SPACE_CHAR} { } 71 | -------------------------------------------------------------------------------- /phase-1/Java Impl/decaf.l: -------------------------------------------------------------------------------- 1 | %% 2 | 3 | %byaccj 4 | 5 | %{ 6 | /* store a reference to the parser object */ 7 | Parser yyparser; 8 | 9 | /* constructor taking an additional parser object */ 10 | public Yylex(java.io.Reader r, Parser yyparser) { 11 | this(r); 12 | this.yyparser = yyparser; 13 | } 14 | %} 15 | 16 | 17 | DIGIT = [0-9] 18 | HEX_DIGIT = [0-9a-fA-F] 19 | HEX_INTEGER = 0[Xx]{HEX_DIGIT}+ 20 | INTEGER = {DIGIT}+ 21 | EXPONENT = [Ee][-+]?{INTEGER} 22 | DOUBLE = {INTEGER}"."{DIGIT}*{EXPONENT}? 23 | STRING = "\".*?\"" 24 | IDENTIFIER = [a-zA-Z][a-zA-Z_0-9]* 25 | ARITH_OP = ([+/*%]) 26 | REL_OP = [<|>] 27 | WHITE_SPACE_CHAR=[\ \n\r\t\f] 28 | 29 | %% 30 | "boolean" { yyparser.yylval=new ParserVal(yytext());return yyparser.BOOLEAN; } 31 | "break" { yyparser.yylval=new ParserVal(yytext()); return yyparser.BREAK; } 32 | "callout" { yyparser.yylval=new ParserVal(yytext()); return yyparser.CALLOUT; } 33 | "class" { yyparser.yylval=new ParserVal(yytext()); return yyparser.CLASS; } 34 | "true" { yyparser.yylval=new ParserVal(yytext()); return yyparser.TRUE; } 35 | "false" { yyparser.yylval=new ParserVal(yytext()); return yyparser.FALSE; } 36 | "for" { yyparser.yylval=new ParserVal(yytext()); return yyparser.FOR; } 37 | "if" { yyparser.yylval=new ParserVal(yytext()); return yyparser.IF; } 38 | "else" { yyparser.yylval=new ParserVal(yytext()); return yyparser.ELSE; } 39 | "return" { yyparser.yylval=new ParserVal(yytext()); return yyparser.RETURN; } 40 | "void" { yyparser.yylval=new ParserVal(yytext()); return yyparser.VOID; } 41 | "int" { yyparser.yylval=new ParserVal(yytext()); return yyparser.INT; } 42 | "continue" { yyparser.yylval=new ParserVal(yytext()); return yyparser.CONTINUE; } 43 | //"Program" { yyparser.yylval=new ParserVal(yytext()); return yyparser.Program; } 44 | {STRING} { yyparser.yylval=new ParserVal(yytext()); return yyparser.STRING_LITERAL; } 45 | {INTEGER} { yyparser.yylval=new ParserVal(Integer.parseInt(yytext())); return yyparser.INT_LITERAL; } 46 | {DOUBLE} { yyparser.yylval=new ParserVal(Double.parseDouble(yytext())); return yyparser.INT_LITERAL; } 47 | {HEX_INTEGER} { yyparser.yylval=new ParserVal(Integer.parseInt(yytext(),16)); return yyparser.INT_LITERAL; } 48 | {IDENTIFIER} { yyparser.yylval=new ParserVal(yytext()); return yyparser.ID; } 49 | "=" { yyparser.yylval=new ParserVal(yytext()); return yyparser.E_ASSIGN_OP; } 50 | "-=" { yyparser.yylval=new ParserVal(yytext()); return yyparser.ASSIGN_OP; } 51 | "+=" { yyparser.yylval=new ParserVal(yytext()); return yyparser.ASSIGN_OP; } 52 | "<=" { yyparser.yylval=new ParserVal(yytext()); return yyparser.EQ_OP; } 53 | ">=" { yyparser.yylval=new ParserVal(yytext()); return yyparser.EQ_OP;} 54 | "==" { yyparser.yylval=new ParserVal(yytext()); return yyparser.EQ_OP; } 55 | "!=" { yyparser.yylval=new ParserVal(yytext()); return yyparser.EQ_OP; } 56 | "&&" { yyparser.yylval=new ParserVal(yytext()); return yyparser.COND_OP; } 57 | "||" { yyparser.yylval=new ParserVal(yytext()); return yyparser.COND_OP; } 58 | {REL_OP} { yyparser.yylval=new ParserVal(yytext()); return yyparser.REL_OP; } 59 | {ARITH_OP} { yyparser.yylval=new ParserVal(yytext()); return yyparser.ARITH_OP; } 60 | "-" { yyparser.yylval=new ParserVal(yytext()); return yyparser.MINUS ; } 61 | "{" { yyparser.yylval=new ParserVal(yytext());return '{' ;} 62 | "}" { yyparser.yylval=new ParserVal(yytext()); return '}' ; } 63 | ";" { yyparser.yylval=new ParserVal(yytext()); return ';' ;} 64 | "[" { yyparser.yylval=new ParserVal(yytext()); return '[';} 65 | "]" { yyparser.yylval=new ParserVal(yytext()); return ']';} 66 | "(" { yyparser.yylval=new ParserVal(yytext()); return '(';} 67 | ")" { yyparser.yylval=new ParserVal(yytext()); return ')';} 68 | "," { yyparser.yylval=new ParserVal(yytext()); return ',';} 69 | "!" { yyparser.yylval=new ParserVal(yytext()); return '!';} 70 | {WHITE_SPACE_CHAR} { } -------------------------------------------------------------------------------- /phase-3/src/decaf.l: -------------------------------------------------------------------------------- 1 | %% 2 | 3 | %byaccj 4 | 5 | %{ 6 | /* store a reference to the parser object */ 7 | Parser yyparser; 8 | 9 | /* constructor taking an additional parser object */ 10 | public Yylex(java.io.Reader r, Parser yyparser) { 11 | this(r); 12 | this.yyparser = yyparser; 13 | } 14 | %} 15 | 16 | 17 | DIGIT = [0-9] 18 | HEX_DIGIT = [0-9a-fA-F] 19 | HEX_INTEGER = 0[Xx]{HEX_DIGIT}+ 20 | INTEGER = {DIGIT}+ 21 | EXPONENT = [Ee][-+]?{INTEGER} 22 | DOUBLE = {INTEGER}"."{DIGIT}*{EXPONENT}? 23 | STRING = \"[^\n\"]*\" 24 | IDENTIFIER = [a-zA-Z][a-zA-Z_0-9]* 25 | ARITH_OP = ([+/*%]) 26 | REL_OP = [<|>] 27 | WHITE_SPACE_CHAR=[\ \n\r\t\f] 28 | 29 | %% 30 | "boolean" { yyparser.yylval=new ParserVal(yytext());return yyparser.BOOLEAN; } 31 | "break" { yyparser.yylval=new ParserVal(yytext()); return yyparser.BREAK; } 32 | "callout" { yyparser.yylval=new ParserVal(yytext()); return yyparser.CALLOUT; } 33 | "class" { yyparser.yylval=new ParserVal(yytext()); return yyparser.CLASS; } 34 | "true" { yyparser.yylval=new ParserVal(yytext()); return yyparser.TRUE; } 35 | "false" { yyparser.yylval=new ParserVal(yytext()); return yyparser.FALSE; } 36 | "for" { yyparser.yylval=new ParserVal(yytext()); return yyparser.FOR; } 37 | "if" { yyparser.yylval=new ParserVal(yytext()); return yyparser.IF; } 38 | "else" { yyparser.yylval=new ParserVal(yytext()); return yyparser.ELSE; } 39 | "return" { yyparser.yylval=new ParserVal(yytext()); return yyparser.RETURN; } 40 | "void" { yyparser.yylval=new ParserVal(yytext()); return yyparser.VOID; } 41 | "int" { yyparser.yylval=new ParserVal(yytext()); return yyparser.INT; } 42 | "continue" { yyparser.yylval=new ParserVal(yytext()); return yyparser.CONTINUE; } 43 | //"Program" { yyparser.yylval=new ParserVal(yytext()); return yyparser.Program; } 44 | {STRING} { yyparser.yylval=new ParserVal(yytext()); return yyparser.STRING_LITERAL; } 45 | {INTEGER} { yyparser.yylval=new ParserVal(Integer.parseInt(yytext())); return yyparser.INT_LITERAL; } 46 | {DOUBLE} { yyparser.yylval=new ParserVal(Double.parseDouble(yytext())); return yyparser.INT_LITERAL; } 47 | {HEX_INTEGER} { yyparser.yylval=new ParserVal(Integer.parseInt(yytext(),16)); return yyparser.INT_LITERAL; } 48 | {IDENTIFIER} { yyparser.yylval=new ParserVal(yytext()); return yyparser.ID; } 49 | "=" { yyparser.yylval=new ParserVal(yytext()); return yyparser.E_ASSIGN_OP; } 50 | "-=" { yyparser.yylval=new ParserVal(yytext()); return yyparser.ASSIGN_OP; } 51 | "+=" { yyparser.yylval=new ParserVal(yytext()); return yyparser.ASSIGN_OP; } 52 | "<=" { yyparser.yylval=new ParserVal(yytext()); return yyparser.EQ_OP; } 53 | ">=" { yyparser.yylval=new ParserVal(yytext()); return yyparser.EQ_OP;} 54 | "==" { yyparser.yylval=new ParserVal(yytext()); return yyparser.EQ_OP; } 55 | "!=" { yyparser.yylval=new ParserVal(yytext()); return yyparser.EQ_OP; } 56 | "&&" { yyparser.yylval=new ParserVal(yytext()); return yyparser.COND_OP; } 57 | "||" { yyparser.yylval=new ParserVal(yytext()); return yyparser.COND_OP; } 58 | {REL_OP} { yyparser.yylval=new ParserVal(yytext()); return yyparser.REL_OP; } 59 | {ARITH_OP} { yyparser.yylval=new ParserVal(yytext()); return yyparser.ARITH_OP; } 60 | "-" { yyparser.yylval=new ParserVal(yytext()); return yyparser.MINUS ; } 61 | "{" { yyparser.yylval=new ParserVal(yytext());return '{' ;} 62 | "}" { yyparser.yylval=new ParserVal(yytext()); return '}' ; } 63 | ";" { yyparser.yylval=new ParserVal(yytext()); return ';' ;} 64 | "[" { yyparser.yylval=new ParserVal(yytext()); return '[';} 65 | "]" { yyparser.yylval=new ParserVal(yytext()); return ']';} 66 | "(" { yyparser.yylval=new ParserVal(yytext()); return '(';} 67 | ")" { yyparser.yylval=new ParserVal(yytext()); return ')';} 68 | "," { yyparser.yylval=new ParserVal(yytext()); return ',';} 69 | "!" { yyparser.yylval=new ParserVal(yytext()); return '!';} 70 | {WHITE_SPACE_CHAR} { } 71 | -------------------------------------------------------------------------------- /phase-3/test-cases/test-cases: -------------------------------------------------------------------------------- 1 | 2 | //For loop 3 | class p{ int a; int b; int main(){ for i=0, 3 { a=i;} callout("printf","%d",a);return a;}} 4 | class p{ int a; int b; int main(){ for i=0, 3 { a=10;} callout("printf","%d",a); return a;}} 5 | class p{ int a; int main(){int a;for i=0,8 {a=10+i;} callout("printf","%d",a);return a;}} 6 | class p{ int a; int main(){int a;for i=0,8 {a+=i;} callout("printf","%d",a);return a;}} 7 | class p{ int a; int main(){ int a; for i=0,3 {a=i;} callout("printf","%d",a);return a;}} 8 | 9 | 10 | //Callout 11 | class p{ int a; int b; int main(){ callout("printf","spandan"); return 0;}} 12 | class p{ int a; int b; int main(){ callout("printf","spandan",a); return 0;}} 13 | class p{ int a; int b; int main(){ callout("printf","spandan %d",a); return 0;}} 14 | class p{ int a; int b; int main(){ callout("printf","spandan %d",10); return 0;}} 15 | class p{ int a; int b; void ab(){ for i=0, 3 { a=10; if(a!=10){ break;} else { a=200;}}} int main(){callout("printf","%d\n",ab());return 0;}}//should fail printing void value 16 | class p{ boolean ab(boolean a,boolean b){return b;} int main() { callout("printf","%d", ab(true,true)); return 0; }} 17 | class p{ boolean ab(boolean a,boolean b){return b;} int main() { boolean pl;pl=ab(true,true);callout("printf","%d", pl); return 0; }} 18 | 19 | //callout without arg 20 | class p{ int main(){ callout("printf"); return 0;}} 21 | class p{ int main(){ callout("sys_setup"); return 0;}} 22 | 23 | 24 | //method calls 25 | class p{ int ab(int a,int b){return b;} int s(){int arg; arg=90; ab(arg,arg); return 0;} int main() { s(); callout("printf","%d", ab(3,4)); return 0; }} 26 | class p{ int ad(int a, int b){ ad(6,8); return a;} int main() { ad(3,4); return 0;}} //this should fail 27 | class p{ int ab(int a,int b){return b;} int s(){int arg; arg=90; return ab(arg,arg);} int main() { s(); callout("printf","%d", ab(3,4)); ab(3,4); return 0; }} 28 | class p{ int ab(int a){return 0;} int s(){int arg; arg=90; ab(arg); return 0;} int main(){s(); ab(3); return 0; } }//not a recursion 29 | 30 | class p{ int ab(int a,int b){return true;} int main() { callout("printf","%d", ab(3,4)); return 0; }}//should fail 31 | class p{ boolean ab(int a,int b){return b;} int main() { callout("printf","%d", ab(3,4)); return 0; }}//should fail 32 | class p{ boolean ab(boolean a,boolean b){return b;} int main() { int pl;pl=ab(true,true);callout("printf","%d", pl); return 0; }}//should fail 33 | class p{ boolean ab(boolean a,boolean b){return b;} int main() { int pl;pl=8+ab(true,true);callout("printf","%d", pl); return 0; }}//should fail 34 | 35 | //using return values of method statements; 36 | 37 | class p{ void ab(int a){a=39;} int s(){int arg; arg=ab(arg); return 0;} int main(){s();return 0; } }//should through an error 38 | class p{ void ab(int a){a=39;} int main(){return ab(8) ; } }//should fail 39 | class p{ int ab(int a){a=39; return a;} int main(){int t; t=ab(8) ; return t; } } 40 | 41 | 42 | 43 | //initializations 44 | class p{ } 45 | class p { int a,b;} 46 | class p{ int a[9];} 47 | class p{ int a,b,c,d; boolean s; } 48 | class p{ boolean a[9];} 49 | class p{ boolean a[9]; int a[9];} 50 | class p{ boolean a[9],b; int a[9],b;} 51 | class p{ int a[0];}//should fail 52 | 53 | //function declarations - with and without main - scope 54 | class p{ int ab(){return 0;} int s(){return 0;}} //no main 55 | class p{ int ab(){return 0;} int s(){ab(); return 0;}} //no main 56 | class p{ int a; int ab(){ a=b+v;}}//should fail as there is no variable decl 57 | class p{ int a, b; int ab(){ a=10;}}//should fail no return statement 58 | class p{ int main(){ int a; a=-9; return a;}} 59 | class prg { void ab(){ int a;}} 60 | class p{ int ab(){ int a; int b; b=32; a=b+10; return a;}} 61 | class p{ int ab(){ int a; a=32; a=a+10; return a;}} 62 | class p{ int a; int b; int main(){ int a; int b; b=32; a=b+10; a=-(a);return a;}} 63 | class p{ int main(){int a; a=1000; a-=10; return a;}} 64 | class p{ int main(){int a; a=1000; a+=10; return a;}} 65 | class p{ int a; int b; int main(){ int a; int a; int b; b=32; a=b+10; a=-(a);return a;}}//fails multiple local variable declaration 66 | class p { int ab(int b){ return b;} int main() { ab(); return 0; }} 67 | class p { int ab(int b){ return a;} int main() { ab(); return 0; }}//should return error 68 | class p{ int ad(int a, int b){ ad(6,8); return a;} int main(){ ad(3,8);return 0;}}//fails - recursion 69 | class p{ int ab(int a,int b){return b;} int s(){int arg; arg=90; ab(arg,arg); return 0;}} 70 | class p{ int ab(int a,int b){return b;} int s(){int arg; arg=90; return ab(arg,arg);}} 71 | class p{ int ab(int a){return 0;} int s(){int arg; arg=90; ab(arg); return 0;}} 72 | 73 | 74 | //if-else 75 | class p { int a; int ab(){ if(a==b) {a=b; } return b;}}//no variable declration for b 76 | class p{int main(){ int a,b; a=9;b=8; if(a>b){ callout("printf","%d",a);} return 0;}} 77 | class p{int main(){ int a,b; a=9;b=8; if(a>b){a=10;} else{ a=b;} return 0;}} 78 | class p{int main(){ boolean a,b; a=true;b=false; if(a||b){} else{ a=b;} return 0;}} 79 | class p{int main(){ boolean a,b; a=true;b=false; if(a||b){} else{ a=b;} return a;}}// should fail 80 | 81 | 82 | 83 | 84 | 85 | 86 | //arithmetic-op 87 | class p{ int a; int b; int ab(){ int a; int b; b=32; a=b+10; return a;} int main(){ callout("printf","%d",ab()); return 0;}} 88 | class p{ int a; int b; int ab(){ int a; int b; b=32; a=b-10; return a;} int main(){ callout("printf","%d",ab()); return 0;}} 89 | class p{ int a; int b; int ab(){ int a; int b; b=32; a=b/10; return a;} int main(){ callout("printf","%d",ab()); return 0;}} 90 | class p{ int a; int b; int ab(){ int a; int b; b=32; a=b*10; return a;} int main(){ callout("printf","%d",ab()); return 0;}} 91 | class p{ int a; int b; int ab(){ int a; int b; b=32; a=b%10; return a;} int main(){ callout("printf","%d",ab()); return 0;}} 92 | 93 | //arithmeticop for expr 94 | class p{ int a; int b; int ab(){ int a; int b; b=32; a=(b*10); return a;} int main(){ callout("printf","%d",ab()); return 0;}} 95 | 96 | //unary 97 | class p{ int a; int b; int ab(){ int a; int b; b=32; a=-(b*10); return a;} int main(){ callout("printf","%d",ab()); return 0;}} 98 | class p{ int a; int b; int ab(){ int a; int b; b=32; a=b+10; a=-(a);return a;} int main(){ callout("printf","%d",ab()); return 0;}} 99 | class p{ int a; int b; int ab(){ int a; int b; b=32; a=b+10; a=!a;return a;} int main(){ callout("printf","%d",ab()); return 0;}} 100 | class p{ int a; int b; int ab(){ int a; int b; b=32; a=b+10; a=-a;return a;} int main(){ callout("printf","%d",ab()); return 0;}} 101 | class p{ int a; int b; int ab(){ int a; int b; b=32; a=b+10; a=-(a);return a;} int main(){ callout("printf","%d",ab()); return 0;}} 102 | 103 | //local and global variable demo 104 | class p{ int a; int b; int ab(){ int a; int b; b=32; a=b+10; return a;} int main(){ return 0;}} //check llvm-ir for conf 105 | class p{ int a; int b; int ab(){ int b; b=32; a=b+10; return a;}int main(){ return 0;}} //check llvm-ir for conf 106 | 107 | //conditional 108 | class p{int main(){ boolean a,b; a=true;b=false; if(1||b){} else{ a=b;} return 0;}}//fails; printerror 109 | class p{int main(){ boolean a,b; a=true;b=false; if(true||b){} else{ a=b;} return 0;}} 110 | 111 | //break;; continue;; failure cases 112 | class p{ int a; int b; int ab(){ for i=0, 3 { a=10; break;} return a;} int main(){callout("printf","%d\n",ab());return 0;}} 113 | class p{ int a; int b; int ab(){ for i=0, 3 { a=10; continue;} return a;} int main(){callout("printf","%d\n",ab());return 0;}} 114 | class p{ int a; int b; int ab(){ for i=0, 3 { a=20; if(a>10){ break;} else {continue;}} return a;} int main(){callout("printf","%d\n",ab());return 0;}} 115 | class p{ int a; int b; int ab(){ for i=0, 3 { a=10; break;a=20;} return a;} int main(){callout("printf","%d\n",ab());return 0;}} 116 | class p{ int a; int b; int ab(){ for i=0, 3 { a=10; if(a==10){ a=1000; break;} } return a;} int main(){callout("printf","%d\n",ab());return 0;}} 117 | class p{ int a; int b; int ab(){ for i=0, 3 { a=10; if(a!=10){ break;} else { a=200;}} return a;} int main(){callout("printf","%d\n",ab());return 0;}} 118 | class p{ int a; int b; int ab(){ a=10; break;a=20; return a;} int main(){return 0;}}//break failure case 119 | class p{ int a; int b; int ab(){ a=10; continue;a=20; return a;} int main(){return 0;}}//continue failure case 120 | 121 | -------------------------------------------------------------------------------- /phase-2/VisitorImpl_bak.java: -------------------------------------------------------------------------------- 1 | public class VisitorImpl_bak implements Visitor { 2 | 3 | @Override 4 | public void visit(Decaf_Class decaf_Class) { 5 | decaf_Class.cls.accept(this); 6 | decaf_Class.id.accept(this); 7 | 8 | System.out.print(" { "); 9 | for (DeclarationsIntf decl : decaf_Class.decls) { 10 | decl.accept(this); 11 | } 12 | System.out.print(" } "); 13 | 14 | } 15 | 16 | @Override 17 | public void visit(Declarations1 declarations1) { 18 | for (DeclarationsIntf d : declarations1.methodDecls) 19 | d.accept(this); 20 | } 21 | 22 | @Override 23 | public void visit(FieldDecl fieldDecl) { 24 | 25 | fieldDecl.type.accept(this); 26 | for (Fields f : fieldDecl.fields) 27 | f.accept(this); 28 | } 29 | 30 | @Override 31 | public void visit(Fields1 fields1) { 32 | fields1.field.accept(this); 33 | } 34 | 35 | @Override 36 | public void visit(Identifier identifier) { 37 | 38 | System.out.print(" " + identifier.id + " "); 39 | } 40 | 41 | @Override 42 | public void visit(Field field) { 43 | field.id.accept(this); 44 | } 45 | 46 | @Override 47 | public void visit(MethodDeclaration1 methodDeclaration1) { 48 | methodDeclaration1.type.accept(this); 49 | methodDeclaration1.id.accept(this); 50 | System.out.print("("); 51 | for (Arguement a : methodDeclaration1.argList) 52 | a.accept(this); 53 | System.out.print(")"); 54 | methodDeclaration1.block.accept(this); 55 | } 56 | 57 | @Override 58 | public void visit(MethodDeclaration2 methodDeclaration2) { 59 | methodDeclaration2.type.accept(this); 60 | methodDeclaration2.id.accept(this); 61 | System.out.print("("); 62 | for (Arguement a : methodDeclaration2.argList) 63 | a.accept(this); 64 | System.out.print(")"); 65 | methodDeclaration2.block.accept(this); 66 | 67 | } 68 | 69 | @Override 70 | public void visit(Arguement1 arguement1) { 71 | arguement1.type.accept(this); 72 | arguement1.id.accept(this); 73 | } 74 | 75 | @Override 76 | public void visit(Var_Decl var_Decl) { 77 | var_Decl.type.accept(this); 78 | for (Identifier id : var_Decl.vars) 79 | id.accept(this); 80 | System.out.print(";"); 81 | } 82 | 83 | @Override 84 | public void visit(Statement1 statement1) { 85 | statement1.expr.accept(this); 86 | System.out.print(" " + statement1.op + " "); 87 | statement1.loc.accept(this); 88 | 89 | } 90 | 91 | @Override 92 | public void visit(ReturnStatement returnStatement) { 93 | returnStatement.s.accept(this); 94 | System.out.print(";"); 95 | } 96 | 97 | @Override 98 | public void visit(ReturnStatement1 returnStatement1) { 99 | returnStatement1.s.accept(this); 100 | returnStatement1.expr.accept(this); 101 | System.out.print(";"); 102 | } 103 | 104 | @Override 105 | public void visit(BreakStatement breakStatement) { 106 | breakStatement.accept(this); 107 | System.out.print(";"); 108 | } 109 | 110 | @Override 111 | public void visit(ContinueStatement continueStatement) { 112 | continueStatement.accept(this); 113 | System.out.print(";" ); 114 | 115 | } 116 | 117 | @Override 118 | public void visit(IfStatement ifStatement) { 119 | ifStatement.s1.accept(this); 120 | System.out.print("("); 121 | ifStatement.expr.accept(this); 122 | System.out.print(")"); 123 | ifStatement.block.accept(this); 124 | 125 | } 126 | 127 | @Override 128 | public void visit(IfElseStatement ifElseStatement) { 129 | ifElseStatement.s1.accept(this); 130 | System.out.print("("); 131 | ifElseStatement.expr.accept(this); 132 | System.out.print(")"); 133 | ifElseStatement.s1.accept(this); 134 | ifElseStatement.s2.accept(this); 135 | ifElseStatement.elseBlock.accept(this); 136 | } 137 | 138 | @Override 139 | public void visit(ForStatement forStatement) { 140 | forStatement.s.accept(this); 141 | forStatement.id.accept(this); 142 | System.out.print(forStatement.ch); 143 | forStatement.expr1.accept(this); 144 | System.out.print(" , "); 145 | forStatement.expr2.accept(this); 146 | forStatement.block.accept(this); 147 | } 148 | 149 | @Override 150 | public void visit(Block1 block) { 151 | System.out.print(" { "); 152 | for (Var_Decl var : block.vars) 153 | var.accept(this); 154 | for (StatementIntf stmt : block.stmts) 155 | stmt.accept(this); 156 | System.out.print(" } "); 157 | 158 | } 159 | 160 | @Override 161 | public void visit(IntegerType integerType) { 162 | System.out.print(" " + integerType.s + " "); 163 | } 164 | 165 | @Override 166 | public void visit(BooleanType booleanType) { 167 | System.out.print(" " + booleanType.s + " "); 168 | 169 | } 170 | 171 | @Override 172 | public void visit(Void void1) { 173 | System.out.print(" " + void1.s + " "); 174 | 175 | } 176 | 177 | @Override 178 | public void visit(callout_arg callout_arg) { 179 | System.out.print(" " + callout_arg.s + " "); 180 | 181 | } 182 | 183 | @Override 184 | public void visit(MethodStatement1 methodStatement1) { 185 | methodStatement1.id.accept(this); 186 | System.out.print("("); 187 | System.out.print(")"); 188 | System.out.print(";"); 189 | } 190 | 191 | @Override 192 | public void visit(MethodStatement2 methodStatement2) { 193 | methodStatement2.id.accept(this); 194 | System.out.print("("); 195 | for (ExpressionIntf expr : methodStatement2.exprs) 196 | expr.accept(this); 197 | System.out.print("("); 198 | System.out.print(";"); 199 | 200 | } 201 | 202 | @Override 203 | public void visit(MethodStatementCallOut1 methodStatementCallOut1) { 204 | methodStatementCallOut1.s.accept(this); 205 | System.out.print("("); 206 | System.out.print(methodStatementCallOut1.s1); 207 | System.out.print(" , "); 208 | for (callout_arg arg : methodStatementCallOut1.calloutargs) 209 | arg.accept(this); 210 | System.out.print(")"); 211 | 212 | } 213 | 214 | @Override 215 | public void visit(MethodStatementCallOut2 methodStatementCallOut2) { 216 | methodStatementCallOut2.s.accept(this); 217 | System.out.print("("); 218 | System.out.print(methodStatementCallOut2.s1); 219 | System.out.print(")"); 220 | 221 | } 222 | 223 | @Override 224 | public void visit(Location1 location) { 225 | location.id.accept(this); 226 | 227 | } 228 | 229 | @Override 230 | public void visit(Location2 location) { 231 | location.id.accept(this); 232 | System.out.print(" [ "); 233 | location.expr.accept(this); 234 | System.out.print(" ] "); 235 | } 236 | 237 | @Override 238 | public void visit(Expression1 expression1) { 239 | expression1.expr.accept(this); 240 | System.out.print(" " + expression1.operator + " "); 241 | expression1.term1.accept(this); 242 | } 243 | 244 | @Override 245 | public void visit(Expression2 expression2) { 246 | System.out.print(" " + expression2.operator + " "); 247 | expression2.term5.accept(this); 248 | 249 | } 250 | 251 | @Override 252 | public void visit(Expression3 expression3) { 253 | System.out.print("("); 254 | expression3.term5.accept(this); 255 | System.out.print(")"); 256 | } 257 | 258 | @Override 259 | public void visit(IntegerLiteral integerLiteral) { 260 | System.out.print(" " + integerLiteral.value + " "); 261 | } 262 | 263 | @Override 264 | public void visit(BooleanLiteral booleanLiteral) { 265 | System.out.print(" " + booleanLiteral.value + " "); 266 | 267 | } 268 | 269 | @Override 270 | public void visit(Return return1) { 271 | System.out.print(" " + return1.s + " "); 272 | 273 | } 274 | 275 | @Override 276 | public void visit(Break break1) { 277 | System.out.print(" " + break1.s + " "); 278 | 279 | } 280 | 281 | @Override 282 | public void visit(Continue continue1) { 283 | System.out.print(" " + continue1.s + " "); 284 | 285 | } 286 | 287 | @Override 288 | public void visit(Class class1) { 289 | System.out.print(" " + class1.id + " "); 290 | 291 | } 292 | 293 | @Override 294 | public void visit(ELSE else1) { 295 | System.out.print(" " + else1.s + " "); 296 | 297 | } 298 | 299 | @Override 300 | public void visit(IF if1) { 301 | System.out.print(" " + if1.s1 + " "); 302 | 303 | } 304 | 305 | @Override 306 | public void visit(For for1) { 307 | System.out.print(" " + for1.s + " "); 308 | 309 | } 310 | 311 | @Override 312 | public void visit(StringLiteral stringLiteral) { 313 | System.out.print(" " + stringLiteral.s + " "); 314 | 315 | } 316 | 317 | @Override 318 | public void visit(CallOut callOut) { 319 | System.out.print(" " + callOut.s + " "); 320 | 321 | } 322 | 323 | @Override 324 | public void visit(Field1 field1) { 325 | field1.id.accept(this); 326 | System.out.print(" [ "); 327 | System.out.print(field1.val); 328 | System.out.print(" ] "); 329 | } 330 | 331 | @Override 332 | public void visit(Block2 block2) { 333 | for (Var_Decl var : block2.vars) 334 | var.accept(this); 335 | System.out.print(";"); 336 | 337 | } 338 | 339 | @Override 340 | public void visit(Block3 block3) { 341 | for (StatementIntf stmt : block3.stmts) 342 | stmt.accept(this); 343 | 344 | } 345 | 346 | } 347 | -------------------------------------------------------------------------------- /phase-2/VisitorImpl.java: -------------------------------------------------------------------------------- 1 | public class VisitorImpl implements Visitor { 2 | 3 | @Override 4 | public void visit(Decaf_Class decaf_Class) { 5 | decaf_Class.cls.accept(this); 6 | System.out.print(" CLASSNAME:"); 7 | decaf_Class.id.accept(this); 8 | 9 | System.out.print(" CLASS BODY:{ "); 10 | for (DeclarationsIntf decl : decaf_Class.decls) { 11 | decl.accept(this); 12 | } 13 | System.out.print(" } "); 14 | 15 | } 16 | 17 | @Override 18 | public void visit(Declarations1 declarations1) { 19 | for (DeclarationsIntf d : declarations1.methodDecls) 20 | d.accept(this); 21 | } 22 | 23 | @Override 24 | public void visit(FieldDecl fieldDecl) { 25 | System.out.print(" FIELDS:"); 26 | fieldDecl.type.accept(this); 27 | for (Fields f : fieldDecl.fields) 28 | f.accept(this); 29 | } 30 | 31 | @Override 32 | public void visit(Fields1 fields1) { 33 | fields1.field.accept(this); 34 | } 35 | 36 | @Override 37 | public void visit(Identifier identifier) { 38 | 39 | System.out.print(" " + identifier.id + " "); 40 | } 41 | 42 | @Override 43 | public void visit(Field field) { 44 | field.id.accept(this); 45 | } 46 | 47 | @Override 48 | public void visit(MethodDeclaration1 methodDeclaration1) { 49 | System.out.print(" FUNCTION:"); 50 | methodDeclaration1.type.accept(this); 51 | methodDeclaration1.id.accept(this); 52 | System.out.print(" FUNCTION ARGS:( "); 53 | 54 | for (Arguement a : methodDeclaration1.argList) 55 | a.accept(this); 56 | System.out.print(")"); 57 | System.out.print(" FUNCTION BODY: "); 58 | methodDeclaration1.block.accept(this); 59 | } 60 | 61 | @Override 62 | public void visit(MethodDeclaration2 methodDeclaration2) { 63 | System.out.print(" FUNCTION:"); 64 | methodDeclaration2.type.accept(this); 65 | methodDeclaration2.id.accept(this); 66 | System.out.print(" FUNCTION ARGS:( "); 67 | for (Arguement a : methodDeclaration2.argList) 68 | a.accept(this); 69 | System.out.print(")"); 70 | System.out.print(" FUNCTION BODY: "); 71 | methodDeclaration2.block.accept(this); 72 | 73 | } 74 | 75 | @Override 76 | public void visit(Arguement1 arguement1) { 77 | arguement1.type.accept(this); 78 | arguement1.id.accept(this); 79 | } 80 | 81 | @Override 82 | public void visit(Var_Decl var_Decl) { 83 | System.out.print(" VARIABLES:"); 84 | var_Decl.type.accept(this); 85 | for (Identifier id : var_Decl.vars) 86 | id.accept(this); 87 | System.out.print(";"); 88 | } 89 | 90 | @Override 91 | public void visit(Statement1 statement1) { 92 | System.out.print(" STATEMENT:"); 93 | statement1.expr.accept(this); 94 | System.out.print(" " + statement1.op + " "); 95 | statement1.loc.accept(this); 96 | 97 | } 98 | 99 | @Override 100 | public void visit(ReturnStatement returnStatement) { 101 | System.out.print(" RETURN STATEMENT:"); 102 | returnStatement.s.accept(this); 103 | System.out.print(";"); 104 | } 105 | 106 | @Override 107 | public void visit(ReturnStatement1 returnStatement1) { 108 | System.out.print(" RETURN STATEMENT:"); 109 | returnStatement1.s.accept(this); 110 | returnStatement1.expr.accept(this); 111 | System.out.print(";"); 112 | } 113 | 114 | @Override 115 | public void visit(BreakStatement breakStatement) { 116 | System.out.print(" BREAK STATEMENT:"); 117 | breakStatement.accept(this); 118 | System.out.print(";"); 119 | } 120 | 121 | @Override 122 | public void visit(ContinueStatement continueStatement) { 123 | System.out.print(" CONTINUE STATEMENT:"); 124 | continueStatement.accept(this); 125 | System.out.print(";" ); 126 | 127 | } 128 | 129 | @Override 130 | public void visit(IfStatement ifStatement) { 131 | System.out.print(" IF STATEMENT:"); 132 | ifStatement.s1.accept(this); 133 | System.out.print("("); 134 | ifStatement.expr.accept(this); 135 | System.out.print(")"); 136 | ifStatement.block.accept(this); 137 | 138 | } 139 | 140 | @Override 141 | public void visit(IfElseStatement ifElseStatement) { 142 | System.out.print(" IFELSE STATEMENT:"); 143 | ifElseStatement.s1.accept(this); 144 | System.out.print("("); 145 | ifElseStatement.expr.accept(this); 146 | System.out.print(")"); 147 | ifElseStatement.s1.accept(this); 148 | ifElseStatement.s2.accept(this); 149 | ifElseStatement.elseBlock.accept(this); 150 | } 151 | 152 | @Override 153 | public void visit(ForStatement forStatement) { 154 | System.out.print(" FOR STATEMENT:"); 155 | forStatement.s.accept(this); 156 | forStatement.id.accept(this); 157 | System.out.print(forStatement.ch); 158 | forStatement.expr1.accept(this); 159 | System.out.print(" , "); 160 | forStatement.expr2.accept(this); 161 | forStatement.block.accept(this); 162 | } 163 | 164 | @Override 165 | public void visit(Block1 block) { 166 | System.out.print(" { "); 167 | for (Var_Decl var : block.vars) 168 | var.accept(this); 169 | for (StatementIntf stmt : block.stmts) 170 | stmt.accept(this); 171 | System.out.print(" } "); 172 | 173 | } 174 | 175 | @Override 176 | public void visit(IntegerType integerType) { 177 | System.out.print(" " + integerType.s + " "); 178 | } 179 | 180 | @Override 181 | public void visit(BooleanType booleanType) { 182 | System.out.print(" " + booleanType.s + " "); 183 | 184 | } 185 | 186 | @Override 187 | public void visit(Void void1) { 188 | System.out.print(" " + void1.s + " "); 189 | 190 | } 191 | 192 | @Override 193 | public void visit(callout_arg callout_arg) { 194 | System.out.print(" " + callout_arg.s + " "); 195 | 196 | } 197 | 198 | @Override 199 | public void visit(MethodStatement1 methodStatement1) { 200 | System.out.print(" METHOD CALL:"); 201 | methodStatement1.id.accept(this); 202 | System.out.print("("); 203 | System.out.print(")"); 204 | System.out.print(";"); 205 | } 206 | 207 | @Override 208 | public void visit(MethodStatement2 methodStatement2) { 209 | System.out.print(" METHOD CALL:"); 210 | methodStatement2.id.accept(this); 211 | System.out.print("("); 212 | for (ExpressionIntf expr : methodStatement2.exprs) 213 | expr.accept(this); 214 | System.out.print("("); 215 | System.out.print(";"); 216 | 217 | } 218 | 219 | @Override 220 | public void visit(MethodStatementCallOut1 methodStatementCallOut1) { 221 | System.out.print(" CALLOUT F'n CALL:"); 222 | methodStatementCallOut1.s.accept(this); 223 | System.out.print("("); 224 | methodStatementCallOut1.s1.accept(this); 225 | System.out.print(" , "); 226 | for (callout_arg arg : methodStatementCallOut1.calloutargs) 227 | arg.accept(this); 228 | System.out.print(")"); 229 | 230 | } 231 | 232 | @Override 233 | public void visit(MethodStatementCallOut2 methodStatementCallOut2) { 234 | System.out.print(" CALLOUT F'n CALL:"); 235 | methodStatementCallOut2.s.accept(this); 236 | System.out.print("("); 237 | methodStatementCallOut2.s1.accept(this); 238 | System.out.print(")"); 239 | 240 | } 241 | 242 | @Override 243 | public void visit(Location1 location) { 244 | location.id.accept(this); 245 | 246 | } 247 | 248 | @Override 249 | public void visit(Location2 location) { 250 | location.id.accept(this); 251 | System.out.print(" [ "); 252 | location.expr.accept(this); 253 | System.out.print(" ] "); 254 | } 255 | 256 | @Override 257 | public void visit(Expression1 expression1) { 258 | System.out.print(" EXPRESSIONS:"); 259 | expression1.expr.accept(this); 260 | System.out.print(" " + expression1.operator + " "); 261 | expression1.term1.accept(this); 262 | } 263 | 264 | @Override 265 | public void visit(Expression2 expression2) { 266 | System.out.print(" EXPRESSIONS:"); 267 | System.out.print(" " + expression2.operator + " "); 268 | expression2.term5.accept(this); 269 | 270 | } 271 | 272 | @Override 273 | public void visit(Expression3 expression3) { 274 | System.out.print(" EXPRESSIONS:"); 275 | System.out.print("("); 276 | expression3.term5.accept(this); 277 | System.out.print(")"); 278 | } 279 | 280 | @Override 281 | public void visit(IntegerLiteral integerLiteral) { 282 | System.out.print(" " + integerLiteral.value + " "); 283 | } 284 | 285 | @Override 286 | public void visit(BooleanLiteral booleanLiteral) { 287 | System.out.print(" " + booleanLiteral.value + " "); 288 | 289 | } 290 | 291 | @Override 292 | public void visit(Return return1) { 293 | System.out.print(" " + return1.s + " "); 294 | 295 | } 296 | 297 | @Override 298 | public void visit(Break break1) { 299 | System.out.print(" " + break1.s + " "); 300 | 301 | } 302 | 303 | @Override 304 | public void visit(Continue continue1) { 305 | System.out.print(" " + continue1.s + " "); 306 | 307 | } 308 | 309 | @Override 310 | public void visit(Class class1) { 311 | System.out.print(" " + class1.id + " "); 312 | 313 | } 314 | 315 | @Override 316 | public void visit(ELSE else1) { 317 | System.out.print(" " + else1.s + " "); 318 | 319 | } 320 | 321 | @Override 322 | public void visit(IF if1) { 323 | System.out.print(" " + if1.s1 + " "); 324 | 325 | } 326 | 327 | @Override 328 | public void visit(For for1) { 329 | System.out.print(" " + for1.s + " "); 330 | 331 | } 332 | 333 | @Override 334 | public void visit(StringLiteral stringLiteral) { 335 | System.out.print(" " + stringLiteral.s + " "); 336 | 337 | } 338 | 339 | @Override 340 | public void visit(CallOut callOut) { 341 | System.out.print(" " + callOut.s + " "); 342 | 343 | } 344 | 345 | @Override 346 | public void visit(Field1 field1) { 347 | field1.id.accept(this); 348 | System.out.print(" [ "); 349 | System.out.print(field1.val); 350 | System.out.print(" ] "); 351 | } 352 | 353 | @Override 354 | public void visit(Block2 block2) { 355 | for (Var_Decl var : block2.vars) 356 | var.accept(this); 357 | System.out.print(";"); 358 | 359 | } 360 | 361 | @Override 362 | public void visit(Block3 block3) { 363 | for (StatementIntf stmt : block3.stmts) 364 | stmt.accept(this); 365 | 366 | } 367 | 368 | } 369 | -------------------------------------------------------------------------------- /phase-2/parser.y: -------------------------------------------------------------------------------- 1 | %{ 2 | 3 | import java.lang.Math; 4 | import java.io.*; 5 | import java.util.*; 6 | 7 | %} 8 | 9 | %token INT BOOLEAN IF ELSE FOR RETURN BREAK CONTINUE CLASS VOID CALLOUT TRUE FALSE 10 | 11 | %token ASSIGN_OP ID STRING_LITERAL ARITH_OP REL_OP EQ_OP COND_OP INT_LITERAL E_ASSIGN_OP MINUS Program 12 | 13 | %left '-' '!' ARITH_OP REL_OP EQ_OP COND_OP 14 | 15 | %% 16 | 17 | program : CLASS ID '{' declarations '}' 18 | { Decaf_Class program = new Decaf_Class(new Class($1.sval),new Identifier($2.sval),(ArrayList)$4.obj); 19 | program.accept(new VisitorImpl()); 20 | }; 21 | 22 | declarations : type fields ';' declarations 23 | { ArrayList decls=((ArrayList)$4.obj); 24 | decls.add(new FieldDecl((Type)$1.obj, (ArrayList)$2.obj)); 25 | $$.obj=decls; 26 | } 27 | | method_decl; 28 | { $$=$1; } 29 | ; 30 | 31 | 32 | 33 | fields : field 34 | { ArrayList field = new ArrayList(); 35 | field.add((Field)$1.obj); 36 | $$.obj = field; 37 | } 38 | | field ',' fields 39 | { ArrayList field1 = (ArrayList)$3.obj; 40 | field1.add((Field)$1.obj); 41 | $$.obj=field1; 42 | } 43 | ; 44 | 45 | field : ID { $$.obj = new Field(new Identifier($1.sval)); } 46 | | ID '[' INT_LITERAL ']' 47 | { $$.obj = new Field1(new Identifier($1.sval), $2.ival); } 48 | ; 49 | 50 | method_decl : type ID '(' args_decl ')' block method_decl 51 | { ArrayList methodDecls1=((ArrayList)$7.obj); 52 | methodDecls1.add(new MethodDeclaration1((Type)$1.obj,new Identifier($2.sval),(ArrayList)$4.obj,(Block)$6.obj)); 53 | $$.obj = methodDecls1; 54 | } 55 | | VOID ID '(' args_decl ')' block method_decl 56 | { ArrayList methodDecls2= ((ArrayList)$7.obj); 57 | methodDecls2.add(new MethodDeclaration2(new Void($1.sval),new Identifier($2.sval),(ArrayList)$4.obj,(Block)$6.obj)); 58 | $$.obj =methodDecls2; 59 | } 60 | | { $$.obj = new ArrayList(); } 61 | ; 62 | 63 | args_decl :arg ',' args_decl 64 | { ArrayList argsDecl=((ArrayList)$3.obj) ; 65 | argsDecl.add((Arguement)$1.obj); 66 | $$.obj= argsDecl; 67 | } 68 | | arg 69 | { ArrayList argsDecl2=new ArrayList(); 70 | argsDecl2.add((Arguement)$1.obj); 71 | $$.obj= argsDecl2; 72 | } 73 | | { $$.obj=new ArrayList(); } 74 | ; 75 | 76 | arg : type ID ; { $$.obj= new Arguement1((Type)$1.obj, new Identifier($2.sval)); } 77 | ; 78 | 79 | vars : ID ';' { ArrayList idList=new ArrayList(); 80 | idList.add(new Identifier($1.sval)); 81 | $$.obj=idList; 82 | } 83 | | ID ',' vars { ArrayList idList2=((ArrayList)$3.obj); 84 | idList2.add(new Identifier($1.sval)); 85 | $$.obj=idList2; 86 | } 87 | ; 88 | 89 | var_decl : type vars { $$.obj = new Var_Decl((Type)$1.obj,(ArrayList) $2.obj); } 90 | ; 91 | 92 | var_decls : var_decl var_decls 93 | { ArrayList var_decls = ((ArrayList)$2.obj); 94 | var_decls.add((Var_Decl) $1.obj); 95 | $$.obj =var_decls; 96 | } 97 | | var_decl { ArrayList var_decls1= new ArrayList(); 98 | var_decls1.add((Var_Decl)$1.obj); 99 | $$.obj =var_decls1; 100 | } 101 | ; 102 | 103 | block : '{' block_body '}' 104 | { $$ = $2; } 105 | ; 106 | 107 | statements : statement { ArrayList statement1 = new ArrayList(); 108 | statement1.add((StatementIntf)$1.obj); 109 | $$.obj = statement1; 110 | } 111 | | statement statements 112 | { ArrayList statement2 = ((ArrayList)$1.obj); 113 | statement2.add((StatementIntf)$1.obj); 114 | $$.obj = statement2; 115 | }; 116 | 117 | block_body : var_decls statements 118 | { if($1.obj!=null && $2.obj!=null){ 119 | $$.obj= new Block1((ArrayList)$1.obj, (ArrayList)$2.obj); 120 | 121 | } 122 | if($1.obj!=null && $2.obj==null){ 123 | $$.obj= new Block2((ArrayList)$1.obj); 124 | } 125 | if ($1.obj==null && $2.obj!=null){ 126 | $$.obj= new Block3((ArrayList)$2.obj); 127 | } 128 | } 129 | | var_decls { $$.obj = new Block2((ArrayList)$1.obj); } 130 | | statements { $$.obj = new Block3((ArrayList)$1.obj); } 131 | | { } 132 | ; 133 | 134 | type : INT { $$.obj=new IntegerType($1.sval); } 135 | | BOOLEAN { $$.obj=new BooleanType($1.sval); } 136 | ; 137 | 138 | statement: location ASSGN_OP expr ';' 139 | { $$.obj = new Statement1((Location)$1.obj,(ExpressionIntf)$3.obj, (char)$2.sval.charAt(0)); } 140 | | method_call ';' 141 | { $$=$1; } 142 | | IF '(' expr ')' block ELSE block 143 | { $$.obj = new IfElseStatement(new IF($1.sval),(ExpressionIntf)$3.obj,(Block)$5.obj, new ELSE($6.sval),(Block)$7.obj); } 144 | | IF '(' expr ')' block 145 | { $$.obj = new IfStatement(new IF($1.sval),(ExpressionIntf)$3.obj,(Block)$5.obj); } 146 | | FOR ID E_ASSIGN_OP expr ',' expr block 147 | { $$.obj = new ForStatement(new For($1.sval),new Identifier($2.sval),(char)$3.sval.charAt(0),(ExpressionIntf)$4.obj,(ExpressionIntf)$6.obj,(Block)$7.obj); } 148 | | RETURN ';' { $$.obj= new ReturnStatement(new Return($1.sval)); } 149 | | RETURN expr ';' 150 | { $$.obj= new ReturnStatement1(new Return($1.sval),(ExpressionIntf)$2.obj); } 151 | | BREAK ';' { $$.obj= new BreakStatement(new Break($1.sval)); } 152 | | CONTINUE ';' { $$.obj= new ContinueStatement(new Continue($1.sval)); } 153 | | block { $$=$1; }; 154 | 155 | ASSGN_OP : ASSIGN_OP { $$=$1; } 156 | | E_ASSIGN_OP { $$=$1; } 157 | ; 158 | 159 | exprs : expr { ArrayList exprs1= new ArrayList(); 160 | exprs1.add((ExpressionIntf)$1.obj); 161 | $$.obj = exprs1; 162 | } 163 | | expr ',' exprs { ArrayList exprs2= ((ArrayList)($3.obj)); 164 | exprs2.add((ExpressionIntf)$1.obj); 165 | $$.obj =exprs2; 166 | } 167 | ; 168 | 169 | callout_args : callout_arg 170 | { ArrayList calloutargs1 = new ArrayList(); 171 | calloutargs1.add((callout_arg)$1.obj); 172 | $$.obj = calloutargs1; 173 | } 174 | | callout_arg ',' callout_args ; 175 | { ArrayList calloutargs2 = ((ArrayList)($3.obj)); 176 | calloutargs2.add((callout_arg)$1.obj); 177 | $$.obj = calloutargs2; 178 | }; 179 | 180 | method_call : method_name '(' ')' 181 | { $$.obj= new MethodStatement1((Identifier)$1.obj); } 182 | | method_name '(' exprs ')' 183 | { $$.obj= new MethodStatement2((Identifier)$1.obj,(ArrayList)$3.obj); } 184 | | CALLOUT '(' STRING_LITERAL ',' callout_args ')' 185 | { $$.obj = new MethodStatementCallOut1 (new CallOut($1.sval), new StringLiteral($3.sval),(ArrayList) $5.obj); } 186 | | CALLOUT '(' STRING_LITERAL ')' 187 | { $$.obj = new MethodStatementCallOut2 (new CallOut($1.sval), new StringLiteral($3.sval)); } 188 | ; 189 | 190 | method_name : ID { $$.obj = new Identifier($1.sval); } 191 | ; 192 | 193 | location : ID { $$.obj = new Location1(new Identifier($1.sval)); } 194 | | ID '[' expr ']' 195 | { $$.obj = new Location2(new Identifier($1.sval), (ExpressionIntf) $2.obj); } 196 | ; 197 | 198 | ARTH_OP : ARITH_OP { $$=$1; } 199 | | MINUS { $$=$1; } 200 | ; 201 | 202 | expr : expr ARTH_OP term1 203 | { $$.obj = new Expression1((ExpressionIntf)$1.obj,(ExpressionIntf)$3.obj, (char)$2.sval.charAt(0)); } 204 | | term1 { $$=$1; } 205 | ; 206 | 207 | term1 : term1 REL_OP term2 208 | { $$.obj = new Expression1((ExpressionIntf)$1.obj,(ExpressionIntf)$3.obj, (char)$2.sval.charAt(0)); } 209 | | term2 { $$=$1 ;} 210 | ; 211 | 212 | 213 | term2 : term2 EQ_OP term3 214 | { $$.obj = new Expression1((ExpressionIntf)$1.obj,(ExpressionIntf)$3.obj, (char)$2.sval.charAt(0)); } 215 | | term3 { $$=$1 ; } 216 | ; 217 | 218 | 219 | term3 : term3 COND_OP term4 220 | { $$.obj = new Expression1((ExpressionIntf)$1.obj,(ExpressionIntf)$3.obj, (char)$2.sval.charAt(0)); } 221 | | term4 { $$=$1; } 222 | ; 223 | 224 | term4 : location { $$=$1; } 225 | | method_call { $$=$1; } 226 | | literal { $$=$1; } 227 | | MINUS term4 { $$.obj = new Expression2((ExpressionIntf)$2.obj, (char)$1.sval.charAt(0)); } 228 | | '!' term4 { $$.obj = new Expression2((ExpressionIntf)$2.obj, (char)$1.sval.charAt(0)); } 229 | | '(' expr ')' { $$.obj = new Expression3((ExpressionIntf)$2.obj); } 230 | ; 231 | 232 | callout_arg : expr { $$=$1; } 233 | | STRING_LITERAL 234 | { $$.obj = new callout_arg($1.sval); } 235 | ; 236 | 237 | bool_literal : TRUE {$$=$1;} 238 | | FALSE {$$=$1;} 239 | ; 240 | 241 | literal : INT_LITERAL { $$.obj = new IntegerLiteral($1.ival); } 242 | // | CHAR_LITERAL 243 | | bool_literal { $$.obj = new BooleanLiteral($1.sval); } 244 | ; 245 | 246 | %% 247 | 248 | 249 | 250 | private Yylex lexer; 251 | 252 | 253 | private int yylex () { 254 | int yyl_return = -1; 255 | try { 256 | yyl_return = lexer.yylex(); 257 | } 258 | catch (IOException e) { 259 | System.err.println("IO error :"+e); 260 | } 261 | return yyl_return; 262 | } 263 | 264 | 265 | public void yyerror (String error) { 266 | System.err.println ("Error: " + error); 267 | } 268 | 269 | 270 | public Parser(Reader r) { 271 | lexer = new Yylex(r, this); 272 | } 273 | 274 | 275 | public static void main(String args[]) throws IOException { 276 | 277 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 278 | Parser yyparser = new Parser(br); 279 | yyparser.yyparse(); 280 | 281 | } -------------------------------------------------------------------------------- /phase-3/src/parser.y: -------------------------------------------------------------------------------- 1 | %{ 2 | 3 | import java.lang.Math; 4 | import java.io.*; 5 | import java.util.*; 6 | 7 | %} 8 | 9 | %token INT BOOLEAN IF ELSE FOR RETURN BREAK CONTINUE CLASS VOID CALLOUT TRUE FALSE 10 | 11 | %token ASSIGN_OP ID STRING_LITERAL ARITH_OP REL_OP EQ_OP COND_OP INT_LITERAL E_ASSIGN_OP MINUS Program 12 | 13 | %left '-' '!' ARITH_OP REL_OP EQ_OP COND_OP 14 | 15 | %% 16 | 17 | program : CLASS ID '{' declarations '}' 18 | { Decaf_Class program = new Decaf_Class(new Class($1.sval),new Identifier($2.sval),(ArrayList)$4.obj); 19 | program.accept(new VisitorImpl()); 20 | }; 21 | 22 | declarations : type fields ';' declarations 23 | { ArrayList decls= new ArrayList(); 24 | decls.add(new FieldDecl((Type)$1.obj, (ArrayList)$2.obj)); 25 | decls.addAll((ArrayList)$4.obj); 26 | 27 | $$.obj=decls; 28 | } 29 | | method_decl; 30 | { $$=$1; } 31 | ; 32 | 33 | 34 | 35 | fields : field 36 | { ArrayList field = new ArrayList(); 37 | field.add((Fields)$1.obj); 38 | $$.obj = field; 39 | } 40 | | field ',' fields 41 | { ArrayList field1 = (ArrayList)$3.obj; 42 | field1.add((Fields)$1.obj); 43 | $$.obj=field1; 44 | } 45 | ; 46 | 47 | field : ID { $$.obj = new Field(new Identifier($1.sval)); } 48 | | ID '[' INT_LITERAL ']' 49 | { $$.obj = new Field1(new Identifier($1.sval), $3.ival); } 50 | ; 51 | 52 | method_decl : type ID '(' args_decl ')' block method_decl 53 | { ArrayList methodDecls1=new ArrayList(); 54 | methodDecls1.add(new MethodDeclaration1((Type)$1.obj,new Identifier($2.sval),(ArrayList)$4.obj,(Block)$6.obj)); 55 | methodDecls1.addAll((ArrayList)$7.obj); 56 | $$.obj = methodDecls1; 57 | } 58 | | VOID ID '(' args_decl ')' block method_decl 59 | { ArrayList methodDecls2=new ArrayList(); 60 | 61 | methodDecls2.add(new MethodDeclaration2(new Void($1.sval),new Identifier($2.sval),(ArrayList)$4.obj,(Block)$6.obj)); 62 | methodDecls2.addAll((ArrayList)$7.obj); 63 | $$.obj =methodDecls2; 64 | } 65 | | { $$.obj = new ArrayList(); } 66 | ; 67 | 68 | args_decl :arg ',' args_decl 69 | { ArrayList argsDecl=((ArrayList)$3.obj) ; 70 | argsDecl.add((Arguement)$1.obj); 71 | $$.obj= argsDecl; 72 | } 73 | | arg 74 | { ArrayList argsDecl2=new ArrayList(); 75 | argsDecl2.add((Arguement)$1.obj); 76 | $$.obj= argsDecl2; 77 | } 78 | | { $$.obj=new ArrayList(); } 79 | ; 80 | 81 | arg : type ID ; { $$.obj= new Arguement1((Type)$1.obj, new Identifier($2.sval)); } 82 | ; 83 | 84 | vars : ID ';' { ArrayList idList=new ArrayList(); 85 | idList.add(new Identifier($1.sval)); 86 | $$.obj=idList; 87 | } 88 | | ID ',' vars { ArrayList idList2=((ArrayList)$3.obj); 89 | idList2.add(new Identifier($1.sval)); 90 | $$.obj=idList2; 91 | } 92 | ; 93 | 94 | var_decl : type vars { $$.obj = new Var_Decl((Type)$1.obj,(ArrayList) $2.obj); } 95 | ; 96 | 97 | var_decls : var_decl var_decls 98 | { ArrayList var_decls = ((ArrayList)$2.obj); 99 | var_decls.add((Var_Decl) $1.obj); 100 | $$.obj =var_decls; 101 | } 102 | | var_decl { ArrayList var_decls1= new ArrayList(); 103 | var_decls1.add((Var_Decl)$1.obj); 104 | $$.obj =var_decls1; 105 | } 106 | ; 107 | 108 | block : '{' block_body '}' 109 | { $$ = $2; } 110 | ; 111 | 112 | statements : statement { ArrayList statement1 = new ArrayList(); 113 | statement1.add((StatementIntf)$1.obj); 114 | $$.obj = statement1; 115 | } 116 | | statement statements 117 | { ArrayList statement2 = new ArrayList(); 118 | statement2.add((StatementIntf)$1.obj); 119 | statement2.addAll((ArrayList)$2.obj); 120 | $$.obj = statement2; 121 | }; 122 | 123 | block_body : var_decls statements 124 | { if($1.obj!=null && $2.obj!=null){ 125 | $$.obj= new Block1((ArrayList)$1.obj, (ArrayList)$2.obj); 126 | 127 | } 128 | if($1.obj!=null && $2.obj==null){ 129 | $$.obj= new Block2((ArrayList)$1.obj); 130 | } 131 | if ($1.obj==null && $2.obj!=null){ 132 | $$.obj= new Block3((ArrayList)$2.obj); 133 | } 134 | } 135 | | var_decls { $$.obj = new Block2((ArrayList)$1.obj); } 136 | | statements { $$.obj = new Block3((ArrayList)$1.obj); } 137 | | { $$.obj=new Block4();} 138 | ; 139 | 140 | type : INT { $$.obj=new IntegerType($1.sval); } 141 | | BOOLEAN { $$.obj=new BooleanType($1.sval); } 142 | ; 143 | 144 | statement: location ASSGN_OP expr ';' 145 | { $$.obj = new Statement1((Location)$1.obj,(ExpressionIntf)$3.obj, $2.sval); } 146 | | method_call ';' 147 | { $$=$1; } 148 | | IF '(' expr ')' block ELSE block 149 | { $$.obj = new IfElseStatement(new IF($1.sval),(ExpressionIntf)$3.obj,(Block)$5.obj, new ELSE($6.sval),(Block)$7.obj); } 150 | | IF '(' expr ')' block 151 | { $$.obj = new IfStatement(new IF($1.sval),(ExpressionIntf)$3.obj,(Block)$5.obj); } 152 | | FOR ID E_ASSIGN_OP expr ',' expr block 153 | { $$.obj = new ForStatement(new For($1.sval),new Identifier($2.sval),$3.sval,(ExpressionIntf)$4.obj,(ExpressionIntf)$6.obj,(Block)$7.obj); } 154 | | RETURN ';' { $$.obj= new ReturnStatement(new Return($1.sval)); } 155 | | RETURN expr ';' 156 | { $$.obj= new ReturnStatement1(new Return($1.sval),(ExpressionIntf)$2.obj); } 157 | | BREAK ';' { $$.obj= new BreakStatement(new Break($1.sval)); } 158 | | CONTINUE ';' { $$.obj= new ContinueStatement(new Continue($1.sval)); } 159 | | block { $$=$1; }; 160 | 161 | ASSGN_OP : ASSIGN_OP { $$=$1; } 162 | | E_ASSIGN_OP { $$=$1; } 163 | ; 164 | 165 | exprs : expr { ArrayList exprs1= new ArrayList(); 166 | exprs1.add((ExpressionIntf)$1.obj); 167 | $$.obj = exprs1; 168 | } 169 | | expr ',' exprs { ArrayList exprs2= ((ArrayList)($3.obj)); 170 | exprs2.add((ExpressionIntf)$1.obj); 171 | $$.obj =exprs2; 172 | } 173 | ; 174 | 175 | callout_args : callout_arg 176 | { ArrayList calloutargs1 = new ArrayList(); 177 | calloutargs1.add((ExpressionIntf)$1.obj); 178 | $$.obj = calloutargs1; 179 | } 180 | | callout_arg ',' callout_args ; 181 | { ArrayList calloutargs2 = new ArrayList(); 182 | calloutargs2.add((ExpressionIntf)$1.obj); 183 | calloutargs2.addAll((ArrayList)($3.obj)); 184 | 185 | $$.obj = calloutargs2; 186 | }; 187 | 188 | method_call : method_name '(' ')' 189 | { $$.obj= new MethodStatement1((Identifier)$1.obj); } 190 | | method_name '(' exprs ')' 191 | { $$.obj= new MethodStatement2((Identifier)$1.obj,(ArrayList)$3.obj); } 192 | | CALLOUT '(' STRING_LITERAL ',' callout_args ')' 193 | { $$.obj = new MethodStatementCallOut1 (new CallOut($1.sval), new StringLiteral($3.sval),(ArrayList) $5.obj); } 194 | | CALLOUT '(' STRING_LITERAL ')' 195 | { $$.obj = new MethodStatementCallOut2 (new CallOut($1.sval), new StringLiteral($3.sval)); } 196 | ; 197 | 198 | method_name : ID { $$.obj = new Identifier($1.sval); } 199 | ; 200 | 201 | location : ID { $$.obj = new Location1(new Identifier($1.sval)); } 202 | | ID '[' expr ']' 203 | { $$.obj = new Location2(new Identifier($1.sval), (ExpressionIntf) $3.obj); } 204 | ; 205 | 206 | ARTH_OP : ARITH_OP { $$=$1; } 207 | | MINUS { $$=$1; } 208 | ; 209 | 210 | expr : expr ARTH_OP term1 211 | { $$.obj = new Expression1((ExpressionIntf)$1.obj,(ExpressionIntf)$3.obj, $2.sval); } 212 | | term1 { $$=$1; } 213 | ; 214 | 215 | term1 : term1 REL_OP term2 216 | { $$.obj = new Expression1((ExpressionIntf)$1.obj,(ExpressionIntf)$3.obj, $2.sval); } 217 | | term2 { $$=$1 ;} 218 | ; 219 | 220 | 221 | term2 : term2 EQ_OP term3 222 | { $$.obj = new Expression1((ExpressionIntf)$1.obj,(ExpressionIntf)$3.obj, $2.sval); } 223 | | term3 { $$=$1 ; } 224 | ; 225 | 226 | 227 | term3 : term3 COND_OP term4 228 | { $$.obj = new Expression1((ExpressionIntf)$1.obj,(ExpressionIntf)$3.obj, $2.sval); } 229 | | term4 { $$=$1; } 230 | ; 231 | 232 | term4 : location { $$=$1; } 233 | | method_call { $$=$1; } 234 | | literal { $$=$1; } 235 | | MINUS term4 { $$.obj = new Expression2((ExpressionIntf)$2.obj, $1.sval); } 236 | | '!' term4 { $$.obj = new Expression2((ExpressionIntf)$2.obj, $1.sval); } 237 | | '(' expr ')' { $$.obj = new Expression3((ExpressionIntf)$2.obj); } 238 | ; 239 | 240 | callout_arg : expr { $$=$1; } 241 | | STRING_LITERAL 242 | { $$.obj = new callout_arg($1.sval); } 243 | ; 244 | 245 | bool_literal : TRUE {$$=$1;} 246 | | FALSE {$$=$1;} 247 | ; 248 | 249 | literal : INT_LITERAL { $$.obj = new IntegerLiteral($1.ival); } 250 | // | CHAR_LITERAL 251 | | bool_literal { $$.obj = new BooleanLiteral($1.sval); } 252 | ; 253 | 254 | %% 255 | 256 | 257 | 258 | private Yylex lexer; 259 | 260 | 261 | private int yylex () { 262 | int yyl_return = -1; 263 | try { 264 | yyl_return = lexer.yylex(); 265 | } 266 | catch (IOException e) { 267 | System.err.println("IO error :"+e); 268 | } 269 | return yyl_return; 270 | } 271 | 272 | 273 | public void yyerror (String error) { 274 | System.err.println ("Error: " + error); 275 | } 276 | 277 | 278 | public Parser(Reader r) { 279 | lexer = new Yylex(r, this); 280 | } 281 | 282 | 283 | public static void main(String args[]) throws IOException { 284 | BufferedReader br = null; 285 | if(args.length > 0){ 286 | File f = new File(args[0]); 287 | if(!f.exists()) 288 | { 289 | System.err.println(args[0]+": File doesn't exist"); 290 | System.exit(1); 291 | } 292 | br = new BufferedReader(new InputStreamReader(new FileInputStream(args[0]))); 293 | } 294 | else 295 | br = new BufferedReader(new InputStreamReader(System.in)); 296 | Parser yyparser = new Parser(br); 297 | yyparser.yyparse(); 298 | 299 | } -------------------------------------------------------------------------------- /phase-2/Ast.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | 3 | abstract class DecafIntf { 4 | public abstract void accept(Visitor v); 5 | } 6 | 7 | /* 8 | * program : CLASS Program '{' declarations '}' 9 | */ 10 | class Class { 11 | String id = null; 12 | 13 | public Class(String id) { 14 | super(); 15 | this.id = id; 16 | } 17 | 18 | public void accept(Visitor v) { 19 | v.visit(this); 20 | } 21 | 22 | } 23 | 24 | class Decaf_Class extends DecafIntf { 25 | 26 | Class cls = null; 27 | Identifier id = null; 28 | ArrayList decls = null; 29 | 30 | public Decaf_Class(Class cls, Identifier id, 31 | ArrayList decls) { 32 | super(); 33 | this.cls = cls; 34 | this.id = id; 35 | this.decls = decls; 36 | } 37 | 38 | public void accept(Visitor v) { 39 | v.visit(this); 40 | } 41 | 42 | } 43 | 44 | /* 45 | * declarations : type fields ';' declarations | method_decl; 46 | */ 47 | 48 | abstract class DeclarationsIntf extends DecafIntf { 49 | public abstract void accept(Visitor v); 50 | 51 | } 52 | 53 | class Declarations1 extends DeclarationsIntf { 54 | ArrayList methodDecls = null; 55 | 56 | public Declarations1(ArrayList methodDecls) { 57 | super(); 58 | this.methodDecls = methodDecls; 59 | } 60 | 61 | public void setMethodDecls(ArrayList methodDecls) { 62 | this.methodDecls = methodDecls; 63 | } 64 | 65 | public void accept(Visitor v) { 66 | v.visit(this); 67 | } 68 | 69 | } 70 | 71 | abstract class FieldDeclIntf extends DeclarationsIntf { 72 | public abstract void accept(Visitor v); 73 | 74 | } 75 | 76 | class FieldDecl extends FieldDeclIntf { 77 | Type type = null; 78 | ArrayList fields = null; 79 | 80 | public FieldDecl(Type type, ArrayList fields) { 81 | super(); 82 | this.type = type; 83 | this.fields = fields; 84 | } 85 | 86 | public void accept(Visitor v) { 87 | v.visit(this); 88 | } 89 | 90 | } 91 | 92 | /* 93 | * fields : field | field ',' fields ; 94 | */ 95 | 96 | abstract class Fields extends DecafIntf { 97 | public abstract void accept(Visitor v); 98 | 99 | } 100 | 101 | class Fields1 extends Fields { 102 | Field field = null; 103 | 104 | public Fields1(Field field) { 105 | super(); 106 | this.field = field; 107 | } 108 | 109 | public void accept(Visitor v) { 110 | v.visit(this); 111 | } 112 | 113 | } 114 | 115 | /* 116 | * field : ID | ID '[' INT_LITERAL ']' ; 117 | */ 118 | 119 | abstract class FieldIntf extends Fields { 120 | public abstract void accept(Visitor v); 121 | 122 | } 123 | 124 | class Identifier { 125 | String id = null; 126 | 127 | public Identifier(String id) { 128 | super(); 129 | this.id = id; 130 | } 131 | 132 | public void accept(Visitor v) { 133 | v.visit(this); 134 | } 135 | 136 | } 137 | 138 | class Field extends FieldIntf { 139 | Identifier id = null; 140 | 141 | public Field(Identifier id) { 142 | super(); 143 | this.id = id; 144 | } 145 | 146 | public void accept(Visitor v) { 147 | v.visit(this); 148 | } 149 | 150 | } 151 | 152 | class Field1 extends FieldIntf { 153 | Identifier id = null; 154 | int val = -1; 155 | 156 | public Field1(Identifier id, int val) { 157 | super(); 158 | this.id = id; 159 | this.val = val; 160 | } 161 | 162 | public void accept(Visitor v) { 163 | v.visit(this); 164 | 165 | } 166 | } 167 | 168 | abstract class MethodDeclarations extends DeclarationsIntf { 169 | public abstract void accept(Visitor v); 170 | 171 | } 172 | 173 | class MethodDeclaration1 extends MethodDeclarations { 174 | Type type = null; 175 | Identifier id = null; 176 | ArrayList argList = null; 177 | Block block = null; 178 | 179 | public MethodDeclaration1(Type type, Identifier id, 180 | ArrayList argList, Block block) { 181 | super(); 182 | this.type = type; 183 | this.id = id; 184 | this.argList = argList; 185 | this.block = block; 186 | } 187 | 188 | public void accept(Visitor v) { 189 | v.visit(this); 190 | } 191 | 192 | } 193 | 194 | class MethodDeclaration2 extends MethodDeclarations { 195 | Void type = null; 196 | Identifier id = null; 197 | ArrayList argList = null; 198 | Block block = null; 199 | 200 | public MethodDeclaration2(Void type, Identifier id, 201 | ArrayList argList, Block block) { 202 | super(); 203 | this.type = type; 204 | this.id = id; 205 | this.argList = argList; 206 | this.block = block; 207 | } 208 | 209 | public void accept(Visitor v) { 210 | v.visit(this); 211 | } 212 | 213 | } 214 | 215 | /* 216 | * args_decl :arg ',' args_decl | arg | ; arg : type ID ; 217 | */ 218 | abstract class Arguement { 219 | public abstract void accept(Visitor v); 220 | 221 | } 222 | 223 | class Arguement1 extends Arguement { 224 | Type type = null; 225 | Identifier id = null; 226 | 227 | public Arguement1(Type type, Identifier sym) { 228 | super(); 229 | this.type = type; 230 | this.id = sym; 231 | } 232 | 233 | public void accept(Visitor v) { 234 | v.visit(this); 235 | } 236 | 237 | } 238 | 239 | /* 240 | * vars : ID ';' | ID ',' vars; var_decl : type vars ; var_decls : var_decl 241 | * var_decls | var_decl ; 242 | */ 243 | 244 | class Var_Decl { 245 | Type type; 246 | ArrayList vars = null; 247 | 248 | public Var_Decl(Type type, ArrayList vars) { 249 | super(); 250 | this.type = type; 251 | this.vars = vars; 252 | } 253 | 254 | public void accept(Visitor v) { 255 | v.visit(this); 256 | } 257 | 258 | } 259 | 260 | /* 261 | * statements : statement | statement statements ; 262 | */ 263 | 264 | /* 265 | * statement: location ASSGN_OP expr ';' | method_call ';' {$$=$1;} | IF '(' 266 | * expr ')' block ELSE block | IF '(' expr ')' block | FOR ID E_ASSIGN_OP expr 267 | * ',' expr block | RETURN ';' | RETURN expr ';' | BREAK ';' | CONTINUE ';' | 268 | * block {$$=$1;}; 269 | */ 270 | 271 | abstract class StatementIntf { 272 | public abstract void accept(Visitor v); 273 | 274 | } 275 | 276 | class Statement1 extends StatementIntf { 277 | Location loc = null; 278 | ExpressionIntf expr = null; 279 | char op; 280 | 281 | public Statement1(Location loc, ExpressionIntf expr, char op) { 282 | super(); 283 | this.loc = loc; 284 | this.expr = expr; 285 | this.op = op; 286 | } 287 | 288 | public void accept(Visitor v) { 289 | v.visit(this); 290 | } 291 | 292 | } 293 | 294 | class Return { 295 | String s = null; 296 | 297 | public Return(String s) { 298 | super(); 299 | this.s = s; 300 | } 301 | 302 | public void accept(Visitor v) { 303 | v.visit(this); 304 | } 305 | 306 | } 307 | 308 | class ReturnStatement extends StatementIntf { 309 | 310 | Return s = null; 311 | 312 | public ReturnStatement(Return s) { 313 | super(); 314 | this.s = s; 315 | } 316 | 317 | public void accept(Visitor v) { 318 | v.visit(this); 319 | } 320 | 321 | } 322 | 323 | class ReturnStatement1 extends StatementIntf { 324 | Return s = null; 325 | ExpressionIntf expr = null; 326 | 327 | public ReturnStatement1(Return s, ExpressionIntf expr) { 328 | super(); 329 | this.s = s; 330 | this.expr = expr; 331 | } 332 | 333 | public void accept(Visitor v) { 334 | v.visit(this); 335 | } 336 | 337 | } 338 | 339 | class Break { 340 | String s = null; 341 | 342 | public Break(String s) { 343 | super(); 344 | this.s = s; 345 | } 346 | 347 | public void accept(Visitor v) { 348 | v.visit(this); 349 | } 350 | 351 | } 352 | 353 | class BreakStatement extends StatementIntf { 354 | Break s = null; 355 | 356 | public BreakStatement(Break s) { 357 | super(); 358 | this.s = s; 359 | } 360 | 361 | public void accept(Visitor v) { 362 | v.visit(this); 363 | } 364 | 365 | } 366 | 367 | class Continue { 368 | 369 | String s = null; 370 | 371 | public Continue(String s) { 372 | super(); 373 | this.s = s; 374 | } 375 | 376 | public void accept(Visitor v) { 377 | v.visit(this); 378 | } 379 | 380 | } 381 | 382 | class ContinueStatement extends StatementIntf { 383 | Continue s = null; 384 | 385 | public ContinueStatement(Continue s) { 386 | super(); 387 | this.s = s; 388 | } 389 | 390 | public void accept(Visitor v) { 391 | v.visit(this); 392 | } 393 | 394 | } 395 | 396 | class IfStatement extends StatementIntf { 397 | public IF s1 = null; 398 | public ExpressionIntf expr = null; 399 | public Block block = null; 400 | 401 | public IfStatement(IF s1, ExpressionIntf expr, Block block) { 402 | super(); 403 | this.s1 = s1; 404 | this.expr = expr; 405 | this.block = block; 406 | } 407 | 408 | public void accept(Visitor v) { 409 | v.visit(this); 410 | } 411 | 412 | } 413 | 414 | class IF { 415 | public String s1 = null; 416 | 417 | public IF(String s1) { 418 | super(); 419 | this.s1 = s1; 420 | } 421 | 422 | public void accept(Visitor v) { 423 | v.visit(this); 424 | } 425 | 426 | } 427 | 428 | class ELSE { 429 | String s = null; 430 | 431 | public ELSE(String s) { 432 | super(); 433 | this.s = s; 434 | } 435 | 436 | public void accept(Visitor v) { 437 | v.visit(this); 438 | } 439 | 440 | } 441 | 442 | class IfElseStatement extends StatementIntf { 443 | IF s1 = null; 444 | public ExpressionIntf expr = null; 445 | public Block ifBlock = null; 446 | public ELSE s2 = null; 447 | public Block elseBlock = null; 448 | 449 | public IfElseStatement(IF s1, ExpressionIntf expr, Block ifBlock, ELSE s2, 450 | Block elseBlock) { 451 | super(); 452 | this.s1 = s1; 453 | this.expr = expr; 454 | this.ifBlock = ifBlock; 455 | this.s2 = s2; 456 | this.elseBlock = elseBlock; 457 | } 458 | 459 | public void accept(Visitor v) { 460 | v.visit(this); 461 | } 462 | 463 | } 464 | 465 | class For { 466 | String s = null; 467 | 468 | public For(String s) { 469 | super(); 470 | this.s = s; 471 | } 472 | 473 | public void accept(Visitor v) { 474 | v.visit(this); 475 | } 476 | 477 | } 478 | 479 | class ForStatement extends StatementIntf { 480 | public For s = null; 481 | public Identifier id = null; 482 | public char ch = 0; 483 | public ExpressionIntf expr1 = null; 484 | public ExpressionIntf expr2 = null; 485 | public Block block = null; 486 | 487 | public ForStatement(For s, Identifier id, char ch, ExpressionIntf expr1, 488 | ExpressionIntf expr2, Block block) { 489 | super(); 490 | this.s = s; 491 | this.id = id; 492 | this.ch = ch; 493 | this.expr1 = expr1; 494 | this.expr2 = expr2; 495 | this.block = block; 496 | } 497 | 498 | public void accept(Visitor v) { 499 | v.visit(this); 500 | } 501 | 502 | } 503 | 504 | /* 505 | * 506 | * block_body : var_decls statements | var_decls | statements ; 507 | */ 508 | 509 | abstract class Block extends StatementIntf { 510 | public abstract void accept(Visitor v); 511 | } 512 | 513 | class Block1 extends Block { 514 | ArrayList vars = null; 515 | ArrayList stmts = null; 516 | 517 | public Block1(ArrayList vars, ArrayList stmts) { 518 | 519 | super(); 520 | this.vars = vars; 521 | this.stmts = stmts; 522 | 523 | } 524 | 525 | public void accept(Visitor v) { 526 | v.visit(this); 527 | } 528 | } 529 | 530 | class Block2 extends Block { 531 | ArrayList vars = null; 532 | 533 | public Block2(ArrayList vars) { 534 | this.vars = vars; 535 | } 536 | 537 | public void accept(Visitor v) { 538 | v.visit(this); 539 | } 540 | } 541 | 542 | class Block3 extends Block { 543 | ArrayList stmts = null; 544 | 545 | public Block3(ArrayList stmts) { 546 | this.stmts = stmts; 547 | } 548 | 549 | public void accept(Visitor v) { 550 | v.visit(this); 551 | } 552 | } 553 | 554 | /* 555 | * 556 | * type : INT | BOOLEAN ; 557 | */ 558 | 559 | abstract class Type { 560 | public abstract void accept(Visitor v); 561 | 562 | } 563 | 564 | class IntegerType extends Type { 565 | String s = null; 566 | 567 | public IntegerType(String s) { 568 | super(); 569 | this.s = s; 570 | } 571 | 572 | public void accept(Visitor v) { 573 | v.visit(this); 574 | } 575 | 576 | } 577 | 578 | class BooleanType extends Type { 579 | String s = null; 580 | 581 | public BooleanType(String s) { 582 | super(); 583 | this.s = s; 584 | } 585 | 586 | public void accept(Visitor v) { 587 | v.visit(this); 588 | } 589 | 590 | } 591 | 592 | class Void { 593 | 594 | String s = null; 595 | 596 | public Void(String s) { 597 | super(); 598 | this.s = s; 599 | } 600 | 601 | public void accept(Visitor v) { 602 | v.visit(this); 603 | } 604 | 605 | } 606 | 607 | /* 608 | * exprs : expr | expr ',' exprs ; 609 | */ 610 | abstract class ExpressionIntf { 611 | public abstract void accept(Visitor v); 612 | 613 | } 614 | 615 | /* 616 | * callout_args : callout_arg | callout_arg ',' callout_args ; 617 | */ 618 | 619 | /* 620 | * callout_arg : expr | STRING_LITERAL ; 621 | */ 622 | 623 | abstract class callout_args { 624 | public abstract void accept(Visitor v); 625 | 626 | } 627 | 628 | class callout_arg { 629 | String s = null; 630 | 631 | public callout_arg(String s) { 632 | super(); 633 | this.s = s; 634 | } 635 | 636 | public void accept(Visitor v) { 637 | v.visit(this); 638 | } 639 | 640 | } 641 | 642 | /* 643 | * method_call : method_name '(' ')' | method_name '(' exprs ')' | CALLOUT '(' 644 | * STRING_LITERAL ',' callout_args ')' | CALLOUT '(' STRING_LITERAL ')' ; 645 | */ 646 | 647 | /* 648 | * method_name : ID 649 | */ 650 | 651 | class MethodStatement1 extends StatementIntf { 652 | Identifier id = null; 653 | 654 | public MethodStatement1(Identifier id) { 655 | super(); 656 | this.id = id; 657 | } 658 | 659 | public void accept(Visitor v) { 660 | v.visit(this); 661 | } 662 | 663 | } 664 | 665 | class MethodStatement2 extends StatementIntf { 666 | Identifier id = null; 667 | 668 | ArrayList exprs = null; 669 | 670 | public MethodStatement2(Identifier id, ArrayList exprs) { 671 | super(); 672 | this.id = id; 673 | this.exprs = exprs; 674 | } 675 | 676 | public void accept(Visitor v) { 677 | v.visit(this); 678 | } 679 | 680 | } 681 | 682 | class StringLiteral { 683 | String s = null; 684 | 685 | public StringLiteral(String s) { 686 | super(); 687 | this.s = s; 688 | } 689 | 690 | public void accept(Visitor v) { 691 | v.visit(this); 692 | } 693 | 694 | } 695 | 696 | class MethodStatementCallOut1 extends StatementIntf { 697 | CallOut s = null; 698 | StringLiteral s1 = null; 699 | ArrayList calloutargs = null; 700 | 701 | public MethodStatementCallOut1(CallOut s, StringLiteral s1, 702 | ArrayList calloutargs) { 703 | super(); 704 | this.s = s; 705 | this.s1 = s1; 706 | this.calloutargs = calloutargs; 707 | } 708 | 709 | public void accept(Visitor v) { 710 | v.visit(this); 711 | } 712 | 713 | } 714 | 715 | class CallOut { 716 | String s = null; 717 | 718 | public CallOut(String s) { 719 | super(); 720 | this.s = s; 721 | } 722 | 723 | public void accept(Visitor v) { 724 | v.visit(this); 725 | } 726 | 727 | } 728 | 729 | class MethodStatementCallOut2 extends StatementIntf { 730 | CallOut s = null; 731 | StringLiteral s1 = null; 732 | 733 | public MethodStatementCallOut2(CallOut s, StringLiteral s1) { 734 | super(); 735 | this.s = s; 736 | this.s1 = s1; 737 | } 738 | 739 | public void accept(Visitor v) { 740 | v.visit(this); 741 | } 742 | 743 | } 744 | 745 | /* 746 | * location : ID { $$.obj = new Identifier($1.sval); } | ID '[' expr ']' ; 747 | */ 748 | abstract class Location extends ExpressionIntf { 749 | public abstract void accept(Visitor v); 750 | 751 | } 752 | 753 | class Location1 extends Location { 754 | Identifier id = null; 755 | 756 | public Location1(Identifier id) { 757 | super(); 758 | this.id = id; 759 | } 760 | 761 | public void accept(Visitor v) { 762 | v.visit(this); 763 | } 764 | 765 | } 766 | 767 | class Location2 extends Location { 768 | Identifier id = null; 769 | ExpressionIntf expr = null; 770 | 771 | public Location2(Identifier id, ExpressionIntf expr) { 772 | super(); 773 | this.id = id; 774 | this.expr = expr; 775 | } 776 | 777 | public void accept(Visitor v) { 778 | v.visit(this); 779 | } 780 | 781 | } 782 | 783 | /* 784 | * expr : expr ARTH_OP term1 | term1 ; term1 : term1 REL_OP term2 | term2 ; 785 | * term2 : term2 EQ_OP term3 | term3 ; term3 : term3 COND_OP term4 | term4 ; 786 | * term4 : location | method_call | literal | MINUS term4 | '!' term4 | '(' expr 787 | * ')' ; 788 | */ 789 | 790 | class Expression1 extends ExpressionIntf { 791 | ExpressionIntf expr = null; 792 | ExpressionIntf term1 = null; 793 | char operator; 794 | 795 | public Expression1(ExpressionIntf expr, ExpressionIntf term1, char operator) { 796 | super(); 797 | this.expr = expr; 798 | this.term1 = term1; 799 | this.operator = operator; 800 | } 801 | 802 | public void accept(Visitor v) { 803 | v.visit(this); 804 | } 805 | 806 | } 807 | 808 | class Expression2 extends ExpressionIntf { 809 | ExpressionIntf term5 = null; 810 | char operator; 811 | 812 | public Expression2(ExpressionIntf term5, char operator) { 813 | super(); 814 | this.term5 = term5; 815 | this.operator = operator; 816 | } 817 | 818 | public void accept(Visitor v) { 819 | v.visit(this); 820 | } 821 | 822 | } 823 | 824 | class Expression3 extends ExpressionIntf { 825 | ExpressionIntf term5 = null; 826 | 827 | public Expression3(ExpressionIntf term5) { 828 | super(); 829 | this.term5 = term5; 830 | } 831 | 832 | public void accept(Visitor v) { 833 | v.visit(this); 834 | } 835 | 836 | } 837 | 838 | /* 839 | * literal : INT_LITERAL | CHAR_LITERAL | bool_literal ; 840 | */ 841 | 842 | abstract class Literal extends ExpressionIntf { 843 | public abstract void accept(Visitor v); 844 | 845 | } 846 | 847 | class IntegerLiteral extends Literal { 848 | public int value = 0; 849 | 850 | public IntegerLiteral(int value) { 851 | super(); 852 | this.value = value; 853 | } 854 | 855 | public void accept(Visitor v) { 856 | v.visit(this); 857 | } 858 | 859 | } 860 | 861 | class BooleanLiteral extends Literal { 862 | public String value; 863 | 864 | public BooleanLiteral(String value) { 865 | super(); 866 | this.value = value; 867 | } 868 | 869 | public void accept(Visitor v) { 870 | v.visit(this); 871 | } 872 | 873 | } 874 | -------------------------------------------------------------------------------- /phase-3/src/Ast.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import org.jllvm.LLVMFunction; 3 | import org.jllvm.LLVMIntegerType; 4 | import org.jllvm.LLVMValue; 5 | import org.jllvm.LLVMVoidType; 6 | 7 | 8 | 9 | abstract class DecafIntf { 10 | public abstract LLVMValue accept(Visitor v); 11 | } 12 | 13 | /* 14 | * program : CLASS Program '{' declarations '}' 15 | */ 16 | class Class { 17 | String id = null; 18 | 19 | public Class(String id) { 20 | super(); 21 | this.id = id; 22 | } 23 | 24 | public String accept(Visitor v) { 25 | return v.visit(this); 26 | } 27 | 28 | } 29 | 30 | class Decaf_Class extends DecafIntf { 31 | 32 | Class cls = null; 33 | Identifier id = null; 34 | ArrayList decls = null; 35 | 36 | public Decaf_Class(Class cls, Identifier id, 37 | ArrayList decls) { 38 | super(); 39 | this.cls = cls; 40 | this.id = id; 41 | this.decls = decls; 42 | } 43 | 44 | public LLVMValue accept(Visitor v) { 45 | return v.visit(this); 46 | } 47 | 48 | } 49 | 50 | /* 51 | * declarations : type fields ';' declarations | method_decl; 52 | */ 53 | 54 | abstract class DeclarationsIntf extends DecafIntf { 55 | public abstract LLVMValue accept(Visitor v); 56 | 57 | } 58 | 59 | class Declarations1 extends DeclarationsIntf { 60 | ArrayList methodDecls = null; 61 | 62 | public Declarations1(ArrayList methodDecls) { 63 | super(); 64 | this.methodDecls = methodDecls; 65 | } 66 | 67 | public void setMethodDecls(ArrayList methodDecls) { 68 | this.methodDecls = methodDecls; 69 | } 70 | 71 | public LLVMValue accept(Visitor v) { 72 | return v.visit(this); 73 | } 74 | 75 | } 76 | 77 | abstract class FieldDeclIntf extends DeclarationsIntf { 78 | public abstract LLVMValue accept(Visitor v); 79 | 80 | } 81 | 82 | class FieldDecl extends FieldDeclIntf { 83 | Type type = null; 84 | ArrayList fields = null; 85 | 86 | public FieldDecl(Type type, ArrayList fields) { 87 | super(); 88 | this.type = type; 89 | this.fields = fields; 90 | } 91 | 92 | public LLVMValue accept(Visitor v) { 93 | return v.visit(this); 94 | } 95 | 96 | } 97 | 98 | /* 99 | * fields : field | field ',' fields ; 100 | */ 101 | 102 | abstract class Fields{ 103 | public abstract void accept(Visitor v); 104 | 105 | } 106 | 107 | class Fields1 extends Fields { 108 | Field field = null; 109 | 110 | public Fields1(Field field) { 111 | super(); 112 | this.field = field; 113 | } 114 | 115 | public void accept(Visitor v) { 116 | v.visit(this); 117 | } 118 | 119 | } 120 | 121 | /* 122 | * field : ID | ID '[' INT_LITERAL ']' ; 123 | */ 124 | 125 | 126 | class Identifier { 127 | String id = null; 128 | 129 | public Identifier(String id) { 130 | super(); 131 | this.id = id; 132 | } 133 | 134 | public String accept(Visitor v) { 135 | return v.visit(this); 136 | } 137 | 138 | } 139 | 140 | class Field extends Fields { 141 | Identifier id = null; 142 | 143 | public Field(Identifier id) { 144 | super(); 145 | this.id = id; 146 | } 147 | 148 | public void accept(Visitor v) { 149 | v.visit(this); 150 | } 151 | 152 | } 153 | 154 | class Field1 extends Fields { 155 | Identifier id = null; 156 | int val = -1; 157 | 158 | public Field1(Identifier id, int val) { 159 | super(); 160 | this.id = id; 161 | this.val = val; 162 | } 163 | 164 | public void accept(Visitor v) { 165 | v.visit(this); 166 | 167 | } 168 | } 169 | 170 | abstract class MethodDeclarations extends DeclarationsIntf { 171 | public abstract LLVMValue accept(Visitor v); 172 | 173 | } 174 | 175 | class MethodDeclaration1 extends MethodDeclarations { 176 | Type type = null; 177 | Identifier id = null; 178 | ArrayList argList = null; 179 | Block block = null; 180 | 181 | public MethodDeclaration1(Type type, Identifier id, 182 | ArrayList argList, Block block) { 183 | super(); 184 | this.type = type; 185 | this.id = id; 186 | this.argList = argList; 187 | this.block = block; 188 | } 189 | 190 | public LLVMFunction accept(Visitor v) { 191 | return v.visit(this); 192 | } 193 | 194 | } 195 | 196 | class MethodDeclaration2 extends MethodDeclarations { 197 | Void type = null; 198 | Identifier id = null; 199 | ArrayList argList = null; 200 | Block block = null; 201 | 202 | public MethodDeclaration2(Void type, Identifier id, 203 | ArrayList argList, Block block) { 204 | super(); 205 | this.type = type; 206 | this.id = id; 207 | this.argList = argList; 208 | this.block = block; 209 | } 210 | 211 | public LLVMFunction accept(Visitor v) { 212 | return v.visit(this); 213 | } 214 | 215 | } 216 | 217 | /* 218 | * args_decl :arg ',' args_decl | arg | ; arg : type ID ; 219 | */ 220 | abstract class Arguement { 221 | public abstract NameTypePair accept(Visitor v); 222 | 223 | } 224 | 225 | class Arguement1 extends Arguement { 226 | Type type = null; 227 | Identifier id = null; 228 | 229 | public Arguement1(Type type, Identifier sym) { 230 | super(); 231 | this.type = type; 232 | this.id = sym; 233 | } 234 | 235 | public NameTypePair accept(Visitor v) { 236 | return v.visit(this); 237 | } 238 | 239 | } 240 | 241 | /* 242 | * vars : ID ';' | ID ',' vars; var_decl : type vars ; var_decls : var_decl 243 | * var_decls | var_decl ; 244 | */ 245 | 246 | class Var_Decl { 247 | Type type; 248 | ArrayList vars = null; 249 | 250 | public Var_Decl(Type type, ArrayList vars) { 251 | super(); 252 | this.type = type; 253 | this.vars = vars; 254 | } 255 | 256 | public void accept(Visitor v) { 257 | v.visit(this); 258 | } 259 | 260 | } 261 | 262 | /* 263 | * statements : statement | statement statements ; 264 | */ 265 | 266 | /* 267 | * statement: location ASSGN_OP expr ';' | method_call ';' {$$=$1;} | IF '(' 268 | * expr ')' block ELSE block | IF '(' expr ')' block | FOR ID E_ASSIGN_OP expr 269 | * ',' expr block | RETURN ';' | RETURN expr ';' | BREAK ';' | CONTINUE ';' | 270 | * block {$$=$1;}; 271 | */ 272 | 273 | abstract class StatementIntf extends ExpressionIntf { 274 | public abstract LLVMValue accept(Visitor v); 275 | 276 | } 277 | 278 | class Statement1 extends StatementIntf { 279 | Location loc = null; 280 | ExpressionIntf expr = null; 281 | String op=null; 282 | 283 | public Statement1(Location loc, ExpressionIntf expr, String op) { 284 | super(); 285 | this.loc = loc; 286 | this.expr = expr; 287 | this.op = op; 288 | } 289 | 290 | public LLVMValue accept(Visitor v) { 291 | return v.visit(this); 292 | } 293 | 294 | } 295 | 296 | class Return { 297 | String s = null; 298 | 299 | public Return(String s) { 300 | super(); 301 | this.s = s; 302 | } 303 | 304 | public String accept(Visitor v) { 305 | return v.visit(this); 306 | } 307 | 308 | } 309 | 310 | class ReturnStatement extends StatementIntf { 311 | 312 | Return s = null; 313 | 314 | public ReturnStatement(Return s) { 315 | super(); 316 | this.s = s; 317 | } 318 | 319 | public LLVMValue accept(Visitor v) { 320 | return v.visit(this); 321 | } 322 | 323 | } 324 | 325 | class ReturnStatement1 extends StatementIntf { 326 | Return s = null; 327 | ExpressionIntf expr = null; 328 | 329 | public ReturnStatement1(Return s, ExpressionIntf expr) { 330 | super(); 331 | this.s = s; 332 | this.expr = expr; 333 | } 334 | 335 | public LLVMValue accept(Visitor v) { 336 | return v.visit(this); 337 | } 338 | 339 | } 340 | 341 | class Break { 342 | String s = null; 343 | 344 | public Break(String s) { 345 | super(); 346 | this.s = s; 347 | } 348 | 349 | public String accept(Visitor v) { 350 | return v.visit(this); 351 | } 352 | 353 | } 354 | 355 | class BreakStatement extends StatementIntf { 356 | Break s = null; 357 | 358 | public BreakStatement(Break s) { 359 | super(); 360 | this.s = s; 361 | } 362 | 363 | public LLVMValue accept(Visitor v) { 364 | return v.visit(this); 365 | } 366 | 367 | } 368 | 369 | class Continue { 370 | 371 | String s = null; 372 | 373 | public Continue(String s) { 374 | super(); 375 | this.s = s; 376 | } 377 | 378 | public String accept(Visitor v) { 379 | return v.visit(this); 380 | } 381 | 382 | } 383 | 384 | class ContinueStatement extends StatementIntf { 385 | Continue s = null; 386 | 387 | public ContinueStatement(Continue s) { 388 | super(); 389 | this.s = s; 390 | } 391 | 392 | public LLVMValue accept(Visitor v) { 393 | return v.visit(this); 394 | } 395 | 396 | } 397 | 398 | class IfStatement extends StatementIntf { 399 | public IF s1 = null; 400 | public ExpressionIntf expr = null; 401 | public Block block = null; 402 | 403 | public IfStatement(IF s1, ExpressionIntf expr, Block block) { 404 | super(); 405 | this.s1 = s1; 406 | this.expr = expr; 407 | this.block = block; 408 | } 409 | 410 | public LLVMValue accept(Visitor v) { 411 | return v.visit(this); 412 | } 413 | 414 | } 415 | 416 | class IF { 417 | public String s1 = null; 418 | 419 | public IF(String s1) { 420 | super(); 421 | this.s1 = s1; 422 | } 423 | 424 | public String accept(Visitor v) { 425 | return v.visit(this); 426 | } 427 | 428 | } 429 | 430 | class ELSE { 431 | String s = null; 432 | 433 | public ELSE(String s) { 434 | super(); 435 | this.s = s; 436 | } 437 | 438 | public String accept(Visitor v) { 439 | return v.visit(this); 440 | } 441 | 442 | } 443 | 444 | class IfElseStatement extends StatementIntf { 445 | IF s1 = null; 446 | public ExpressionIntf expr = null; 447 | public Block ifBlock = null; 448 | public ELSE s2 = null; 449 | public Block elseBlock = null; 450 | 451 | public IfElseStatement(IF s1, ExpressionIntf expr, Block ifBlock, ELSE s2, 452 | Block elseBlock) { 453 | super(); 454 | this.s1 = s1; 455 | this.expr = expr; 456 | this.ifBlock = ifBlock; 457 | this.s2 = s2; 458 | this.elseBlock = elseBlock; 459 | } 460 | 461 | public LLVMValue accept(Visitor v) { 462 | return v.visit(this); 463 | } 464 | 465 | } 466 | 467 | class For { 468 | String s = null; 469 | 470 | public For(String s) { 471 | super(); 472 | this.s = s; 473 | } 474 | 475 | public String accept(Visitor v) { 476 | return v.visit(this); 477 | } 478 | 479 | } 480 | 481 | class ForStatement extends StatementIntf { 482 | public For s = null; 483 | public Identifier id = null; 484 | public String ch = null; 485 | public ExpressionIntf expr1 = null; 486 | public ExpressionIntf expr2 = null; 487 | public Block block = null; 488 | 489 | public ForStatement(For s, Identifier id, String ch, ExpressionIntf expr1, 490 | ExpressionIntf expr2, Block block) { 491 | super(); 492 | this.s = s; 493 | this.id = id; 494 | this.ch = ch; 495 | this.expr1 = expr1; 496 | this.expr2 = expr2; 497 | this.block = block; 498 | } 499 | 500 | public LLVMValue accept(Visitor v) { 501 | return v.visit(this); 502 | } 503 | 504 | } 505 | 506 | /* 507 | * 508 | * block_body : var_decls statements | var_decls | statements ; 509 | */ 510 | 511 | abstract class Block extends StatementIntf { 512 | public abstract LLVMValue accept(Visitor v); 513 | } 514 | 515 | class Block1 extends Block { 516 | ArrayList vars = null; 517 | ArrayList stmts = null; 518 | 519 | public Block1(ArrayList vars, ArrayList stmts) { 520 | 521 | super(); 522 | this.vars = vars; 523 | this.stmts = stmts; 524 | 525 | } 526 | 527 | public LLVMValue accept(Visitor v) { 528 | return v.visit(this); 529 | } 530 | } 531 | 532 | class Block2 extends Block { 533 | ArrayList vars = null; 534 | 535 | public Block2(ArrayList vars) { 536 | this.vars = vars; 537 | } 538 | 539 | public LLVMValue accept(Visitor v) { 540 | return v.visit(this); 541 | } 542 | } 543 | 544 | class Block3 extends Block { 545 | ArrayList stmts = null; 546 | 547 | public Block3(ArrayList stmts) { 548 | this.stmts = stmts; 549 | } 550 | 551 | public LLVMValue accept(Visitor v) { 552 | return v.visit(this); 553 | } 554 | } 555 | class Block4 extends Block{ 556 | 557 | @Override 558 | public LLVMValue accept(Visitor v) { 559 | 560 | return null; 561 | } 562 | 563 | } 564 | 565 | /* 566 | * 567 | * type : INT | BOOLEAN ; 568 | */ 569 | 570 | abstract class Type { 571 | public abstract LLVMIntegerType accept(Visitor v); 572 | 573 | } 574 | 575 | class IntegerType extends Type { 576 | String s = null; 577 | 578 | public IntegerType(String s) { 579 | super(); 580 | this.s = s; 581 | } 582 | 583 | public LLVMIntegerType accept(Visitor v) { 584 | return v.visit(this); 585 | } 586 | 587 | } 588 | 589 | class BooleanType extends Type { 590 | String s = null; 591 | 592 | public BooleanType(String s) { 593 | super(); 594 | this.s = s; 595 | } 596 | 597 | public LLVMIntegerType accept(Visitor v) { 598 | return v.visit(this); 599 | } 600 | 601 | } 602 | 603 | class Void { 604 | 605 | String s = null; 606 | 607 | public Void(String s) { 608 | super(); 609 | this.s = s; 610 | } 611 | 612 | public LLVMVoidType accept(Visitor v) { 613 | return v.visit(this); 614 | } 615 | 616 | } 617 | 618 | /* 619 | * exprs : expr | expr ',' exprs ; 620 | */ 621 | abstract class ExpressionIntf { 622 | public abstract LLVMValue accept(Visitor v); 623 | 624 | } 625 | 626 | /* 627 | * callout_args : callout_arg | callout_arg ',' callout_args ; 628 | */ 629 | 630 | /* 631 | * callout_arg : expr | STRING_LITERAL ; 632 | */ 633 | 634 | abstract class callout_args extends ExpressionIntf{ 635 | public abstract LLVMValue accept(Visitor v); 636 | 637 | } 638 | 639 | class callout_arg extends callout_args { 640 | String s = null; 641 | 642 | public callout_arg(String s) { 643 | super(); 644 | this.s = s; 645 | } 646 | 647 | public LLVMValue accept(Visitor v) { 648 | return v.visit(this); 649 | } 650 | 651 | } 652 | class callout_arg1 extends callout_args { 653 | ExpressionIntf expr=null; 654 | 655 | public callout_arg1(ExpressionIntf expr) { 656 | super(); 657 | this.expr=expr; 658 | } 659 | 660 | public LLVMValue accept(Visitor v) { 661 | return v.visit(this); 662 | } 663 | 664 | } 665 | 666 | /* 667 | * method_call : method_name '(' ')' | method_name '(' exprs ')' | CALLOUT '(' 668 | * STRING_LITERAL ',' callout_args ')' | CALLOUT '(' STRING_LITERAL ')' ; 669 | */ 670 | 671 | /* 672 | * method_name : ID 673 | */ 674 | 675 | class MethodStatement1 extends StatementIntf { 676 | Identifier id = null; 677 | 678 | public MethodStatement1(Identifier id) { 679 | super(); 680 | this.id = id; 681 | } 682 | 683 | public LLVMValue accept(Visitor v) { 684 | return v.visit(this); 685 | } 686 | 687 | } 688 | 689 | class MethodStatement2 extends StatementIntf { 690 | Identifier id = null; 691 | 692 | ArrayList exprs = null; 693 | 694 | public MethodStatement2(Identifier id, ArrayList exprs) { 695 | super(); 696 | this.id = id; 697 | this.exprs = exprs; 698 | } 699 | 700 | public LLVMValue accept(Visitor v) { 701 | return v.visit(this); 702 | } 703 | 704 | } 705 | 706 | class StringLiteral { 707 | String s = null; 708 | 709 | public StringLiteral(String s) { 710 | super(); 711 | this.s = s; 712 | } 713 | 714 | public String accept(Visitor v) { 715 | return v.visit(this); 716 | } 717 | 718 | } 719 | 720 | class MethodStatementCallOut1 extends StatementIntf { 721 | CallOut s = null; 722 | StringLiteral s1 = null; 723 | ArrayList calloutargs = null; 724 | 725 | public MethodStatementCallOut1(CallOut s, StringLiteral s1, 726 | ArrayList calloutargs) { 727 | super(); 728 | this.s = s; 729 | this.s1 = s1; 730 | this.calloutargs = calloutargs; 731 | } 732 | 733 | public LLVMValue accept(Visitor v) { 734 | return v.visit(this); 735 | } 736 | 737 | } 738 | 739 | class CallOut { 740 | String s = null; 741 | 742 | public CallOut(String s) { 743 | super(); 744 | this.s = s; 745 | } 746 | 747 | public String accept(Visitor v) { 748 | return v.visit(this); 749 | } 750 | 751 | } 752 | 753 | class MethodStatementCallOut2 extends StatementIntf { 754 | CallOut s = null; 755 | StringLiteral s1 = null; 756 | 757 | public MethodStatementCallOut2(CallOut s, StringLiteral s1) { 758 | super(); 759 | this.s = s; 760 | this.s1 = s1; 761 | } 762 | 763 | public LLVMValue accept(Visitor v) { 764 | return v.visit(this); 765 | } 766 | 767 | } 768 | 769 | /* 770 | * location : ID { $$.obj = new Identifier($1.sval); } | ID '[' expr ']' ; 771 | */ 772 | abstract class Location extends ExpressionIntf { 773 | public Identifier id; 774 | 775 | public abstract LLVMValue accept(Visitor v); 776 | 777 | } 778 | 779 | class Location1 extends Location { 780 | 781 | public Location1(Identifier id) { 782 | super(); 783 | super.id = id; 784 | } 785 | 786 | public LLVMValue accept(Visitor v) { 787 | return v.visit(this); 788 | } 789 | 790 | } 791 | 792 | class Location2 extends Location { 793 | ExpressionIntf expr = null; 794 | 795 | public Location2(Identifier id, ExpressionIntf expr) { 796 | super(); 797 | super.id = id; 798 | this.expr = expr; 799 | } 800 | 801 | public LLVMValue accept(Visitor v) { 802 | return v.visit(this); 803 | } 804 | 805 | } 806 | 807 | /* 808 | * expr : expr ARTH_OP term1 | term1 ; term1 : term1 REL_OP term2 | term2 ; 809 | * term2 : term2 EQ_OP term3 | term3 ; term3 : term3 COND_OP term4 | term4 ; 810 | * term4 : location | method_call | literal | MINUS term4 | '!' term4 | '(' expr 811 | * ')' ; 812 | */ 813 | 814 | class Expression1 extends ExpressionIntf { 815 | ExpressionIntf expr = null; 816 | ExpressionIntf term1 = null; 817 | String operator = null; 818 | 819 | public Expression1(ExpressionIntf expr, ExpressionIntf term1, String operator) { 820 | super(); 821 | this.expr = expr; 822 | this.term1 = term1; 823 | this.operator = operator; 824 | } 825 | 826 | public LLVMValue accept(Visitor v) { 827 | return v.visit(this); 828 | } 829 | 830 | } 831 | 832 | class Expression2 extends ExpressionIntf { 833 | ExpressionIntf term5 = null; 834 | String operator; 835 | 836 | public Expression2(ExpressionIntf term5, String operator) { 837 | super(); 838 | this.term5 = term5; 839 | this.operator = operator; 840 | } 841 | 842 | public LLVMValue accept(Visitor v) { 843 | return v.visit(this); 844 | } 845 | 846 | } 847 | 848 | class Expression3 extends ExpressionIntf { 849 | ExpressionIntf term5 = null; 850 | 851 | public Expression3(ExpressionIntf term5) { 852 | super(); 853 | this.term5 = term5; 854 | } 855 | 856 | public LLVMValue accept(Visitor v) { 857 | return v.visit(this); 858 | } 859 | 860 | } 861 | 862 | /* 863 | * literal : INT_LITERAL | CHAR_LITERAL | bool_literal ; 864 | */ 865 | 866 | abstract class Literal extends ExpressionIntf { 867 | public abstract LLVMValue accept(Visitor v); 868 | 869 | } 870 | 871 | class IntegerLiteral extends Literal { 872 | public int value = 0; 873 | 874 | public IntegerLiteral(int value) { 875 | super(); 876 | this.value = value; 877 | } 878 | 879 | public LLVMValue accept(Visitor v) { 880 | return v.visit(this); 881 | } 882 | 883 | } 884 | 885 | class BooleanLiteral extends Literal { 886 | public String value; 887 | 888 | public BooleanLiteral(String value) { 889 | super(); 890 | this.value = value; 891 | } 892 | 893 | public LLVMValue accept(Visitor v) { 894 | return v.visit(this); 895 | } 896 | 897 | } 898 | -------------------------------------------------------------------------------- /phase-3/src/Yylex.java: -------------------------------------------------------------------------------- 1 | /* The following code was generated by JFlex 1.4.3 on 8/11/14 4:44 PM */ 2 | 3 | /** 4 | * This class is a scanner generated by 5 | * JFlex 1.4.3 6 | * on 8/11/14 4:44 PM from the specification file 7 | * decaf.l 8 | */ 9 | class Yylex { 10 | 11 | /** This character denotes the end of file */ 12 | public static final int YYEOF = -1; 13 | 14 | /** initial size of the lookahead buffer */ 15 | private static final int ZZ_BUFFERSIZE = 16384; 16 | 17 | /** lexical states */ 18 | public static final int YYINITIAL = 0; 19 | 20 | /** 21 | * ZZ_LEXSTATE[l] is the state in the DFA for the lexical state l 22 | * ZZ_LEXSTATE[l+1] is the state in the DFA for the lexical state l 23 | * at the beginning of a line 24 | * l is of the form l = 2*k, k a non negative integer 25 | */ 26 | private static final int ZZ_LEXSTATE[] = { 27 | 0, 0 28 | }; 29 | 30 | /** 31 | * Translates characters to character classes 32 | */ 33 | private static final String ZZ_CMAP_PACKED = 34 | "\11\0\1\17\1\11\1\0\2\17\22\0\1\17\1\42\1\10\2\0"+ 35 | "\1\14\1\43\1\0\1\51\1\52\1\14\1\15\1\53\1\6\1\7"+ 36 | "\1\14\1\3\11\1\1\0\1\46\1\41\1\40\1\41\2\0\4\2"+ 37 | "\1\5\1\2\21\12\1\4\2\12\1\47\1\0\1\50\1\0\1\13"+ 38 | "\1\0\1\24\1\20\1\30\1\37\1\23\1\34\2\12\1\35\1\12"+ 39 | "\1\27\1\22\1\12\1\25\1\21\2\12\1\26\1\33\1\32\1\31"+ 40 | "\1\36\1\12\1\4\2\12\1\44\1\16\1\45\uff82\0"; 41 | 42 | /** 43 | * Translates characters to character classes 44 | */ 45 | private static final char [] ZZ_CMAP = zzUnpackCMap(ZZ_CMAP_PACKED); 46 | 47 | /** 48 | * Translates DFA states to action switch labels. 49 | */ 50 | private static final int [] ZZ_ACTION = zzUnpackAction(); 51 | 52 | private static final String ZZ_ACTION_PACKED_0 = 53 | "\1\0\1\1\1\2\1\1\1\3\1\0\1\4\2\5"+ 54 | "\1\6\10\2\1\7\1\6\1\10\1\0\1\11\1\12"+ 55 | "\1\13\1\14\1\15\1\16\1\17\1\20\1\21\1\0"+ 56 | "\1\22\1\23\1\24\13\2\1\25\1\2\1\26\1\0"+ 57 | "\1\27\10\2\1\30\1\2\1\31\1\2\1\21\1\0"+ 58 | "\2\2\1\32\4\2\1\33\1\2\1\34\1\2\1\35"+ 59 | "\2\2\1\36\1\2\1\37\1\2\1\40\2\2\1\41"+ 60 | "\1\2\1\42\1\43"; 61 | 62 | private static int [] zzUnpackAction() { 63 | int [] result = new int[90]; 64 | int offset = 0; 65 | offset = zzUnpackAction(ZZ_ACTION_PACKED_0, offset, result); 66 | return result; 67 | } 68 | 69 | private static int zzUnpackAction(String packed, int offset, int [] result) { 70 | int i = 0; /* index in packed string */ 71 | int j = offset; /* index in unpacked array */ 72 | int l = packed.length(); 73 | while (i < l) { 74 | int count = packed.charAt(i++); 75 | int value = packed.charAt(i++); 76 | do result[j++] = value; while (--count > 0); 77 | } 78 | return j; 79 | } 80 | 81 | 82 | /** 83 | * Translates a state to a row index in the transition table 84 | */ 85 | private static final int [] ZZ_ROWMAP = zzUnpackRowMap(); 86 | 87 | private static final String ZZ_ROWMAP_PACKED_0 = 88 | "\0\0\0\54\0\130\0\204\0\260\0\334\0\u0108\0\u0108"+ 89 | "\0\260\0\u0134\0\u0160\0\u018c\0\u01b8\0\u01e4\0\u0210\0\u023c"+ 90 | "\0\u0268\0\u0294\0\u02c0\0\u02c0\0\u02c0\0\u02ec\0\u0108\0\u0108"+ 91 | "\0\u0108\0\u0108\0\u0108\0\u0108\0\u0108\0\u0108\0\u0318\0\u0344"+ 92 | "\0\u0108\0\u0108\0\u0108\0\u0370\0\u039c\0\u03c8\0\u03f4\0\u0420"+ 93 | "\0\u044c\0\u0478\0\u04a4\0\u04d0\0\u04fc\0\u0528\0\130\0\u0554"+ 94 | "\0\u0108\0\u0580\0\u0344\0\u05ac\0\u05d8\0\u0604\0\u0630\0\u065c"+ 95 | "\0\u0688\0\u06b4\0\u06e0\0\130\0\u070c\0\130\0\u0738\0\u0764"+ 96 | "\0\u0764\0\u0790\0\u07bc\0\130\0\u07e8\0\u0814\0\u0840\0\u086c"+ 97 | "\0\130\0\u0898\0\130\0\u08c4\0\130\0\u08f0\0\u091c\0\130"+ 98 | "\0\u0948\0\130\0\u0974\0\130\0\u09a0\0\u09cc\0\130\0\u09f8"+ 99 | "\0\130\0\130"; 100 | 101 | private static int [] zzUnpackRowMap() { 102 | int [] result = new int[90]; 103 | int offset = 0; 104 | offset = zzUnpackRowMap(ZZ_ROWMAP_PACKED_0, offset, result); 105 | return result; 106 | } 107 | 108 | private static int zzUnpackRowMap(String packed, int offset, int [] result) { 109 | int i = 0; /* index in packed string */ 110 | int j = offset; /* index in unpacked array */ 111 | int l = packed.length(); 112 | while (i < l) { 113 | int high = packed.charAt(i++) << 16; 114 | result[j++] = high | packed.charAt(i++); 115 | } 116 | return j; 117 | } 118 | 119 | /** 120 | * The transition table of the DFA 121 | */ 122 | private static final int [] ZZ_TRANS = zzUnpackTrans(); 123 | 124 | private static final String ZZ_TRANS_PACKED_0 = 125 | "\1\0\1\2\1\3\1\4\2\3\1\5\1\0\1\6"+ 126 | "\1\7\1\3\1\0\1\10\1\11\1\12\1\7\1\13"+ 127 | "\2\3\1\14\2\3\1\15\1\3\1\16\1\3\1\17"+ 128 | "\1\3\1\20\1\21\1\22\1\3\1\23\1\24\1\25"+ 129 | "\1\26\1\27\1\30\1\31\1\32\1\33\1\34\1\35"+ 130 | "\1\36\1\0\1\2\1\0\1\2\3\0\1\37\45\0"+ 131 | "\5\3\4\0\2\3\4\0\20\3\15\0\1\2\1\0"+ 132 | "\1\2\1\40\2\0\1\37\104\0\1\41\13\0\10\6"+ 133 | "\1\42\1\0\42\6\72\0\1\43\36\0\5\3\4\0"+ 134 | "\2\3\4\0\1\3\1\44\4\3\1\45\11\3\15\0"+ 135 | "\5\3\4\0\2\3\4\0\2\3\1\46\15\3\15\0"+ 136 | "\5\3\4\0\2\3\4\0\3\3\1\47\14\3\15\0"+ 137 | "\5\3\4\0\2\3\4\0\1\3\1\50\1\51\1\3"+ 138 | "\1\52\13\3\15\0\5\3\4\0\2\3\4\0\6\3"+ 139 | "\1\53\11\3\15\0\5\3\4\0\2\3\4\0\1\3"+ 140 | "\1\54\2\3\1\55\13\3\15\0\5\3\4\0\2\3"+ 141 | "\4\0\5\3\1\56\6\3\1\57\3\3\15\0\5\3"+ 142 | "\4\0\2\3\4\0\1\3\1\60\16\3\54\0\1\61"+ 143 | "\56\0\1\43\11\0\1\37\1\0\1\37\1\0\1\62"+ 144 | "\15\0\1\62\31\0\3\63\1\0\1\63\12\0\1\63"+ 145 | "\2\0\2\63\3\0\1\63\3\0\1\63\2\0\1\63"+ 146 | "\15\0\5\3\4\0\2\3\4\0\1\3\1\64\16\3"+ 147 | "\15\0\5\3\4\0\2\3\4\0\3\3\1\65\14\3"+ 148 | "\15\0\5\3\4\0\2\3\4\0\13\3\1\66\4\3"+ 149 | "\15\0\5\3\4\0\2\3\4\0\12\3\1\67\5\3"+ 150 | "\15\0\5\3\4\0\2\3\4\0\5\3\1\70\12\3"+ 151 | "\15\0\5\3\4\0\2\3\4\0\4\3\1\71\13\3"+ 152 | "\15\0\5\3\4\0\2\3\4\0\2\3\1\72\15\3"+ 153 | "\15\0\5\3\4\0\2\3\4\0\11\3\1\73\6\3"+ 154 | "\15\0\5\3\4\0\2\3\4\0\6\3\1\74\11\3"+ 155 | "\15\0\5\3\4\0\2\3\4\0\2\3\1\75\15\3"+ 156 | "\15\0\5\3\4\0\2\3\4\0\12\3\1\76\5\3"+ 157 | "\15\0\5\3\4\0\2\3\4\0\15\3\1\77\2\3"+ 158 | "\15\0\1\100\1\0\1\100\2\0\1\101\6\0\1\101"+ 159 | "\37\0\5\3\4\0\2\3\4\0\2\3\1\102\15\3"+ 160 | "\15\0\5\3\4\0\2\3\4\0\4\3\1\103\13\3"+ 161 | "\15\0\5\3\4\0\2\3\4\0\3\3\1\104\14\3"+ 162 | "\15\0\5\3\4\0\2\3\4\0\11\3\1\105\6\3"+ 163 | "\15\0\5\3\4\0\2\3\4\0\12\3\1\106\5\3"+ 164 | "\15\0\5\3\4\0\2\3\4\0\13\3\1\107\4\3"+ 165 | "\15\0\5\3\4\0\2\3\4\0\2\3\1\110\15\3"+ 166 | "\15\0\5\3\4\0\2\3\4\0\3\3\1\111\14\3"+ 167 | "\15\0\5\3\4\0\2\3\4\0\13\3\1\112\4\3"+ 168 | "\15\0\5\3\4\0\2\3\4\0\17\3\1\113\15\0"+ 169 | "\1\100\1\0\1\100\51\0\5\3\4\0\2\3\4\0"+ 170 | "\3\3\1\114\14\3\15\0\5\3\4\0\2\3\4\0"+ 171 | "\7\3\1\115\10\3\15\0\5\3\4\0\2\3\4\0"+ 172 | "\6\3\1\116\11\3\15\0\5\3\4\0\2\3\4\0"+ 173 | "\15\3\1\117\2\3\15\0\5\3\4\0\2\3\4\0"+ 174 | "\13\3\1\120\4\3\15\0\5\3\4\0\2\3\4\0"+ 175 | "\1\3\1\121\16\3\15\0\5\3\4\0\2\3\4\0"+ 176 | "\3\3\1\122\14\3\15\0\5\3\4\0\2\3\4\0"+ 177 | "\4\3\1\123\13\3\15\0\5\3\4\0\2\3\4\0"+ 178 | "\5\3\1\124\12\3\15\0\5\3\4\0\2\3\4\0"+ 179 | "\5\3\1\125\12\3\15\0\5\3\4\0\2\3\4\0"+ 180 | "\11\3\1\126\6\3\15\0\5\3\4\0\2\3\4\0"+ 181 | "\5\3\1\127\12\3\15\0\5\3\4\0\2\3\4\0"+ 182 | "\11\3\1\130\6\3\15\0\5\3\4\0\2\3\4\0"+ 183 | "\12\3\1\131\5\3\15\0\5\3\4\0\2\3\4\0"+ 184 | "\3\3\1\132\14\3\14\0"; 185 | 186 | private static int [] zzUnpackTrans() { 187 | int [] result = new int[2596]; 188 | int offset = 0; 189 | offset = zzUnpackTrans(ZZ_TRANS_PACKED_0, offset, result); 190 | return result; 191 | } 192 | 193 | private static int zzUnpackTrans(String packed, int offset, int [] result) { 194 | int i = 0; /* index in packed string */ 195 | int j = offset; /* index in unpacked array */ 196 | int l = packed.length(); 197 | while (i < l) { 198 | int count = packed.charAt(i++); 199 | int value = packed.charAt(i++); 200 | value--; 201 | do result[j++] = value; while (--count > 0); 202 | } 203 | return j; 204 | } 205 | 206 | 207 | /* error codes */ 208 | private static final int ZZ_UNKNOWN_ERROR = 0; 209 | private static final int ZZ_NO_MATCH = 1; 210 | private static final int ZZ_PUSHBACK_2BIG = 2; 211 | 212 | /* error messages for the codes above */ 213 | private static final String ZZ_ERROR_MSG[] = { 214 | "Unkown internal scanner error", 215 | "Error: could not match input", 216 | "Error: pushback value was too large" 217 | }; 218 | 219 | /** 220 | * ZZ_ATTRIBUTE[aState] contains the attributes of state aState 221 | */ 222 | private static final int [] ZZ_ATTRIBUTE = zzUnpackAttribute(); 223 | 224 | private static final String ZZ_ATTRIBUTE_PACKED_0 = 225 | "\1\0\4\1\1\0\2\11\15\1\1\0\10\11\1\1"+ 226 | "\1\0\3\11\15\1\1\11\1\0\16\1\1\0\31\1"; 227 | 228 | private static int [] zzUnpackAttribute() { 229 | int [] result = new int[90]; 230 | int offset = 0; 231 | offset = zzUnpackAttribute(ZZ_ATTRIBUTE_PACKED_0, offset, result); 232 | return result; 233 | } 234 | 235 | private static int zzUnpackAttribute(String packed, int offset, int [] result) { 236 | int i = 0; /* index in packed string */ 237 | int j = offset; /* index in unpacked array */ 238 | int l = packed.length(); 239 | while (i < l) { 240 | int count = packed.charAt(i++); 241 | int value = packed.charAt(i++); 242 | do result[j++] = value; while (--count > 0); 243 | } 244 | return j; 245 | } 246 | 247 | /** the input device */ 248 | private java.io.Reader zzReader; 249 | 250 | /** the current state of the DFA */ 251 | private int zzState; 252 | 253 | /** the current lexical state */ 254 | private int zzLexicalState = YYINITIAL; 255 | 256 | /** this buffer contains the current text to be matched and is 257 | the source of the yytext() string */ 258 | private char zzBuffer[] = new char[ZZ_BUFFERSIZE]; 259 | 260 | /** the textposition at the last accepting state */ 261 | private int zzMarkedPos; 262 | 263 | /** the current text position in the buffer */ 264 | private int zzCurrentPos; 265 | 266 | /** startRead marks the beginning of the yytext() string in the buffer */ 267 | private int zzStartRead; 268 | 269 | /** endRead marks the last character in the buffer, that has been read 270 | from input */ 271 | private int zzEndRead; 272 | 273 | /** number of newlines encountered up to the start of the matched text */ 274 | private int yyline; 275 | 276 | /** the number of characters up to the start of the matched text */ 277 | private int yychar; 278 | 279 | /** 280 | * the number of characters from the last newline up to the start of the 281 | * matched text 282 | */ 283 | private int yycolumn; 284 | 285 | /** 286 | * zzAtBOL == true <=> the scanner is currently at the beginning of a line 287 | */ 288 | private boolean zzAtBOL = true; 289 | 290 | /** zzAtEOF == true <=> the scanner is at the EOF */ 291 | private boolean zzAtEOF; 292 | 293 | /** denotes if the user-EOF-code has already been executed */ 294 | private boolean zzEOFDone; 295 | 296 | /* user code: */ 297 | /* store a reference to the parser object */ 298 | Parser yyparser; 299 | 300 | /* constructor taking an additional parser object */ 301 | public Yylex(java.io.Reader r, Parser yyparser) { 302 | this(r); 303 | this.yyparser = yyparser; 304 | } 305 | 306 | 307 | /** 308 | * Creates a new scanner 309 | * There is also a java.io.InputStream version of this constructor. 310 | * 311 | * @param in the java.io.Reader to read input from. 312 | */ 313 | Yylex(java.io.Reader in) { 314 | this.zzReader = in; 315 | } 316 | 317 | /** 318 | * Creates a new scanner. 319 | * There is also java.io.Reader version of this constructor. 320 | * 321 | * @param in the java.io.Inputstream to read input from. 322 | */ 323 | Yylex(java.io.InputStream in) { 324 | this(new java.io.InputStreamReader(in)); 325 | } 326 | 327 | /** 328 | * Unpacks the compressed character translation table. 329 | * 330 | * @param packed the packed character translation table 331 | * @return the unpacked character translation table 332 | */ 333 | private static char [] zzUnpackCMap(String packed) { 334 | char [] map = new char[0x10000]; 335 | int i = 0; /* index in packed string */ 336 | int j = 0; /* index in unpacked array */ 337 | while (i < 136) { 338 | int count = packed.charAt(i++); 339 | char value = packed.charAt(i++); 340 | do map[j++] = value; while (--count > 0); 341 | } 342 | return map; 343 | } 344 | 345 | 346 | /** 347 | * Refills the input buffer. 348 | * 349 | * @return false, iff there was new input. 350 | * 351 | * @exception java.io.IOException if any I/O-Error occurs 352 | */ 353 | private boolean zzRefill() throws java.io.IOException { 354 | 355 | /* first: make room (if you can) */ 356 | if (zzStartRead > 0) { 357 | System.arraycopy(zzBuffer, zzStartRead, 358 | zzBuffer, 0, 359 | zzEndRead-zzStartRead); 360 | 361 | /* translate stored positions */ 362 | zzEndRead-= zzStartRead; 363 | zzCurrentPos-= zzStartRead; 364 | zzMarkedPos-= zzStartRead; 365 | zzStartRead = 0; 366 | } 367 | 368 | /* is the buffer big enough? */ 369 | if (zzCurrentPos >= zzBuffer.length) { 370 | /* if not: blow it up */ 371 | char newBuffer[] = new char[zzCurrentPos*2]; 372 | System.arraycopy(zzBuffer, 0, newBuffer, 0, zzBuffer.length); 373 | zzBuffer = newBuffer; 374 | } 375 | 376 | /* finally: fill the buffer with new input */ 377 | int numRead = zzReader.read(zzBuffer, zzEndRead, 378 | zzBuffer.length-zzEndRead); 379 | 380 | if (numRead > 0) { 381 | zzEndRead+= numRead; 382 | return false; 383 | } 384 | // unlikely but not impossible: read 0 characters, but not at end of stream 385 | if (numRead == 0) { 386 | int c = zzReader.read(); 387 | if (c == -1) { 388 | return true; 389 | } else { 390 | zzBuffer[zzEndRead++] = (char) c; 391 | return false; 392 | } 393 | } 394 | 395 | // numRead < 0 396 | return true; 397 | } 398 | 399 | 400 | /** 401 | * Closes the input stream. 402 | */ 403 | public final void yyclose() throws java.io.IOException { 404 | zzAtEOF = true; /* indicate end of file */ 405 | zzEndRead = zzStartRead; /* invalidate buffer */ 406 | 407 | if (zzReader != null) 408 | zzReader.close(); 409 | } 410 | 411 | 412 | /** 413 | * Resets the scanner to read from a new input stream. 414 | * Does not close the old reader. 415 | * 416 | * All internal variables are reset, the old input stream 417 | * cannot be reused (internal buffer is discarded and lost). 418 | * Lexical state is set to ZZ_INITIAL. 419 | * 420 | * @param reader the new input stream 421 | */ 422 | public final void yyreset(java.io.Reader reader) { 423 | zzReader = reader; 424 | zzAtBOL = true; 425 | zzAtEOF = false; 426 | zzEOFDone = false; 427 | zzEndRead = zzStartRead = 0; 428 | zzCurrentPos = zzMarkedPos = 0; 429 | yyline = yychar = yycolumn = 0; 430 | zzLexicalState = YYINITIAL; 431 | } 432 | 433 | 434 | /** 435 | * Returns the current lexical state. 436 | */ 437 | public final int yystate() { 438 | return zzLexicalState; 439 | } 440 | 441 | 442 | /** 443 | * Enters a new lexical state 444 | * 445 | * @param newState the new lexical state 446 | */ 447 | public final void yybegin(int newState) { 448 | zzLexicalState = newState; 449 | } 450 | 451 | 452 | /** 453 | * Returns the text matched by the current regular expression. 454 | */ 455 | public final String yytext() { 456 | return new String( zzBuffer, zzStartRead, zzMarkedPos-zzStartRead ); 457 | } 458 | 459 | 460 | /** 461 | * Returns the character at position pos from the 462 | * matched text. 463 | * 464 | * It is equivalent to yytext().charAt(pos), but faster 465 | * 466 | * @param pos the position of the character to fetch. 467 | * A value from 0 to yylength()-1. 468 | * 469 | * @return the character at position pos 470 | */ 471 | public final char yycharat(int pos) { 472 | return zzBuffer[zzStartRead+pos]; 473 | } 474 | 475 | 476 | /** 477 | * Returns the length of the matched text region. 478 | */ 479 | public final int yylength() { 480 | return zzMarkedPos-zzStartRead; 481 | } 482 | 483 | 484 | /** 485 | * Reports an error that occured while scanning. 486 | * 487 | * In a wellformed scanner (no or only correct usage of 488 | * yypushback(int) and a match-all fallback rule) this method 489 | * will only be called with things that "Can't Possibly Happen". 490 | * If this method is called, something is seriously wrong 491 | * (e.g. a JFlex bug producing a faulty scanner etc.). 492 | * 493 | * Usual syntax/scanner level error handling should be done 494 | * in error fallback rules. 495 | * 496 | * @param errorCode the code of the errormessage to display 497 | */ 498 | private void zzScanError(int errorCode) { 499 | String message; 500 | try { 501 | message = ZZ_ERROR_MSG[errorCode]; 502 | } 503 | catch (ArrayIndexOutOfBoundsException e) { 504 | message = ZZ_ERROR_MSG[ZZ_UNKNOWN_ERROR]; 505 | } 506 | 507 | throw new Error(message); 508 | } 509 | 510 | 511 | /** 512 | * Pushes the specified amount of characters back into the input stream. 513 | * 514 | * They will be read again by then next call of the scanning method 515 | * 516 | * @param number the number of characters to be read again. 517 | * This number must not be greater than yylength()! 518 | */ 519 | public void yypushback(int number) { 520 | if ( number > yylength() ) 521 | zzScanError(ZZ_PUSHBACK_2BIG); 522 | 523 | zzMarkedPos -= number; 524 | } 525 | 526 | 527 | /** 528 | * Contains user EOF-code, which will be executed exactly once, 529 | * when the end of file is reached 530 | */ 531 | private void zzDoEOF() throws java.io.IOException { 532 | if (!zzEOFDone) { 533 | zzEOFDone = true; 534 | yyclose(); 535 | } 536 | } 537 | 538 | 539 | /** 540 | * Resumes scanning until the next regular expression is matched, 541 | * the end of input is encountered or an I/O-Error occurs. 542 | * 543 | * @return the next token 544 | * @exception java.io.IOException if any I/O-Error occurs 545 | */ 546 | public int yylex() throws java.io.IOException { 547 | int zzInput; 548 | int zzAction; 549 | 550 | // cached fields: 551 | int zzCurrentPosL; 552 | int zzMarkedPosL; 553 | int zzEndReadL = zzEndRead; 554 | char [] zzBufferL = zzBuffer; 555 | char [] zzCMapL = ZZ_CMAP; 556 | 557 | int [] zzTransL = ZZ_TRANS; 558 | int [] zzRowMapL = ZZ_ROWMAP; 559 | int [] zzAttrL = ZZ_ATTRIBUTE; 560 | 561 | while (true) { 562 | zzMarkedPosL = zzMarkedPos; 563 | 564 | zzAction = -1; 565 | 566 | zzCurrentPosL = zzCurrentPos = zzStartRead = zzMarkedPosL; 567 | 568 | zzState = ZZ_LEXSTATE[zzLexicalState]; 569 | 570 | 571 | zzForAction: { 572 | while (true) { 573 | 574 | if (zzCurrentPosL < zzEndReadL) 575 | zzInput = zzBufferL[zzCurrentPosL++]; 576 | else if (zzAtEOF) { 577 | zzInput = YYEOF; 578 | break zzForAction; 579 | } 580 | else { 581 | // store back cached positions 582 | zzCurrentPos = zzCurrentPosL; 583 | zzMarkedPos = zzMarkedPosL; 584 | boolean eof = zzRefill(); 585 | // get translated positions and possibly new buffer 586 | zzCurrentPosL = zzCurrentPos; 587 | zzMarkedPosL = zzMarkedPos; 588 | zzBufferL = zzBuffer; 589 | zzEndReadL = zzEndRead; 590 | if (eof) { 591 | zzInput = YYEOF; 592 | break zzForAction; 593 | } 594 | else { 595 | zzInput = zzBufferL[zzCurrentPosL++]; 596 | } 597 | } 598 | int zzNext = zzTransL[ zzRowMapL[zzState] + zzCMapL[zzInput] ]; 599 | if (zzNext == -1) break zzForAction; 600 | zzState = zzNext; 601 | 602 | int zzAttributes = zzAttrL[zzState]; 603 | if ( (zzAttributes & 1) == 1 ) { 604 | zzAction = zzState; 605 | zzMarkedPosL = zzCurrentPosL; 606 | if ( (zzAttributes & 8) == 8 ) break zzForAction; 607 | } 608 | 609 | } 610 | } 611 | 612 | // store back cached position 613 | zzMarkedPos = zzMarkedPosL; 614 | 615 | switch (zzAction < 0 ? zzAction : ZZ_ACTION[zzAction]) { 616 | case 35: 617 | { yyparser.yylval=new ParserVal(yytext()); return yyparser.CONTINUE; 618 | } 619 | case 36: break; 620 | case 7: 621 | { yyparser.yylval=new ParserVal(yytext()); return yyparser.E_ASSIGN_OP; 622 | } 623 | case 37: break; 624 | case 13: 625 | { yyparser.yylval=new ParserVal(yytext()); return ']'; 626 | } 627 | case 38: break; 628 | case 11: 629 | { yyparser.yylval=new ParserVal(yytext()); return ';' ; 630 | } 631 | case 39: break; 632 | case 17: 633 | { yyparser.yylval=new ParserVal(Double.parseDouble(yytext())); return yyparser.INT_LITERAL; 634 | } 635 | case 40: break; 636 | case 2: 637 | { yyparser.yylval=new ParserVal(yytext()); return yyparser.ID; 638 | } 639 | case 41: break; 640 | case 26: 641 | { yyparser.yylval=new ParserVal(yytext()); return yyparser.ELSE; 642 | } 643 | case 42: break; 644 | case 31: 645 | { yyparser.yylval=new ParserVal(yytext()); return yyparser.FALSE; 646 | } 647 | case 43: break; 648 | case 29: 649 | { yyparser.yylval=new ParserVal(yytext()); return yyparser.BREAK; 650 | } 651 | case 44: break; 652 | case 28: 653 | { yyparser.yylval=new ParserVal(yytext()); return yyparser.VOID; 654 | } 655 | case 45: break; 656 | case 25: 657 | { yyparser.yylval=new ParserVal(yytext()); return yyparser.INT; 658 | } 659 | case 46: break; 660 | case 15: 661 | { yyparser.yylval=new ParserVal(yytext()); return ')'; 662 | } 663 | case 47: break; 664 | case 6: 665 | { yyparser.yylval=new ParserVal(yytext()); return yyparser.REL_OP; 666 | } 667 | case 48: break; 668 | case 22: 669 | { yyparser.yylval=new ParserVal(yytext()); return yyparser.EQ_OP; 670 | } 671 | case 49: break; 672 | case 33: 673 | { yyparser.yylval=new ParserVal(yytext());return yyparser.BOOLEAN; 674 | } 675 | case 50: break; 676 | case 27: 677 | { yyparser.yylval=new ParserVal(yytext()); return yyparser.TRUE; 678 | } 679 | case 51: break; 680 | case 18: 681 | { yyparser.yylval=new ParserVal(yytext()); return yyparser.ASSIGN_OP; 682 | } 683 | case 52: break; 684 | case 1: 685 | { yyparser.yylval=new ParserVal(Integer.parseInt(yytext())); return yyparser.INT_LITERAL; 686 | } 687 | case 53: break; 688 | case 9: 689 | { yyparser.yylval=new ParserVal(yytext());return '{' ; 690 | } 691 | case 54: break; 692 | case 24: 693 | { yyparser.yylval=new ParserVal(yytext()); return yyparser.FOR; 694 | } 695 | case 55: break; 696 | case 32: 697 | { yyparser.yylval=new ParserVal(yytext()); return yyparser.RETURN; 698 | } 699 | case 56: break; 700 | case 14: 701 | { yyparser.yylval=new ParserVal(yytext()); return '('; 702 | } 703 | case 57: break; 704 | case 3: 705 | { yyparser.yylval=new ParserVal(yytext()); return yyparser.MINUS ; 706 | } 707 | case 58: break; 708 | case 5: 709 | { yyparser.yylval=new ParserVal(yytext()); return yyparser.ARITH_OP; 710 | } 711 | case 59: break; 712 | case 30: 713 | { yyparser.yylval=new ParserVal(yytext()); return yyparser.CLASS; 714 | } 715 | case 60: break; 716 | case 34: 717 | { yyparser.yylval=new ParserVal(yytext()); return yyparser.CALLOUT; 718 | } 719 | case 61: break; 720 | case 23: 721 | { yyparser.yylval=new ParserVal(Integer.parseInt(yytext(),16)); return yyparser.INT_LITERAL; 722 | } 723 | case 62: break; 724 | case 8: 725 | { yyparser.yylval=new ParserVal(yytext()); return '!'; 726 | } 727 | case 63: break; 728 | case 21: 729 | { yyparser.yylval=new ParserVal(yytext()); return yyparser.IF; 730 | } 731 | case 64: break; 732 | case 20: 733 | { yyparser.yylval=new ParserVal(yytext()); return yyparser.COND_OP; 734 | } 735 | case 65: break; 736 | case 10: 737 | { yyparser.yylval=new ParserVal(yytext()); return '}' ; 738 | } 739 | case 66: break; 740 | case 12: 741 | { yyparser.yylval=new ParserVal(yytext()); return '['; 742 | } 743 | case 67: break; 744 | case 16: 745 | { yyparser.yylval=new ParserVal(yytext()); return ','; 746 | } 747 | case 68: break; 748 | case 19: 749 | { yyparser.yylval=new ParserVal(yytext()); return yyparser.STRING_LITERAL; 750 | } 751 | case 69: break; 752 | case 4: 753 | { 754 | } 755 | case 70: break; 756 | default: 757 | if (zzInput == YYEOF && zzStartRead == zzCurrentPos) { 758 | zzAtEOF = true; 759 | zzDoEOF(); 760 | { return 0; } 761 | } 762 | else { 763 | zzScanError(ZZ_NO_MATCH); 764 | } 765 | } 766 | } 767 | } 768 | 769 | 770 | } 771 | -------------------------------------------------------------------------------- /phase-3/src/VisitorImpl.java: -------------------------------------------------------------------------------- 1 | import java.io.File; 2 | 3 | import org.jllvm.LLVMAddInstruction; 4 | import org.jllvm.LLVMBasicBlock; 5 | import org.jllvm.LLVMCallInstruction; 6 | import org.jllvm.LLVMConstant; 7 | import org.jllvm.LLVMConstantArray; 8 | import org.jllvm.LLVMConstantInteger; 9 | import org.jllvm.LLVMConstantString; 10 | import org.jllvm.LLVMDivideInstruction; 11 | import org.jllvm.LLVMDivideInstruction.DivisionType; 12 | import org.jllvm.LLVMExecutionEngine; 13 | import org.jllvm.LLVMIntegerComparison; 14 | import org.jllvm.LLVMPointerType; 15 | import org.jllvm.LLVMRemainderInstruction.RemainderType; 16 | import org.jllvm.LLVMArrayType; 17 | import org.jllvm.LLVMBranchInstruction; 18 | import org.jllvm.LLVMFunction; 19 | import org.jllvm.LLVMFunctionType; 20 | import org.jllvm.LLVMGetElementPointerInstruction; 21 | import org.jllvm.LLVMGlobalVariable; 22 | import org.jllvm.LLVMIntegerType; 23 | import org.jllvm.LLVMLoadInstruction; 24 | import org.jllvm.LLVMMultiplyInstruction; 25 | import org.jllvm.LLVMRemainderInstruction; 26 | import org.jllvm.LLVMReturnInstruction; 27 | import org.jllvm.LLVMStackAllocation; 28 | import org.jllvm.LLVMStoreInstruction; 29 | import org.jllvm.LLVMSubtractInstruction; 30 | import org.jllvm.LLVMType; 31 | import org.jllvm.LLVMUnaryBitwiseInstruction; 32 | import org.jllvm.LLVMUnaryBitwiseInstruction.UnaryBitwiseInstructionType; 33 | import org.jllvm.LLVMValue; 34 | import org.jllvm.LLVMVoidType; 35 | import org.jllvm.bindings.Analysis; 36 | import org.jllvm.bindings.LLVMIntPredicate; 37 | import org.jllvm.bindings.LLVMLinkage; 38 | import org.jllvm.bindings.LLVMVerifierFailureAction; 39 | 40 | public class VisitorImpl implements Visitor { 41 | static { 42 | System.loadLibrary("jllvm"); 43 | 44 | } 45 | LLVMExecutionEngine ex = null; 46 | 47 | LLVMFunction iffunc1 = null;; 48 | LLVMBasicBlock ifelse1 = null; 49 | LLVMBasicBlock ifend1 = null; 50 | 51 | LLVMBasicBlock fblock = null; 52 | LLVMFunction f = null;; 53 | LLVMBasicBlock forInc1 = null; 54 | LLVMBasicBlock forEnd1 = null; 55 | 56 | boolean hasMainFunc = false; // disable this flag for removing main method 57 | // constraint 58 | boolean funcReturnReq = false; 59 | boolean errorFlag = false; 60 | boolean breakOrContStat = false; 61 | boolean operandIncompatible = false; 62 | boolean isForStatement = false; 63 | boolean isMainFunc = false; 64 | boolean isBoolFunc = false; 65 | 66 | @Override 67 | public LLVMValue visit(Decaf_Class decaf_Class) { 68 | try { 69 | ex = new LLVMExecutionEngine(module); 70 | 71 | decaf_Class.cls.accept(this); 72 | 73 | for (DeclarationsIntf decl : decaf_Class.decls) { 74 | decl.accept(this); 75 | } 76 | 77 | /* 78 | * Disabling output file name to be class name; for time being 79 | * seting it to default 80 | */ 81 | // String className = decaf_Class.id.accept(this); 82 | // String llvmBitCodeFileName = className + ".bc"; 83 | String llvmBitCodeFileName = "decaf.out"; 84 | File file = new File(llvmBitCodeFileName); 85 | if (file.exists()) 86 | file.delete(); 87 | 88 | if (!errorFlag) { 89 | if (hasMainFunc) { 90 | // module.writeBitcodeToFile(llvmBitCodeFileName); 91 | Analysis.LLVMVerifyModule(module.getInstance(), 92 | LLVMVerifierFailureAction.LLVMPrintMessageAction, 93 | null); 94 | module.writeBitcodeToFile(llvmBitCodeFileName); 95 | // ex.linkInJit(); 96 | // ex.runFunction(module.getNamedFunction("main"), new 97 | // LLVMGenericValue[]{}); 98 | } else { 99 | System.err.println("Main Function: Undefined "); 100 | } 101 | } else { 102 | System.err.println("Compilation Errors"); 103 | } 104 | 105 | } catch (Exception e1) { 106 | System.err.println(e1.getMessage()); 107 | ; 108 | } finally { 109 | System.exit(0); 110 | } 111 | 112 | return null; 113 | } 114 | 115 | @Override 116 | public LLVMValue visit(Declarations1 declarations1) { 117 | for (DeclarationsIntf d : declarations1.methodDecls) 118 | d.accept(this); 119 | return null; 120 | } 121 | 122 | @Override 123 | public LLVMValue visit(FieldDecl fieldDecl) { 124 | LLVMIntegerType ty = fieldDecl.type.accept(this); 125 | 126 | if (ty instanceof LLVMIntegerType) { 127 | long width = ty.getWidth(); 128 | LLVMIntegerType inty = null; 129 | if (width == 1) 130 | inty = LLVMIntegerType.i1; 131 | else 132 | inty = LLVMIntegerType.i32; 133 | for (Fields f : fieldDecl.fields) { 134 | f.accept(this); 135 | if (f instanceof Field1) { 136 | int size = ((Field1) f).val; 137 | if (size > 0) { 138 | LLVMArrayType arrTy = new LLVMArrayType(inty, size); 139 | LLVMConstantInteger instConsts[] = new LLVMConstantInteger[size]; 140 | for (int i = 0; i < size; i++) { 141 | instConsts[i] = LLVMConstantInteger 142 | .constantInteger(inty, 0, false); 143 | } 144 | LLVMConstantArray arry = new LLVMConstantArray(inty, 145 | instConsts); 146 | LLVMGlobalVariable var = module.addGlobal(arrTy, 147 | ((Field1) f).id.id); 148 | var.setInitializer(arry); 149 | var.setLinkage(LLVMLinkage.LLVMCommonLinkage); 150 | namedValues.put(((Field1) f).id.id, var); 151 | 152 | } else { 153 | errorFlag = true; 154 | System.err 155 | .println("Size of an array must be greater than zero"); 156 | } 157 | } 158 | if (f instanceof Field) { 159 | LLVMConstantInteger val = LLVMConstantInteger 160 | .constantInteger(inty, 0, false); 161 | 162 | LLVMGlobalVariable var = module.addGlobal(inty, 163 | ((Field) f).id.id); 164 | var.setInitializer(val); 165 | var.setLinkage(LLVMLinkage.LLVMExternalLinkage); 166 | namedValues.put(((Field) f).id.id, var); 167 | 168 | } 169 | } 170 | } 171 | return null; 172 | } 173 | 174 | @Override 175 | public void visit(Fields1 fields1) { 176 | fields1.field.accept(this); 177 | } 178 | 179 | @Override 180 | public String visit(Identifier identifier) { 181 | return identifier.id; 182 | } 183 | 184 | @Override 185 | public void visit(Field field) { 186 | field.id.accept(this); 187 | } 188 | 189 | @Override 190 | public LLVMFunction visit(MethodDeclaration1 methodDeclaration1) { 191 | 192 | LLVMType typeLLVM = methodDeclaration1.type.accept(this); 193 | if (typeLLVM instanceof LLVMIntegerType) { 194 | 195 | if (typeLLVM.equals(LLVMIntegerType.i1)) 196 | isBoolFunc = true; 197 | String functionName = methodDeclaration1.id.accept(this); 198 | if (namedValues.containsKey(functionName)) { 199 | System.err.println(functionName + ": IDentifier already used"); 200 | errorFlag = true; 201 | return null; 202 | } 203 | funcReturnReq = true; 204 | 205 | LLVMType[] args = new LLVMType[methodDeclaration1.argList.size()]; 206 | int size = methodDeclaration1.argList.size(); 207 | NameTypePair ntPair[] = new NameTypePair[size]; 208 | 209 | for (int i = 0; i < size; i++) { 210 | ntPair[i] = methodDeclaration1.argList.get(i).accept(this); 211 | args[i] = ntPair[i].type; 212 | } 213 | if (functionName.equals("main") && args.length == 0) { 214 | hasMainFunc = true; 215 | } 216 | LLVMFunctionType FT = new LLVMFunctionType(typeLLVM, args, false); 217 | f = new LLVMFunction(module, functionName, FT); 218 | namedValues.put(functionName, f); 219 | 220 | fblock = f.appendBasicBlock("entry"); 221 | builder.positionBuilderAtEnd(fblock); 222 | 223 | for (int i = 0; i < f.countParameters(); i++) { 224 | f.getParameter(i).setValueName(ntPair[i].name + ".formal"); 225 | LLVMValue argStackPtr = new LLVMStackAllocation(builder, f 226 | .getParameter(i).typeOf(), null, ntPair[i].name); 227 | namedValues.put(ntPair[i].name, argStackPtr); 228 | new LLVMStoreInstruction(builder, f.getParameter(i), 229 | argStackPtr); 230 | 231 | } 232 | 233 | methodDeclaration1.block.accept(this); 234 | 235 | if (funcReturnReq) { 236 | System.err.println("No Return Statement"); 237 | errorFlag = true; 238 | } 239 | //namedValues.put(functionName, f); 240 | return f; 241 | 242 | } 243 | return null; 244 | } 245 | 246 | @Override 247 | public LLVMFunction visit(MethodDeclaration2 methodDeclaration2) { 248 | LLVMType voidType = methodDeclaration2.type.accept(this); 249 | if (voidType instanceof LLVMVoidType) { 250 | 251 | String functionName = methodDeclaration2.id.accept(this); 252 | if (namedValues.containsKey(functionName)) { 253 | System.err.println(functionName + ": IDentifier already used"); 254 | errorFlag = true; 255 | return null; 256 | } 257 | LLVMType[] args = new LLVMType[methodDeclaration2.argList.size()]; 258 | int size = methodDeclaration2.argList.size(); 259 | NameTypePair ntPair[] = new NameTypePair[size]; 260 | 261 | for (int i = 0; i < size; i++) { 262 | ntPair[i] = methodDeclaration2.argList.get(i).accept(this); 263 | args[i] = ntPair[i].type; 264 | } 265 | LLVMFunctionType FT = new LLVMFunctionType(voidType, args, false); 266 | f = new LLVMFunction(module, functionName, FT); 267 | namedValues.put(functionName, f); 268 | 269 | fblock = f.appendBasicBlock("entry"); 270 | builder.positionBuilderAtEnd(fblock); 271 | for (int i = 0; i < f.countParameters(); i++) { 272 | f.getParameter(i).setValueName(ntPair[i].name + ".formal"); 273 | LLVMValue argStackPtr = new LLVMStackAllocation(builder, f 274 | .getParameter(i).typeOf(), null, ntPair[i].name); 275 | namedValues.put(ntPair[i].name, argStackPtr); 276 | new LLVMStoreInstruction(builder, f.getParameter(i), 277 | argStackPtr); 278 | } 279 | methodDeclaration2.block.accept(this); 280 | new LLVMReturnInstruction(builder, null); 281 | //namedValues.put(functionName, f); 282 | return f; 283 | } 284 | return null; 285 | 286 | } 287 | 288 | @Override 289 | public NameTypePair visit(Arguement1 arguement1) { 290 | return new NameTypePair(arguement1.id.accept(this), 291 | arguement1.type.accept(this)); 292 | } 293 | 294 | @Override 295 | public void visit(Var_Decl var_Decl) { 296 | LLVMType ty = var_Decl.type.accept(this); 297 | for (Identifier id : var_Decl.vars) { 298 | String ident = id.accept(this); 299 | if (namedValues.containsKey(ident)) { 300 | 301 | if (localVar.contains(ident)) { 302 | System.err.println(ident + ": IDentifier already used"); 303 | errorFlag = true; 304 | return; 305 | } 306 | } 307 | if (ty instanceof LLVMIntegerType) { 308 | long width = ((LLVMIntegerType) ty).getWidth(); 309 | LLVMIntegerType inty = null; 310 | if (width == 1) 311 | inty = LLVMIntegerType.i1; 312 | else 313 | inty = LLVMIntegerType.i32; 314 | 315 | LLVMConstantInteger intConst = LLVMConstantInteger 316 | .constantInteger(inty, 0, false); 317 | namedValues.put(ident, new LLVMStackAllocation(builder, inty, 318 | intConst, ident)); 319 | localVar.add(ident); 320 | } 321 | 322 | } 323 | } 324 | 325 | @Override 326 | public LLVMValue visit(Statement1 statement1) { 327 | LLVMValue rhs = statement1.expr.accept(this); 328 | String idLHS = statement1.loc.id.id; 329 | if (rhs == null) { 330 | errorFlag = true; 331 | return null; 332 | } 333 | if (rhs.typeOf() instanceof LLVMVoidType) { 334 | System.err.println("Invalid Operand: void function found"); 335 | errorFlag = true; 336 | return null; 337 | } 338 | if (!(rhs.typeOf() instanceof LLVMIntegerType)) { 339 | rhs = new LLVMLoadInstruction(builder, rhs, "1"); 340 | } 341 | 342 | if (namedValues.containsKey(idLHS)) { 343 | LLVMValue v = namedValues.get(idLHS); 344 | ; 345 | if (statement1.loc instanceof Location2) { 346 | LLVMValue v1 = statement1.loc.accept(this); 347 | 348 | new LLVMStoreInstruction(builder, rhs, v1); 349 | return null; 350 | } 351 | if (v.typeOf() instanceof LLVMPointerType) { 352 | // TODO optimize this.. 353 | 354 | if (((LLVMPointerType) v.typeOf()).getElementType() instanceof LLVMIntegerType 355 | && rhs.typeOf() instanceof LLVMIntegerType) { 356 | long lw = ((LLVMIntegerType) ((LLVMPointerType) v.typeOf()) 357 | .getElementType()).getWidth(); 358 | long rw = ((LLVMIntegerType) rhs.typeOf()).getWidth(); 359 | if (lw != rw) { 360 | operandIncompatible = true; 361 | errorFlag = true; 362 | return null; 363 | } 364 | 365 | } 366 | 367 | } 368 | switch (statement1.op) { 369 | case "=": 370 | new LLVMStoreInstruction(builder, rhs, v); 371 | return null; 372 | 373 | case "+=": 374 | 375 | new LLVMStoreInstruction(builder, new LLVMAddInstruction( 376 | builder, rhs, new LLVMLoadInstruction(builder, v, "2"), 377 | false, "add"), v); 378 | 379 | return null; 380 | case "-=": 381 | 382 | new LLVMStoreInstruction(builder, new LLVMSubtractInstruction( 383 | builder, rhs, new LLVMLoadInstruction(builder, v, "2"), 384 | false, "add"), v); 385 | return null; 386 | 387 | default: 388 | System.err.println(statement1.op 389 | + ": Assignment operator is expected"); 390 | } 391 | 392 | } else { 393 | System.err.println("Variable Not Declared"); 394 | errorFlag = true; 395 | } 396 | return null; 397 | } 398 | 399 | @Override 400 | public LLVMValue visit(ReturnStatement returnStatement) { 401 | returnStatement.s.accept(this); 402 | return new LLVMReturnInstruction(builder, null); 403 | } 404 | 405 | @Override 406 | public LLVMValue visit(ReturnStatement1 returnStatement1) { 407 | returnStatement1.s.accept(this); 408 | 409 | funcReturnReq = false; 410 | 411 | LLVMValue val = returnStatement1.expr.accept(this); 412 | 413 | if (!operandIncompatible) { 414 | if (val == null) { 415 | errorFlag = true; 416 | return null; 417 | } 418 | if (val.typeOf() instanceof LLVMVoidType) { 419 | errorFlag = true; 420 | System.err.println("Void Function doesn't return any value"); 421 | return null; 422 | } 423 | if (!(val.typeOf() instanceof LLVMIntegerType)) { 424 | val = new LLVMLoadInstruction(builder, val, "ret"); 425 | } 426 | if (val.typeOf() instanceof LLVMIntegerType) { 427 | if (isBoolFunc) { 428 | isBoolFunc = false; 429 | if (((LLVMIntegerType) val.typeOf()).getWidth() == 1) { 430 | new LLVMReturnInstruction(builder, val); 431 | } else { 432 | System.err 433 | .println("incompatible return type : boolean expected; int found"); 434 | errorFlag = true; 435 | } 436 | } else { 437 | if (((LLVMIntegerType) val.typeOf()).getWidth() == 1) { 438 | System.err 439 | .println("incompatible return type : int expected; boolean found"); 440 | errorFlag = true; 441 | } 442 | new LLVMReturnInstruction(builder, val); 443 | } 444 | 445 | } 446 | 447 | } else { 448 | System.err.println("Operand Incompatible"); 449 | errorFlag = true; 450 | } 451 | return null; 452 | } 453 | 454 | @Override 455 | public LLVMIntegerType visit(IntegerType integerType) { 456 | return LLVMIntegerType.i32; 457 | } 458 | 459 | @Override 460 | public LLVMIntegerType visit(BooleanType booleanType) { 461 | return LLVMIntegerType.i1; 462 | } 463 | 464 | @Override 465 | public LLVMVoidType visit(Void void1) { 466 | LLVMVoidType voidType = new LLVMVoidType(); 467 | return voidType; 468 | } 469 | 470 | @Override 471 | public LLVMValue visit(callout_arg callout_arg) { 472 | String arg = callout_arg.s; 473 | if (arg.charAt(0) == '\"') 474 | arg = arg.substring(1, arg.length() - 1).trim(); 475 | return new LLVMConstantString(arg, true); 476 | 477 | } 478 | 479 | @Override 480 | public LLVMValue visit(callout_arg1 callout_arg) { 481 | return callout_arg.expr.accept(this); 482 | } 483 | 484 | @Override 485 | public LLVMValue visit(IntegerLiteral integerLiteral) { 486 | return LLVMConstantInteger.constantInteger(LLVMIntegerType.i32, 487 | integerLiteral.value, false); 488 | } 489 | 490 | @Override 491 | public LLVMValue visit(BooleanLiteral booleanLiteral) { 492 | if (booleanLiteral.value.equalsIgnoreCase("True")) 493 | return LLVMConstantInteger.constantInteger(LLVMIntegerType.i1, 1, 494 | false); 495 | if (booleanLiteral.value.equalsIgnoreCase("False")) 496 | return LLVMConstantInteger.constantInteger(LLVMIntegerType.i1, 0, 497 | false); 498 | return null; 499 | 500 | } 501 | 502 | @Override 503 | public String visit(Return return1) { 504 | return return1.s; 505 | 506 | } 507 | 508 | @Override 509 | public String visit(Break break1) { 510 | return break1.s; 511 | 512 | } 513 | 514 | @Override 515 | public String visit(Continue continue1) { 516 | return continue1.s; 517 | 518 | } 519 | 520 | @Override 521 | public String visit(Class class1) { 522 | return class1.id; 523 | } 524 | 525 | @Override 526 | public String visit(ELSE else1) { 527 | return else1.s; 528 | 529 | } 530 | 531 | @Override 532 | public String visit(IF if1) { 533 | return if1.s1; 534 | 535 | } 536 | 537 | @Override 538 | public String visit(For for1) { 539 | return for1.s; 540 | 541 | } 542 | 543 | @Override 544 | public String visit(StringLiteral stringLiteral) { 545 | return stringLiteral.s; 546 | 547 | } 548 | 549 | @Override 550 | public String visit(CallOut callOut) { 551 | return callOut.s; 552 | 553 | } 554 | 555 | @Override 556 | public void visit(Field1 field1) { 557 | field1.id.accept(this); 558 | } 559 | 560 | @Override 561 | public LLVMValue visit(Block1 block) { 562 | for (Var_Decl var : block.vars) 563 | var.accept(this); 564 | for (StatementIntf stmt : block.stmts) 565 | stmt.accept(this); 566 | return null; 567 | } 568 | 569 | @Override 570 | public LLVMValue visit(Block2 block2) { 571 | for (Var_Decl var : block2.vars) 572 | var.accept(this); 573 | return null; 574 | 575 | } 576 | 577 | @Override 578 | public LLVMValue visit(Block3 block3) { 579 | for (StatementIntf stmt : block3.stmts) 580 | if (!breakOrContStat) 581 | stmt.accept(this); 582 | return null; 583 | } 584 | 585 | @Override 586 | public LLVMValue visit(IfStatement ifStatement) { 587 | 588 | ifStatement.s1.accept(this); 589 | 590 | LLVMFunction iffunc = builder.getInsertBlock().getParent(); 591 | LLVMBasicBlock ifelse = iffunc.appendBasicBlock("if.then"); 592 | LLVMBasicBlock ifend = iffunc.appendBasicBlock("if.end"); 593 | 594 | this.iffunc1 = iffunc; 595 | this.ifend1 = ifend; 596 | this.ifelse1 = ifelse; 597 | 598 | LLVMValue cond = ifStatement.expr.accept(this); 599 | 600 | if (!operandIncompatible) { 601 | 602 | if (cond != null) { 603 | new LLVMBranchInstruction(builder, cond, ifelse, ifend); 604 | builder.positionBuilderAtEnd(ifelse); 605 | ifStatement.block.accept(this); 606 | if (!breakOrContStat) { 607 | new LLVMBranchInstruction(builder, ifend); 608 | } 609 | breakOrContStat = false; 610 | 611 | builder.positionBuilderAtEnd(ifend); 612 | } 613 | 614 | else { 615 | builder.positionBuilderAtEnd(ifelse); 616 | ifStatement.block.accept(this); 617 | if (!breakOrContStat) { 618 | new LLVMBranchInstruction(builder, ifend); 619 | } 620 | breakOrContStat = false; 621 | 622 | builder.positionBuilderAtEnd(ifend); 623 | } 624 | } else { 625 | builder.positionBuilderAtEnd(ifelse); 626 | builder.positionBuilderAtEnd(ifend); 627 | 628 | System.err.println("Incompatible Operands in IF condition"); 629 | errorFlag = true; 630 | } 631 | return null; 632 | } 633 | 634 | @Override 635 | public LLVMValue visit(IfElseStatement ifElseStatement) { 636 | 637 | ifElseStatement.s1.accept(this); 638 | ifElseStatement.s2.accept(this); 639 | 640 | LLVMFunction iffunc = builder.getInsertBlock().getParent(); 641 | LLVMBasicBlock ifelse = iffunc.appendBasicBlock("if.then"); 642 | LLVMBasicBlock ifend = iffunc.appendBasicBlock("if.end"); 643 | LLVMBasicBlock ifelseend = iffunc.appendBasicBlock("ifelse.end"); 644 | this.iffunc1 = iffunc; 645 | this.ifend1 = ifend; 646 | this.ifelse1 = ifelse; 647 | 648 | LLVMValue cond = ifElseStatement.expr.accept(this); 649 | 650 | if (!operandIncompatible) { 651 | if (cond != null) { 652 | new LLVMBranchInstruction(builder, cond, ifelse, ifend); 653 | builder.positionBuilderAtEnd(ifelse); 654 | ifElseStatement.ifBlock.accept(this); 655 | if (!breakOrContStat) { 656 | new LLVMBranchInstruction(builder, ifelseend); 657 | } 658 | breakOrContStat = false; 659 | builder.positionBuilderAtEnd(ifend); 660 | ifElseStatement.elseBlock.accept(this); 661 | if (!breakOrContStat) { 662 | new LLVMBranchInstruction(builder, ifelseend); 663 | } 664 | breakOrContStat = false; 665 | builder.positionBuilderAtEnd(ifelseend); 666 | } else { 667 | builder.positionBuilderAtEnd(ifelse); 668 | ifElseStatement.ifBlock.accept(this); 669 | if (!breakOrContStat) { 670 | new LLVMBranchInstruction(builder, ifelseend); 671 | } 672 | breakOrContStat = false; 673 | builder.positionBuilderAtEnd(ifend); 674 | ifElseStatement.elseBlock.accept(this); 675 | new LLVMBranchInstruction(builder, ifelseend); 676 | builder.positionBuilderAtEnd(ifelseend); 677 | } 678 | } else { 679 | builder.positionBuilderAtEnd(ifelse); 680 | builder.positionBuilderAtEnd(ifend); 681 | 682 | System.err.println("Incompatible Operands - If Statement"); 683 | errorFlag = true; 684 | } 685 | return null; 686 | } 687 | 688 | @Override 689 | public LLVMValue visit(Location1 location) { 690 | String id = location.id.accept(this); 691 | LLVMValue val = null; 692 | if (namedValues.containsKey(id)) { 693 | val = namedValues.get(id); 694 | if (val == null) { 695 | System.err.println(id + ": Variable not initilised"); 696 | errorFlag = true; 697 | } 698 | } else { 699 | System.err.println(id + ": Variable not declared"); 700 | errorFlag = true; 701 | } 702 | return val; 703 | } 704 | 705 | @Override 706 | public LLVMValue visit(Expression1 expression1) { 707 | LLVMValue lhs = expression1.expr.accept(this); 708 | LLVMValue rhs = expression1.term1.accept(this); 709 | if (rhs == null || lhs == null) { 710 | errorFlag = true; 711 | return null; 712 | } 713 | 714 | if (!(lhs.typeOf() instanceof LLVMIntegerType)) { 715 | lhs = new LLVMLoadInstruction(builder, lhs, "1"); 716 | } 717 | if (!(rhs.typeOf() instanceof LLVMIntegerType)) { 718 | rhs = new LLVMLoadInstruction(builder, rhs, "2"); 719 | } 720 | String op = expression1.operator; 721 | 722 | long lw = ((LLVMIntegerType) lhs.typeOf()).getWidth(); 723 | long rw = ((LLVMIntegerType) rhs.typeOf()).getWidth(); 724 | if (lw != rw) { 725 | operandIncompatible = true; 726 | errorFlag = true; 727 | return null; 728 | } 729 | 730 | switch (op) { 731 | case "+": 732 | return new LLVMAddInstruction(builder, lhs, rhs, false, "add1"); 733 | case "-": 734 | return new LLVMSubtractInstruction(builder, lhs, rhs, false, "sub1"); 735 | case "/": 736 | return new LLVMDivideInstruction(builder, lhs, rhs, 737 | DivisionType.UNSIGNEDINT, "divl"); 738 | case "*": 739 | return new LLVMMultiplyInstruction(builder, lhs, rhs, false, 740 | "mul instruction"); 741 | case "%": 742 | return new LLVMRemainderInstruction(builder, lhs, rhs, 743 | RemainderType.UNSIGNEDINT, "mod1"); 744 | 745 | case ">": 746 | return new LLVMIntegerComparison(builder, 747 | LLVMIntPredicate.LLVMIntSGT, lhs, rhs, "gt1"); 748 | case "<": 749 | return new LLVMIntegerComparison(builder, 750 | LLVMIntPredicate.LLVMIntSLT, lhs, rhs, "lt1"); 751 | case ">=": 752 | return new LLVMIntegerComparison(builder, 753 | LLVMIntPredicate.LLVMIntSGE, lhs, rhs, "ge1"); 754 | case "<=": 755 | return new LLVMIntegerComparison(builder, 756 | LLVMIntPredicate.LLVMIntSLE, lhs, rhs, "le1"); 757 | case "==": 758 | return new LLVMIntegerComparison(builder, 759 | LLVMIntPredicate.LLVMIntEQ, rhs, lhs, "eq1"); 760 | case "!=": 761 | return new LLVMIntegerComparison(builder, 762 | LLVMIntPredicate.LLVMIntNE, rhs, lhs, "neq1"); 763 | case "||": 764 | if (rw == 1 && lw == 1) { 765 | LLVMValue cmp1 = new LLVMIntegerComparison(builder, 766 | LLVMIntPredicate.LLVMIntEQ, lhs, 767 | LLVMConstantInteger.constantInteger(LLVMIntegerType.i1, 768 | 0, false), "orOp1"); 769 | LLVMBasicBlock nextCond = iffunc1.appendBasicBlock("next"); 770 | new LLVMBranchInstruction(builder, cmp1, nextCond, ifelse1); 771 | builder.positionBuilderAtEnd(nextCond); 772 | LLVMValue cmp2 = new LLVMIntegerComparison(builder, 773 | LLVMIntPredicate.LLVMIntEQ, rhs, 774 | LLVMConstantInteger.constantInteger(LLVMIntegerType.i1, 775 | 0, false), "orOp2"); 776 | new LLVMBranchInstruction(builder, cmp2, ifend1, ifelse1); 777 | } else { 778 | operandIncompatible = true; 779 | } 780 | 781 | return null; 782 | 783 | case "&&": 784 | if (lw == 1 && rw == 1) { 785 | 786 | LLVMValue cmp3 = new LLVMIntegerComparison(builder, 787 | LLVMIntPredicate.LLVMIntEQ, lhs, 788 | LLVMConstantInteger.constantInteger(LLVMIntegerType.i1, 789 | 0, false), "andOp1"); 790 | LLVMBasicBlock b1 = iffunc1.appendBasicBlock("next"); 791 | new LLVMBranchInstruction(builder, cmp3, ifend1, b1); 792 | builder.positionBuilderAtEnd(b1); 793 | LLVMValue cmp4 = new LLVMIntegerComparison(builder, 794 | LLVMIntPredicate.LLVMIntEQ, rhs, 795 | LLVMConstantInteger.constantInteger(LLVMIntegerType.i1, 796 | 0, false), "andOp2"); 797 | new LLVMBranchInstruction(builder, cmp4, ifend1, ifelse1); 798 | } else { 799 | operandIncompatible = true; 800 | } 801 | 802 | return null; 803 | default: 804 | System.err.println(op + ": Invalid operator"); 805 | 806 | } 807 | 808 | return null; 809 | 810 | } 811 | 812 | @Override 813 | public LLVMValue visit(Expression2 expression2) { 814 | String op = expression2.operator; 815 | LLVMValue val = expression2.term5.accept(this); 816 | 817 | switch (op) { 818 | case "-": 819 | if (!(val.typeOf() instanceof LLVMIntegerType)) { 820 | return new LLVMSubtractInstruction(builder, 821 | LLVMConstantInteger.constantInteger( 822 | LLVMIntegerType.i32, 0, false), 823 | new LLVMLoadInstruction(builder, val, "tmp"), false, 824 | "u_minus"); 825 | } 826 | return new LLVMUnaryBitwiseInstruction( 827 | UnaryBitwiseInstructionType.NEGATIVE, builder, val, 828 | "u_minus"); 829 | case "!": 830 | return new LLVMUnaryBitwiseInstruction( 831 | UnaryBitwiseInstructionType.NOT, builder, 832 | new LLVMLoadInstruction(builder, val, "uminus"), "u_not"); 833 | default: 834 | System.err.println(op + ": Invalid Operator"); 835 | } 836 | return null; 837 | 838 | } 839 | 840 | @Override 841 | public LLVMValue visit(Expression3 expression3) { 842 | return expression3.term5.accept(this); 843 | } 844 | 845 | @Override 846 | public LLVMValue visit(BreakStatement breakStatement) { 847 | if (isForStatement) { 848 | breakStatement.s.accept(this); 849 | breakOrContStat = true; 850 | new LLVMBranchInstruction(builder, forEnd1); 851 | } else { 852 | errorFlag = true; 853 | System.err.println("Error: Break must be inside for statement"); 854 | } 855 | return null; 856 | } 857 | 858 | @Override 859 | public LLVMValue visit(ContinueStatement continueStatement) { 860 | if (isForStatement) { 861 | continueStatement.s.accept(this); 862 | breakOrContStat = true; 863 | new LLVMBranchInstruction(builder, forInc1); 864 | } else { 865 | errorFlag = true; 866 | System.err.println("Error: Continue must be inside for statement"); 867 | } 868 | return null; 869 | } 870 | 871 | @Override 872 | public LLVMValue visit(Location2 location) { 873 | 874 | String id = location.id.accept(this); 875 | LLVMValue val = null; 876 | if (namedValues.containsKey(id)) { 877 | val = namedValues.get(id); 878 | 879 | if (val == null) { 880 | System.err.println(id + ": Variable not initilised"); 881 | errorFlag = true; 882 | } else { 883 | LLVMValue position = location.expr.accept(this); 884 | if (position.typeOf() instanceof LLVMPointerType) { 885 | position = new LLVMLoadInstruction(builder, position, "tmp"); 886 | } 887 | if (position.typeOf() instanceof LLVMIntegerType) { 888 | 889 | LLVMValue in = LLVMConstantInteger.constantInteger( 890 | LLVMIntegerType.i32, 0, false); 891 | return new LLVMGetElementPointerInstruction(builder, val, 892 | new LLVMValue[] { in, position }, "arrayEle"); 893 | 894 | } 895 | System.err.println("Invalid Array Position"); 896 | errorFlag = true; 897 | } 898 | } else { 899 | System.err.println(id + ": Variable not declared"); 900 | errorFlag = true; 901 | } 902 | return val; 903 | 904 | } 905 | 906 | @Override 907 | public LLVMValue visit(ForStatement forStatement) { 908 | isForStatement = true; 909 | forStatement.s.accept(this); 910 | 911 | LLVMValue initialVal = forStatement.expr1.accept(this); 912 | LLVMValue condVal = forStatement.expr2.accept(this); 913 | if (!operandIncompatible) { 914 | LLVMFunction forFunc = builder.getInsertBlock().getParent(); 915 | 916 | LLVMBasicBlock forCond = forFunc.appendBasicBlock("for.cond"); 917 | if (condVal.typeOf() instanceof LLVMPointerType) { 918 | condVal = new LLVMLoadInstruction(builder, condVal, "%1"); 919 | } 920 | if (initialVal.typeOf() instanceof LLVMPointerType) { 921 | initialVal = new LLVMLoadInstruction(builder, initialVal, "%2"); 922 | } 923 | LLVMValue cmpResult = null; 924 | 925 | String id = forStatement.id.accept(this); 926 | LLVMValue idVal = new LLVMStackAllocation(builder, 927 | initialVal.typeOf(), null, id); 928 | namedValues.put(id, idVal); 929 | new LLVMStoreInstruction(builder, initialVal, idVal); 930 | 931 | new LLVMBranchInstruction(builder, forCond); 932 | builder.positionBuilderAtEnd(forCond); 933 | cmpResult = new LLVMIntegerComparison(builder, 934 | LLVMIntPredicate.LLVMIntSLT, new LLVMLoadInstruction( 935 | builder, idVal, "tmp"), condVal, "cond"); 936 | LLVMBasicBlock forBody = forFunc.appendBasicBlock("for.body"); 937 | LLVMBasicBlock forEnd = forFunc.appendBasicBlock("for.end"); 938 | this.forEnd1 = forEnd; 939 | new LLVMBranchInstruction(builder, cmpResult, forBody, forEnd); 940 | builder.positionBuilderAtEnd(forBody); 941 | LLVMBasicBlock forInc = forFunc.appendBasicBlock("for.inc"); 942 | this.forInc1 = forInc; 943 | forStatement.block.accept(this); 944 | if (!breakOrContStat) { 945 | new LLVMBranchInstruction(builder, forInc); 946 | } 947 | breakOrContStat = false; 948 | builder.positionBuilderAtEnd(forInc); 949 | 950 | LLVMValue newIdval = new LLVMAddInstruction(builder, 951 | new LLVMLoadInstruction(builder, idVal, "tmp"), 952 | LLVMConstantInteger.constantInteger(LLVMIntegerType.i32, 1, 953 | false), false, "inc"); 954 | 955 | new LLVMStoreInstruction(builder, newIdval, idVal); 956 | 957 | new LLVMBranchInstruction(builder, forCond); 958 | builder.positionBuilderAtEnd(forEnd); 959 | } else { 960 | System.err 961 | .println("Initial and Ending expression of for must be of integer type"); 962 | errorFlag = true; 963 | } 964 | 965 | isForStatement = false; 966 | 967 | return null; 968 | } 969 | 970 | @Override 971 | public LLVMValue visit(MethodStatement1 methodStatement1) { 972 | 973 | String functionName = methodStatement1.id.accept(this); 974 | 975 | LLVMFunction f = module.getNamedFunction(functionName); 976 | if (f.getInstance() == null) { 977 | System.err.println(functionName 978 | + ": Error: No such function exists; check order of function definitions"); 979 | errorFlag = true; 980 | } else { 981 | if (namedValues.containsKey(functionName)) { 982 | long arg_size = f.countParameters(); 983 | if (arg_size != 0) { 984 | System.err.println("Error: Invalid Number of arguments"); 985 | errorFlag = true; 986 | } else { 987 | return new LLVMCallInstruction(builder, f, 988 | new LLVMValue[0], ""); 989 | } 990 | } else { 991 | System.err 992 | .println("Check order of functions defined"); 993 | errorFlag = true; 994 | } 995 | } 996 | return null; 997 | } 998 | 999 | @Override 1000 | public LLVMValue visit(MethodStatement2 methodStatement2) { 1001 | String functionName = methodStatement2.id.accept(this); 1002 | 1003 | LLVMFunction f = module.getNamedFunction(functionName); 1004 | if (f.getInstance() == null) { 1005 | System.err.println(functionName 1006 | + ": Error: No such function exists; check order of function definitions"); 1007 | errorFlag = true; 1008 | } else { 1009 | if (namedValues.containsKey(functionName)) { 1010 | long arg_size = f.countParameters(); 1011 | if (arg_size != methodStatement2.exprs.size()) { 1012 | System.err.println("Error: Invalid Number of arguments"); 1013 | errorFlag = true; 1014 | } else { 1015 | LLVMValue[] args1 = new LLVMValue[methodStatement2.exprs 1016 | .size()]; 1017 | LLVMValue exprVal = null; 1018 | int i = 0; 1019 | for (ExpressionIntf expr : methodStatement2.exprs) { 1020 | 1021 | exprVal = expr.accept(this); 1022 | if (exprVal.typeOf() instanceof LLVMPointerType) 1023 | args1[i++] = new LLVMLoadInstruction(builder, 1024 | exprVal, "1" + i); 1025 | else 1026 | args1[i++] = exprVal; 1027 | long lw = ((LLVMIntegerType) args1[i - 1].typeOf()) 1028 | .getWidth(); 1029 | long rw = ((LLVMIntegerType) f.getParameter(i - 1) 1030 | .typeOf()).getWidth(); 1031 | if (lw != rw) { 1032 | System.err 1033 | .println("Formal and actual parameters are not type compatible"); 1034 | errorFlag = true; 1035 | return null; 1036 | } 1037 | } 1038 | 1039 | return new LLVMCallInstruction(builder, f, args1, ""); 1040 | } 1041 | } else { 1042 | System.err 1043 | .println("Check order of functions defined"); 1044 | errorFlag = true; 1045 | } 1046 | } 1047 | return null; 1048 | 1049 | } 1050 | 1051 | @Override 1052 | public LLVMValue visit(MethodStatementCallOut1 methodStatementCallOut1) { 1053 | 1054 | LLVMGlobalVariable s = null; 1055 | int i = 0; 1056 | String ident = "str"; 1057 | String functionName = null; 1058 | 1059 | methodStatementCallOut1.s.accept(this); 1060 | functionName = methodStatementCallOut1.s1.accept(this); 1061 | functionName = functionName.substring(1, functionName.length() - 1) 1062 | .trim(); 1063 | LLVMFunction f = null; 1064 | if (module.getNamedFunction(functionName).getInstance() == null) { 1065 | 1066 | LLVMFunctionType fT = new LLVMFunctionType(LLVMIntegerType.i32, 1067 | new LLVMType[] {}, true); 1068 | f = new LLVMFunction(module, functionName, fT); 1069 | f.setLinkage(LLVMLinkage.LLVMExternalLinkage); 1070 | } else 1071 | f = module.getNamedFunction(functionName); 1072 | 1073 | LLVMValue[] args = new LLVMValue[methodStatementCallOut1.calloutargs 1074 | .size()]; 1075 | LLVMValue in = LLVMConstantInteger.constantInteger(LLVMIntegerType.i32, 1076 | 0, false); 1077 | 1078 | for (ExpressionIntf callout_arg : methodStatementCallOut1.calloutargs) { 1079 | LLVMValue arg = callout_arg.accept(this); 1080 | if (arg == null) { 1081 | errorFlag = true; 1082 | return null; 1083 | } 1084 | if (arg.typeOf() instanceof LLVMIntegerType) { 1085 | args[i++] = arg; 1086 | continue; 1087 | } 1088 | if (arg.typeOf() instanceof LLVMPointerType) { 1089 | args[i++] = new LLVMLoadInstruction(builder, arg, "0"); 1090 | continue; 1091 | } 1092 | if (arg.typeOf() instanceof LLVMVoidType) { 1093 | System.err.println("Invalid Operand: void function found"); 1094 | errorFlag = true; 1095 | return null; 1096 | } 1097 | long p = ((LLVMArrayType) arg.typeOf()).getLength(); 1098 | s = module.addGlobal(new LLVMArrayType(LLVMIntegerType.i8, p), 1099 | ident + i); 1100 | s.setInitializer((LLVMConstant) arg); 1101 | s.setConstant(true); 1102 | s.setLinkage(LLVMLinkage.LLVMPrivateLinkage); 1103 | 1104 | args[i++] = new LLVMGetElementPointerInstruction(builder, s, 1105 | new LLVMValue[] { in, in }, "" + i); 1106 | } 1107 | 1108 | return new LLVMCallInstruction(builder, f, args, "call"); 1109 | } 1110 | 1111 | @Override 1112 | public LLVMValue visit(MethodStatementCallOut2 methodStatementCallOut2) { 1113 | String functionName = methodStatementCallOut2.s1.accept(this); 1114 | functionName = functionName.substring(1, functionName.length() - 1) 1115 | .trim(); 1116 | 1117 | LLVMFunction f = null; 1118 | if (module.getNamedFunction(functionName).getInstance() == null) { 1119 | 1120 | LLVMFunctionType fT = new LLVMFunctionType(LLVMIntegerType.i32, 1121 | new LLVMType[] {}, true); 1122 | f = new LLVMFunction(module, functionName, fT); 1123 | f.setLinkage(LLVMLinkage.LLVMExternalLinkage); 1124 | } else 1125 | f = module.getNamedFunction(functionName); 1126 | 1127 | return new LLVMCallInstruction(builder, f, new LLVMValue[] {}, "call"); 1128 | 1129 | } 1130 | 1131 | } --------------------------------------------------------------------------------