-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGrammar.java
More file actions
executable file
·106 lines (92 loc) · 3.35 KB
/
Grammar.java
File metadata and controls
executable file
·106 lines (92 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package sentenceGenerator;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Map;
import java.util.TreeMap;
/**
* Represents a BNF grammar.
* @author <your name goes here>
*/
public class Grammar {
private Map<String, ListOfDefinitions> grammar; // rules for all the nonterminals
/**
* Constructs a new, empty grammar.
*/
public Grammar() {
grammar = new TreeMap<>();
}
/**
* Prompts the user to choose an input file, then reads and parses
* a BNF grammar from that file. Each grammar rule must be on a single
* line, but may have multiple alternatives. The same nonterminal may
* be defined in multiple rules. Example:
* <pre> <np> ::= <det> <n> | <det> <adjs> <n>
* <np> ::= <det> <n> <pp></pre>
*
* @throws IOException If there is an error reading the file.
*/
public Grammar(BufferedReader reader) throws IOException {
grammar = new TreeMap<>();
// TODO: Your code goes here
}
/**
* Adds definitions for a single nonterminal to this grammar. The input
* text should be in the form:
* <ul><li>A single nonterminal (the thing being defined),</li>
* <li>The symbol "::=", and</li>
* <li>A list of zero or more definitions, separated by the "|" symbol.</li>
* </ul>
*
* @param ruleText The text to be parsed and kept as definitions.
* @throws IllegalArgumentException If the input parameter has a syntax error.
*/
public void addRule(String ruleText) throws IllegalArgumentException {
BnfTokenizer tokenizer = new BnfTokenizer(ruleText);
// TODO: Your code goes here
}
/**
* Adds a single definition to this <code>Grammar</code>. If the
* nonterminal has already been defined, the new definition is
* appended to the existing definitions.
*
* @param lhs The nonterminal being defined.
* @param singleDefinition The new definition.
*/
private void addToGrammar(String lhs, SingleDefinition singleDefinition) {
// TODO: Your code goes here
}
/**
* Throws an <code>IllegalArgumentException</code>, with the input parameter
* as part of the exception message.
*
* @param rule The text to be included in the exception.
* @throws IllegalArgumentException To indicate a syntax error.
*/
private void syntaxError(String rule) {
throw new IllegalArgumentException("Syntax error in rule: " + rule);
}
/**
* Returns a list of definitions for the given nonterminal.
*
* @param nonterminal The nonterminal whose definitions are to be returned.
* @return The definitions of the given nonterminal.
*/
public ListOfDefinitions getDefinitions(String nonterminal) {
return grammar.get(nonterminal);
}
/**
* Prints this Grammar.
*/
public void print() {
// TODO: Your code goes here
}
/**
* Returns <code>true</code> if the given string is a nonterminal,
* as indicated by an initial <code>'<'</code>.
* @param s The token to be tested.
* @return <code>true</code> if <code>s</code> is a nonterminal.
*/
private static boolean isNonterminal(String s) {
return s.startsWith("<");
}
}