Grades and deadlines

Getting started: files

Start by downloading the starter code. Running this command will create a new directory lab4.1:

git clone https://github.com/si413usna/startlab.git -b lab4.1 lab4.1

In there you will find:

What you need to fill in for this lab are the YAML file, the syntax specification in tokenSpec.txt and Grammar.g4, and two programs in your language, example_prog and dinner_prog.

To run the ptree program on a program in your lauguage such as example_prog, you just do

./ptree tokenSpec.txt Grammar.g4 example_prog

Task 1: Design your language

You need a language that supports all of the string and boolean operations from Unit 3, but now must additionally support functions with lexical scope and closures.

Your language must treat functions as first-class, so that they can be created within and returned from other functions, and passed as arguments, assigned to variables, and so on.

Your language should be original and should not look the same as existing languages, or other languages we have all used in previous weeks. (You can base it off a language you designed for a previous lab, as long as it wasn’t one of the ones chosen for everyone to use.)

Some of the choices you should think about are:

Fill in the langauge_name and language_description fields in the YAML file.

Task 2: Write the specs for your syntax

Fill in the tokenSpec.txt and Grammar.g4 files with (respectively) your formal token spec and ANTLR grammar.

You can’t test them yet because you first need to…

Task 3: Write an example program.

Fill in the file example_prog with a complete example program that demonstrates all the features of your language. Make good use of variable and function names, spacing, and comments so that your code is easily readable and understandable.

Be sure to feature how your function definitions and function calls work!

If this example program is done correctly, it should be self-documenting: anyone who looks at your example program should pretty much get the idea of how your language works without having to read anything else.

Fill in the example_input1, example_output1, example_input2, and example_output2 fields in the YAML file, demonstrating what successful runs of your program would look like.

Make sure this works with the ptree! When you submit your lab, we will check that the example program you wrote correctly tokenizes and parses using the spec you wrote from the previous part.

To test it yourself, just run

./ptree tokenSpec.txt Grammar.g4 example_prog

and be sure to look over the parse tree to make sure it ended up how you intended it to.

Task 3: Write the dinner program in your language

Here is a simple Python program that demonstrates the features your language should support. (It’s also included in your starter code as dinner.py):

def table_tracker(name):
    first = True
    total = ""

    def order(food):
        nonlocal first, total, name

        print("Table " + name + " ordering " + food)

        if first:
            first = False
            total = food
        else:
            total = total + ", " + food

        return total

    return order

def string_eq(s1, s2):
    return not (s1 < s2 or s2 < s1)

table1 = table_tracker("CS profs")
table1("Kimchi")

print("Your name?")
table2 = table_tracker(input())

done = False
t2got = "nothing"
while not done:
    print("What do you want?")
    food = input()
    if string_eq(food, "done"):
        done = True
    else:
        t2got = table2(food)

table1("Tteokbokki")
total1 = table1("Samgyeopsal")

print("We got " + total1 + " and you got " + t2got)

You need to write a program in your language which is exactly equivalent to this one in terms of its run-time behavior. That is, running your program in your (eventually) working interpreter, should be the same as running the code above using the python3 interpreter.

Fill in the dinner_prog file with your code.

You don’t need to provide sample input/output pairs for this one, but you should still make sure it parses correctly:

./ptree tokenSpec.txt Grammar.g4 dinner_prog

Task 5: Test and submit

Be sure to test your own example programs with the parse tree generator using your own token spec and grammar. This is how you make sure that your syntax spec is correctly describing the language as you intended, and it will also help us get to work more quickly next week when we have to write an interpreter for some of these languages!

Follow the submit commands at the top of this page to turn everything in.