Giter Site home page Giter Site logo

grammar's Introduction

Comp 124: Grammar Lab

Remember the headline generator from earlier in the semester? It works all right, but having everything hard-coded is not ideal. There is a lot of useless text — repeated case and return statements — and because the argument to rand.nextInt() must match the number of cases, adding a new choice to one of the methods is quite error-prone!

Wouldn’t it be nice to have a a friendly, concise, purpose-specific file format? In other words, instead of this:

public String headline() {
    switch(rand.nextInt(3)) {
        case  1: return person() + " TO WED " + person();
        case  2: return person() + " ANNOUNCES " + issue() + " PLAN";
        default: return person() + " ESCAPES FROM " + place();
    }
}

…wouldn’t you rather have this?

headline: {person} TO WED {person}
headline: {person} ANNOUNCES {issue} PLAN
headline: {person} ESCAPES FROM {place}

In this lab, we’ll build it.

You have a head start: I have already written the parsing code for the file format, and included a sample input file (headlines.grammar). I’ve also laid out a class structure. Your job is to understand that class structure, then fill in the missing pieces.

When you arrive in the lab, I will explain the class structure at the board. Then, do the usual drill one last time:

  • find a partner,
  • fork this repository,
  • clone it to your lab machine,

…and then go through the code to implement the missing methods. (Look for the "not implemented yet" exceptions.)

You will have to think carefully about how you want to approach this! You can’t use GrammarParser and my sample input file until all the missing pieces are in place. Will you try to implement everything before you test anything? (You might get a long way down the wrong path!) Or will you devise a strategy for testing small pieces? (That takes extra time!)

Once you have the pieces working, experiment with the headlines file, or create a different grammar altogether! (Use your imagination! This site generates its pages using essentially this same approach.)

Challenge: If you are feeling adventurous, modify the code so that it won’t reuse the same choice within a headline (i.e. prevent “ELVIS TO WED ELVIS”).

grammar's People

Contributors

pcantrell avatar

Watchers

 avatar James Cloos avatar Libby Shoop avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.