Lazy Dungeon Master — Chapter 56

Big homework assignment due on Wednesday, decided to knock this small chapter out before starting on it today. Hope you guys enjoy.

 

(~’.’)~ Read Chapter Here ~(‘.’~)


If anyone has experience in creating a generic parser that’s able to read and evaluate something like [ A = (D * C) / A – B + D] in C++, or knows where to find a decent tutorial for one somewhere on the internet, I’d be grateful for the help. The previous assignment from my teacher had me create a generic List, Stack, and Queue from scratch and now we have to use that same assignment’s classes for this one.

Well, the faster I can get it done the more chapters I can release this week!

*stares at Visual Studio with dead eyes*

Meanwhile, in my mind: You should have started on this much sooner~

Shhh, me!  Other homework got in the way of that D:

25 thoughts on “Lazy Dungeon Master — Chapter 56

  1. I took a class on data structures a year ago and the teacher mentioned something about an expression tree or something. (wasn’t part of the actual class just a side note)

      1. prepare yourself tree is fun until you remember the structure is difficult to create binary tree who have function of auto-regulation… and graphs… is worse

  2. When it comes to parsers I learned by using FLEX and BISON compilers, they are based on regular expressions and output c files (not c++ but should be compatible most of the times).
    So if you want a good parser try to combine both flex and bison with visual studio. I kinda forgot most of it by now lol.

    1. I think that concept is beyond the scope of this class, just using data structures here. Not compilers

  3. You can try to use the istringstream object to do that, I’ve done a parser calculator once, and as long as you do the proper if/else checks later, it works quite nicely.

  4. Hmm, if done more professionally, I’d use Flex/Bison as stated before.

    Since you’re using data structures, we’re talking about forming a parse tree out of the expressions; f.ex. [ A = (D * C) / A – B + D] is first translated to tokens “A” EQ LPAR “D” TIMES “C” RPAR …, so basically tokenized.

    The next step is to interpret it as a grammar, and the choice of one is the tricky part. In the worst case scenario you’ll have to implement a LALR(1) or similar, see f.ex. one of the books shown here https://www.cs.princeton.edu/~appel/modern/ for the theoretical basis (formal language maths also helps to understand).

    Example grammar:
    EQUALS = TARGET ‘EQ’ STATEMENT
    TARGET = *a set of letters*
    STATEMENT= A ‘MULT’ B
    STATEMENT = A ‘PROD’ B

    As a tree:
    EQUALS
    A
    STATEMENT
    DIVISION
    MULT
    D
    C
    SUBTR
    A
    B

    Use inheritance here, f.ex. Rule -parent and Statement, Target, etc. children, and put the logic in the parent class.

    Then just traverse the parse tree, performing the tasks in unique order. If the grammar and parse tree is ambiguous (can be interpreted in several ways) due to bad syntax, you’ll need LALR [ https://en.wikipedia.org/wiki/LALR_parser ].

    Something like that? 🙂

    Anyway, I’ll skip code generation, since that seems to be out-of-scope for you.

    Btw. thanks for the good work. 🙂

  5. Try stackoverflow.com, They have all sorts of useful guides and experienced people to help out with all kinds of coding issues.

    I’d write the code myself but my programming never scores higher than a 70% in classes even though it completes the desired task perfectly and without exceptions.

    1. Oh I know of people who did that in my classes. Its usually cause even though their codes work they skipped functions they were supposed to use without explaining why, or messy codes, cause who likes commenting right?
      And if your lecturer was a sadist like mine, your spelling and grammar in the report that comes with your codes affects your score…

      Also thanks for the chapter~ Cause thanks

      1. He’s actually find with reusing code we find online. We just better know how it works and give credit. His whole thing is “the reusability of code”.

        He’s sadistic when it comes to proving that our data structures work properly though. We have to basically torture test all of our functions in stub tests… Yeah

      2. It scores low because I don’t do exception handling. I initially taught myself programming and thus didn’t learn how to handle exceptions (I didn’t even know it was a thing), only how to prevent them, which is actually more efficient and doesn’t limit you nearly as much as you might think. However teachers will look at the coding before trying the program and mark me down significantly for not having exception handling even though the code doesn’t leave any possibility for exceptions to occur, I even leave notes about it. If I didn’t need to take the classes for my degree, or didn’t need the degree to get a decent job, I wouldn’t even bother with the classes.

Leave a Reply