Skip to content
Snippets Groups Projects
Commit ecd5a07a authored by pycsham's avatar pycsham
Browse files

Made changes to scanner and grammar after going over around 3/4 of error...

Made changes to scanner and grammar after going over around 3/4 of error public test cases. (e.g. bitwise operations, integer range checking)
parent e0c1196b
No related branches found
No related tags found
No related merge requests found
......@@ -85,7 +85,11 @@ wrongJavaKeyWordDict = {
'transient',
'try',
'volatile',
'_'
'_',
'this',
'super',
'Long',
'Float'
}
# a set that contains unsed operators or seperators
......@@ -126,9 +130,6 @@ JoosDFATokens = set([
'SUB', # -
'MULT', # *
'DIV', # /
'BITAND', # &
'BITOR', # |
'EXP', # ^
'MOD', # %
# Separators:
......@@ -142,9 +143,8 @@ JoosDFATokens = set([
'COMMA', # ,
'PERIOD', # .
# Unused Sep and Operators that we need to recognize
# Unused Seperator (TODO: refactor this)
'ELLIPSIS', # ...
'ARROW', # ->
])
......@@ -209,8 +209,6 @@ def JoosTransition(input, state):
return 'BITAND'
if (input == '|'):
return 'BITOR'
if (input == '^'):
return 'EXP'
if (input == '%'):
return 'MOD'
......@@ -240,6 +238,31 @@ def JoosTransition(input, state):
if (input == '\''):
return 'LCHAR'
# Handling all operators that are not allowed in Joos (some cases are handled elsewhere)
elif state == 'ADD':
if input == '+':
return 'OPDISCARD'
if input == '=':
return 'OPDISCARD'
return None
elif state == 'SUB':
if (input == '>'):
return 'OPDISCARD'
if input == '-':
return 'OPDISCARD'
if input == '=':
return 'OPDISCARD'
return None
elif state == 'MULT':
if input == '=':
return 'OPDISCARD'
return None
elif state == 'MOD':
if input == '=':
return 'OPDISCARD'
return None
elif (state == 'ID'):
# dealing with compound names
if input == '.':
......@@ -267,6 +290,8 @@ def JoosTransition(input, state):
elif (state == 'NUM'):
if(input.isdigit()):
return 'NUM'
if input == '.':
return 'FLOAT' # not accepted
return None
# string literal
......@@ -277,6 +302,8 @@ def JoosTransition(input, state):
return 'LITERALSTRING'
return 'LSTRING'
elif (state == 'STRINGESC'):
if input == 'u':
return 'UNICODE' #going to be discarded
return 'LSTRING'
# char literal
......@@ -299,10 +326,14 @@ def JoosTransition(input, state):
elif (state == 'GT'):
if (input == '='):
return 'GE'
elif input == '>':
return 'OPDISCARD'
return None
elif (state == 'LT'):
if (input == '='):
return 'LE'
elif input == '<':
return 'OPDISCARD'
return None
elif (state == 'NOT'):
if (input == '='):
......@@ -311,11 +342,16 @@ def JoosTransition(input, state):
elif (state == 'BITAND'):
if (input == '&'):
return 'AND'
elif input == '=':
return 'OPDISCARD'
return None
elif (state == 'BITOR'):
if (input == '|'):
return 'OR'
elif input == '=':
return 'OPDISCARD'
return None
# Comments
elif(state == 'DIV'):
......@@ -323,6 +359,8 @@ def JoosTransition(input, state):
return 'COMMENT'
elif (input == '*'):
return 'LCOMMENT'
elif input == '=':
return 'OPDISCARD'
return None
elif(state == 'LCOMMENT'):
if (input == '*'):
......@@ -354,12 +392,14 @@ def JoosTransition(input, state):
if(input == '.'):
return 'ELLIPSIS'
return None
elif (state == 'SUB'):
if (input == '>'):
return 'ARROW'
return None
# Handling hexidecimal literals (error case)
elif state == 'ZERO':
if input == 'x':
return 'HEXLITERAL'
elif input == '.':
return 'FLOAT'
else:
return None
......@@ -367,7 +407,7 @@ def JoosTransition(input, state):
#TODO: remove alphabets since it's unecessary in our DFA implementation
specialChars = set(list(".;:,@{}()[]<>!?+-*/&|^%=''\"\\"))
JoosAccept = JoosDFATokens.union({'WHITESPACE', 'COMMENT', 'NEWLINE', 'LCOMMENT', 'RCOMMENT', 'NEWLINEC', 'LCOM2', 'LCOM3'})
JoosStates = JoosAccept.union({'START', 'PERIOD2', 'HALFCOMP'})
JoosStates = JoosAccept.union({'START', 'PERIOD2', 'HALFCOMP', 'HEXLITERAL', 'OPDISCARD', 'BITAND', 'BITOR', 'FLOAT', 'UNICODE'})
JoosAlphabet = set(string.ascii_lowercase).union(set(string.ascii_uppercase)).union(set(string.digits)).union(specialChars)
######################### DFA #######################################
......@@ -431,12 +471,25 @@ def scan(input):
if index < indexRange-1:
if tokens[index+1].name == 'NUM':
return (None, "wrong integer literal: starts with 0")
# Checking integer range (does not cover all edge cases)
elif token.name == 'NUM' and index > 0 and tokens[index-1].name == 'SUB' and int(token.lex) > 2147483648:
return (None, "integer too small")
elif token.name == 'NUM' and int(token.lex) > 2147483647:
return (None, "interger too large")
# dealing with keywords in Java but not in Joos
elif token.name == 'ID' and token.lex in wrongJavaKeyWordDict:
return (None, "keyword in Java but not in Joos")
# dealing with operators and seperators in Java but not in Joos
elif token.name in wrongJavaOpDict:
return (None, "operator in Java but not in Joos")
# Checking wrong keywords in compIDs
elif token.name == 'COMPID':
temp = token.lex.split('.')
if temp[0] in wrongJavaKeyWordDict:
return (None, "wrong keyword in comp id")
# Checking if the multi line comment has a closing tag
if token.name == 'LCOMMENT':
......
64
63
EOF
BOF
INTERFACE
......@@ -24,7 +24,6 @@ PUBLIC
RETURN
SHORT
STATIC
THIS
VOID
WHILE
ID
......@@ -160,7 +159,7 @@ primaryNoArrayAccess
arrayID
methodID
start
200
199
start BOF packageDcl importDcls topDcls EOF
packageDcl PACKAGE name SEMICO
packageDcl PACKAGE ID SEMICO
......@@ -300,7 +299,6 @@ arrayCreationExpr NEW ID LSQRBRACK RSQRBRACK
primary arrayAccess
primary primaryNoArrayAccess
primaryNoArrayAccess literal
primaryNoArrayAccess THIS
primaryNoArrayAccess LPAREN expr RPAREN
primaryNoArrayAccess classInstanceCreate
primaryNoArrayAccess fieldAccess
......
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment