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
2533b452
Commit
2533b452
authored
2 years ago
by
Dominic Daniel Krämer
Browse files
Options
Downloads
Patches
Plain Diff
advanced the parser.java (added basics for the BinaryOperation)
parent
fd8638be
No related branches found
No related tags found
1 merge request
!7
Dominicsbranch
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/Parser.java
+91
-9
91 additions, 9 deletions
src/Parser.java
with
91 additions
and
9 deletions
src/Parser.java
+
91
−
9
View file @
2533b452
...
...
@@ -4,11 +4,25 @@ import java.util.Optional;
public
class
Parser
{
private
abstract
interface
Expression
{
private
abstract
class
Expression
{
}
private
abstract
class
Value
implements
Expression
{
private
class
BinaryOperation
extends
Expression
{
Expression
leftExpression
;
char
operator
;
Expression
rightExpression
;
}
private
class
Variable
extends
Expression
{
String
variableName
;
public
Variable
(
String
i
)
{
this
.
variableName
=
i
;
}
}
private
abstract
class
Value
extends
Expression
{
}
...
...
@@ -21,12 +35,10 @@ public class Parser {
private
class
Decimal
extends
Value
{
public
Number
beforeDot
;
public
char
dot
;
public
Number
afterDot
;
public
Decimal
(
Number
i1
,
Number
i2
)
{
this
.
beforeDot
=
i1
;
dot
=
'.'
;
this
.
afterDot
=
i2
;
}
}
...
...
@@ -55,15 +67,17 @@ public class Parser {
throw
new
ParserException
(
"empty token list"
);
}
return
parseValue
(
ts
).
orElseGet
(()
->
null
);
return
null
;
//return parseValue(ts).orElseGet(() -> parseVariable(ts).orElseGet(() -> null));
}
//checks if a String only contains an allowed operator with parseCharacter(...)
private
void
parseOperator
(
String
operator
)
throws
ParserException
{
private
Boolean
parseOperator
(
String
operator
)
throws
ParserException
{
if
(
operator
.
length
()>
1
)
{
throw
new
ParserException
(
"RuntimeException: invalid length for an operator: "
+
operator
);
}
parseCharacter
(
operator
,
"+-*/^"
);
return
true
;
}
//called by methods parseOperator(...), parseDigit(...), parseDigitWithoutZero(...)
...
...
@@ -80,9 +94,77 @@ public class Parser {
return
true
;
}
private
Optional
<
BinaryOperation
>
parseBinaryOperation
(
List
<
Lexer
.
Token
>
ts
)
throws
ParserException
{
if
(
ts
.
get
(
0
).
getType
()!=
Lexer
.
TokenType
.
SPECIAL
)
{
throw
new
ParserException
(
"SyntaxError: expected bracket, got "
+
ts
.
get
(
0
).
getData
()
+
" instead"
);
}
String
lbracket
=
ts
.
remove
(
0
).
getData
();
parseCharacter
(
lbracket
,
"("
);
int
i
;
boolean
found
=
false
;
//check for bracket
/*
for(i=0; i<ts.size(); i++) {
if(ts.get(0).getType() == Lexer.TokenType.SPECIAL) {
int rightBrackets = 0;
int leftBrackets = 0;
if(parseCharacter(ts.get(0).getData(),"(")) {
leftBrackets += 1;
}
else if(parseCharacter(ts.get(0).getData(),")")) {
rightBrackets += 1;
if(rightBrackets>leftBrackets) {
found = true;
break;
}
}
}
}
if(!found) {
throw new ParserException("SyntaxError: no matching bracket was found");
}
*/
List
<
Lexer
.
Token
>
leftList
=
new
LinkedList
<>();
//check for operator
for
(
i
=
0
;
i
<
ts
.
size
();
i
++)
{
leftList
.
add
(
ts
.
remove
(
i
));
if
(
parseOperator
(
ts
.
get
(
i
).
getData
()))
{
break
;
}
}
ts
.
remove
(
0
);
if
(
ts
.
size
()==
0
)
{
throw
new
ParserException
(
"SyntaxError: could not find a matching operator"
);
}
Expression
leftExpression
=
parseExpression
(
leftList
);
Expression
rightExpression
=
parseExpression
(
ts
);
return
null
;
}
private
Optional
<
Variable
>
parseVariable
(
List
<
Lexer
.
Token
>
ts
)
throws
ParserException
{
if
(
ts
.
isEmpty
())
{
throw
new
ParserException
(
"RuntimeException: empty token list"
);
}
if
(
ts
.
get
(
0
).
getType
()
!=
Lexer
.
TokenType
.
VARIABLE
)
{
return
Optional
.
empty
();
}
String
data
=
ts
.
remove
(
0
).
getData
();
if
(
data
.
isEmpty
())
{
throw
new
ParserException
(
"RuntimException: empty token"
);
}
parseCharacter
(
data
,
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
);
return
Optional
.
of
(
new
Variable
(
data
));
}
//called by method parseException(...)
//parses a list of tokens into a value
private
Optional
<
Expression
>
parseValue
(
List
<
Lexer
.
Token
>
ts
)
throws
ParserException
{
private
Optional
<
Value
>
parseValue
(
List
<
Lexer
.
Token
>
ts
)
throws
ParserException
{
if
(
ts
.
isEmpty
())
{
throw
new
ParserException
(
"RuntimeException: empty token list"
);
}
...
...
@@ -112,7 +194,7 @@ public class Parser {
//called by method parseValue(...)
//parses a decimal of a list of tokens & a string
private
Expression
parseDecimal
(
List
<
Lexer
.
Token
>
ts
,
String
data
)
throws
ParserException
{
private
Decimal
parseDecimal
(
List
<
Lexer
.
Token
>
ts
,
String
data
)
throws
ParserException
{
if
(
ts
.
size
()<
1
)
{
throw
new
ParserException
(
"RuntimeException: empty token list"
);
}
...
...
@@ -131,7 +213,7 @@ public class Parser {
//called by method parseValue(...)
//parses a String into a number
private
Expression
parseNumber
(
String
data
)
throws
ParserException
{
private
Number
parseNumber
(
String
data
)
throws
ParserException
{
if
(
data
.
isEmpty
())
{
throw
new
ParserException
(
"RuntimeException: empty 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