Skip to content
Browse files

Fix a bug in grammar simplification for mixed list.

Signed-off-by: Jerome Dassonville <dassonville.jerome@gmail.com>
  • Loading branch information...
1 parent 10366a3 commit 1dfed2c4807fafbb8081d56888e319b6924dcada @jdassonvil committed Apr 19, 2012
Showing with 28 additions and 40 deletions.
  1. +28 −40 src/org/sablecc/sablecc/grammar/GrammarSimplificator.java
View
68 src/org/sablecc/sablecc/grammar/GrammarSimplificator.java
@@ -56,16 +56,15 @@ public GrammarSimplificator(
GrammarSimplificator.grammar = grammar;
}
-
-
- //This method find a valid name for character declared inline
- //that can't be used in a java identifier.
+
+ // This method find a valid name for character declared inline
+ // that can't be used in a java identifier.
private static String computeValidName(
String name) {
StringBuilder sb = new StringBuilder();
-
- //TODO Complete these list
+
+ // TODO Complete these list
for (char c : name.toCharArray()) {
@@ -149,10 +148,11 @@ private static String computeNewSeparatedProductionName(
String name = leftSourceElement.getTypeName();
if (leftSourceElement.getTypeName().startsWith("'")) {
- name = computeValidName(name.substring(1, name.length() - 1)
- + "Separator");
+ name = computeValidName(name.substring(1, name.length() - 1));
}
+ name += "Separator";
+
if (rightSourceElement.getTypeName().startsWith("'")) {
name += computeValidName(rightSourceElement
.getTypeName()
@@ -199,6 +199,8 @@ private static String computeNewAlternatedProductionName(
name = computeValidName(name.substring(1, name.length() - 1));
}
+ name += "_";
+
if (rightSourceElement.getTypeName().startsWith("'")) {
name += computeValidName(rightSourceElement
.getTypeName()
@@ -1161,34 +1163,16 @@ else if (lowerBoundValue.equals(BigInteger.ONE)) {
}
- private Alternative newAlternative(
- Production production,
- Element element) {
-
- LinkedList<Element> elements = new LinkedList<Element>();
- elements.add(element);
- return newAlternative(production, elements);
- }
-
- private Alternative newAlternative(
- Production production,
- LinkedList<Element> elements) {
-
- Alternative alternative = new Alternative(production, elements);
- alternative.addTransformation(new SAlternativeTransformation(
- alternative, elements));
- return alternative;
- }
-
private Alternative newListAlternative(
Production production,
LinkedList<Element> elements,
LinkedList<SAlternativeTransformationListElement> transformationElements) {
Alternative alternative = new Alternative(production, elements);
alternative.addTransformation(new SAlternativeTransformation(
- transformationElements, alternative, this.parserElement
- .getType()));
+ transformationElements, alternative,
+ new Type.SimpleType.HomogeneousType(this.sElement
+ .getTypeName(), this.cardinality)));
return alternative;
}
@@ -1264,6 +1248,8 @@ public Production getNewProduction() {
private void plusCase() {
+ // (a Sep b)+ = a (b a)*;
+
String plusName = computeNewSeparatedProductionName(
this.sLeftElement, this.sRightElement, this.cardinality);
@@ -1296,7 +1282,7 @@ private void plusCase() {
else {
secondElement = new Element.ProductionElement("",
newAlternatedProduction(this.parserElement,
- this.sLeftElement, this.sRightElement,
+ this.sRightElement, this.sLeftElement,
CardinalityInterval.ZERO_OR_MORE));
}
@@ -1321,6 +1307,8 @@ private void plusCase() {
private void starCase() {
+ // (a Sep b)* = (a Sep b)+ | ;
+
String starName = computeNewSeparatedProductionName(
this.sLeftElement, this.sRightElement, this.cardinality);
@@ -1342,8 +1330,8 @@ private void starCase() {
}
else {
firstElement = new Element.ProductionElement("",
- newAlternatedProduction(this.parserElement,
- this.sRightElement, this.sLeftElement,
+ newSeparatedProduction(this.parserElement,
+ this.sLeftElement, this.sRightElement,
CardinalityInterval.ONE_OR_MORE));
}
@@ -1763,8 +1751,10 @@ private Alternative newListAlternative(
Alternative alternative = new Alternative(production, elements);
alternative.addTransformation(new SAlternativeTransformation(
- transformationElements, alternative, this.parserElement
- .getType()));
+ transformationElements, alternative,
+ new Type.SimpleType.SeparatedType(this.sLeftElement
+ .getTypeName(), this.sRightElement.getTypeName(),
+ this.cardinality)));
return alternative;
}
}
@@ -1858,10 +1848,6 @@ private void plusCase() {
firstAlternativeElements.get(1)));
firstAltTransformationElements
.add(new SAlternativeTransformationListElement.ReferenceElement(
- firstAlternativeElements.get(1),
- firstAlternativeElements.get(1)));
- firstAltTransformationElements
- .add(new SAlternativeTransformationListElement.ReferenceElement(
firstAlternativeElements.get(2),
firstAlternativeElements.get(2)));
@@ -2300,8 +2286,10 @@ private Alternative newListAlternative(
Alternative alternative = new Alternative(production, elements);
alternative.addTransformation(new SAlternativeTransformation(
- transformationElements, alternative, this.parserElement
- .getType()));
+ transformationElements, alternative,
+ new Type.SimpleType.AlternatedType(this.sLeftElement
+ .getTypeName(), this.sRightElement.getTypeName(),
+ this.cardinality)));
return alternative;
}

0 comments on commit 1dfed2c

Please sign in to comment.
Something went wrong with that request. Please try again.