Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
cs444
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Xun Yang
cs444
Commits
0b728a25
Commit
0b728a25
authored
5 years ago
by
Nicholas Robinson
Browse files
Options
Downloads
Patches
Plain Diff
added char, string, bool, null literals
parent
6196c561
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
Scanning.py
+40
-11
40 additions, 11 deletions
Scanning.py
Tests/literals.joos
+11
-0
11 additions, 0 deletions
Tests/literals.joos
with
51 additions
and
11 deletions
Scanning.py
+
40
−
11
View file @
0b728a25
...
@@ -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
tokenToKeyword
s
(
tokens
):
def
idsToToken
s
(
tokens
):
for
t
in
tokens
:
for
t
in
tokens
:
if
t
.
name
==
'
ID
'
:
if
t
.
name
==
'
ID
'
:
if
t
.
lex
in
keyword
Dict
:
if
t
.
lex
in
idToToken
Dict
:
t
.
name
=
keyword
Dict
.
get
(
t
.
lex
)
t
.
name
=
idToToken
Dict
.
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
keyword
Dict above)
# Identifiers or Keywords (keywords are mapped to their token name in
idToToken
Dict 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)
tokenToKeyword
s
(
tokens
)
idsToToken
s
(
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
)
...
...
This diff is collapsed.
Click to expand it.
Tests/literals.joos
0 → 100644
+
11
−
0
View file @
0b728a25
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
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment