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
42f8c59a
Commit
42f8c59a
authored
2 years ago
by
Dominic Daniel Krämer
Browse files
Options
Downloads
Patches
Plain Diff
implemented lexter in Lexter.java and fitting tests in Application.java
parent
9116a7b1
No related branches found
No related tags found
1 merge request
!2
Dominicsbranch
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/Application.java
+12
-1
12 additions, 1 deletion
src/Application.java
src/Lexer.java
+67
-0
67 additions, 0 deletions
src/Lexer.java
with
79 additions
and
1 deletion
src/Application.java
+
12
−
1
View file @
42f8c59a
public
class
Application
{
import
java.util.Arrays
;
import
java.util.List
;
public
class
Application
{
public
static
void
main
(
String
[]
args
)
{
List
<
String
>
test
=
Arrays
.
asList
(
"1 + x^2 - (31 * x)"
);
for
(
String
value:
test
)
{
List
<
Lexer
.
Token
>
tokens
=
Lexer
.
lex
(
value
);
for
(
Lexer
.
Token
singleToken:
tokens
)
{
System
.
out
.
println
(
singleToken
.
toString
());
}
}
}
}
This diff is collapsed.
Click to expand it.
src/Lexer.java
+
67
−
0
View file @
42f8c59a
import
java.util.LinkedList
;
import
java.util.List
;
public
class
Lexer
{
public
static
enum
TokenType
{
NUMBER
,
VARIABLE
,
SPECIAL
}
public
static
class
Token
{
protected
TokenType
type
;
protected
String
data
;
public
Token
(
TokenType
type
,
String
data
)
{
this
.
type
=
type
;
this
.
data
=
data
;
}
public
TokenType
getType
()
{
return
type
;
}
public
String
getData
()
{
return
data
;
}
@Override
public
String
toString
()
{
return
String
.
format
(
"type: %s, data: %s"
,
type
,
data
);
}
}
public
static
List
<
Token
>
lex
(
String
input
)
{
List
<
Token
>
result
=
new
LinkedList
<
Token
>();
for
(
int
index
=
0
;
index
<
input
.
length
();)
{
char
current
=
input
.
charAt
(
index
);
if
(
Character
.
isDigit
(
current
))
{
int
endIndex
=
index
+
1
;
if
(
endIndex
<
input
.
length
())
{
while
(
Character
.
isDigit
(
input
.
charAt
(
endIndex
))
&&
endIndex
<
input
.
length
())
{
endIndex
+=
1
;
}
}
Token
token
=
new
Token
(
TokenType
.
NUMBER
,
input
.
substring
(
index
,
endIndex
));
result
.
add
(
token
);
index
+=
endIndex
-
index
;
}
else
if
(
Character
.
isWhitespace
(
current
))
{
index
++;
}
else
if
(
Character
.
isLetter
(
current
)){
int
endIndex
=
index
+
1
;
if
(
endIndex
<
input
.
length
())
{
while
(
Character
.
isLetter
(
input
.
charAt
(
endIndex
))
&&
endIndex
<
input
.
length
())
{
endIndex
+=
1
;
}
}
Token
token
=
new
Token
(
TokenType
.
VARIABLE
,
input
.
substring
(
index
,
endIndex
));
result
.
add
(
token
);
index
+=
endIndex
-
index
;
}
else
{
Token
token
=
new
Token
(
TokenType
.
SPECIAL
,
String
.
valueOf
(
current
));
result
.
add
(
token
);
index
++;
}
}
return
result
;
}
}
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