Starting from:
$30

$24

Homework 01 Solution




Instructions



In this assignment, you will be required to scan, parse, and check the semantics of a le that encodes the state of a variation of Connect Four. The de nition of a properly formatted input le is given in Section 1.1.




You will be submitting one .java le and two .g4 (ANTLR) les via web hand-in.




1.1 File Speci cation




The le contains two (2) labeled sections: Moves and Game . Each section is enclosed by start and end tags (/* and */, respectively).



Moves is an underscore-separated (_) list of numbered columns that appear between { and } tokens. Valid Moves are numerical symbols.



Game contains a two-dimensional array of space-separated entries that uses numeric symbols (and -) to encode the state of the game. Rows will be ended with a | and the Game will be begun with a [ and ended with a ]. The two-dimensonal array contains numbers where that player (for example, 1 for Player 1) has played a piece and - for an empty space.



An example of a properly formatted le is shown in Figure 1.




f
moves
/∗
























g
5
10
9
4
1
2
10
10
2
2
1
9
1
3



∗/

game /∗

[




j

j

j

j

j

j

1
]

∗/




Figure 1: A properly formatted Connect Four (cnf) encoding




The assignment is made up of two parts: scanning the text of the input le and parsing the information contained in the input le.




1.2 Scanning




Construct a combined grammar in a .g4 le that ANTLR can use to scan a supplied Connect Four encoding. The logic in this le should be robust enough to identify tokens in the encoding and accurately process any correctly formatted encoding. The rules in your .g4 le will be augmented with actions that display information about the input le. An example of that output is speci ed in Section 2.




The purpose of the scanner is to extract tokens from the input and pass those along to the parser. For the Connect Four encoding, the types of tokens that you will need to consider are given in Table 1.










1


Type
Form
Section Beginning
/*
Section Ending
*/
Section Title
game and moves
Move Symbol
One or more Numerical Symbols
Game Symbol
- or One or more Numerical Symbols
Numerical Symbol
0, 1, 2, 3, 4, 5, 6, 7, 8, or 9
Row Ending
|
Game Beginning
[
Game Ending
]
Moves Beginning
{
Moves Ending
}
White Space (to be ignored)
spaces, tabs, newlines



Table 1: Tokens to Consider




1.2.1 Invalid Encodings




For invalid Connect Four encodings, the output SYNTAX PROBLEM ON LINE L should display. L would be the line of input where the symbol was read. Your scanner should stop scanning the le after an unrecognized token is found.




1.3 Parsing




Construct a combined grammar in a .g4 le that ANTLR can use to parse a supplied Connect Four encoding. In addition to the rules for scanning, there are several parsing rules:




Each section appears once and only once. The sections may appear in either Moves /Game or Game /Moves order.



There must be at least four (4) rows in a valid Game .



There must be at least four (4) columns in a valid Game .



You may assume that each row has the same number of columns, and each column has the same number of rows.




There must be at least two (2) locations in the Moves section.



The semantics of a properly formatted Connect Four encoding are:




The Game must have at least 2 players



The Game must have between 6 and 10 (both inclusive) rows



The Game must have between 6 and 10 (both inclusive) columns



Extra Credit (10 points or Honors contract): Every move in the Moves section must be valid (between 1 and the number of columns in the Game )






























Page 2
Output



2.1 Scanner




Your .g4 le should produce output for both correctly formatted les and incorrectly formatted les. For the correctly formatted le in Figure 1, the output would have the form of the output presented in Figure 2




Moves Section




B eg in ni ng of Section




B eg in ni ng of List




Number : 5




Number : 10




...




Number : 1




Number : 3




End of List




End of Section




Game Section




B eg in ni ng of Section




Start of Game




Space : Empty




Space : Empty




...




Space : Empty




End of Row




Space : Empty




...




Space : Empty




End of Row




Space : Empty




...




Space : Empty




End of Row




Space : Empty




Space : Empty




Space : Empty




Number : 2




Space : Empty




Space : Empty




Space : Empty




Number : 1




Space : Empty




Space : Empty




End of Game




End of Section




End of File




Figure 2: Truncated Output of Scanner for File in Figure 1




For a correctly formatted le in Part 2, the output would be: p pieces have been played where p is the number of pieces in the Game . For the le in Figure 1, the output would be




2 pieces have been played.




2.1.1 Invalid Syntax & Semantics in Parsing




For invalid encodings in Part 2, the message SYNTAX PROBLEM ON LINE L should be displayed where L is the line number where the error occurred. For a semantic rule violation, the output




SEMANTIC PROBLEM P should be displayed, where P is the number of the rule (from List 1.3) that was










Page 3
violated, but parsing should continue.




Syntax errors in Part 2 should be reported in the syntaxError method of csce322assignment01partt02error.java.




Naming Conventions



The ANTLR le for the rst part of the assignment should be named csce322assignment01part01.g4. The ANTLR le for the second part of the assignment should be named csce322assignment01part02.g4. Both grammars should contain a start rule named connectFour. The Java le for the second part of the assignment should be named csce322assignment01part02error.java.




webgrader



The webgrader is available for this assignment. You can test your submitted les before the deadline by submitting them on webhandin and going to http://cse.unl.edu/~cse322/grade, choosing the correct assignment and entering your cse.unl.edu credentials




The script should take approximately 2 minutes to run and produce a PDF.




4.1 The Use of diff




Because Part 1 of this assignment only depends on the symbols in the le, the order in which they are displayed should not be submission dependent. Therefore, diff will be used to compare the output of a particular submission against the output of the solution implementation. In Part 2, the output is sorted and the unique lines extracted, so the order and number of times a semantic error is reported will not a ect the di .




Point Allocation



Component
Points








Part 1
35
Part 2
65








Total
100







6 External Resources




ANTLR




Getting Started with ANTLR v4




ANTLR 4 Documentation




Overview (ANTLR 4 Runtime 4.7.2 API)




7
Commands of Interest
alias
antlr4 = ' java - jar / path / to / antlr -4.7.2 - complete . jar '
alias grun =' java org . antlr . v4 . gui . TestRig '
export
C LA SS PA TH ="/ path / to / antlr -4.7.2 - complete . jar : $C L A S S P A T H "
antlr4
/ path / to / c s c e 3 2 2 a s s i g n m e n t 0 1 p a r t 0 #. g4
javac
-d / path / for /. c l a s s f i l e s / path / to / c s c e 3 2 2 a s s i g n m e n t 0 1 p a r t 0 #*. java
java
/ path / of /. c l a s s f i l e s c s c e 3 2 2 a s s i g n m e n t 0 1 p a r t 0 2 d r i v e r / path / to / i np ut fi le
grun
c s c e 3 2 2 a s s i g n m e n t 0 1 p a r t 0 # c o n n e c t F o u r - gui









Page 4



grun c s c e 3 2 2 a s s i g n m e n t 0 1 p a r t 0 # grun c s c e 3 2 2 a s s i g n m e n t 0 1 p a r t 0 # grun c s c e 3 2 2 a s s i g n m e n t 0 1 p a r t 0 #

c o n n e c t F o u r - gui / path / to / i np ut fi le




c o n n e c t F o u r




c o n n e c t F o u r / path / to / i np ut fi le









































































































































































































Page 5

More products