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
f4eda906
Commit
f4eda906
authored
2 years ago
by
Dominic Daniel Krämer
Browse files
Options
Downloads
Patches
Plain Diff
implemented ASTPrinter to test the visitor pattern
parent
9a8c6438
No related branches found
Branches containing commit
No related tags found
1 merge request
!10
Dominicsbranch
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
expressions.txt
+2
-3
2 additions, 3 deletions
expressions.txt
src/ASTPrinter.java
+38
-0
38 additions, 0 deletions
src/ASTPrinter.java
src/Application.java
+2
-1
2 additions, 1 deletion
src/Application.java
with
42 additions
and
4 deletions
expressions.txt
+
2
−
3
View file @
f4eda906
1 + xy^2 - (31 * x)y
1 + x
*
y^2 - (31 * x)
-
y
1 + x^2 - (3 * x)
1 + x^2 - (3 * x)
2x + 0.5x^2 - 1
2-x + 0.5*x^2 - 1
((5+2)-8)-8+12
(5*2-(5^2)+3*(201.01-15))
(5*2-(5^2)+3*(201.01-15))
\ No newline at end of file
This diff is collapsed.
Click to expand it.
src/ASTPrinter.java
0 → 100644
+
38
−
0
View file @
f4eda906
public
class
ASTPrinter
implements
Visitor
<
String
>{
@Override
public
String
visit
(
final
Parser
.
BinaryOperation
binaryOP
)
{
StringBuffer
sb
=
new
StringBuffer
();
if
(
binaryOP
.
capsuled
)
{
sb
.
append
(
"("
);
}
sb
.
append
(
binaryOP
.
leftExpression
.
accept
(
this
));
sb
.
append
(
binaryOP
.
operator
);
sb
.
append
(
binaryOP
.
rightExpression
.
accept
(
this
));
if
(
binaryOP
.
capsuled
)
{
sb
.
append
(
")"
);
}
return
sb
.
toString
();
}
@Override
public
String
visit
(
Parser
.
Variable
variable
)
{
return
variable
.
variableName
;
}
@Override
public
String
visit
(
Parser
.
Number
number
)
{
return
number
.
digits
;
}
@Override
public
String
visit
(
Parser
.
Decimal
decimal
)
{
return
decimal
.
beforeDot
.
digits
+
"."
+
decimal
.
afterDot
.
digits
;
}
public
String
visit
(
final
Parser
.
Expression
ast
)
{
if
(
ast
==
null
)
{
throw
new
RuntimeException
(
"AST is null"
);
}
return
ast
.
accept
(
this
);
}
}
This diff is collapsed.
Click to expand it.
src/Application.java
+
2
−
1
View file @
f4eda906
...
@@ -40,7 +40,8 @@ public class Application {
...
@@ -40,7 +40,8 @@ public class Application {
Parser
parser
=
new
Parser
();
Parser
parser
=
new
Parser
();
try
{
try
{
Parser
.
Expression
exp
=
parser
.
parse
(
tokens
);
Parser
.
Expression
exp
=
parser
.
parse
(
tokens
);
System
.
out
.
println
(
exp
.
toString
());
ASTPrinter
printer
=
new
ASTPrinter
();
System
.
out
.
println
(
"Printing: '"
+
printer
.
visit
(
exp
)
+
"'"
);
}
}
catch
(
ParserException
e
)
{
catch
(
ParserException
e
)
{
System
.
out
.
println
(
e
.
getMessage
());
System
.
out
.
println
(
e
.
getMessage
());
...
...
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