Closed alirazeen closed 4 months ago
I think the arithmetic example is a great way to introduce people to CongoCC if they don't already have all the relevant experience. I'm proposing these changes so that in the future, when writing a tutorial, we already have the base pieces done. Please let me know if I should change/modify something.
Some testing output:
$ ant test
Buildfile: /Users/al/projects/congo-parser-generator/examples/arithmetic/build.xml
init:
parser-gen:
compile:
[javac] Compiling 20 source files
[javac] Compiling 20 source files
test:
[java] Parsing arithmetic expression from the command line
[java] Dumping the AST...
[java] <Root (1, 1)-(1, 3)>
[java] <AdditiveExpression (1, 1)-(1, 3)>
[java] 1
[java] +
[java] 1
[java] EOF
[java] The result is: 2.0
BUILD SUCCESSFUL
Total time: 4 seconds
Passing in an arithmetic expression from the command line:
$ java ex2.Calc 1+3
Parsing arithmetic expression from the command line
Dumping the AST...
<Root (1, 1)-(1, 3)>
<AdditiveExpression (1, 1)-(1, 3)>
1
+
3
EOF
The result is: 4.0
Running the program without passing in arguments
$ java ex2.Calc
Enter an arithmetic expression:
1+3
Dumping the AST...
<Root (1, 1)-(1, 3)>
<AdditiveExpression (1, 1)-(1, 3)>
1
+
3
EOF
The result is: 4.0
It's a good idea certainly and always was in the back of my mind to build a tutorial using this as an example. BTW, I decided to rewrite the arithmetic example somewhat differently, using a tad more recursion. Do you think this is a better style? (See my last commit.) Practically speaking, it's about the same, but this is more pedagogical, I think.
Actually, what I just did is wrong! I guess it has to be written the way it was before because we don't have left recursion.
Gah, I think I also made a mistake with the build.xml file. My bad. I can't have multiple uptodate properties with the same name it appears. It breaks things. I'll send a PR.
I reverted my previous changes. Sorry about that. It's quite late here and I was suffering from insomnia. Of course, I was second-guessing the way I had done it before, and now I realize (after having forgotten) why it had to be written the way it was! You see, otherwise something like 1/2/3 would evaluate to 1.5 instead of 0.6666667 or whatever.
This commit adds a build.xml so that it is easy to build by ant and updates the example so that it can parse arithmetic expressions passed in as an argument. The overall intention is to bring this example to a state where it can be used in a CongoCC tutorial.