Skip to content
Snippets Groups Projects
Commit 0b728a25 authored by Nicholas Robinson's avatar Nicholas Robinson
Browse files

added char, string, bool, null literals

parent 6196c561
No related branches found
No related tags found
No related merge requests found
...@@ -14,10 +14,11 @@ class Token(): ...@@ -14,10 +14,11 @@ class Token():
##################### Joos Tokens Map ############################### ##################### Joos Tokens Map ###############################
# For tokens that are recognized as another name in the maximal munch scanner # For tokens that are recognized as another name in the maximal munch scanner
# e.g. all keywords are scanned as ID first # e.g. all keywords are scanned as ID first, as well as boolean literals, and null
# Key: lexeme, Value: name, use lexeme to reassign those tokens correct names # Key: lexeme, Value: name, use lexeme to reassign those tokens correct names
keywordDict = dict({ idToTokenDict = dict({
# Keywords
'abstract': 'ABSTRACT', 'abstract': 'ABSTRACT',
'boolean': 'BOOLEAN', 'boolean': 'BOOLEAN',
'byte': 'BYTE', 'byte': 'BYTE',
...@@ -42,20 +43,25 @@ keywordDict = dict({ ...@@ -42,20 +43,25 @@ keywordDict = dict({
'String': 'STRING', 'String': 'STRING',
'this': 'THIS', 'this': 'THIS',
'void': 'VOID', 'void': 'VOID',
'while': 'WHILE' 'while': 'WHILE',
# Literals
'true': 'LITERALBOOL',
'false': 'LITERALBOOL',
'null': 'NULL'
}) })
def tokenToKeywords(tokens): def idsToTokens(tokens):
for t in tokens: for t in tokens:
if t.name == 'ID': if t.name == 'ID':
if t.lex in keywordDict: if t.lex in idToTokenDict:
t.name = keywordDict.get(t.lex) t.name = idToTokenDict.get(t.lex)
######################## DFA Stuff ################################### ######################## DFA Stuff ###################################
################# Joos DFA Tokens ################################### ################# Joos DFA Tokens ###################################
JoosDFATokens = set([ JoosDFATokens = set([
# Identifiers or Keywords (keywords are mapped to their token name in keywordDict above) # Identifiers or Keywords (keywords are mapped to their token name in idToTokenDict above)
'ID', 'ID',
# Literals and names (note: 'this' is considered as a keyword) # Literals and names (note: 'this' is considered as a keyword)
...@@ -115,8 +121,6 @@ def JoosTransition(input, state): ...@@ -115,8 +121,6 @@ def JoosTransition(input, state):
return 'WHITESPACE' return 'WHITESPACE'
else: else:
return None return None
elif (state != 'START' and input in (' ', '\n')):
return None
elif (state == 'START'): elif (state == 'START'):
if (input.isalpha()): if (input.isalpha()):
...@@ -183,13 +187,38 @@ def JoosTransition(input, state): ...@@ -183,13 +187,38 @@ def JoosTransition(input, state):
return 'PERIOD' return 'PERIOD'
if (input == '@'): if (input == '@'):
return 'AT' return 'AT'
# literals
if (input == '\"'):
return 'LSTRING'
if (input == '\''):
return 'LCHAR'
elif (state == 'ID'): elif (state == 'ID'):
if (input.isalpha() or input.isdigit()): if (input.isalpha() or input.isdigit()):
return 'ID' return 'ID'
return None return None
elif (state == 'LSTRING'):
if (input == '\\'):
return 'STRINGESC'
if (input == '\"'):
return 'LITERALSTRING'
return 'LSTRING'
elif (state == 'STRINGESC'):
return 'LSTRING'
elif (state == 'LCHAR'):
if (input == '\\'):
return 'CHARESC'
return 'CHAREND'
elif (state == 'CHARESC'):
return 'CHAREND'
elif (state == 'CHAREND'):
if (input == '\''):
return 'LITERALCHAR'
return None
else: else:
return None return None
...@@ -247,7 +276,7 @@ def scan(input): ...@@ -247,7 +276,7 @@ def scan(input):
if (tokens): if (tokens):
# TODO: handle edge cases (e.g. check int range) # TODO: handle edge cases (e.g. check int range)
tokenToKeywords(tokens) idsToTokens(tokens)
# remove whitespace and comments # remove whitespace and comments
tokens = filter(lambda t: t.name not in ("WHITESPACE", "COMMENT"), tokens) tokens = filter(lambda t: t.name not in ("WHITESPACE", "COMMENT"), tokens)
......
public class Literals {
public static void main(String[] args) {
char i = 'h';
char x = '\'';
String s = "hello";
String s2 = "hello \' and \"";
boolean b = true;
boolean a = false;
int x = null;
}
}
\ No newline at end of file
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