uchicago-cs / chiventure

A text adventure game engine developed in UChicago's CMSC 22000 - Introduction to Software Development
40 stars 13 forks source link

Research Bison library #824

Closed jacknugent1529 closed 3 years ago

jacknugent1529 commented 3 years ago

We plan on researching the bison library (https://www.gnu.org/software/bison/) and examining its usefulness for the CLI++ or DSL features. We aim to create a pros and cons list and some sample code using this library.

Assignees: John and Jack team: CLI++/DSL

john-hahn commented 3 years ago

After reading through the Bison library documentation provided on their site, I came up with a few pros and cons.

Pros

The following is an example of a case where GLR can be run while LR fails to run:

type subrange = (a) .. b;
type enum = (a);

In this case, both lines of code are seemingly identical until it encounters the '..' token. LR's one-token lookahead would not be able to determine what form '(a)' should be parsed. GLR, on the other hand, would split the case into two branches, testing out each possible case, and merging in the end with the intended result.

Cons

An example of the grammar they support would be:

sum : NUM '+' NUM        { $$ = std::stoi($1) + std::stoi($2);      }

As you can see, the manipulation is within the brackets for this rule as compared to an example of a rule from ANTLR:

sum : NUM PLUS NUM ;

Sources: https://www.gnu.org/software/bison/manual/bison.pdf https://www.antlr.org/papers/allstar-techreport.pdf

jacknugent1529 commented 3 years ago

I looked into using bison to write parsers in C. Since bison is just a parser and does not include a lexer, I used flex to create a lexer. I followed an example for a basic calculator with 4 operations, and then expanded on the example to create a scientific calculator with additional functions and parentheses (replit).

Pros:

Cons:

Additional Notes: Bison is a parser generator, not a parser combinator. This is not necessarily an advantage or disadvantage, but allows for slightly less flexibility in return for greater speed.

ecoble commented 3 years ago

Issue Score: ✔️++

Comments: Great job on this really thorough research! In the future, it would be better to keep this kind of research documentation in a Wiki page for your team, so it is easier to reference in the future.