Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Homework 4 #3

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions HW4-Ramtin/3Code/MJavaLex.g4
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
lexer grammar MJavaLex;

fragment Digit
: [0-9]
;

fragment Letter
: [a-zA-Z_]
;

fragment LetterOrDigit
: Letter
| Digit
;

RETURN: 'return';
INT: 'int';
BOOLEAN: 'boolean';
IF: 'if';
ELSE: 'else';
WHILE: 'while';
SOUT: 'System.out.println';
ASSIGN: '=';
ADD: '+';
SUB: '-';
MUL: '*';
LT: '<';
ANDAND: '&&';
LEN: 'length';
NOT: '!';
NEW: 'new';
THIS: 'this';
CLASS: 'class';
LPAREN: '(';
RPAREN: ')';
LBRACE: '{';
RBRACE: '}';
LBRACK: '[';
RBRACK: ']';
SEMI: ';';
COMMA: ',';
DOT: '.';
TRUE: 'true';
FALSE: 'false';
PUBLIC: 'public';
STATIC: 'static';
VOID: 'void';
MAIN: 'main';
STRING: 'String';
EXTENDS: 'extends';

IDENTIFIER
: Letter LetterOrDigit*
;

INTEGER_LITERAL
: ( '0' | [1-9] Digit* )
;


WS: [ \t\r\n\u000C]+ -> channel(HIDDEN);
COMMENT: '/*' .*? '*/' -> channel(HIDDEN);
LINE_COMMENT: '//' ~[\r\n]* -> channel(HIDDEN);


235 changes: 235 additions & 0 deletions HW4-Ramtin/3Code/MJavaParser.g4
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
parser grammar MJavaParser;

options { tokenVocab=MJavaLex; }

@parser::members {
temp_counter = 0
goto_counter = 0
def create_goto(self):
self.goto_counter += 1
return 'L' + str(self.goto_counter)
def create_temp(self):
self.temp_counter += 1
return 'T' + str(self.temp_counter)
def remove_temp(self):
self.temp_counter -= 1
def get_temp(self):
return 'T' + str(self.temp_counter)
}

program
: {print('3 Code Address:')} mainClass ( classDeclaration )* EOF
;

mainClass
: 'class' identifier '{' 'public' 'static' 'void' 'main' '(' 'String' '[' ']' identifier ')' '{' s=statement '}' '}'
{print($s.value_attr)}
;

classDeclaration
: 'class' identifier ( 'extends' identifier )? '{' ( varDeclaration )* ( methodDeclaration )* '}'
;

varDeclaration
: t=type_ i=identifier ';'
;

methodDeclaration
: 'public' type_ identifier '(' ( type_ identifier ( ',' type_ identifier )* )? ')' '{' ( varDeclaration )* ( s=statement {print($s.value_attr)} )* 'return' expression ';' '}'
;

type_
: 'int' '[' ']'
| 'boolean'
| 'int'
| i=identifier
;


statement returns [value_attr = str(), type_attr = str(),old_value_attr = str()]
: '{'{$value_attr=''} ( s=statement {$value_attr=$value_attr+$s.value_attr+'\n'} )* '}'
| 'if' '(' e=expression ')' s1=statement 'else' s2=statement
{goto1 = self.create_goto()}
{goto2 = self.create_goto()}
{for item in $e.temp_list:
$value_attr = $value_attr + item + '\n'
}
{$value_attr=$value_attr+'\t\t'+'if ('+$e.value_attr+') goto '+goto1+'\n'}
{$value_attr = $value_attr + $s2.value_attr}
{$value_attr= $value_attr+'\t\t'+'goto '+goto2+'\n'}
{$value_attr = $value_attr+goto1+':'+$s1.value_attr}
{$value_attr= $value_attr + goto2+':\t\t'}
| 'while' '(' e=expression ')' s=statement
{goto1 = self.create_goto()}
{goto2 = self.create_goto()}
{$value_attr= '\t\t'+'goto '+goto1+'\n'}
{$value_attr = $value_attr+goto2+':'+$s.value_attr}
{$value_attr= $value_attr + goto1+':'}
{for item in $e.temp_list:
$value_attr = $value_attr + item + '\n'
}
{$value_attr=$value_attr+'\t\t'+'if ('+$e.value_attr+') goto '+goto2+'\n'}
| 'System.out.println' '(' expression ')' ';'
| i=identifier '=' e=expression ';'
{$value_attr = ''}
{for item in $e.temp_list:
$value_attr=$value_attr+item+'\n'}
{$value_attr=$value_attr+'\t\t'+$i.value_attr +' = '+ $e.value_attr}
| i=identifier '[' e1=expression ']' '=' e2=expression ';'
{$value_attr = ''}
{for item in $e1.temp_list:
$value_attr=$value_attr+item+'\n'}
{for item in $e2.temp_list:
$value_attr=$value_attr+item+'\n'}
{temp = self.create_temp()}
{$value_attr=$value_attr+'\t\t'+temp+ '='+'sizeOf('+ $i.value_attr+')\n'+'\t\t'+temp+ '='+temp+'*'+$e1.value_attr+'\n'+'\t\t'+temp+ '= &'+$i.value_attr+'+'+temp+'\n'+'\t\t'+'*'+ temp +' = '+ $e2.value_attr}
;

