Human: PROJECT – Part 1: Language Design (4% of Course Grade)
Introduction
This is a very important part of your project. The decisions you make here will affect several deliverables, so it is a good idea for you to take the time to think about it carefully. Don’t do anything more complex than necessary, because you will pay for that complexity downstream. You will get several opportunities to retroactively improve your language design and, as a result, your grade on this deliverable.
When you design a PL, you need to think about the different things you want to have in it. You will then describe those more formally in the following phases, but it won’t hurt to start thinking about that specification now, nor to start building a set of examples that you will use to test the different stages of your project.
What’s in a PL?
You should know by now that you have lexemes and rules for putting those lexemes together (the grammar of the PL). That is what the PL design will concern foremost, and you choose those based on needs. The language needs to perform a certain task, so you need to make sure that you have everything in the language that allows you to perform the task.
Lexemes. What are the allowable lexemes in this language? Think by category:
Reserved Words: To use with definitions and declarations and statements.
Punctuation: What will you need?
Operators: What kind and on what kinds of data types? No more than necessary!
Built-in functions and other built-in constants.
User-defined identifiers.
User-defined numeric literals/constants (meaningful integer values, you may need them).
String literals/constants (if you want to use any).
You also need to think about whether you want your language to be case sensitive.
Your lexical description should be in a table similar to the one below. The contents of the table are just an example. You have to make your own decisions. Use the Remarks column if you want to give additional information.
Token (Category)
Regular Expression
(formal specification)
Example Lexeme(s)
Remarks
ID
[a-zA-Z][_a-zA-Z0-9]{0, 15}
myVar1
my_var2
MyVar3
An identifier can only start with a letter, followed by up to 15 alphanumeric or underscore characters.
LPAREN
\(
(
Left parenthesis
IF
if
if
Reserved word
Syntax.
Definitions/Declarations: For constants? For types (and, if so, what kinds)? For variables?
Statements: What kinds? Keep them as simple and as few as possible to reduce the amount of code generation you will have to do.
Function prototypes and definitions.
Use BNF or EBNF to specify the grammar for the language. Note that, where the rule involves a terminal symbol, you should use the token in the grammar. E.g., if you are doing a C-style one-way selection statement and have used tokens IF, LPAREN and RPAREN for lexemes ‘if’, ‘(’, ‘)’, and ‘then’, your grammar rule for a single selection statement might look like this:
<SINGLE-SELECT> ::= IF LPAREN <LOGIC-COND> RPAREN THEN <STATEMENT>
Keep in mind that you will need to use your grammar with an LL(1) parser, so make sure it is unambiguous, not left recursive, and that each rule’s RHSs are pairwise disjoint. (If you don’t understand what this means yet, don’t worry. You’ll soon find out. Otherwise, you’ll have to do this later or you’ll get stuck in the parser phase.
Wumpus World Language Specifications
In addition to the regular lexical and syntactic description of the base language, you will also need to think of elements specific to the purpose of this language. The Wumpus World language should be designed with its purpose in mind, namely that it should be used to generate a Wumpus World and to control the agent. To that end, you will have to define several primitive operations and later implement them in the language as subroutines (functions or procedures). Below are the operations that you must define. In any one program, the Wumpus World needs to be initialized to have a given size, defined in terms of a width and a height, and to contain pits in different locations, the Wumpus in a single location, and the gold in a single location.
World Initialization Operations
place(x, y): This operation places elements of the Wumpus World in a room with given coordinates. You may want to split this operation to allows for different implementations depending on the element to be placed, e.g., placeWumpus(x, y,) placePit(x, y), etc.
clearRoom(x, y): You also need to implement an operation to clear a room with given coordinates from any element. You may be able to implement this as a special case of place.
Agent Control Operations
move(dir): This operation allows for the agent to move to an adjacent room in a given direction (north, south, east, west). This operation will change the position of the agent unless the agent hits a boundary of the Wumpus world, in which case the position won’t be change and the operation should return a Bump percept.
shoot(dir): This operation allows for the agent to shoot an arrow to an adjacent room in a given direction (north, south, east, west). If the arrow hits the Wumpus, the operation will return a Scream percept and the Wumpus will be dead.
Agent Sensing Operations
sense(): This operation is called every time the agent enters a new room and should allow the agent to sense the room for clues such as Breeze, Stench or Glitter.
You do not have to name these operations with the names given above, nor are you required to write them following the syntax of a C function call. Again, deciding the syntax, the acceptable lexemes, and the reserved words for the language is part the main goal if this deliverable.
You also need to think about:
Do you want to use special syntax to indicate the start of a program?
How to initialize the Wumpus World? Do you want the initialization to be part of the program syntax? Will you store it in a file with a specified format and load it? Note that the world dimensions are something you enter as parameters, they are not fixed. You also get to choose where the Wumpus, gold, and pits.
You will need to decide how you will represent the world and its elements in the language. How will you represent constant parameters such as the width and height of the world, or the coordinates of the Wumpus or the gold treasure? What about the variables such as the coordinates of the agent or the score? Will you need rudimentary data structures such as arrays or enumerated data types? If so, what would their syntax look like and how would you handle their implementation?
As stated above, you want to avoid making your language too complex, as you will have to implement your language elements down the line. Note that the ultimate end of this project is for you to design this language and be able to compile it into a version of the VML (or Pseudo-Assembly) code your studied earlier in Module 3. When designing language elements, think ahead about how their implementation would look like in the VML (and, again, this is a reason for keeping it as simple as possible). Some things won’t be obvious yet, e.g., what happens when you call a subroutine, but they will become clearer soon.
Examples, examples, examples …
You want to start now building your test suite by providing examples of everything you will want to process. What is a test suite? It’s a set of examples that will serve to illustrate your design and test the different phases of the compiler, as you develop your code and make changes. You will want to have:
A full set of lexemes for closed categories and representative examples categories like user-defined identifiers and constants. These will be formally specified as regular expressions that will be used by the lexer (as shown above).
Examples for every grammar rule you used to describe the structure of your program. The parts of the example that are described by rules for non-terminals in the RHSs can be expanded with the examples you give for those non-terminals. For example, if you have a rule for a one-way selection statement that contains a non-terminal for a condition, the example(s) you use for testing the rule that defines a condition can be used to expand the example for the selection statement.
Examples of programs:
A program that moves the agent a few steps sensing the environment, finding no dangers (and no gold), but bumps into the edge of the world.
A program where the agent explores the world, senses danger, backtracks and tries exploring another path.
A program that navigates as much of the world as needed to get the gold without killing the Wumpus.
A program where the agent has to kill the Wumpus to get the gold.
A program where the agent cannot do anything because there is no possible safe path.
Deliverables
Submit to Project Part 1 in Canvas a document (or set of documents) containing your language design and examples. There is no coding involved in this part of the project except for the programs you write in the language you design. Only the Team Captain should submit.
Teams
If you are the Team Captain, submit the Team Captain Report in a separate file. The report does not accrue credit of its own, but not submitting it results in a 10% penalty for the Team Captain.
Consult this file for team captains. If a C1 appears next to your name, you are the captain.
Team Not Working Out?
This first phase is a crucial point in the project. If your team is not working out, this is the time to reform the team so that you can all work productively together. I can try to help with team troubles, but ultimately, it’s up to you to find a team you can work with. Teamwork will be a very important part of your professional career and you won’t always have the choice of who is in your team, so learn to make it work. Do the work you are responsible for doing and don’t cover up for those who are not doing what they agreed to do. I will not be kind with students who display unprofessional and uncooperative behavior towards their groupmates and don’t take their responsibility seriously.
Grading/Rubric
In the rubric below, the formal specifications are given less weight than completeness because you will have a chance to revise the specifications as you embark in the following phases (and you probably will find yourself needing to do so). So, focus on the design aspects. Don’t forget that the overall design needs to be principled. We looked at some overall principles of language design applied to the pseudo-code language. You can also consult the short document titled “Language Evaluation”. The latter goes well above and beyond what you need to do for this project, but we will return to some of these criteria at the end of the semester when you have had exposure to more languages.
COMPONENT
MAX PTS
Lexeme Specification
15
Completeness of coverage
8
Formal specification
2
Examples
5
Syntax Specification
15
Completeness of coverage
8
Formal specification
2
Examples
5
Overarching
10
Principled design
5
Whole program examples
5
TOTAL
40
ChatGPT:
Language Design Proposal
- Lexemes
Token (Category) | Regular Expression | Example Lexeme(s) | Remarks |
---|
ID | [a-zA-Z][_a-zA-Z0-9]{0, 15} | myVar1, my_var2, MyVar3 | Identifiers can only start with a letter, followed by up to 15 alphanumeric or underscore characters |
LPAREN | ( | ( | Left parenthesis |
RPAREN | ) | ) | Right parenthesis |
WUMPUS | wumpus | wumpus | Reserved word for Wumpus |
GOLD | gold | gold | Reserved word for Gold |
PIT | pit | pit | Reserved word for Pit |
IF | if | if | Reserved word for one-way selection |
THEN | then | then | Reserved word for one-way selection |
ELSE | else | else | Reserved word for two-way selection |
NORTH | north | north | Reserved word for direction |
SOUTH | south | south | Reserved word for direction |
EAST | east | east | Reserved word for direction |
WEST | west | west | Reserved word for direction |
MOVE | move | move | Reserved word for move operation |
SHOOT | shoot | shoot | Reserved word for shoot operation |
SENSE | sense | sense | Reserved word for sense operation |
PLACE | place | place | Reserved word for place operation |
CLEAR | clear | clear | Reserved word for clear operation |
INT_LITERAL | [0-9]+ | 123, 5 | Integer literals |
COMMA | , | , | Comma for separating arguments |
*Note: The language will be case-sensitive.
- Syntax
Definitions/Declarations:
- Constants for width and height of the world, Wumpus location, Gold location, and Pit locations.
- Variables for agent's current position and score.
Statements:
- If-then and If-then-else selection statements.
- Move, Shoot, Sense, Place, and Clear operations.
Grammar Rules (BNF):
<PROGRAM> ::= <INIT> <STATEMENTS>
<INIT> ::= width = INT_LITERAL, height = INT_LITERAL, <PLACE_STATEMENTS>
<PLACE_STATEMENTS> ::= <PLACE_STATEMENT> | <PLACE_STATEMENTS> <PLACE_STATEMENT>
<PLACE_STATEMENT> ::= PLACE WUMPUS (INT_LITERAL, INT_LITERAL) | PLACE GOLD (INT_LITERAL, INT_LITERAL) | PLACE PIT (INT_LITERAL, INT_LITERAL) | CLEAR (INT_LITERAL, INT_LITERAL)
<STATEMENTS> ::= <STATEMENT> | <STATEMENTS> <STATEMENT>
<STATEMENT> ::= <IF_THEN> | <IF_THEN_ELSE> | <MOVE> | <SHOOT> | <SENSE> | <PLACE_STATEMENT> | <CLEAR_STATEMENT>
<IF_THEN> ::= IF <CONDITION> THEN <STATEMENT>
<IF_THEN_ELSE> ::= IF <CONDITION> THEN <STATEMENT> ELSE <STATEMENT>
<CONDITION> ::= SENSE BREEZE | SENSE STENCH | SENSE GLITTER | SENSE BUMP | SENSE SCREAM
<MOVE> ::= MOVE <DIRECTION>
<SHOOT> ::= SHOOT <DIRECTION>
<SENSE> ::= SENSE()
<DIRECTION> ::= NORTH | SOUTH | EAST | WEST
The language will use LL(1) parsing, which means it should be unambiguous, not left recursive, and have pairwise disjoint RHSs for each rule.
- Examples
Examples of programs:
- A program that moves the agent a few steps sensing the environment, finding no dangers (and no gold), but bumps into the edge of the world.
- A program where the agent explores the world, senses danger, backtracks
Human: carry on
ChatGPT:
, and tries exploring another path.
3. A program that navigates as much of the world as needed to get the gold without killing the Wumpus.
- A program where the agent has to kill the Wumpus to get the gold.
- A program where the agent cannot do anything because there is no possible safe path.
Example 1:
width = 4, height = 4
PLACE WUMPUS(3, 3)
PLACE GOLD(4, 4)
PLACE PIT(2, 2)
PLACE PIT(3, 1)
CLEAR(1, 1)
MOVE EAST
SENSE()
MOVE NORTH
SENSE()
Example 2:
width = 4, height = 4
PLACE WUMPUS(3, 3)
PLACE GOLD(4, 4)
PLACE PIT(2, 2)
PLACE PIT(3, 1)
CLEAR(1, 1)
MOVE EAST
SENSE()
IF SENSE BREEZE THEN
MOVE WEST
MOVE NORTH
ELSE
MOVE EAST
ENDIF
SENSE()
Example 3:
width = 4, height = 4
PLACE WUMPUS(3, 3)
PLACE GOLD(3, 4)
PLACE PIT(2, 2)
PLACE PIT(3, 1)
CLEAR(1, 1)
MOVE EAST
SENSE()
MOVE EAST
SENSE()
IF SENSE GLITTER THEN
MOVE NORTH
ENDIF
Example 4:
width = 4, height = 4
PLACE WUMPUS(2, 1)
PLACE GOLD(3, 1)
PLACE PIT(2, 2)
PLACE PIT(3, 2)
CLEAR(1, 1)
SENSE()
IF SENSE STENCH THEN
SHOOT EAST
ENDIF
SENSE()
IF SENSE SCREAM THEN
MOVE EAST
MOVE EAST
ENDIF
Example 5:
width = 4, height = 4
PLACE WUMPUS(3, 1)
PLACE GOLD(3, 3)
PLACE PIT(2, 1)
PLACE PIT(2, 2)
CLEAR(1, 1)
SENSE()
IF SENSE STENCH THEN
MOVE NORTH
ELSE
MOVE EAST
ENDIF
SENSE()