Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
Abgabe4Java
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
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
Code review analytics
Issue analytics
Insights
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
Dominic Daniel Krämer
Abgabe4Java
Commits
b7b368c6
Commit
b7b368c6
authored
2 years ago
by
Dominic Daniel Krämer
Browse files
Options
Downloads
Patches
Plain Diff
added comments on the methods & classes
parent
f914c527
No related branches found
Branches containing commit
No related tags found
1 merge request
!3
added comments on the methods & classes
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/Application.java
+4
-0
4 additions, 0 deletions
src/Application.java
src/Lexer.java
+17
-3
17 additions, 3 deletions
src/Lexer.java
with
21 additions
and
3 deletions
src/Application.java
+
4
−
0
View file @
b7b368c6
...
@@ -3,9 +3,13 @@ import java.util.List;
...
@@ -3,9 +3,13 @@ import java.util.List;
public
class
Application
{
public
class
Application
{
public
static
void
main
(
String
[]
args
)
{
public
static
void
main
(
String
[]
args
)
{
//List with a fitting String to be parsed into multiple tokens
List
<
String
>
test
=
Arrays
.
asList
(
"1 + xy^2 - (31 * x)y"
);
List
<
String
>
test
=
Arrays
.
asList
(
"1 + xy^2 - (31 * x)y"
);
//Iterates through every String
for
(
String
value:
test
)
{
for
(
String
value:
test
)
{
//creates a list of tokens out of the String
List
<
Lexer
.
Token
>
tokens
=
Lexer
.
lex
(
value
);
List
<
Lexer
.
Token
>
tokens
=
Lexer
.
lex
(
value
);
//prints each token out (with .toString())
for
(
Lexer
.
Token
singleToken:
tokens
)
{
for
(
Lexer
.
Token
singleToken:
tokens
)
{
System
.
out
.
println
(
singleToken
.
toString
());
System
.
out
.
println
(
singleToken
.
toString
());
}
}
...
...
This diff is collapsed.
Click to expand it.
src/Lexer.java
+
17
−
3
View file @
b7b368c6
...
@@ -2,10 +2,14 @@ import java.util.LinkedList;
...
@@ -2,10 +2,14 @@ import java.util.LinkedList;
import
java.util.List
;
import
java.util.List
;
public
class
Lexer
{
public
class
Lexer
{
//enum TokenType that can either be a number, variable or a special character (like * or / etc.)
public
static
enum
TokenType
{
public
static
enum
TokenType
{
NUMBER
,
VARIABLE
,
SPECIAL
NUMBER
,
VARIABLE
,
SPECIAL
}
}
//tokens with a TokenType that represents their type and a String with the actual data
//has for both values a getter method and overrides toString with it's fitting values
public
static
class
Token
{
public
static
class
Token
{
protected
TokenType
type
;
protected
TokenType
type
;
protected
String
data
;
protected
String
data
;
...
@@ -28,11 +32,16 @@ public class Lexer {
...
@@ -28,11 +32,16 @@ public class Lexer {
}
}
}
}
//creates a list of tokens from a given String
public
static
List
<
Token
>
lex
(
String
input
)
{
public
static
List
<
Token
>
lex
(
String
input
)
{
//the list that will be returned at the end
List
<
Token
>
result
=
new
LinkedList
<
Token
>();
List
<
Token
>
result
=
new
LinkedList
<
Token
>();
//for-loop that iterates through each character of the String
for
(
int
index
=
0
;
index
<
input
.
length
();)
{
for
(
int
index
=
0
;
index
<
input
.
length
();)
{
char
current
=
input
.
charAt
(
index
);
char
current
=
input
.
charAt
(
index
);
//checks if a character is a digit, then creates a token with one or multiple numbers
//with TokenType=NUMBER
if
(
Character
.
isDigit
(
current
))
{
if
(
Character
.
isDigit
(
current
))
{
int
endIndex
=
index
+
1
;
int
endIndex
=
index
+
1
;
if
(
endIndex
<
input
.
length
())
{
if
(
endIndex
<
input
.
length
())
{
...
@@ -44,9 +53,8 @@ public class Lexer {
...
@@ -44,9 +53,8 @@ public class Lexer {
result
.
add
(
token
);
result
.
add
(
token
);
index
+=
endIndex
-
index
;
index
+=
endIndex
-
index
;
}
}
else
if
(
Character
.
isWhitespace
(
current
))
{
//checks if a character is a letter, then creates for each following character a separate token
index
++;
//with TokenType=VARIABLE
}
else
if
(
Character
.
isLetter
(
current
)){
else
if
(
Character
.
isLetter
(
current
)){
int
iterator
=
index
;
int
iterator
=
index
;
while
(
Character
.
isLetter
(
input
.
charAt
(
iterator
))
&&
iterator
<
input
.
length
())
{
while
(
Character
.
isLetter
(
input
.
charAt
(
iterator
))
&&
iterator
<
input
.
length
())
{
...
@@ -59,6 +67,12 @@ public class Lexer {
...
@@ -59,6 +67,12 @@ public class Lexer {
}
}
index
+=
iterator
-
index
;
index
+=
iterator
-
index
;
}
}
//whitespaces are ignored
else
if
(
Character
.
isWhitespace
(
current
))
{
index
++;
}
//every char of the string that isn't a digit, letter or whitespace will be
//used for creating a token with TokenType=SPECIAL
else
{
else
{
Token
token
=
new
Token
(
TokenType
.
SPECIAL
,
String
.
valueOf
(
current
));
Token
token
=
new
Token
(
TokenType
.
SPECIAL
,
String
.
valueOf
(
current
));
result
.
add
(
token
);
result
.
add
(
token
);
...
...
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