Skip to content
Snippets Groups Projects
Commit a7603daf authored by Jens Bendisposto's avatar Jens Bendisposto
Browse files

initial import

parent 9acec9b1
No related branches found
No related tags found
No related merge requests found
Showing
with 6319 additions and 0 deletions
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* This file is part of SableCC. *
* See the file "LICENSE" for copyright information and the *
* terms and conditions for copying, distribution and *
* modification of SableCC. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/*
* Last Modification date :: 04-February-2004
* Add termtail to simple term and simple listterm
* in order to support scripting generation for parser by
* the new scripting engine.
*
* Fix bug related to code generation.
* Method #public void caseASimpleTerm(ASimpleTerm node)#
* and #public void caseASimpleTerm(ASimpleTerm node)#
* were rewrite. The type of simpleTerm was the problem.
*/
package org.sablecc.sablecc;
import org.sablecc.sablecc.analysis.*;
import org.sablecc.sablecc.node.*;
import java.util.*;
import java.io.*;
public class ComputeCGNomenclature extends DepthFirstAdapter
{
private String currentProd;
private String currentAlt;
private int counter;
private ResolveIds ids;
private ResolveProdTransformIds prodTransformIds;
private Map altElemTypes;
private final Map altTransformElemTypes = new TypedHashMap(
NodeCast.instance,
StringCast.instance);
private final Map termNumbers = new TypedHashMap(NodeCast.instance,
IntegerCast.instance);
public ComputeCGNomenclature(ResolveIds ids, ResolveProdTransformIds prodTransformIds)
{
this.ids = ids;
this.prodTransformIds = prodTransformIds;
}
public void setAltElemTypes(Map aMap)
{
this.altElemTypes = aMap;
}
public Map getAltTransformElemTypes()
{
return altTransformElemTypes;
}
public Map getTermNumbers()
{
return termNumbers;
}
public void caseAProd(final AProd production)
{
currentProd = "P" + ids.name(production.getId().getText());
Object []temp = production.getAlts().toArray();
for(int i = 0; i<temp.length; i++)
{
((PAlt)temp[i]).apply(this);
}
}
public void inAAlt(AAlt nodeAlt)
{
counter = 0;
if(nodeAlt.getAltName() != null)
{
currentAlt = "A"+
ids.name( nodeAlt.getAltName().getText() )+
currentProd.substring(1);
}
else
{
currentAlt = "A" + currentProd.substring(1);
}
counter = 0;
}
public void caseAAst(AAst node)
{}
public void inAElem(AElem node)
{
String elemType = (String)ids.elemTypes.get(node);
if(node.getElemName() != null)
{
ids.altsElemNameTypes.put(currentAlt+"."+node.getElemName().getText(), elemType );
}
}
public void inANewTerm(ANewTerm node)
{
AProdName aProdName = (AProdName)node.getProdName();
String type = "P" + ids.name(aProdName.getId().getText());
altTransformElemTypes.put(node, type);
termNumbers.put(node, new Integer(++counter));
}
public void inANewListTerm(ANewListTerm node)
{
AProdName aProdName = (AProdName)node.getProdName();
String type = "P" + ids.name(aProdName.getId().getText());
altTransformElemTypes.put(node, type);
termNumbers.put(node, new Integer(++counter));
}
public void outAListTerm(AListTerm node)
{
if( node.getListTerms().size() > 0 )
{
Object[] temp = node.getListTerms().toArray();
String firstTermType = (String)altTransformElemTypes.get(temp[0]);
if(firstTermType != null)
{
if(!firstTermType.startsWith("L"))
{
altTransformElemTypes.put(node, "L" + firstTermType);
}
else
{
altTransformElemTypes.put(node, firstTermType);
}
}
}
else
{
altTransformElemTypes.put(node, "Lnull");
}
termNumbers.put(node, new Integer(++counter));
}
public void caseASimpleTerm(ASimpleTerm node)
{
String name;
String elemType = (String) this.altElemTypes.get( currentAlt+"."+node.getId().getText() );
if(node.getSimpleTermTail() == null)
{
name = elemType;
if(name.startsWith("P") )
{
//add termtail to the simpleterm
node.setSimpleTermTail( (TId)node.getId().clone() );
}
}
else
{
String termTail = node.getSimpleTermTail().getText();
name = (String)prodTransformIds.prodTransformElemTypesString.get(elemType+"."+termTail);
}
if(name.endsWith("?"))
{
name = name.substring(0, name.length()-1);
}
altTransformElemTypes.put(node, name);
termNumbers.put(node, new Integer(++counter));
}
/*
public void caseASimpleTerm(ASimpleTerm node)
{
String name;
String elemType = (String)altElemTypes.get( currentAlt+"."+node.getId().getText() );
if( ( (elemType != null) && elemType.startsWith("T") ) ||
( (elemType == null) && ids.tokens.get("T" + ids.name(node.getId().getText())) != null ) )
{
if(elemType != null)
{
name = elemType;
}
else
{
name = "T" + ids.name(node.getId().getText());
}
}
else
{
if(node.getSimpleTermTail() == null)
{
if(elemType != null)
{
name = elemType;
}
else
{
name = "P" + ids.name(node.getId().getText());
}
//add termtail to the simpleterm
node.setSimpleTermTail( (TId)node.getId().clone() );
}
else
{
String prodType;
if(elemType != null)
{
prodType = elemType;
}
else
{
prodType = "P" + ids.name(node.getId().getText());
}
String termTail = node.getSimpleTermTail().getText();
name = (String)prodTransformIds.prodTransformElemTypesString.get(prodType+"."+termTail);
}
}
if(name.endsWith("?"))
{
name = name.substring(0, name.length()-1);
}
altTransformElemTypes.put(node, name);
termNumbers.put(node, new Integer(++counter));
}
*/
public void caseANullTerm(ANullTerm node)
{
altTransformElemTypes.put(node, "null");
termNumbers.put(node, new Integer(++counter));
}
public void caseASimpleListTerm(ASimpleListTerm node)
{
String name;
String elemType = (String)altElemTypes.get( currentAlt+"."+node.getId().getText() );
if(node.getSimpleTermTail() == null)
{
name = elemType;
if( name.startsWith("P") )
{
//add termtail to the simpleterm
node.setSimpleTermTail( (TId)node.getId().clone() );
}
}
else
{
String termTail = node.getSimpleTermTail().getText();
name = (String)prodTransformIds.prodTransformElemTypesString.get(elemType+"."+termTail);
}
if(name.endsWith("?"))
{
name = name.substring(0, name.length()-1);
}
altTransformElemTypes.put(node, name);
termNumbers.put(node, new Integer(++counter));
}
/*
public void caseASimpleListTerm(ASimpleListTerm node)
{
String name;
String elemType = (String)altElemTypes.get( currentAlt+"."+node.getId().getText() );
if( ( (elemType != null) && elemType.startsWith("T") ) ||
( (elemType == null) && ids.tokens.get("T" + ids.name(node.getId().getText())) != null ) )
{
if(elemType != null)
{
name = elemType;
}
else
{
name = "T" + ids.name(node.getId().getText());
}
}
//it seems to be a production without a specifier
else
{
if(node.getSimpleTermTail() == null)
{
if(elemType != null)
{
name = elemType;
}
else
{
name = "P" + ids.name(node.getId().getText());
}
//add termtail to the simpleterm
node.setSimpleTermTail( (TId)node.getId().clone() );
}
else
{
String prodType;
if(elemType != null)
{
prodType = elemType;
}
else
{
prodType = "P" + ids.name(node.getId().getText());
}
String termTail = node.getSimpleTermTail().getText();
name = (String)prodTransformIds.prodTransformElemTypesString.get(prodType+"."+termTail);
}
}
if(name.endsWith("?"))
{
name = name.substring(0, name.length()-1);
}
altTransformElemTypes.put(node, name);
termNumbers.put(node, new Integer(++counter));
}
*/
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* This file is part of SableCC. *
* See the file "LICENSE" for copyright information and the *
* terms and conditions for copying, distribution and *
* modification of SableCC. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
package org.sablecc.sablecc;
import java.util.*;
import org.sablecc.sablecc.analysis.*;
import org.sablecc.sablecc.node.*;
/*
* Last Modification date : 23 07 2004
* fix bug : when an inlining of a production does not resolve a conflict
* the associated alternatives transformation should not be transformed.
* The bug was about transforming this instead.
*/
/*
* ComputeInlining
* This class takes SableCC grammar represented by the tree
* and a list of production to inline within this grammar and
* try to inline those productions.
*/
public class ComputeInlining
{
//Productions implied in a conflict
private Set setOfProdToBeInline;
//Map of all productions in the grammar
private Map productionsMap;
private Start tree;
public ComputeInlining(Set set
,
Map productionsMap,
Start tree)
{
this.setOfProdToBeInline = set
;
this.productionsMap = productionsMap;
this.tree = tree;
}
/**
* This method compute the inline of a all productions implied in a conflict
* in the grammar.
* It returns :
* -- true if at least one production is inlined with success
* -- and false otherwise.
*/
public boolean computeInlining()
{
final BooleanEx atLeastOneProductionInlined = new BooleanEx(false);
String []nameOfProds = (String[])setOfProdToBeInline.toArray(new String[0]);
for(int i=0; i<nameOfProds.length; i++)
{
final AProd prod = (AProd)productionsMap.get(nameOfProds[i]);
//We proceed inlining only if the production to inline is not recursive
//and if it doesn't have more than SableCC.inliningMaxAlts alternatives.
if( prod.getAlts().size() <= SableCC.inliningMaxAlts && !isProductionRecursive(prod) )
{
//This class construct a special data structure for the production to inline.
final In_Production in_production = new In_Production((AProd)prod.clone());
tree.apply(new DepthFirstAdapter()
{
public void caseAProd(AProd node)
{
//We do not inline the production itself.
if(node.getId().getText().equals(prod.getId().getText()))
{
return;
}
Inlining inliningClass = new Inlining(node, in_production);
//The proper inlining is done here(method inlineProduction)
if( inliningClass.inlineProduction() && !atLeastOneProductionInlined.getValue())
{
atLeastOneProductionInlined.setValue(true);
}
}
}
);
}
}
LinkedList listOfGrammarProds = ((AProductions)((AGrammar)tree.getPGrammar()).getProductions()).getProds();
//Once the production is inlined, we do not need it anymore, so we remove it from the grammar.
String[] inlinedProductionsToRemove = (String[])Inlining.productionsToBeRemoved.toArray(new String[0]);
for(int i=0; i<inlinedProductionsToRemove.length; i++)
{
listOfGrammarProds.remove(productionsMap.get(inlinedProductionsToRemove[i]) );
}
Inlining.productionsToBeRemoved.clear();
return atLeastOneProductionInlined.getValue();
}
/*
* A production is recursive if one of its alternatives contains an occurrence
* of itself.
*/
public boolean isProductionRecursive(final AProd production)
{
final BooleanEx recursive = new BooleanEx(false);
final String currentProdName = production.getId().getText();
production.apply(new DepthFirstAdapter()
{
public void caseAProd(AProd node)
{
Object temp[] = node.getAlts().toArray();
for(int i = 0; i < temp.length; i++)
{
((PAlt) temp[i]).apply(this);
}
}
public void caseAAlt(AAlt node)
{
Object temp[] = node.getElems().toArray();
for(int i = 0; i < temp.length; i++)
{
((PElem) temp[i]).apply(this);
}
}
public void caseAElem(AElem node)
{
if(node.getId().getText().equals(currentProdName))
{
if(node.getSpecifier() != null && node.getSpecifier() instanceof ATokenSpecifier)
{
return;
}
recursive.setValue(true);
}
}
}
);
return recursive.getValue();
}
/* This class is used to simulate final Boolean.
* Since final variable cannot be assigned value more than
* one time, we need another class which boolean value field
* can be changed so often as necessary.
*/
class BooleanEx
{
boolean value;
BooleanEx(boolean value)
{
this.value = value;
}
void setValue(boolean value)
{
this.value = value;
}
boolean getValue()
{
return value;
}
}
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* This file is part of SableCC. *
* See the file "LICENSE" for copyright information and the *
* terms and conditions for copying, distribution and *
* modification of SableCC. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
package org.sablecc.sablecc;
import org.sablecc.sablecc.analysis.*;
import org.sablecc.sablecc.node.*;
import java.util.*;
import java.io.*;
public class ComputeSimpleTermPosition extends DepthFirstAdapter
{
String currentAlt;
String currentProd;
boolean processingParsedAlt;
private ResolveIds ids;
private int counter;
public final Map positionsMap = new TypedHashMap(
StringCast.instance,
StringCast.instance);
public final Map elems_position = new TypedHashMap(
StringCast.instance,
IntegerCast.instance);
public ComputeSimpleTermPosition(ResolveIds ids)
{
this.ids = ids;
}
public void inAProd(AProd node)
{
currentProd = ids.name(node.getId().getText());
ids.names.put(node, currentProd);
}
public void inAAlt(AAlt node)
{
counter = 0;
processingParsedAlt = true;
if(node.getAltName() != null)
{
currentAlt = "A" +
ids.name( node.getAltName().getText() ) +
currentProd;
}
else
{
currentAlt = "A" + currentProd;
}
ids.names.put(node, currentAlt);
}
public void inAElem(AElem node)
{
if(processingParsedAlt)
{
String currentElemName;
if(node.getElemName() != null)
{
currentElemName = currentAlt + "." + node.getElemName().getText();
}
else
{
currentElemName = currentAlt + "." + node.getId().getText();
}
elems_position.put(currentElemName, new Integer(++counter));
}
if(node.getSpecifier() != null &&
node.getSpecifier() instanceof ATokenSpecifier)
{
return;
}
String name = ids.name( node.getId().getText() );
String elemType = (String)ids.elemTypes.get(node);
if(processingParsedAlt && elemType.startsWith("P"))
{
String elemName;
if(node.getElemName() != null)
{
elemName = node.getElemName().getText();
}
else
{
elemName = node.getId().getText();
}
positionsMap.put(currentAlt+"."+elemName, elemType);
}
}
public void outAAlt(AAlt node)
{
processingParsedAlt = false;
}
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* This file is part of SableCC. *
* See the file "LICENSE" for copyright information and the *
* terms and conditions for copying, distribution and *
* modification of SableCC. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
package org.sablecc.sablecc;
import org.sablecc.sablecc.node.*;
import java.util.Set;
public class ConflictException extends Exception
{
private Set conflictualProductions;
public ConflictException(Set conflictualProductions, String message)
{
super(message);
this.conflictualProductions = conflictualProductions;
}
public Set getConflictualProductions()
{
return conflictualProductions;
}
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* This file is part of SableCC. *
* See the file "LICENSE" for copyright information and the *
* terms and conditions for copying, distribution and *
* modification of SableCC. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
package org.sablecc.sablecc;
import java.util.*;
import org.sablecc.sablecc.analysis.*;
import org.sablecc.sablecc.node.*;
public class ConstructNFA extends DepthFirstAdapter
{
private ResolveIds ids;
private String stateName;
private int i;
ConstructNFA(ResolveIds ids, String stateName)
{
this.ids = ids;
this.stateName = stateName;
}
public void outStart(Start node)
{
setOut(node, getOut(node.getPGrammar()));
// free memory
if(getOut(node.getPGrammar()) != null)
setOut(node.getPGrammar(), null);
}
public void outAGrammar(AGrammar node)
{
setOut(node, getOut(node.getTokens()));
// free memory
if(getOut(node.getTokens()) != null)
setOut(node.getTokens(), null);
}
public void outAHelperDef(AHelperDef node)
{
setOut(node, getOut(node.getRegExp()));
// free memory
if(getOut(node.getRegExp()) != null)
setOut(node.getRegExp(), null);
}
public void outATokens(ATokens node)
{
ATokenDef[] tokenDefs = (ATokenDef[]) node.getTokenDefs().toArray(new ATokenDef[0]);
NFA result = null;
for(int i = tokenDefs.length - 1; i >= 0 ; i--)
{
NFA nfa = (NFA) getOut(tokenDefs[i]);
if(nfa != null)
{
if(result == null)
{
result = nfa;
}
else
{
result = nfa.merge(result);
}
// free memory
if(getOut(tokenDefs[i]) != null)
setOut(tokenDefs[i], null);
}
}
if(result != null)
setOut(node, result);
}
public void outATokenDef(ATokenDef node)
{
Set set
= (Set) getOut(node.getStateList());
Object o1 = getOut(node.getRegExp());
if((set
== null) || (set.size() == 0) || set.contains(stateName))
{
//System.out.print("*");
NFA n1 = (o1 instanceof NFA) ? (NFA) o1 : new NFA((CharSet) o1);
String name = (String) ids.names.get(node);
n1.states[n1.states.length - 1].accept = name;
setOut(node, n1);
}
else
{
//System.out.print("-");
}
// free memory
if(getOut(node.getStateList()) != null)
setOut(node.getStateList(), null);
if(getOut(node.getRegExp()) != null)
setOut(node.getRegExp(), null);
}
public void outAStateList(AStateList node)
{
Set set
= new TreeSet();
AStateListTail[] stateListTails = (AStateListTail[]) node.getStateLists().toArray(new AStateListTail[0]);
for(int i = stateListTails.length - 1; i >= 0 ; i--)
{
String str = stateListTails[i].getId().getText().toUpperCase();
set.add(str);
}
set.add(node.getId().getText().toUpperCase());
setOut(node, set
);
}
public void outARegExp(ARegExp node)
{
AConcat[] concats = (AConcat[]) node.getConcats().toArray(new AConcat[0]);
NFA result = null;
if(concats.length > 1)
{
for(int i = concats.length - 1; i >= 0 ; i--)
{
Object o = getOut(concats[i]);
NFA nfa = (o instanceof NFA) ? (NFA) o : new NFA((CharSet) o);
if(result == null)
{
result = nfa;
}
else
{
result = nfa.alternate(result);
}
// free memory
if(getOut(concats[i]) != null)
setOut(concats[i], null);
}
setOut(node, result);
}
else if(concats.length == 1)
{
setOut(node, getOut(concats[0]));
// free memory
if(getOut(concats[0]) != null)
setOut(concats[0], null);
}
}
public void outAConcat(AConcat node)
{
AUnExp[] unExps = (AUnExp[]) node.getUnExps().toArray(new AUnExp[0]);
if(unExps.length == 0)
{
setOut(node, new NFA());
}
else if(unExps.length == 1)
{
setOut(node, getOut(unExps[0]));
// free memory
if(getOut(unExps[0]) != null)
setOut(unExps[0], null);
}
else
{
NFA result = null;
for(int i = unExps.length - 1; i >= 0 ; i--)
{
Object o = getOut(unExps[i]);
NFA nfa = (o instanceof NFA) ? (NFA) o : new NFA((CharSet) o);
if(result == null)
{
result = nfa;
}
else
{
result = nfa.concatenate(result);
}
// free memory
if(getOut(unExps[i]) != null)
setOut(unExps[i], null);
}
setOut(node, result);
}
}
public void outAUnExp(AUnExp node)
{
Object o = getOut(node.getBasic());
char c = ' ';
if(node.getUnOp() != null)
c = ((Character) getOut(node.getUnOp())).charValue();
switch(c)
{
case '*':
{
NFA n = (o instanceof NFA) ? (NFA) o : new NFA((CharSet) o);
setOut(node, n.zeroOrMore());
}
break;
case '?':
{
NFA n = (o instanceof NFA) ? (NFA) o : new NFA((CharSet) o);
setOut(node, n.zeroOrOne());
}
break;
case '+':
{
NFA n = (o instanceof NFA) ? (NFA) o : new NFA((CharSet) o);
setOut(node, n.oneOrMore());
}
break;
default:
{
setOut(node, o);
}
break;
}
// free memory
if(getOut(node.getBasic()) != null)
setOut(node.getBasic(), null);
if(getOut(node.getUnOp()) != null)
setOut(node.getUnOp(), null);
}
public void outACharBasic(ACharBasic node)
{
char c = ((Character) getOut(node.getChar())).charValue();
setOut(node, new CharSet(c));
// free memory
if(getOut(node.getChar()) != null)
setOut(node.getChar(), null);
}
public void outASetBasic(ASetBasic node)
{
setOut(node, getOut(node.getSet()));
// free memory
if(getOut(node.getSet()) != null)
setOut(node.getSet(), null);
}
public void outAStringBasic(AStringBasic node)
{
String s = node.getString().getText();
s = s.substring(1, s.length() -1);
setOut(node, new NFA(s));
}
public void outAIdBasic(AIdBasic node)
{
Object o = getOut((Node) ids.helpers.get(node.getId().getText()));
if(o instanceof NFA)
{
setOut(node, ((NFA) o).clone());
}
else
{
setOut(node, ((CharSet) o).clone());
}
}
public void outARegExpBasic(ARegExpBasic node)
{
setOut(node, getOut(node.getRegExp()));
// free memory
if(getOut(node.getRegExp()) != null)
setOut(node.getRegExp(), null);
}
public void outACharChar(ACharChar node)
{
setOut(node, new Character(node.getChar().getText().charAt(1)));
}
public void outADecChar(ADecChar node)
{
setOut(node, new Character((char) Integer.parseInt(node.getDecChar().getText())));
}
public void outAHexChar(AHexChar node)
{
setOut(node, new Character((char)
Integer.parseInt(node.getHexChar().getText().substring(2), 16)));
}
public void outAOperationSet(AOperationSet node)
{
try
{
CharSet cs1 = (CharSet) getOut(node.getLeft());
CharSet cs2 = (CharSet) getOut(node.getRight());
char binop = ((Character) getOut(node.getBinOp())).charValue();
switch(binop)
{
case '+':
{
setOut(node, cs1.union(cs2));
}
break;
case '-':
{
setOut(node, cs1.diff(cs2));
}
break;
}
}
catch(Exception e)
{
throw new RuntimeException(node + " is invalid.");
}
// free memory
if(getOut(node.getLeft()) != null)
setOut(node.getLeft(), null);
if(getOut(node.getBinOp()) != null)
setOut(node.getBinOp(), null);
if(getOut(node.getRight()) != null)
setOut(node.getRight(), null);
}
public void outAIntervalSet(AIntervalSet node)
{
char c1 = ((Character) getOut(node.getLeft())).charValue();
char c2 = ((Character) getOut(node.getRight())).charValue();
if(c1 > c2)
{
throw new RuntimeException(node + " is invalid.");
}
setOut(node, new CharSet(c1, c2));
// free memory
if(getOut(node.getLeft()) != null)
setOut(node.getLeft(), null);
if(getOut(node.getRight()) != null)
setOut(node.getRight(), null);
}
public void outAStarUnOp(AStarUnOp node)
{
setOut(node, new Character('*'));
}
public void outAQMarkUnOp(AQMarkUnOp node)
{
setOut(node, new Character('?'));
}
public void outAPlusUnOp(APlusUnOp node)
{
setOut(node, new Character('+'));
}
public void outAPlusBinOp(APlusBinOp node)
{
setOut(node, new Character('+'));
}
public void outAMinusBinOp(AMinusBinOp node)
{
setOut(node, new Character('-'));
}
public Object getOut(Node node)
{
if(node == null)
{
return null;
}
return super.getOut(node);
}
public void setOut(Node node, Object out)
{
if(node == null)
{
throw new NullPointerException();
}
super.setOut(node, out);
}
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* This file is part of SableCC. *
* See the file "LICENSE" for copyright information and the *
* terms and conditions for copying, distribution and *
* modification of SableCC. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
package org.sablecc.sablecc;
import java.util.*;
import org.sablecc.sablecc.analysis.*;
import org.sablecc.sablecc.node.*;
public class ConstructParserGenerationDatas extends DepthFirstAdapter
{
private ResolveIds ids;
private int currentAlt;
private boolean processingAst;
private String currentProd;
private Map alts;
public ConstructParserGenerationDatas(ResolveIds ids, Map alts)
{
this.ids = ids;
this.alts = alts;
}
public void caseAAst(AAst node)
{}
public void caseAProd(AProd node)
{
currentProd = (String) ids.names.get(node);
AAlt[] alts = (AAlt[])node.getAlts().toArray(new AAlt[0]);
for(int i=0; i<alts.length; i++)
{
alts[i].apply(this);
}
}
public void caseAAlt(AAlt node)
{
currentAlt = Grammar.addProduction(currentProd, (String) ids.names.get(node));
alts.put(ids.names.get(node), node);
AElem[] temp = (AElem[])node.getElems().toArray(new AElem[0]);
for(int i = 0; i < temp.length; i++)
{
temp[i].apply(this);
}
}
public void caseAElem(AElem node)
{
String name = ids.name(node.getId().getText());
if(node.getSpecifier() != null)
{
if(node.getSpecifier() instanceof ATokenSpecifier)
{
ids.elemTypes.put(node, "T" + name);
}
else
{
ids.elemTypes.put(node, "P" + name);
}
}
else
{
Object token = ids.tokens.get("T" + name);
Object production = ids.prods.get("P" + name);
if(token != null)
{
ids.elemTypes.put(node, "T" + name);
}
else
{
ids.elemTypes.put(node, "P" + name);
}
}
name = (String) ids.elemTypes.get(node);
Grammar.addSymbolToProduction(name, currentAlt);
}
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* This file is part of SableCC. *
* See the file "LICENSE" for copyright information and the *
* terms and conditions for copying, distribution and *
* modification of SableCC. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
package org.sablecc.sablecc;
import java.util.*;
import org.sablecc.sablecc.analysis.*;
import org.sablecc.sablecc.node.*;
public class ConstructProdsMap extends DepthFirstAdapter
{
public Map productionsMap =
new TypedTreeMap(StringCast.instance,
NodeCast.instance);
private String currentProd;
public void caseAProd(AProd node)
{
currentProd = ResolveIds.name(node.getId().getText());
productionsMap.put("P" + currentProd, node);
}
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* This file is part of SableCC. *
* See the file "LICENSE" for copyright information and the *
* terms and conditions for copying, distribution and *
* modification of SableCC. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
package org.sablecc.sablecc;
import java.util.Vector;
import java.util.Hashtable;
public class DFA
{
public DFA(NFA nfa)
{
this.nfa = nfa;
construct();
optimize();
}
public NFA nfa;
public final Vector states = new Vector(0);
public final Hashtable finder = new Hashtable(1);
private void optimize()
{
Vector transitions = new Vector(0);
for(int i = 0; i < states.size(); i++)
{
DFA.State state = (DFA.State) states.elementAt(i);
transitions.addElement(new Vector(0));
for(int j = 0; j < state.transitions.size(); j++)
{
int max = 0;
int st = -1;
for(int k = 0; k < i; k++)
{
int match = match(i,j,k);
if(match > max)
{
max = match;
st = k;
}
}
if(max < 2)
{
((Vector) transitions.elementAt(i)).addElement(
state.transitions.elementAt(j));
}
else
{
DFA.Transition transition1 =
(DFA.Transition) state.transitions.elementAt(j);
DFA.Transition transition2 =
(DFA.Transition) state.transitions.elementAt(j + max - 1);
DFA.Transition transition =
new DFA.Transition(
new CharSet.Interval(
transition1.interval().start,
transition2.interval().end),
-2 - st);
((Vector) transitions.elementAt(i)).addElement(transition);
j += max - 1;
}
}
}
for(int i = 0; i < states.size(); i++)
{
DFA.State state = (DFA.State) states.elementAt(i);
state.transitions = (Vector) transitions.elementAt(i);
}
}
private int match(int st1, int tr, int st2)
{
DFA.State state1 = (DFA.State) states.elementAt(st1);
DFA.State state2 = (DFA.State) states.elementAt(st2);
DFA.Transition first =
(DFA.Transition) state1.transitions.elementAt(tr);
int j = -1;
for(int i = 0; i < state2.transitions.size(); i++)
{
DFA.Transition transition =
(DFA.Transition) state2.transitions.elementAt(i);
if(transition.match(first))
{
j = i;
break;
}
}
if(j == -1)
{
return 0;
}
int max = 0;
int i = tr;
while((i < state1.transitions.size()) &&
(j < state2.transitions.size()))
{
DFA.Transition transition1 =
(DFA.Transition) state1.transitions.elementAt(i);
DFA.Transition transition2 =
(DFA.Transition) state2.transitions.elementAt(j);
if(!transition1.match(transition2))
{
return max;
}
max++;
i++;
j++;
}
return max;
}
private void construct()
{
computeEClosures();
IntSet initial = new IntSet();
initial.or(eclosure(0));
State state = new State(initial);
states.addElement(state);
finder.put(state.nfaStates, new Integer(0));
int i = -1;
while(++i < states.size())
{
System.out.print(".");
state = (State) states.elementAt(i);
CharSet.Interval interval = new CharSet.Interval((char) 0, (char) 0xffff);
do
{
IntSet destination = new IntSet();
interval.end = (char) 0xffff;
boolean modified = false;
int[] elements = state.nfaStates.elements();
for(int k = 0; k < elements.length; k++)
{
int j = elements[k];
if((nfa.states[j].transitions[0] != null) &&
(nfa.states[j].transitions[0].chars != null))
{
CharSet.Interval overlap =
nfa.states[j].transitions[0].chars.findOverlap(interval);
if(overlap != null)
{
if(overlap.start > interval.start)
{
interval.end = (char) (overlap.start - 1);
}
else
{
destination.set(nfa.states[j].transitions[0].destination);
modified = true;
if(overlap.end < interval.end)
{
interval.end = overlap.end;
}
}
}
}
if((nfa.states[j].transitions[1] != null) &&
(nfa.states[j].transitions[1].chars != null))
{
CharSet.Interval overlap =
nfa.states[j].transitions[1].chars.findOverlap(interval);
if(overlap != null)
{
if(overlap.start > interval.start)
{
interval.end = (char) (overlap.start - 1);
}
else
{
destination.set(nfa.states[j].transitions[1].destination);
if(overlap.end < interval.end)
{
interval.end = overlap.end;
}
}
}
}
}
if(modified)
{
destination = eclosure(destination);
Integer dest = (Integer) finder.get(destination);
if(dest != null)
{
state.transitions.addElement(
new Transition((CharSet.Interval) interval.clone(), dest.intValue()));
}
else
{
State s = new State(destination);
states.addElement(s);
finder.put(s.nfaStates, new Integer(states.size() - 1));
state.transitions.addElement(
new Transition((CharSet.Interval) interval.clone(), states.size() - 1));
}
}
interval.start = (char) (interval.end + 1);
}
while(interval.end != (char) 0xffff);
// System.out.println(state);
}
// System.out.println(this);
}
private IntSet[] eclosures;
private void computeEClosures()
{
eclosures = new IntSet[nfa.states.length];
for(int i = 0; i < nfa.states.length; i++)
{
System.out.print(".");
IntSet set
= new IntSet();
eclosure(i, set
);
eclosures[i] = set
;
}
System.out.println();
}
private IntSet eclosure(int state)
{
return eclosures[state];
}
private void eclosure(int state, IntSet nfaStates)
{
if(eclosures[state] != null)
{
nfaStates.or(eclosures[state]);
return;
}
nfaStates.set(state);
if((nfa.states[state].transitions[0] != null) &&
(nfa.states[state].transitions[0].chars == null) &&
(!nfaStates.get(nfa.states[state].transitions[0].destination)))
{
eclosure(nfa.states[state].transitions[0].destination, nfaStates);
}
if((nfa.states[state].transitions[1] != null) &&
(nfa.states[state].transitions[1].chars == null) &&
(!nfaStates.get(nfa.states[state].transitions[1].destination)))
{
eclosure(nfa.states[state].transitions[1].destination, nfaStates);
}
}
private IntSet eclosure(IntSet nfaStates)
{
IntSet result = new IntSet();
int[] elements = nfaStates.elements();
for(int j = 0; j < elements.length; j++)
{
int i = elements[j];
result.or(eclosure(i));
}
return result;
}
public String toString()
{
StringBuffer result = new StringBuffer();
for(int i = 0; i < states.size(); i++)
{
result.append(i + ": " + states.elementAt(i) +
System.getProperty("line.separator"));
}
return result.toString();
}
public static class State
{
public State(IntSet nfaStates)
{
this.nfaStates = nfaStates;
}
public IntSet nfaStates = new IntSet();
public Vector transitions = new Vector(0);
public int accept;
public String toString()
{
StringBuffer result = new StringBuffer();
/* for(int i = 0; i < nfaStates.size(); i++)
{
if(nfaStates.get(i))
{
if(nfa.states[i].accept != null)
{
result.append("(" + nfa.states[i].accept + ")");
}
}
}*/
for(int i = 0; i < transitions.size(); i++)
{
result.append(transitions.elementAt(i) + ",");
}
return result /*+ " " + nfaStates*/ + "";
}
}
public static class Transition
{
private char start;
private char end;
public int destination;
public Transition(CharSet.Interval interval, int destination)
{
this.start = interval.start;
this.end = interval.end;
this.destination = destination;
}
public CharSet.Interval interval()
{
return new CharSet.Interval(start, end);
}
public Transition(Transition transition)
{
start = transition.start;
end = transition.end;
destination = transition.destination;
}
public String toString()
{
return destination + ":[" + interval() + "]";
}
public boolean match(Transition transition)
{
return (start == transition.start) &&
(end == transition.end) &&
(destination == transition.destination);
}
}
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* This file is part of SableCC. *
* See the file "LICENSE" for copyright information and the *
* terms and conditions for copying, distribution and *
* modification of SableCC. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
package org.sablecc.sablecc;
import java.io.*;
class DisplayLicense
{
DisplayLicense()
{
try
{
BufferedReader in =
new BufferedReader(
new InputStreamReader(
getClass().getResourceAsStream("LICENSE")));
System.out.println("---- FILE: LICENSE ----");
String s;
while((s = in.readLine()) != null)
{
System.out.println(s);
}
in.close();
System.out.println("---- END OF FILE: SableCC-LICENSE ----");
System.out.println();
System.out.println("---- FILE: AUTHORS ----");
in =
new BufferedReader(
new InputStreamReader(
getClass().getResourceAsStream("AUTHORS")));
while((s = in.readLine()) != null)
{
System.out.println(s);
}
in.close();
System.out.println("---- END OF FILE: AUTHORS ----");
System.out.println();
System.out.println("---- FILE: COPYING-LESSER ----");
in =
new BufferedReader(
new InputStreamReader(
getClass().getResourceAsStream("COPYING-LESSER")));
while((s = in.readLine()) != null)
{
System.out.println(s);
}
in.close();
System.out.println("---- END OF FILE: COPYING-LESSER ----");
}
catch(Exception e)
{
System.out.println(e);
System.exit(1);
}
}
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* This file is part of SableCC. *
* See the file "LICENSE" for copyright information and the *
* terms and conditions for copying, distribution and *
* modification of SableCC. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
package org.sablecc.sablecc;
import org.sablecc.sablecc.analysis.*;
import org.sablecc.sablecc.node.*;
import java.util.*;
import java.io.*;
public class GenAlts extends DepthFirstAdapter
{
private MacroExpander macros;
private ResolveAstIds ast_ids;
private File pkgDir;
private String pkgName;
private List elemList;
private String currentProd;
ElemInfo info;
// final GenAlts instance = this;
public GenAlts(ResolveAstIds ast_ids)
{
this.ast_ids = ast_ids;
try
{
macros = new MacroExpander(
new InputStreamReader(
getClass().getResourceAsStream("alternatives.txt")));
}
catch(IOException e)
{
throw new RuntimeException("unable to open alternatives.txt.");
}
pkgDir = new File(ast_ids.astIds.pkgDir, "node");
pkgName = ast_ids.astIds.pkgName.equals("") ? "node" : ast_ids.astIds.pkgName + ".node";
if(!pkgDir.exists())
{
if(!pkgDir.mkdir())
{
throw new RuntimeException("Unable to create " + pkgDir.getAbsolutePath());
}
}
}
public void inAAstProd(AAstProd node)
{
currentProd = (String) ast_ids.ast_names.get(node);
}
public void inAAstAlt(AAstAlt node)
{
elemList = new TypedLinkedList(ElemInfoCast.instance);
}
public void caseAProductions(AProductions node)
{}
public void inAElem(AElem node)
{
info = new ElemInfo();
info.name = (String) ast_ids.ast_names.get(node);
info.type = (String) ast_ids.ast_elemTypes.get(node);
info.operator = ElemInfo.NONE;
if(node.getUnOp() != null)
{
node.getUnOp().apply(new DepthFirstAdapter()
{
public void caseAStarUnOp(AStarUnOp node)
{
info.operator = ElemInfo.STAR;
}
public void caseAQMarkUnOp(AQMarkUnOp node)
{
info.operator = ElemInfo.QMARK;
}
public void caseAPlusUnOp(APlusUnOp node)
{
info.operator = ElemInfo.PLUS;
}
}
);
}
elemList.add(info);
info = null;
}
public void outAAstAlt(AAstAlt node)
{
String name = (String) ast_ids.ast_names.get(node);
BufferedWriter file;
try
{
file = new BufferedWriter(
new FileWriter(
new File(pkgDir, name + ".java")));
}
catch(IOException e)
{
throw new RuntimeException("Unable to create " + new File(pkgDir, name + ".java").getAbsolutePath());
}
try
{
boolean hasOperator = false;
boolean hasList = false;
for(Iterator i = elemList.iterator(); i.hasNext();)
{
ElemInfo info = (ElemInfo) i.next();
if(info != null)
switch(info.operator)
{
case ElemInfo.STAR:
case ElemInfo.PLUS:
{
hasList = true;
}
break;
}
}
macros.apply(file, "AlternativeHeader1", new String[] {pkgName});
if(hasList)
{
macros.apply(file, "AlternativeHeaderList", new String[] {});
}
macros.apply(file, "AlternativeHeader2", new String[] {
ast_ids.astIds.pkgName.equals("") ? "analysis" : ast_ids.astIds.pkgName + ".analysis",
name, currentProd});
for(Iterator i = elemList.iterator(); i.hasNext();)
{
ElemInfo info = (ElemInfo) i.next();
if(info != null)
switch(info.operator)
{
case ElemInfo.QMARK:
case ElemInfo.NONE:
{
macros.apply(file, "NodeElement",
new String[] {info.type,
nodeName(info.name)});
}
break;
case ElemInfo.STAR:
case ElemInfo.PLUS:
{
hasOperator = true;
macros.apply(file, "ListElement",
new String[] {info.type, nodeName(info.name)});
}
break;
}
}
macros.apply(file, "ConstructorHeader",
new String[] {name});
macros.apply(file, "ConstructorBodyHeader", null);
macros.apply(file, "ConstructorBodyTail", null);
if(elemList.size() > 0)
{
macros.apply(file, "ConstructorHeader",
new String[] {name});
for(Iterator i = elemList.iterator(); i.hasNext();)
{
ElemInfo info = (ElemInfo) i.next();
if(info != null)
switch(info.operator)
{
case ElemInfo.QMARK:
case ElemInfo.NONE:
{
macros.apply(file, "ConstructorHeaderDeclNode",
new String[] {info.type, nodeName(info.name), i.hasNext() ? "," : ""});
}
break;
case ElemInfo.STAR:
case ElemInfo.PLUS:
{
macros.apply(file, "ConstructorHeaderDeclList",
new String[] {info.type, nodeName(info.name), i.hasNext() ? "," : ""});
}
break;
}
}
macros.apply(file, "ConstructorBodyHeader", null);
for(Iterator i = elemList.iterator(); i.hasNext();)
{
ElemInfo info = (ElemInfo) i.next();
if(info != null )
switch(info.operator)
{
case ElemInfo.QMARK:
case ElemInfo.NONE:
{
macros.apply(file, "ConstructorBodyNode",
new String[] {info.name, nodeName(info.name)});
}
break;
case ElemInfo.STAR:
case ElemInfo.PLUS:
{
macros.apply(file, "ConstructorBodyList",
new String[] {info.name, nodeName(info.name)});
}
break;
}
}
macros.apply(file, "ConstructorBodyTail", null);
}
//****************
macros.apply(file, "CloneHeader",
new String[] {name});
for(Iterator i = elemList.iterator(); i.hasNext();)
{
ElemInfo info = (ElemInfo) i.next();
if(info != null)
switch(info.operator)
{
case ElemInfo.QMARK:
case ElemInfo.NONE:
{
macros.apply(file, "CloneBodyNode",
new String[] {info.type, nodeName(info.name), i.hasNext() ? "," : ""});
}
break;
case ElemInfo.STAR:
case ElemInfo.PLUS:
{
macros.apply(file, "CloneBodyList",
new String[] {nodeName(info.name), i.hasNext() ? "," : ""});
}
break;
}
}
macros.apply(file, "CloneTail", null);
macros.apply(file, "Apply", new String[] {name});
for(Iterator i = elemList.iterator(); i.hasNext();)
{
ElemInfo info = (ElemInfo) i.next();
if(info != null)
switch(info.operator)
{
case ElemInfo.QMARK:
case ElemInfo.NONE:
{
macros.apply(file, "GetSetNode",
new String[] {info.type, info.name, nodeName(info.name)});
}
break;
case ElemInfo.STAR:
case ElemInfo.PLUS:
{
macros.apply(file, "GetSetList",
new String[] {info.name, nodeName(info.name), info.type});
}
break;
}
}
macros.apply(file, "ToStringHeader", null);
for(Iterator i = elemList.iterator(); i.hasNext();)
{
ElemInfo info = (ElemInfo) i.next();
if(info != null)
switch(info.operator)
{
case ElemInfo.QMARK:
case ElemInfo.NONE:
{
macros.apply(file, "ToStringBodyNode",
new String[] {nodeName(info.name)});
}
break;
case ElemInfo.STAR:
case ElemInfo.PLUS:
{
macros.apply(file, "ToStringBodyList",
new String[] {nodeName(info.name)});
}
break;
}
}
macros.apply(file, "ToStringTail", null);
macros.apply(file, "RemoveChildHeader", null);
for(Iterator i = elemList.iterator(); i.hasNext();)
{
ElemInfo info = (ElemInfo) i.next();
if(info != null)
switch(info.operator)
{
case ElemInfo.QMARK:
case ElemInfo.NONE:
{
macros.apply(file, "RemoveChildNode",
new String[] {nodeName(info.name)});
}
break;
case ElemInfo.STAR:
case ElemInfo.PLUS:
{
macros.apply(file, "RemoveChildList",
new String[] {nodeName(info.name)});
}
break;
}
}
macros.apply(file, "RemoveChildTail", null);
macros.apply(file, "ReplaceChildHeader", null);
for(Iterator i = elemList.iterator(); i.hasNext();)
{
ElemInfo info = (ElemInfo) i.next();
if(info != null)
switch(info.operator)
{
case ElemInfo.QMARK:
case ElemInfo.NONE:
{
macros.apply(file, "ReplaceChildNode",
new String[] {nodeName(info.name), info.name, info.type});
}
break;
case ElemInfo.STAR:
case ElemInfo.PLUS:
{
macros.apply(file, "ReplaceChildList",
new String[] {nodeName(info.name), info.type});
}
break;
}
}
macros.apply(file, "ReplaceChildTail", null);
macros.apply(file, "AlternativeTail", null);
}
catch(IOException e)
{
throw new RuntimeException("An error occured while writing to " +
new File(pkgDir, name + ".java").getAbsolutePath());
}
try
{
file.close();
}
catch(IOException e)
{}
elemList = null;
}
public static String nodeName(String s)
{
StringBuffer result = new StringBuffer(s);
if(result.length() > 0)
{
result.setCharAt(0, Character.toLowerCase(result.charAt(0)));
}
return result.toString();
}
private static class ElemInfo
{
final static int NONE = 0;
final static int STAR = 1;
final static int QMARK = 2;
final static int PLUS = 3;
String name;
String type;
int operator;
}
private static class ElemInfoCast implements Cast
{
public final static ElemInfoCast instance = new ElemInfoCast();
private ElemInfoCast()
{}
public Object cast(Object o)
{
return (ElemInfo) o;
}
}
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* This file is part of SableCC. *
* See the file "LICENSE" for copyright information and the *
* terms and conditions for copying, distribution and *
* modification of SableCC. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
package org.sablecc.sablecc;
import org.sablecc.sablecc.analysis.*;
import org.sablecc.sablecc.node.*;
import java.util.*;
import java.io.*;
public class GenAnalyses extends DepthFirstAdapter
{
private MacroExpander macros;
private ResolveAstIds ast_ids;
private File pkgDir;
private String pkgName;
private List elemList;
private List altList = new TypedLinkedList(AltInfoCast.instance);
private List tokenList = new TypedLinkedList(StringCast.instance);
private String mainProduction;
ElemInfo info;
// final GenAnalyses instance = this;
public GenAnalyses(ResolveAstIds ast_ids)
{
this.ast_ids = ast_ids;
try
{
macros = new MacroExpander(
new InputStreamReader(
getClass().getResourceAsStream("analyses.txt")));
}
catch(IOException e)
{
throw new RuntimeException("unable to open analyses.txt.");
}
pkgDir = new File(ast_ids.astIds.pkgDir, "analysis");
pkgName = ast_ids.astIds.pkgName.equals("") ? "analysis" : ast_ids.astIds.pkgName + ".analysis";
if(!pkgDir.exists())
{
if(!pkgDir.mkdir())
{
throw new RuntimeException("Unable to create " + pkgDir.getAbsolutePath());
}
}
}
public void inAAstProd(AAstProd node)
{
if(mainProduction == null)
{
mainProduction = (String) ast_ids.ast_names.get(node);
}
}
public void inATokenDef(ATokenDef node)
{
tokenList.add(ast_ids.astIds.names.get(node));
}
public void inAAstAlt(AAstAlt node)
{
elemList = new TypedLinkedList(ElemInfoCast.instance);
}
public void caseAProductions(AProductions node)
{}
public void inAElem(AElem node)
{
info = new ElemInfo();
info.name = (String) ast_ids.ast_names.get(node);
info.type = (String) ast_ids.ast_elemTypes.get(node);
info.operator = ElemInfo.NONE;
if(node.getUnOp() != null)
{
node.getUnOp().apply(new DepthFirstAdapter()
{
public void caseAStarUnOp(AStarUnOp node)
{
info.operator = ElemInfo.STAR;
}
public void caseAQMarkUnOp(AQMarkUnOp node)
{
info.operator = ElemInfo.QMARK;
}
public void caseAPlusUnOp(APlusUnOp node)
{
info.operator = ElemInfo.PLUS;
}
}
);
}
elemList.add(info);
info = null;
}
public void outAAstAlt(AAstAlt node)
{
AltInfo info = new AltInfo();
info.name = (String) ast_ids.ast_names.get(node);
info.elems.addAll(elemList);
elemList = null;
altList.add(info);
}
public void outStart(Start node)
{
createAnalysis();
createAnalysisAdapter();
if(mainProduction != null)
{
createDepthFirstAdapter();
createReversedDepthFirstAdapter();
}
}
public void createAnalysis()
{
BufferedWriter file;
try
{
file = new BufferedWriter(
new FileWriter(
new File(pkgDir, "Analysis.java")));
}
catch(IOException e)
{
throw new RuntimeException("Unable to create " + new File(pkgDir, "Analysis.java").getAbsolutePath());
}
try
{
macros.apply(file, "AnalysisHeader", new String[] {pkgName,
ast_ids.astIds.pkgName.equals("") ? "node" : ast_ids.astIds.pkgName + ".node"});
if(mainProduction != null)
{
macros.apply(file, "AnalysisStart", null);
for(Iterator i = altList.iterator(); i.hasNext();)
{
AltInfo info = (AltInfo) i.next();
macros.apply(file, "AnalysisBody",
new String[] {info.name});
}
file.newLine();
}
for(Iterator i = tokenList.iterator(); i.hasNext();)
{
macros.apply(file, "AnalysisBody",
new String[] {(String) i.next()});
}
macros.apply(file, "AnalysisTail", null);
}
catch(IOException e)
{
throw new RuntimeException("An error occured while writing to " +
new File(pkgDir, "Analysis.java").getAbsolutePath());
}
try
{
file.close();
}
catch(IOException e)
{}
}
public void createAnalysisAdapter()
{
BufferedWriter file;
try
{
file = new BufferedWriter(
new FileWriter(
new File(pkgDir, "AnalysisAdapter.java")));
}
catch(IOException e)
{
throw new RuntimeException("Unable to create " + new File(pkgDir, "AnalysisAdapter.java").getAbsolutePath());
}
try
{
macros.apply(file, "AnalysisAdapterHeader", new String[] {pkgName,
ast_ids.astIds.pkgName.equals("") ? "node" : ast_ids.astIds.pkgName + ".node"});
if(mainProduction != null)
{
macros.apply(file, "AnalysisAdapterStart", null);
for(Iterator i = altList.iterator(); i.hasNext();)
{
AltInfo info = (AltInfo) i.next();
macros.apply(file, "AnalysisAdapterBody",
new String[] {info.name});
}
}
for(Iterator i = tokenList.iterator(); i.hasNext();)
{
macros.apply(file, "AnalysisAdapterBody",
new String[] {(String) i.next()});
}
macros.apply(file, "AnalysisAdapterTail", null);
}
catch(IOException e)
{
throw new RuntimeException("An error occured while writing to " +
new File(pkgDir, "AnalysisAdapter.java").getAbsolutePath());
}
try
{
file.close();
}
catch(IOException e)
{}
}
public void createDepthFirstAdapter()
{
BufferedWriter file;
try
{
file = new BufferedWriter(
new FileWriter(
new File(pkgDir, "DepthFirstAdapter.java")));
}
catch(IOException e)
{
throw new RuntimeException("Unable to create " + new File(pkgDir, "DepthFirstAdapter.java").getAbsolutePath());
}
try
{
macros.apply(file, "DepthFirstAdapterHeader", new String[] {pkgName,
ast_ids.astIds.pkgName.equals("") ? "node" : ast_ids.astIds.pkgName + ".node",
mainProduction});
for(Iterator i = altList.iterator(); i.hasNext();)
{
AltInfo info = (AltInfo) i.next();
macros.apply(file, "DepthFirstAdapterInOut",
new String[] {info.name});
macros.apply(file, "DepthFirstAdapterCaseHeader",
new String[] {info.name});
for(Iterator j = info.elems.iterator(); j.hasNext();)
{
ElemInfo eInfo = (ElemInfo) j.next();
switch(eInfo.operator)
{
case ElemInfo.QMARK:
case ElemInfo.NONE:
{
macros.apply(file, "DepthFirstAdapterCaseBodyNode",
new String[] {eInfo.name});
}
break;
case ElemInfo.STAR:
case ElemInfo.PLUS:
{
macros.apply(file, "DepthFirstAdapterCaseBodyList",
new String[] {eInfo.name, eInfo.type});
}
break;
}
}
macros.apply(file, "DepthFirstAdapterCaseTail",
new String[] {info.name});
}
macros.apply(file, "DepthFirstAdapterTail", null);
}
catch(IOException e)
{
throw new RuntimeException("An error occured while writing to " +
new File(pkgDir, "DepthFirstAdapter.java").getAbsolutePath());
}
try
{
file.close();
}
catch(IOException e)
{}
}
public void createReversedDepthFirstAdapter()
{
BufferedWriter file;
try
{
file = new BufferedWriter(
new FileWriter(
new File(pkgDir, "ReversedDepthFirstAdapter.java")));
}
catch(IOException e)
{
throw new RuntimeException("Unable to create " + new File(pkgDir, "ReversedDepthFirstAdapter.java").getAbsolutePath());
}
try
{
macros.apply(file, "ReversedDepthFirstAdapterHeader", new String[] {pkgName,
ast_ids.astIds.pkgName.equals("") ? "node" : ast_ids.astIds.pkgName + ".node",
mainProduction});
for(Iterator i = altList.iterator(); i.hasNext();)
{
AltInfo info = (AltInfo) i.next();
macros.apply(file, "DepthFirstAdapterInOut",
new String[] {info.name});
macros.apply(file, "DepthFirstAdapterCaseHeader",
new String[] {info.name});
for(ListIterator j = info.elems.listIterator(info.elems.size()); j.hasPrevious();)
{
ElemInfo eInfo = (ElemInfo) j.previous();
switch(eInfo.operator)
{
case ElemInfo.QMARK:
case ElemInfo.NONE:
{
macros.apply(file, "DepthFirstAdapterCaseBodyNode",
new String[] {eInfo.name});
}
break;
case ElemInfo.STAR:
case ElemInfo.PLUS:
{
macros.apply(file, "ReversedDepthFirstAdapterCaseBodyList",
new String[] {eInfo.name, eInfo.type});
}
break;
}
}
macros.apply(file, "DepthFirstAdapterCaseTail",
new String[] {info.name});
}
macros.apply(file, "DepthFirstAdapterTail", null);
}
catch(IOException e)
{
throw new RuntimeException("An error occured while writing to " +
new File(pkgDir, "ReversedDepthFirstAdapter.java").getAbsolutePath());
}
try
{
file.close();
}
catch(IOException e)
{}
}
private static class ElemInfo
{
final static int NONE = 0;
final static int STAR = 1;
final static int QMARK = 2;
final static int PLUS = 3;
String name;
String type;
int operator;
}
private static class ElemInfoCast implements Cast
{
final static ElemInfoCast instance = new ElemInfoCast();
private ElemInfoCast()
{}
public Object cast(Object o)
{
return (ElemInfo) o;
}
}
private static class AltInfo
{
String name;
final List elems = new TypedLinkedList(ElemInfoCast.instance);
}
private static class AltInfoCast implements Cast
{
final static AltInfoCast instance = new AltInfoCast();
private AltInfoCast()
{}
public Object cast(Object o)
{
return (AltInfo) o;
}
}
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* This file is part of SableCC. *
* See the file "LICENSE" for copyright information and the *
* terms and conditions for copying, distribution and *
* modification of SableCC. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
package org.sablecc.sablecc;
import java.util.*;
import org.sablecc.sablecc.analysis.*;
import org.sablecc.sablecc.node.*;
import java.io.*;
import java.util.Vector;
import java.util.Enumeration;
public class GenLexer extends AnalysisAdapter
{
private MacroExpander macros;
private ResolveIds ids;
private File pkgDir;
private String pkgName;
private AcceptStates[] acceptStatesArray;
private Transitions transitions;
public GenLexer(ResolveIds ids)
{
this.ids = ids;
try
{
macros = new MacroExpander(
new InputStreamReader(
getClass().getResourceAsStream("lexer.txt")));
}
catch(IOException e)
{
throw new RuntimeException("unable to open lexer.txt.");
}
pkgDir = new File(ids.pkgDir, "lexer");
pkgName = ids.pkgName.equals("") ? "lexer" : ids.pkgName + ".lexer";
if(!pkgDir.exists())
{
if(!pkgDir.mkdir())
{
throw new RuntimeException("Unable to create " + pkgDir.getAbsolutePath());
}
}
}
public void caseStart(Start tree)
{
String[] names;
int numStates = Math.max(1, ids.stateList.size());
acceptStatesArray = new AcceptStates[numStates];
names = new String[numStates];
if(ids.stateList.size() == 0)
{
names[0] = "INITIAL";
}
else
{
Iterator iter = ids.stateList.iterator();
for(int i = 0; i < numStates; i++)
{
names[i] = (String) iter.next();
}
}
for(int i = 0; i < numStates; i++)
{
System.out.println(" State: " + names[i]);
System.out.println(" - Constructing NFA.");
ConstructNFA nfaConstructor = new ConstructNFA(ids, names[i]);
tree.apply(nfaConstructor);
System.out.println();
NFA nfa = (NFA) nfaConstructor.getOut(tree);
nfaConstructor = null;
System.out.println(" - Constructing DFA.");
DFA dfa = new DFA(nfa);
System.out.println();
System.out.println(" - resolving ACCEPT states.");
acceptStatesArray[i] = new AcceptStates(dfa, ids, names[i]);
tree.apply(acceptStatesArray[i]);
}
transitions = new Transitions();
tree.apply(transitions);
createLexerException();
createLexer();
}
private void createLexerException()
{
BufferedWriter file;
try
{
file = new BufferedWriter(
new FileWriter(
new File(pkgDir, "LexerException.java")));
}
catch(IOException e)
{
throw new RuntimeException("Unable to create " + new File(pkgDir, "LexerException.java").getAbsolutePath());
}
try
{
macros.apply(file, "LexerException", new String[] {pkgName});
}
catch(IOException e)
{
throw new RuntimeException("An error occured while writing to " +
new File(pkgDir, "LexerException.java").getAbsolutePath());
}
try
{
file.close();
}
catch(IOException e)
{}
}
private void createLexer()
{
BufferedWriter file;
try
{
file = new BufferedWriter(
new FileWriter(
new File(pkgDir, "Lexer.java")));
}
catch(IOException e)
{
throw new RuntimeException("Unable to create " + new File(pkgDir, "Lexer.java").getAbsolutePath());
}
try
{
String startState = "INITIAL";
if(ids.stateList.size() > 0)
{
startState = (String) ids.stateList.getFirst();
}
macros.apply(file, "LexerHeader", new String[] {pkgName,
ids.pkgName.equals("") ? "node" : ids.pkgName + ".node",
startState});
for(ListIterator i = ids.tokenList.listIterator(); i.hasNext();)
{
String name = (String) i.next();
Node node = (Node) ids.tokens.get(name);
boolean fixed = ((Boolean) ids.fixedTokens.get(node))
.booleanValue();
if(fixed)
{
macros.apply(file, "LexerFixedToken",
new String[] {"" + i.previousIndex(), name});
}
else
{
macros.apply(file, "LexerVariableToken",
new String[] {"" + i.previousIndex(), name});
}
Map map = (Map) transitions.tokenStates.get(node);
if(map.size() > 0)
{
macros.apply(file, "TokenSwitchHeader", null);
for(Iterator j = map.entrySet().iterator(); j.hasNext();)
{
Map.Entry entry = (Map.Entry) j.next();
macros.apply(file, "TokenCase",
new String[] {ids.stateList.indexOf((String) entry.getKey()) + "",
(String) entry.getValue()});
}
macros.apply(file, "TokenSwitchTail", null);
}
macros.apply(file, "LexerTokenTail", null);
}
macros.apply(file, "LexerBody1");
for(ListIterator i = ids.tokenList.listIterator(); i.hasNext();)
{
String name = (String) i.next();
Node node = (Node) ids.tokens.get(name);
boolean fixed = ((Boolean) ids.fixedTokens.get(node))
.booleanValue();
if(fixed)
{
macros.apply(file, "LexerNewFixedToken",
new String[] {"" + i.previousIndex(), name});
}
else
{
macros.apply(file, "LexerNewVariableToken",
new String[] {"" + i.previousIndex(), name});
}
}
macros.apply(file, "LexerBody2");
DataOutputStream out = new DataOutputStream(
new BufferedOutputStream(
new FileOutputStream(
new File(pkgDir, "lexer.dat"))));
out.writeInt(acceptStatesArray.length);
for(int accSt = 0; accSt < acceptStatesArray.length; accSt++)
{
DFA dfa = acceptStatesArray[accSt].dfa;
file.write(" { // " + acceptStatesArray[accSt].stateName + System.getProperty("line.separator"));
Vector outerArray = new Vector();
for(int i = 0; i < dfa.states.size(); i++)
{
Vector innerArray = new Vector();
DFA.State state = (DFA.State) dfa.states.elementAt(i);
file.write(" {");
for(int j = 0; j < state.transitions.size(); j++)
{
DFA.Transition transition =
(DFA.Transition) state.transitions.elementAt(j);
file.write("{" + ((int) transition.interval().start) + ", " +
((int) transition.interval().end) + ", " +
transition.destination + "}, ");
innerArray.addElement(new int[] {
((int) transition.interval().start),
((int) transition.interval().end),
transition.destination});
}
file.write("}," + System.getProperty("line.separator"));
outerArray.addElement(innerArray);
}
file.write(" }" + System.getProperty("line.separator"));
out.writeInt(outerArray.size());
for(Enumeration e = outerArray.elements(); e.hasMoreElements();)
{
Vector innerArray = (Vector) e.nextElement();
out.writeInt(innerArray.size());
for(Enumeration n = innerArray.elements(); n.hasMoreElements();)
{
int[] array = (int[]) n.nextElement();
for(int i = 0; i < 3; i++)
{
out.writeInt(array[i]);
}
}
}
}
macros.apply(file, "LexerAcceptHeader");
final int stateNumber = acceptStatesArray.length;
Vector outerArray = new Vector();
for(int i = 0; i < stateNumber; i++)
{
DFA dfa = acceptStatesArray[i].dfa;
Vector innerArray = new Vector();
file.write(" // " + acceptStatesArray[i].stateName + System.getProperty("line.separator"));
file.write(" {");
for(int j = 0; j < dfa.states.size(); j++)
{
DFA.State state = (DFA.State) dfa.states.elementAt(j);
file.write(state.accept + ", ");
innerArray.addElement(new Integer(state.accept));
}
file.write("}," + System.getProperty("line.separator"));
outerArray.addElement(innerArray);
}
out.writeInt(outerArray.size());
for(Enumeration e = outerArray.elements(); e.hasMoreElements();)
{
Vector innerArray = (Vector) e.nextElement();
out.writeInt(innerArray.size());
for(Enumeration n = innerArray.elements(); n.hasMoreElements();)
{
Integer i = (Integer) n.nextElement();
out.writeInt(i.intValue());
}
}
out.close();
file.write(System.getProperty("line.separator"));
macros.apply(file, "LexerAcceptTail");
macros.apply(file, "LexerStateHeader");
if(ids.stateList.size() > 0)
{
for(ListIterator i = ids.stateList.listIterator(); i.hasNext();)
{
String s = (String) i.next();
macros.apply(file, "LexerStateBody",
new String[] {s, "" + i.previousIndex()});
}
}
else
{
macros.apply(file, "LexerStateBody",
new String[] {"INITIAL", "" + 0});
}
macros.apply(file, "LexerStateTail");
macros.apply(file, "LexerTail");
}
catch(IOException e)
{
throw new RuntimeException("An error occured while writing to " +
new File(pkgDir, "Lexer.java").getAbsolutePath());
}
try
{
file.close();
}
catch(IOException e)
{}
}
}
This diff is collapsed.
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* This file is part of SableCC. *
* See the file "LICENSE" for copyright information and the *
* terms and conditions for copying, distribution and *
* modification of SableCC. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
package org.sablecc.sablecc;
import org.sablecc.sablecc.analysis.*;
import org.sablecc.sablecc.node.*;
import java.util.*;
import java.io.*;
public class GenProds extends DepthFirstAdapter
{
private MacroExpander macros;
private ResolveAstIds ast_ids;
private File pkgDir;
private String pkgName;
private Map hiddenProds = new TypedTreeMap(
StringComparator.instance,
StringCast.instance,
NodeCast.instance);
public GenProds(ResolveAstIds ast_ids)
{
this.ast_ids = ast_ids;
try
{
macros = new MacroExpander(
new InputStreamReader(
getClass().getResourceAsStream("productions.txt")));
}
catch(IOException e)
{
throw new RuntimeException("unable to open productions.txt.");
}
pkgDir = new File(ast_ids.astIds.pkgDir, "node");
pkgName = ast_ids.astIds.pkgName.equals("") ? "node" : ast_ids.astIds.pkgName + ".node";
if(!pkgDir.exists())
{
if(!pkgDir.mkdir())
{
throw new RuntimeException("Unable to create " + pkgDir.getAbsolutePath());
}
}
}
public void inAAstProd(AAstProd node)
{
String name = (String) ast_ids.ast_names.get(node);
createProduction(name);
}
private void createProduction(String name)
{
BufferedWriter file;
try
{
file = new BufferedWriter(
new FileWriter(
new File(pkgDir, name + ".java")));
}
catch(IOException e)
{
throw new RuntimeException("Unable to create " + new File(pkgDir, name + ".java").getAbsolutePath());
}
try
{
macros.apply(file, "Production", new String[] {pkgName, name});
}
catch(IOException e)
{
throw new RuntimeException("An error occured while writing to " +
new File(pkgDir, name + ".java").getAbsolutePath());
}
try
{
file.close();
}
catch(IOException e)
{}
}
private void createAlternative(String name, String macro, String[] arg)
{
BufferedWriter file;
try
{
file = new BufferedWriter(
new FileWriter(
new File(pkgDir, name + ".java")));
}
catch(IOException e)
{
throw new RuntimeException("Unable to create " + new File(pkgDir, name + ".java").getAbsolutePath());
}
try
{
macros.apply(file, macro, arg);
}
catch(IOException e)
{
throw new RuntimeException("An error occured while writing to " +
new File(pkgDir, name + ".java").getAbsolutePath());
}
try
{
file.close();
}
catch(IOException e)
{}
}
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* This file is part of SableCC. *
* See the file "LICENSE" for copyright information and the *
* terms and conditions for copying, distribution and *
* modification of SableCC. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
package org.sablecc.sablecc;
import org.sablecc.sablecc.analysis.*;
import org.sablecc.sablecc.node.*;
import java.io.*;
import java.util.LinkedList;
public class GenTokens extends DepthFirstAdapter
{
private MacroExpander macros;
private ResolveIds ids;
private File pkgDir;
private String pkgName;
// final GenTokens instance = this;
String text;
public GenTokens(ResolveIds ids)
{
this.ids = ids;
try
{
macros = new MacroExpander(
new InputStreamReader(
getClass().getResourceAsStream("tokens.txt")));
}
catch(IOException e)
{
throw new RuntimeException("unable to open tokens.txt.");
}
pkgDir = new File(ids.pkgDir, "node");
pkgName = ids.pkgName.equals("") ? "node" : ids.pkgName + ".node";
if(!pkgDir.exists())
{
if(!pkgDir.mkdir())
{
throw new RuntimeException("Unable to create " + pkgDir.getAbsolutePath());
}
}
}
public void inATokenDef(ATokenDef node)
{
String name = (String) ids.names.get(node);
BufferedWriter file;
try
{
file = new BufferedWriter(
new FileWriter(
new File(pkgDir, name + ".java")));
}
catch(IOException e)
{
throw new RuntimeException("Unable to create " + new File(pkgDir, name + ".java").getAbsolutePath());
}
text = null;
ARegExp regExp = (ARegExp) node.getRegExp();
LinkedList concats = regExp.getConcats();
if(concats.size() == 1)
{
AConcat concat = (AConcat)concats.getFirst();
LinkedList unExps = concat.getUnExps();
if(unExps.size() == 1)
{
AUnExp unExp = (AUnExp) unExps.getFirst();
PBasic basic = unExp.getBasic();
if((basic instanceof AStringBasic) &&
(unExp.getUnOp() == null))
{
text = ((AStringBasic) basic).getString().getText();
text = text.substring(1, text.length() - 1);
}
else if((basic instanceof ACharBasic) &&
(unExp.getUnOp() == null))
{
PChar pChar = ((ACharBasic) basic).getChar();
if(pChar instanceof ACharChar)
{
text = ((ACharChar) pChar).getChar().getText();
text = text.substring(1, text.length() - 1);
}
}
}
}
try
{
if(text == null)
{
ids.fixedTokens.put(node, new Boolean(false));
macros.apply(file, "VariableTextToken", new String[] { pkgName,
ids.pkgName.equals("") ? "analysis" : ids.pkgName + ".analysis",
name});
}
else
{
ids.fixedTokens.put(node, new Boolean(true));
macros.apply(file, "FixedTextToken", new String[] { pkgName,
ids.pkgName.equals("") ? "analysis" : ids.pkgName + ".analysis",
name, processText(text)});
ids.errorNames.put(node, "'" + text + "'");
}
}
catch(IOException e)
{
throw new RuntimeException("An error occured while writing to " +
new File(pkgDir, name + ".java").getAbsolutePath());
}
try
{
file.close();
}
catch(IOException e)
{}
}
private String processText(String s)
{
StringBuffer result = new StringBuffer();
for(int i = 0; i < s.length(); i++)
{
char c = s.charAt(i);
switch(c)
{
case '\b':
{
result.append("\\t");
break;
}
case '\t':
{
result.append("\\t");
break;
}
case '\n':
{
result.append("\\n");
break;
}
case '\f':
{
result.append("\\f");
break;
}
case '\r':
{
result.append("\\r");
break;
}
case '\"':
{
result.append("\\\"");
break;
}
case '\'':
{
result.append("\\\'");
break;
}
case '\\':
{
result.append("\\\\");
break;
}
default:
{
result.append(c);
}
}
}
return result.toString();
}
}
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment