Git Product home page Git Product logo

skyfire's Introduction

skyfire

  1. to generate Lexer, Parser, Visitor automatically

add antlr-4.7-complete.jar to classpath

xml:

java org.antlr.v4.Tool -o E:\xml\ -visitor -no-listener -Dlanguage=Java E:\xml\XMLLexer.g4 java org.antlr.v4.Tool -o E:\xml\ -visitor -no-listener -Dlanguage=Java E:\xml\XMLParser.g4

javascript:

java org.antlr.v4.Tool -o E:\jsgrammar\ -visitor -no-listener -Dlanguage=Java E:\jsgrammar\JavaScriptLexer.g4 java org.antlr.v4.Tool -o E:\jsgrammar\ -visitor -no-listener -Dlanguage=Java E:\jsgrammar\JavaScriptParser.g4

  1. install MySQL

download MySQL community version: mysql-installer-community-5.7.21.0.msi double click and install create new database pcsg create new table pcsg

xml

create table xmlpcsg ( id int auto_increment, parent varchar(100) not null, context varchar(500) not null, rule varchar(4000) not null, prob float not null, primary key(id) );

javascript

create table jspcsg ( id int auto_increment, parent varchar(100) not null, context varchar(500) not null, rule varchar(4000) not null, prob float not null, primary key(id) );

download mysql-connector-java-5.1.46.tar.gz from http://dev.mysql.com/downloads/connector/j/ and unzip it add mysql-connector-java-5.1.46-bin.jar to build path

3.change Visitor

change the original visitor function to extract context and rule information

public T visitDocument(XMLParser.DocumentContext ctx) {
	return visitChildren(ctx);
}

xml:

public T visitDocument(XMLParser.DocumentContext ctx) {
	String parent = "null", grandparent = "null", greatparent = "null", sibling = "null";
	if (ctx.getParent() != null) {
		parent = ctx.getParent().getClass().getSimpleName();
		if (ctx.getParent().getParent() != null) {
			grandparent = ctx.getParent().getParent().getClass().getSimpleName();
			if (ctx.getParent().getParent().getParent() != null) {
				greatparent = ctx.getParent().getParent().getParent().getClass().getSimpleName();
			}
		}
		if (ctx.getParent().getChild(0) != null) {
			// sibling = ctx.getParent().getChild(0).getClass().getSimpleName();
		}
	}
	String rule = "<" + greatparent + "," + grandparent + "," + parent + "," + sibling + ">";
	rule += ctx.getClass().getSimpleName() + "->";
	if (ctx.getChildCount() > 0 && ctx.getChildCount() < PCSGLearner.maxChildCount) {
		for (int i = 0; i < ctx.children.size(); i++) {
			if (ctx.getChild(i).getClass().getSimpleName().equals("TerminalNodeImpl")) {
				rule += ctx.getChild(i).getText().length() > PCSGLearner.maxChildLength
						? ctx.getChild(i).getText().substring(0, PCSGLearner.maxChildLength).trim()
						: ctx.getChild(i).getText().trim();
			} else {
				rule += "@@@@@" + ctx.getChild(i).getClass().getSimpleName() + "#####";
			}
		}
	} else {
		return visitChildren(ctx);
	}
	// System.out.println(rule);
	PCSGLearner.updateParentCount(ctx.getClass().getSimpleName());
	PCSGLearner.updateRuleCount(rule);
	return visitChildren(ctx);

}

javascript: pay special attention to ErrorNodeImpl.

public T visitDocument(XMLParser.DocumentContext ctx) {
	String parent = "null", grandparent = "null", greatparent = "null", sibling = "null";
	if (ctx.getParent() != null) {
		parent = ctx.getParent().getClass().getSimpleName();
		if (ctx.getParent().getParent() != null) {
			grandparent = ctx.getParent().getParent().getClass().getSimpleName();
			if (ctx.getParent().getParent().getParent() != null) {
				greatparent = ctx.getParent().getParent().getParent().getClass().getSimpleName();
			}
		}
		if (ctx.getParent().getChild(0) != null) {
			// sibling = ctx.getParent().getChild(0).getClass().getSimpleName();
		}
	}
	String rule = "<" + greatparent + "," + grandparent + "," + parent + "," + sibling + ">";
	rule += ctx.getClass().getSimpleName() + "->";
	if (ctx.getChildCount() > 0 && ctx.getChildCount() < JSPCSGLearner.maxChildCount) {
		for (int i = 0; i < ctx.children.size(); i++) {
			if (ctx.getChild(i).getClass().getSimpleName().equals("TerminalNodeImpl")) {
				rule += ctx.getChild(i).getText().length() > JSPCSGLearner.maxChildLength
						? ctx.getChild(i).getText().substring(0, JSPCSGLearner.maxChildLength).trim()
						: ctx.getChild(i).getText().trim();
			} else if (!ctx.getChild(i).getClass().getSimpleName().equals("ErrorNodeImpl")){
				rule += "@@@@@" + ctx.getChild(i).getClass().getSimpleName() + "#####";
			}
		}
	} else {
		return visitChildren(ctx);
	}
	// System.out.println(rule);
	JSPCSGLearner.updateParentCount(ctx.getClass().getSimpleName());
	JSPCSGLearner.updateRuleCount(rule);
	return visitChildren(ctx);

} 4. first run PCSGLearner class to learn from samples

some optimizations for XML: ChardataContext and MiscContext nodes normally contain a string whose value is useless to grammar. Therefore, we omit those nodes to get a much smaller PCSG. The non-terminal child of ContentContext contains comment and we omit them to reduce size of PCSG.

  1. run Generator class to generate samples

Several parameters affect the seed and complexity of generation and users can change them as their needs.

static int maxDerivation = 50;			// the max number of derivation
static int maxDerivationDepth = 8;		// the max iteration depth of derivation
int numOfSamplesToGenerate = 10; 		// the number of seeds to generate
String outputPath = "E:\\xml_gen\\";	//the path to save generated seeds

skyfire's People

Contributors

zhunki avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

skyfire's Issues

求教

大佬,这个javascript: pay special attention to ErrorNodeImpl.地方怎么改啊,可以说明一下吗。

how can get "&#x002A; &pt456; &#x2502; &#x03AF; &#x397;" in your generated xml seed 20.xml

in function visitChardata , your program do not get the chardata's content,how can get chardata such as "* &pt456; │ ί Η" in your generated xml seed 20.xml

visitChardata (XMLParser.ChardataContext ctx) {
String parent = "null", grandparent = "null", greatparent = "null", sibling = "null";
if (ctx.getParent() != null) {
parent = ctx.getParent().getClass().getSimpleName();
if (ctx.getParent().getParent() != null) {
grandparent = ctx.getParent().getParent().getClass().getSimpleName();
if (ctx.getParent().getParent().getParent() != null) {
greatparent = ctx.getParent().getParent().getParent().getClass().getSimpleName();
}
}
if (ctx.getParent().getChild(0) != null) {
// sibling = ctx.getParent().getChild(0).getClass().getSimpleName();
}
}
String rule = "<" + greatparent + "," + grandparent + "," + parent + "," + sibling + ">";
rule += ctx.getClass().getSimpleName() + "->";
if (ctx.getChildCount() > 0 && ctx.getChildCount() < XMLPCSGLearner.maxChildCount) {
for (int i = 0; i < ctx.children.size(); i++) {
if (ctx.getChild(i).getClass().getSimpleName().equals("TerminalNodeImpl")) {
rule += " ";
} else {
rule += "@@@@@" + ctx.getChild(i).getClass().getSimpleName() + "#####";
}
}
} else {
return visitChildren(ctx);
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.