use of br.ufpe.cin.if688.infpos.v2.parser.Parser in project if688.github.io by if688.
the class App method main.
public static void main(String[] args) throws NotLL1Exception, ErrorOnParseException {
/*
* Gramática de exemplo:
* A -> aB
* B -> cC
* C -> d
*
*/
Nonterminal start = new Nonterminal("A");
Nonterminal B = new Nonterminal("B");
Nonterminal C = new Nonterminal("C");
Terminal a = new Terminal("a");
Terminal c = new Terminal("c");
Terminal d = new Terminal("d");
List<GeneralSymbol> prodA = new ArrayList<GeneralSymbol>();
prodA.add(a);
prodA.add(B);
List<GeneralSymbol> prodB = new ArrayList<GeneralSymbol>();
prodB.add(c);
prodB.add(C);
List<GeneralSymbol> prodC = new ArrayList<GeneralSymbol>();
prodC.add(d);
Production fpA = new Production(start, prodA);
Production pB = new Production(B, prodB);
Production pC = new Production(C, prodC);
Collection<Production> col = new ArrayList<Production>();
col.add(fpA);
col.add(pB);
col.add(pC);
List<Terminal> example = new ArrayList<Terminal>();
example.add(a);
example.add(c);
example.add(d);
Grammar g = new Grammar(col, start);
Map<Nonterminal, Set<GeneralSymbol>> first = SetGenerator.getFirst(g);
Map<Nonterminal, Set<GeneralSymbol>> follow = SetGenerator.getFollow(g, first);
Map<LL1Key, List<GeneralSymbol>> table = Table.createTable(g);
Parser parser = ParserGenerator.createParser(g);
ParseTree parseTree = ParserUtils.parseSequence(parser, example);
System.out.println("Exemplo 1:\n" + "A -> aB\n" + "B -> cC\n" + "C -> d");
System.out.println("Conjunto first: " + first.toString());
System.out.println("Conjunto follow: " + follow.toString());
System.out.println("Tabela de parsing: " + table.toString());
System.out.println("Exemplo de parsing: " + parseTree.toString() + "\n");
}
use of br.ufpe.cin.if688.infpos.v2.parser.Parser in project if688.github.io by if688.
the class PosFixa method main.
public static void main(String[] args) throws IOException {
String expression = readFile(file, StandardCharsets.UTF_8);
Lexer lexer = new Lexer(expression);
// System.out.println(lexer.tokens());
Parser parse = new Parser(lexer);
parse.expr();
System.out.write('\n');
/**/
}
use of br.ufpe.cin.if688.infpos.v2.parser.Parser in project syncope by apache.
the class AbstractAnyDAO method getWhereClause.
/**
* Generate one where clause for each different attribute schema into the derived schema expression provided.
*
* @param expression derived schema expression
* @param value derived attribute value
* @param attrUtils USER / GROUP
* @return where clauses to use to build the query
*/
private Set<String> getWhereClause(final String expression, final String value) {
Parser parser = new Parser(new StringReader(expression));
// Schema names
List<String> identifiers = new ArrayList<>();
// Literals
List<String> literals = new ArrayList<>();
// Get schema names and literals
for (Token token = parser.getNextToken(); token != null && StringUtils.isNotBlank(token.toString()); token = parser.getNextToken()) {
if (token.kind == ParserConstants.STRING_LITERAL) {
literals.add(token.toString().substring(1, token.toString().length() - 1));
}
if (token.kind == ParserConstants.IDENTIFIER) {
identifiers.add(token.toString());
}
}
// Sort literals in order to process later literals included into others
Collections.sort(literals, (final String t, final String t1) -> {
if (t == null && t1 == null) {
return 0;
} else if (t != null && t1 == null) {
return -1;
} else if (t == null && t1 != null) {
return 1;
} else if (t.length() == t1.length()) {
return 0;
} else if (t.length() > t1.length()) {
return -1;
} else {
return 1;
}
});
// Split value on provided literals
List<String> attrValues = split(value, literals);
if (attrValues.size() != identifiers.size()) {
LOG.error("Ambiguous JEXL expression resolution: literals and values have different size");
return Collections.emptySet();
}
// clauses to be used with INTERSECTed queries
Set<String> clauses = new HashSet<>();
// builder to build the clauses
StringBuilder bld = new StringBuilder();
// Contains used identifiers in order to avoid replications
Set<String> used = new HashSet<>();
// Create several clauses: one for eanch identifiers
for (int i = 0; i < identifiers.size(); i++) {
if (!used.contains(identifiers.get(i))) {
// verify schema existence and get schema type
PlainSchema schema = plainSchemaDAO().find(identifiers.get(i));
if (schema == null) {
LOG.error("Invalid schema '{}', ignoring", identifiers.get(i));
} else {
// clear builder
bld.delete(0, bld.length());
bld.append("(");
// set schema name
bld.append("s.id = '").append(identifiers.get(i)).append("'");
bld.append(" AND ");
bld.append("s.id = a.schema_id").append(" AND ");
bld.append("a.id = v.attribute_id");
bld.append(" AND ");
// use a value clause different for eanch different schema type
switch(schema.getType()) {
case Boolean:
bld.append("v.booleanValue = '").append(attrValues.get(i)).append("'");
break;
case Long:
bld.append("v.longValue = ").append(attrValues.get(i));
break;
case Double:
bld.append("v.doubleValue = ").append(attrValues.get(i));
break;
case Date:
bld.append("v.dateValue = '").append(attrValues.get(i)).append("'");
break;
default:
bld.append("v.stringValue = '").append(attrValues.get(i)).append("'");
}
bld.append(")");
used.add(identifiers.get(i));
clauses.add(bld.toString());
}
}
}
LOG.debug("Generated where clauses {}", clauses);
return clauses;
}
use of br.ufpe.cin.if688.infpos.v2.parser.Parser in project jetty.project by eclipse.
the class WindowUpdateGenerateParseTest method testGenerateParseOneByteAtATime.
@Test
public void testGenerateParseOneByteAtATime() throws Exception {
WindowUpdateGenerator generator = new WindowUpdateGenerator(new HeaderGenerator());
final List<WindowUpdateFrame> frames = new ArrayList<>();
Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() {
@Override
public void onWindowUpdate(WindowUpdateFrame frame) {
frames.add(frame);
}
}, 4096, 8192);
int streamId = 13;
int windowUpdate = 17;
// Iterate a few times to be sure generator and parser are properly reset.
for (int i = 0; i < 2; ++i) {
ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
generator.generateWindowUpdate(lease, streamId, windowUpdate);
frames.clear();
for (ByteBuffer buffer : lease.getByteBuffers()) {
while (buffer.hasRemaining()) {
parser.parse(ByteBuffer.wrap(new byte[] { buffer.get() }));
}
}
Assert.assertEquals(1, frames.size());
WindowUpdateFrame frame = frames.get(0);
Assert.assertEquals(streamId, frame.getStreamId());
Assert.assertEquals(windowUpdate, frame.getWindowDelta());
}
}
use of br.ufpe.cin.if688.infpos.v2.parser.Parser in project jetty.project by eclipse.
the class WindowUpdateGenerateParseTest method testGenerateParse.
@Test
public void testGenerateParse() throws Exception {
WindowUpdateGenerator generator = new WindowUpdateGenerator(new HeaderGenerator());
final List<WindowUpdateFrame> frames = new ArrayList<>();
Parser parser = new Parser(byteBufferPool, new Parser.Listener.Adapter() {
@Override
public void onWindowUpdate(WindowUpdateFrame frame) {
frames.add(frame);
}
}, 4096, 8192);
int streamId = 13;
int windowUpdate = 17;
// Iterate a few times to be sure generator and parser are properly reset.
for (int i = 0; i < 2; ++i) {
ByteBufferPool.Lease lease = new ByteBufferPool.Lease(byteBufferPool);
generator.generateWindowUpdate(lease, streamId, windowUpdate);
frames.clear();
for (ByteBuffer buffer : lease.getByteBuffers()) {
while (buffer.hasRemaining()) {
parser.parse(buffer);
}
}
}
Assert.assertEquals(1, frames.size());
WindowUpdateFrame frame = frames.get(0);
Assert.assertEquals(streamId, frame.getStreamId());
Assert.assertEquals(windowUpdate, frame.getWindowDelta());
}
Aggregations