public static void main(String[] args) throws IOException { PGF gr = null; try { gr = PGF.readPGF( "/home/krasimir/www.grammaticalframework.org/examples/phrasebook/Phrasebook.pgf"); } catch (FileNotFoundException e) { e.printStackTrace(); return; } catch (PGFError e) { e.printStackTrace(); return; } Type typ = gr.getFunctionType("Bulgarian"); System.out.println(typ.getCategory()); System.out.println(gr.getAbstractName()); for (Map.Entry<String, Concr> entry : gr.getLanguages().entrySet()) { System.out.println( entry.getKey() + " " + entry.getValue() + " " + entry.getValue().getName()); } Concr eng = gr.getLanguages().get("SimpleEng"); try { for (ExprProb ep : eng.parse(gr.getStartCat(), "persons who work with Malmö")) { System.out.println("[" + ep.getProb() + "] " + ep.getExpr()); } } catch (ParseError e) { System.out.println("Parsing failed at token \"" + e.getToken() + "\""); } }
public static void main(String[] args) { while (true) { System.out.println("\n\nEnter an expression, or press return to end."); System.out.print("\n? "); TextIO.skipBlanks(); if (TextIO.peek() == '\n') break; try { ExpNode exp = expressionTree(); TextIO.skipBlanks(); if (TextIO.peek() != '\n') throw new ParseError("Extra data after end of expression."); TextIO.getln(); ExpNode deriv = exp.derivative(); System.out.println("\nA fully parenthesized expression for the derivative is:"); System.out.print(" "); deriv.printInfix(); System.out.println(); System.out.println("\nValue of derivative at x = 0 is " + deriv.value(0)); System.out.println("Value of derivative at x = 1 is " + deriv.value(1)); System.out.println("Value of derivative at x = 2 is " + deriv.value(2)); System.out.println("Value of derivative at x = 3 is " + deriv.value(3)); System.out.println("\nOrder of postfix evaluation for derivative is:\n"); deriv.printStackCommands(); } catch (ParseError e) { System.out.println("\n*** Error in input: " + e.getMessage()); System.out.println("*** Discarding input: " + TextIO.getln()); } } System.out.println("\n\nDone."); } // end main()
private boolean event(String event, int line) { try { machine().event(event, line); return true; } catch (ParseError e) { if (throwOnError) { throw e; } else { int l = lineOffset + line; listener.syntaxError(e.getState(), event, e.getLegalEvents(), featureURI, l); return false; } } }
/** * Process the user's command and execute it accordingly * * @param command: string of command keyed in by user * @return message to be displayed */ public ProcessedObject parseInput(String stringInput) { assert (stringInput != null); ProcessedObject processed = null; String command = getCommand(stringInput); switch (command) { // don't need to check date, just get "task" case "view": processed = parseView.processView(stringInput); break; case "del": processed = parseDelete.processDelete(stringInput); break; case "setdir": processed = parseDir.processLoc(stringInput); break; case "save": processed = parseSave.processSave(command); break; case "clear": processed = parseClear.processClear(command); break; // need to check date: case "add": processed = parseAdd.processAdd(command, stringInput); break; case "set": processed = parseEdit.processSet(stringInput); break; case "search": processed = parseSearch.processSearch(command, stringInput); break; case "undo": processed = parseUndo.processUndo(command); break; case "done": processed = parseDone.processDone(stringInput); break; default: // error goes here processed = parseError.processError(String.format(ParserConstants.ERROR_COMMAND, command)); break; } return processed; }
public ParseException(ParseError error) { super(error.getErrorLocation() + ": " + error.getErrorMessage()); _error = error; }
public SourceFiles parse( final JNAeratorConfig config, TypeConversion typeConverter, MacroUseCallback macrosDependenciesOut) throws IOException, LexerException { SourceFiles sourceFiles = new SourceFiles(); StringBuilder syntheticTypeDefsBuilder = new StringBuilder(); for (Map.Entry<String, String> e : config.preprocessorConfig.forcedTypeDefs.entrySet()) { syntheticTypeDefsBuilder .append("typedef ") .append(e.getValue()) .append(" ") .append(e.getKey()) .append(";"); } config.preprocessorConfig.includeStrings.add(0, syntheticTypeDefsBuilder.toString()); String sourceContent = PreprocessorUtils.preprocessSources( config, sourceFiles.defines, config.verbose, typeConverter, macrosDependenciesOut); if (config.removeInlineAsm) { sourceContent = removeInlineAsm(sourceContent); } ExecutorService executor = Executors.newSingleThreadExecutor(); try { final Set<String> topLevelTypeDefs = Collections.synchronizedSet(config.preprocessorConfig.forcedTypeDefs.keySet()); if (!config.parseInChunks) { Future<SourceFile> fut = executor.submit( createParsingCallable( config, typeConverter, sourceContent, topLevelTypeDefs, true)); try { sourceFiles.add(fut.get(config.fullParsingTimeout, TimeUnit.MILLISECONDS)); return removeTypeDefsConflictingWithForcedTypeDefs(sourceFiles, topLevelTypeDefs); } catch (Throwable ex) { ex.printStackTrace(); System.err.println("Parsing failed : " + ex); fut.cancel(true); // Thread.sleep(200); } System.gc(); System.err.println( "Regular parsing took too long, now trying to parse sources slice by slice."); } // compartimented parsing (at each change of file) List<Slice> slices = cutSourceContentInSlices(sourceContent, System.out); if (config.verbose) { System.out.println("Now parsing " + slices.size() + " slices"); } boolean firstFailure = true; for (Slice slice : slices) { try { sourceFiles.add( executor .submit( createParsingCallable( config, typeConverter, slice.text, topLevelTypeDefs, false)) .get(config.sliceParsingTimeout, TimeUnit.MILLISECONDS)); } catch (Throwable ex) { if (firstFailure) { WriteText.writeText(slice.text, new File("splitParsing.firstFailure.source.txt")); if (ex.getCause() instanceof ParseError) { ParseError pe = (ParseError) ex.getCause(); WriteText.writeText(pe.getErrors(), new File("splitParsing.firstFailure.errors.txt")); // ex.printStackTrace(); } firstFailure = false; } System.gc(); ex.printStackTrace(); System.err.println("Parsing failed : " + ex); } } return removeTypeDefsConflictingWithForcedTypeDefs(sourceFiles, topLevelTypeDefs); } finally { executor.shutdown(); } }