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
e04b6e92
Commit
e04b6e92
authored
2 years ago
by
Dominic Daniel Krämer
Browse files
Options
Downloads
Patches
Plain Diff
parser is now matching to the lexer.java pattern for decimals
parent
2312f0ef
No related branches found
Branches containing commit
No related tags found
1 merge request
!9
Dominicsbranch
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
expressions.txt
+1
-1
1 addition, 1 deletion
expressions.txt
src/Application.java
+4
-4
4 additions, 4 deletions
src/Application.java
src/Lexer.java
+2
-2
2 additions, 2 deletions
src/Lexer.java
src/Parser.java
+52
-57
52 additions, 57 deletions
src/Parser.java
with
59 additions
and
64 deletions
expressions.txt
+
1
−
1
View file @
e04b6e92
40.2+3*x
(5*2-(5^2)+3*(201.01-15))
\ No newline at end of file
\ No newline at end of file
This diff is collapsed.
Click to expand it.
src/Application.java
+
4
−
4
View file @
e04b6e92
...
@@ -12,10 +12,10 @@ public class Application {
...
@@ -12,10 +12,10 @@ public class Application {
List
<
String
>
expressions
=
new
LinkedList
<>();
List
<
String
>
expressions
=
new
LinkedList
<>();
//Reads the file "expressions.txt" and adds its values to a list
//Reads the file "expressions.txt" and adds its values to a list
BufferedReader
reader
;
BufferedReader
reader
;
String
data
p
ath
=
"expressions.txt"
;
String
data
P
ath
=
"expressions.txt"
;
try
{
try
{
System
.
out
.
println
(
"Reading the file...\n"
);
System
.
out
.
println
(
"Reading the file...\n"
);
reader
=
new
BufferedReader
(
new
FileReader
(
data
p
ath
));
reader
=
new
BufferedReader
(
new
FileReader
(
data
P
ath
));
String
line
=
reader
.
readLine
();
String
line
=
reader
.
readLine
();
while
(
line
!=
null
)
{
while
(
line
!=
null
)
{
expressions
.
add
(
line
);
expressions
.
add
(
line
);
...
@@ -39,8 +39,8 @@ public class Application {
...
@@ -39,8 +39,8 @@ public class Application {
}
}
Parser
parser
=
new
Parser
();
Parser
parser
=
new
Parser
();
try
{
try
{
parser
.
parse
(
tokens
);
Parser
.
Expression
exp
=
parser
.
parse
(
tokens
);
System
.
out
.
println
();
System
.
out
.
println
(
exp
.
toString
()
);
}
}
catch
(
ParserException
e
)
{
catch
(
ParserException
e
)
{
System
.
out
.
println
(
e
.
getMessage
());
System
.
out
.
println
(
e
.
getMessage
());
...
...
This diff is collapsed.
Click to expand it.
src/Lexer.java
+
2
−
2
View file @
e04b6e92
...
@@ -43,14 +43,14 @@ public class Lexer {
...
@@ -43,14 +43,14 @@ public class Lexer {
//checks if a character is a digit, then creates a token with one or multiple numbers
//checks if a character is a digit, then creates a token with one or multiple numbers
//with TokenType=NUMBER
//with TokenType=NUMBER
if
(
Character
.
isDigit
(
current
))
{
if
(
Character
.
isDigit
(
current
))
{
int
endIndex
=
index
+
1
;
int
endIndex
=
index
;
if
(
endIndex
<
input
.
length
())
{
if
(
endIndex
<
input
.
length
())
{
while
(
Character
.
isDigit
(
input
.
charAt
(
endIndex
))
&&
endIndex
<
input
.
length
())
{
while
(
Character
.
isDigit
(
input
.
charAt
(
endIndex
))
&&
endIndex
<
input
.
length
())
{
endIndex
+=
1
;
endIndex
+=
1
;
if
(
endIndex
==
input
.
length
())
{
if
(
endIndex
==
input
.
length
())
{
break
;
break
;
}
}
if
(
Character
.
compare
(
input
.
charAt
(
endIndex
)
,
'.'
)==
0
)
{
if
(
input
.
charAt
(
endIndex
)
==
'.'
)
{
endIndex
+=
1
;
endIndex
+=
1
;
}
}
}
}
...
...
This diff is collapsed.
Click to expand it.
src/Parser.java
+
52
−
57
View file @
e04b6e92
...
@@ -3,10 +3,10 @@ import java.util.List;
...
@@ -3,10 +3,10 @@ import java.util.List;
public
class
Parser
{
public
class
Parser
{
p
rivate
abstract
class
Expression
{
p
ublic
static
class
Expression
{
boolean
capsuled
;
@Override
public
Expression
(
boolean
capsuled
)
{
public
String
toString
(
)
{
this
.
capsuled
=
capsuled
;
return
""
;
}
}
}
}
...
@@ -14,45 +14,60 @@ public class Parser {
...
@@ -14,45 +14,60 @@ public class Parser {
Expression
leftExpression
;
Expression
leftExpression
;
String
operator
;
String
operator
;
Expression
rightExpression
;
Expression
rightExpression
;
public
BinaryOperation
(
Expression
leftExpression
,
String
operator
,
Expression
rightExpression
,
b
oolean
capsuled
)
{
B
oolean
capsuled
;
super
(
capsuled
)
;
public
BinaryOperation
(
Expression
leftExpression
,
String
operator
,
Expression
rightExpression
,
Boolean
capsuled
)
{
this
.
leftExpression
=
leftExpression
;
this
.
leftExpression
=
leftExpression
;
this
.
operator
=
operator
;
this
.
operator
=
operator
;
this
.
rightExpression
=
rightExpression
;
this
.
rightExpression
=
rightExpression
;
this
.
capsuled
=
capsuled
;
}
@Override
public
String
toString
()
{
if
(
capsuled
)
{
return
"("
+
leftExpression
.
toString
()
+
operator
+
rightExpression
.
toString
()
+
")"
;
}
return
leftExpression
.
toString
()
+
operator
+
rightExpression
.
toString
();
}
}
}
}
private
class
Variable
extends
Expression
{
private
class
Variable
extends
Expression
{
String
variableName
;
String
variableName
;
public
Variable
(
String
i
,
Boolean
capsuled
)
{
public
Variable
(
String
i
,
Boolean
capsuled
)
{
super
(
capsuled
);
this
.
variableName
=
i
;
this
.
variableName
=
i
;
}
}
@Override
public
String
toString
()
{
return
variableName
;
}
}
}
private
abstract
class
Value
extends
Expression
{
private
abstract
class
Value
extends
Expression
{
public
Value
(
boolean
capsuled
)
{
super
(
capsuled
);
}
}
}
private
class
Number
extends
Value
{
private
class
Number
extends
Value
{
public
String
digits
;
public
String
digits
;
public
Number
(
String
i
,
boolean
capsuled
)
{
public
Number
(
String
i
)
{
super
(
capsuled
);
this
.
digits
=
i
;
this
.
digits
=
i
;
}
}
@Override
public
String
toString
()
{
return
digits
;
}
}
}
private
class
Decimal
extends
Value
{
private
class
Decimal
extends
Value
{
public
Number
beforeDot
;
public
Number
beforeDot
;
public
Number
afterDot
;
public
Number
afterDot
;
public
Decimal
(
Number
i1
,
Number
i2
,
boolean
capsuled
)
{
public
Decimal
(
Number
i1
,
Number
i2
)
{
super
(
capsuled
);
this
.
beforeDot
=
i1
;
this
.
beforeDot
=
i1
;
this
.
afterDot
=
i2
;
this
.
afterDot
=
i2
;
}
}
@Override
public
String
toString
()
{
return
beforeDot
.
toString
()
+
"."
+
afterDot
.
toString
();
}
}
}
//starting method of the parser
//starting method of the parser
...
@@ -193,64 +208,55 @@ public class Parser {
...
@@ -193,64 +208,55 @@ public class Parser {
if
(
ts
.
get
(
0
).
getType
()
!=
Lexer
.
TokenType
.
NUMBER
)
{
if
(
ts
.
get
(
0
).
getType
()
!=
Lexer
.
TokenType
.
NUMBER
)
{
return
null
;
return
null
;
}
}
String
data
=
ts
.
get
(
0
).
getData
();
String
data
=
ts
.
remove
(
0
).
getData
();
if
(
data
.
isEmpty
())
{
if
(
data
.
isEmpty
())
{
throw
new
ParserException
(
"SyntaxError: empty token"
);
throw
new
ParserException
(
"SyntaxError: empty token"
);
}
}
Value
value
=
null
;
if
(
data
.
contains
(
"."
))
{
if
(
ts
.
size
()>
1
)
{
return
parseDecimal
(
data
);
//if the next token is of TokenType.SPECIAL, check if it's a comma
//if it is, create a decimal
if
(
ts
.
get
(
1
).
getType
()
==
Lexer
.
TokenType
.
SPECIAL
)
{
if
(
parseComma
(
ts
))
{
value
=
parseDecimal
(
ts
);
}
}
}
if
(
value
==
null
)
{
value
=
parseNumber
(
ts
.
remove
(
0
).
getData
());
}
}
return
value
;
return
parseNumber
(
data
,
false
)
;
}
}
//called by method parseValue(...)
//called by method parseValue(...)
//parses a decimal of a list of tokens & a string
//parses a decimal of a list of tokens & a string
private
Decimal
parseDecimal
(
List
<
Lexer
.
Token
>
ts
)
throws
ParserException
{
private
Decimal
parseDecimal
(
String
data
)
throws
ParserException
{
String
data
=
ts
.
remove
(
0
).
getData
();
if
(
data
.
isEmpty
())
{
if
(
data
.
isEmpty
())
{
throw
new
ParserException
(
"SyntaxError: empty data"
);
throw
new
ParserException
(
"SyntaxError: empty data"
);
}
}
if
(
ts
.
size
()==
0
)
{
int
dot
=
data
.
indexOf
(
'.'
);
throw
new
ParserException
(
"SyntaxError: no tokens left to create Decimal"
);
String
number1
=
data
.
substring
(
0
,
dot
);
}
String
number2
=
data
.
substring
(
dot
+
1
);
Number
beforeDot
=
(
Number
)
parseNumber
(
data
);
Number
beforeDot
=
(
Number
)
parseNumber
(
number1
,
true
);
ts
.
remove
(
0
);
Number
afterDot
=
(
Number
)
parseNumber
(
number2
,
false
);
data
=
ts
.
remove
(
0
).
getData
();
return
new
Decimal
(
beforeDot
,
afterDot
);
Number
afterDot
=
(
Number
)
parseNumber
(
data
);
return
new
Decimal
(
beforeDot
,
afterDot
,
false
);
}
}
//called by method parseValue(...)
//called by method parseValue(...)
//parses a String into a number
//parses a String into a number
private
Number
parseNumber
(
String
data
)
throws
ParserException
{
private
Number
parseNumber
(
String
data
,
boolean
afterDot
)
throws
ParserException
{
if
(
data
.
isEmpty
())
{
if
(
data
.
isEmpty
())
{
throw
new
ParserException
(
"RuntimeException: empty token"
);
throw
new
ParserException
(
"RuntimeException: empty token"
);
}
}
if
(
data
.
startsWith
(
"0"
)
&&
data
.
length
()
==
1
)
{
if
(
data
.
startsWith
(
"0"
)
&&
data
.
length
()
==
1
)
{
return
new
Number
(
data
,
false
);
return
new
Number
(
data
);
}
}
if
(
afterDot
)
{
parseDigitWithoutZero
(
data
.
substring
(
0
,
1
));
parseDigitWithoutZero
(
data
.
substring
(
0
,
1
));
parseDigit
(
data
.
substring
(
1
));
parseDigit
(
data
.
substring
(
1
));
return
new
Number
(
data
,
false
);
}
else
{
parseDigit
(
data
);
}
return
new
Number
(
data
);
}
}
//called by method parseNumber(...)
//called by method parseNumber(...)
//checks if a String only contains numbers(including zero) with parseCharacter(...)
//checks if a String only contains numbers(including zero) with parseCharacter(...)
private
void
parseDigit
(
String
data
)
throws
ParserException
{
private
void
parseDigit
(
String
data
)
throws
ParserException
{
for
(
int
index
=
1
;
index
<
data
.
length
();
index
++)
{
for
(
int
index
=
0
;
index
<
data
.
length
();
index
++)
{
String
character
=
Character
.
toString
(
data
.
charAt
(
index
));
String
character
=
Character
.
toString
(
data
.
charAt
(
index
));
if
(!
parseCharacter
(
character
,
"0123456789"
))
{
if
(!
parseCharacter
(
character
,
"0123456789"
))
{
throw
new
ParserException
(
"SyntaxError: unexpected character: "
+
character
);
throw
new
ParserException
(
"SyntaxError: unexpected character: "
+
character
);
...
@@ -261,7 +267,7 @@ public class Parser {
...
@@ -261,7 +267,7 @@ public class Parser {
//called by method parseNumber(...)
//called by method parseNumber(...)
//checks if a String only contains numbers(excluding zero) with parseCharacter(...)
//checks if a String only contains numbers(excluding zero) with parseCharacter(...)
private
void
parseDigitWithoutZero
(
String
data
)
throws
ParserException
{
private
void
parseDigitWithoutZero
(
String
data
)
throws
ParserException
{
for
(
int
index
=
1
;
index
<
data
.
length
();
index
++)
{
for
(
int
index
=
0
;
index
<
data
.
length
();
index
++)
{
String
character
=
Character
.
toString
(
data
.
charAt
(
index
));
String
character
=
Character
.
toString
(
data
.
charAt
(
index
));
if
(!
parseCharacter
(
character
,
"123456789"
))
{
if
(!
parseCharacter
(
character
,
"123456789"
))
{
throw
new
ParserException
(
"SyntaxError: unexpected character: "
+
character
);
throw
new
ParserException
(
"SyntaxError: unexpected character: "
+
character
);
...
@@ -269,17 +275,6 @@ public class Parser {
...
@@ -269,17 +275,6 @@ public class Parser {
}
}
}
}
private
boolean
parseComma
(
List
<
Lexer
.
Token
>
ts
)
throws
ParserException
{
String
data
=
ts
.
get
(
1
).
getData
();
if
(
parseCharacter
(
data
,
"."
))
{
if
(
ts
.
get
(
2
).
getType
()!=
Lexer
.
TokenType
.
NUMBER
)
{
throw
new
ParserException
(
"SyntaxError: no number after comma"
);
}
return
true
;
}
return
false
;
}
//called by methods parseOperator(...), parseDigit(...), parseDigitWithoutZero(...)
//called by methods parseOperator(...), parseDigit(...), parseDigitWithoutZero(...)
//checks if a certain string can be found in a string of allowed character
//checks if a certain string can be found in a string of allowed character
private
boolean
parseCharacter
(
String
data
,
String
allowedCharacters
)
throws
ParserException
{
private
boolean
parseCharacter
(
String
data
,
String
allowedCharacters
)
throws
ParserException
{
...
...
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