expression returns [value_attr = str(), type_attr = str(),old_value_attr = str(),temp_list = []]
: '(' e=expression ')'
{$type_attr = $e.type_attr}
{$value_attr = $e.value_attr}
{$old_value_attr = $e.old_value_attr}
{$temp_list = $e.temp_list}
| e1=expression '<' e2=expression
{$temp_list = $e1.temp_list}
{$type_attr = 'BOOL'}
{if $e1.old_value_attr is not None:
temp = self.create_temp()
$temp_list.append('\t\t'+temp+'='+$e1.old_value_attr)
$e1.value_attr = temp}
{$temp_list =$temp_list + $e2.temp_list}
{if $e2.old_value_attr is not None:
temp = self.create_temp()
$temp_list.append('\t\t'+temp+'='+$e2.old_value_attr)
$e2.value_attr = temp}
{$value_attr = $e1.value_attr + " < " + $e2.value_attr}
{$old_value_attr = $e1.value_attr + " < " + $e2.value_attr}
| e1=expression '&&' e2=expression
{$type_attr = 'BOOL'}
{$temp_list = $e1.temp_list}
{if $e1.old_value_attr is not None:
temp = self.create_temp()
$temp_list.append('\t\t'+temp+'='+$e1.old_value_attr)
$e1.value_attr = temp}
{$temp_list =$temp_list + $e2.temp_list}
{if $e2.old_value_attr is not None:
temp = self.create_temp()
$temp_list.append('\t\t'+temp+'='+$e2.old_value_attr)
$e2.value_attr = temp}
{$value_attr = $e1.value_attr + " && " + $e2.value_attr}
{$old_value_attr = $e1.value_attr + " && " + $e2.value_attr}
| e1=expression '*' e2=expression
{$temp_list = $e1.temp_list}
{$type_attr = 'INT'}
{if $e1.old_value_attr is not None:
temp = self.create_temp()
$temp_list.append('\t\t'+temp+'='+$e1.old_value_attr)
$e1.value_attr = temp}
{$temp_list =$temp_list + $e2.temp_list}
{if $e2.old_value_attr is not None:
temp = self.create_temp()
$temp_list.append('\t\t'+temp+'='+$e2.old_value_attr)
$e2.value_attr = temp}
{$value_attr = $e1.value_attr + " * " + $e2.value_attr}
{$old_value_attr = $e1.value_attr + " * " + $e2.value_attr}
| e1=expression '+' e2=expression
{$temp_list = $e1.temp_list}
{$type_attr = 'INT'}
{if $e1.old_value_attr is not None:
temp = self.create_temp()
$temp_list.append('\t\t'+temp+'='+$e1.old_value_attr)
$e1.value_attr = temp}
{$temp_list =$temp_list + $e2.temp_list}
{if $e2.old_value_attr is not None:
temp = self.create_temp()
$temp_list.append('\t\t'+temp+'='+$e2.old_value_attr)
$e2.value_attr = temp}
{$value_attr = $e1.value_attr + " + " + $e2.value_attr}
{$old_value_attr = $e1.value_attr + " + " + $e2.value_attr}
| e1=expression '-' e2=expression
{$temp_list = $e1.temp_list}
{$type_attr = 'INT'}
{if $e1.old_value_attr is not None:
temp = self.create_temp()
$temp_list.append('\t\t'+temp+'='+$e1.old_value_attr)
$e1.value_attr = temp}
{$temp_list =$temp_list + $e2.temp_list}
{if $e2.old_value_attr is not None:
temp = self.create_temp()
$temp_list.append('\t\t'+temp+'='+$e2.old_value_attr)
$e2.value_attr = temp}
{$value_attr = $e1.value_attr + " - " + $e2.value_attr}
{$old_value_attr = $e1.value_attr + " - " + $e2.value_attr}
| e1=expression '[' e2=expression ']'
{$temp_list = $e1.temp_list}
{$temp_list =$temp_list + $e2.temp_list}
{$type_attr = 'INT'}
{if $e1.old_value_attr is not None:
print('\t\t','Error in '+$e1.value_attr)}
{if $e2.old_value_attr is not None:
temp = self.create_temp()
$temp_list.append('\t\t'+temp+'='+$e2.old_value_attr)
$e2.value_attr = temp}
{temp = self.create_temp()}
{$temp_list.append('\t\t'+temp+ '='+'sizeOf('+ $e1.value_attr+')')}
{$temp_list.append('\t\t'+temp+ '='+temp+'*'+$e2.value_attr)}
{$temp_list.append('\t\t'+temp+ '= &'+$e1.value_attr+'+'+temp)}
{$value_attr = '*'+temp}
{$old_value_attr = '*'+temp}
| e=expression '.' 'length'
{$temp_list = $e.temp_list}
{$type_attr = 'INT'}
{if $e.old_value_attr is not None:
temp = self.create_temp()
$temp_list.append('\t\t'+temp+'='+$e.old_value_attr)
$e.value_attr = temp}
{$value_attr = $e.value_attr + ".length "}
{$old_value_attr = $e.value_attr + ".length "}
| expression '.' identifier '(' ( expression ( ',' expression )* )? ')'
| INTEGER_LITERAL
{
$type_attr = 'INT'
$value_attr = $INTEGER_LITERAL.text
$old_value_attr = None
}
| 'true'
{
$type_attr = 'BOOL'
$value_attr = 'true'
$old_value_attr = None
}
| 'false'
{
$type_attr = 'BOOL'
$value_attr = 'false'
$old_value_attr = None
}
| i=identifier
{
if $i.type_attr == 'UNKOWN':
$type_attr = $i.type_attr
$value_attr = $i.value_attr
$old_value_attr = None
}
| 'this'
| 'new' 'int' '[' expression ']'
| 'new' identifier '(' ')'
| '!' e=expression
{$temp_list = $e.temp_list}
{$type_attr = 'BOOL'}
{if $e.old_value_attr is not None:
temp = self.create_temp()
$temp_list.append('\t\t'+temp+'='+$e.old_value_attr)
$e1.value_attr = temp}
{$value_attr = '!'+$e.value_attr}
{$old_value_attr = '!'+$e.value_attr}
;

identifier returns [value_attr = str(), type_attr = str()]
: IDENTIFIER
{
$value_attr = $IDENTIFIER.text
$type_attr = 'UNKOWN'
}
;
41 changes: 41 additions & 0 deletions HW4-Ramtin/3Code/QS.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
class QuickSort{
public static void main(String[] a){
System.out.println(new QS().Start(10));
}
}

class QS{
public int Start(int sz){

m = num < x && k < num;
n = num2 < x2 && k2 < num2;

if(m < 10){
k = 10;
}
else{
k = 5;
}

arr[5] = sz + k * 5;

if(k && num<7)
{
arr[1] = b + (num - k) * a;
k = arr[2] + k * 10;
}
else
{
k = arr[5] + k;
}

k = arr[2] + k * 5;

while(k<num)
{
arr[5] = num + x;
}

return 0;
}
}
5 changes: 5 additions & 0 deletions HW4-Ramtin/3Code/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Three Code Addressing

Only implemented "while", "if", "else" statements and variable, array assignments.

The test file "QS.java" covers all of the things that is implemented and supported in this code.
Loading