|
1 | | -""" |
2 | | -A representation of a duplication rule, i.e. a rule that duplicates the stack |
| 1 | +"""Representation of a duplication rule. |
| 2 | +
|
| 3 | +A rule that duplicates the stack. |
3 | 4 | """ |
4 | 5 |
|
5 | 6 | from typing import List, Set, Hashable, Any |
|
11 | 12 |
|
12 | 13 |
|
13 | 14 | class DuplicationRule(ReducedRule): |
14 | | - """Represents a duplication rule, i.e. a rule of the form: |
| 15 | + """Representation of a duplication rule. |
| 16 | +
|
| 17 | + It is a rule of form: |
15 | 18 | A[sigma] -> B[sigma] C[sigma] |
16 | 19 |
|
17 | 20 | Parameters |
18 | 21 | ---------- |
19 | | - left_term : any |
20 | | - The non-terminal on the left of the rule (A here) |
21 | | - right_term0 : any |
22 | | - The first non-terminal on the right of the rule (B here) |
23 | | - right_term1 : any |
24 | | - The second non-terminal on the right of the rule (C here) |
| 22 | + left_term: |
| 23 | + The non-terminal on the left of the rule, "A" here. |
| 24 | + right_term0: |
| 25 | + The first non-terminal on the right of the rule, "B" here. |
| 26 | + right_term1: |
| 27 | + The second non-terminal on the right of the rule, "C" here. |
25 | 28 | """ |
26 | 29 |
|
27 | 30 | def __init__(self, |
28 | 31 | left_term: Hashable, |
29 | 32 | right_term0: Hashable, |
30 | 33 | right_term1: Hashable) -> None: |
| 34 | + """Initializes the duplication rule.""" |
31 | 35 | self._left_term = to_variable(left_term) |
32 | 36 | self._right_terms = (to_variable(right_term0), |
33 | 37 | to_variable(right_term1)) |
34 | 38 |
|
35 | 39 | @property |
36 | 40 | def f_parameter(self) -> Terminal: |
| 41 | + """Gets the symbol consumed by the rule.""" |
37 | 42 | raise NotImplementedError |
38 | 43 |
|
39 | 44 | @property |
40 | 45 | def production(self) -> Terminal: |
| 46 | + """Gets the symbol produced by the rule.""" |
41 | 47 | raise NotImplementedError |
42 | 48 |
|
43 | 49 | @property |
44 | 50 | def left_term(self) -> Variable: |
45 | | - """Gives the non-terminal on the left of the rule |
46 | | -
|
47 | | - Returns |
48 | | - --------- |
49 | | - left_term : any |
50 | | - The left term of the rule |
51 | | - """ |
| 51 | + """Gets a nonterminal on the left of the rule.""" |
52 | 52 | return self._left_term |
53 | 53 |
|
54 | 54 | @property |
55 | 55 | def right_term(self) -> CFGObject: |
| 56 | + """Gets the single right term of the rule.""" |
56 | 57 | raise NotImplementedError |
57 | 58 |
|
58 | 59 | @property |
59 | 60 | def right_terms(self) -> List[CFGObject]: |
60 | | - """Gives the non-terminals on the right of the rule |
61 | | -
|
62 | | - Returns |
63 | | - --------- |
64 | | - right_terms : iterable of any |
65 | | - The right terms of the rule |
66 | | - """ |
| 61 | + """Gets a list of right terms of the rule.""" |
67 | 62 | return list(self._right_terms) |
68 | 63 |
|
69 | 64 | @property |
70 | 65 | def non_terminals(self) -> Set[Variable]: |
71 | | - """Gives the set of non-terminals used in this rule |
72 | | -
|
73 | | - Returns |
74 | | - --------- |
75 | | - non_terminals : iterable of any |
76 | | - The non terminals used in this rule |
77 | | - """ |
| 66 | + """Gets the nonterminals used in the rule.""" |
78 | 67 | return {self._left_term, *self._right_terms} |
79 | 68 |
|
80 | 69 | @property |
81 | 70 | def terminals(self) -> Set[Terminal]: |
82 | | - """Gets the terminals used in the rule |
83 | | -
|
84 | | - Returns |
85 | | - ---------- |
86 | | - terminals : set of any |
87 | | - The terminals used in this rule |
88 | | - """ |
| 71 | + """Gets the terminals used in the rule.""" |
89 | 72 | return set() |
90 | 73 |
|
91 | 74 | def __eq__(self, other: Any) -> bool: |
| 75 | + """Checks if the rule is equal to the given object.""" |
92 | 76 | if not isinstance(other, DuplicationRule): |
93 | 77 | return False |
94 | 78 | return other.left_term == self._left_term \ |
95 | 79 | and other.right_terms == self.right_terms |
96 | 80 |
|
97 | 81 | def __repr__(self) -> str: |
98 | | - """Gives a string representation of the rule, ignoring the sigmas""" |
| 82 | + """Gets a string representation of the rule.""" |
99 | 83 | return f"{self._left_term} -> " \ |
100 | 84 | + f"{self._right_terms[0]} {self._right_terms[1]}" |
0 commit comments