congo-cc / congo-parser-generator

The CongoCC Parser Generator, the Next Generation of JavaCC 21, which in turn was the next generation of JavaCC
https://discuss.congocc.org/
Other
33 stars 9 forks source link

Make the Arithmetic example more newbie-friendly #181

Closed alirazeen closed 4 months ago

alirazeen commented 4 months ago

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.

alirazeen commented 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
revusky commented 4 months ago

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.

revusky commented 4 months ago

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.

alirazeen commented 4 months ago

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.

revusky commented 4 months ago

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.