Beispiel #1
0
  public static void main(String[] args) throws Exception {

    CharStream in = new ANTLRFileStream(args[0]);
    MicroLexer lexer = new MicroLexer(in);
    CommonTokenStream tks = new CommonTokenStream(lexer);
    MicroParser psr = new MicroParser(tks);
    ParseTreeWalker walker = new ParseTreeWalker();
    ExtractionListener extractor = new ExtractionListener(psr);
    psr.setErrorHandler(new BailErrorStrategy());
    ParseTree t;

    try {
      t = psr.program();
    } catch (Exception fpe) {
      fpe.printStackTrace();
      System.out.println("Not accepted");
      return;
    }

    walker.walk(extractor, t);
    TinyGenerator asmgen = new TinyGenerator(extractor.getIRList());

    // Utils.printSymbolTable(extractor.getRootSymbolTable());

    Utils.printIR(extractor.getIRList());
    asmgen.printAll();
  }
Beispiel #2
0
  @Test
  public void sampleTest() {
    String input =
        "C,, D,, E,, F,, | G,, A,, B,, C, | "
            + "C, D, E, F, | G, A, B, C | "
            + "C D E F | G A B c "
            + "| c d e f | g a b c' | "
            + "c' d' e' f' | g' a' b' c''\n";

    CharStream stream = new ANTLRInputStream(input);
    MusicGrammarLexer lexer = new MusicGrammarLexer(stream);
    lexer.reportErrorsAsExceptions();
    TokenStream tokens = new CommonTokenStream(lexer);
    MusicGrammarParser parser = new MusicGrammarParser(tokens);
    parser.reportErrorsAsExceptions();
    ParseTree tree = parser.root();
    ParseTreeWalker walker = new ParseTreeWalker();
    ConstructMusic listener = new ConstructMusic(new HeaderInfo());
    walker.walk(listener, tree);
    Music sample = listener.getExpression();
    try {
      SequencePlayer player = new SequencePlayer(140, 12);
      sample.play(player, 0);
      player.play();
      Thread.sleep((long) sample.duration() * 1000);
    } catch (MidiUnavailableException | InvalidMidiDataException e) {
      e.printStackTrace();
    } catch (InterruptedException uie) {
      uie.printStackTrace();
    }
  }
Beispiel #3
0
  @Test
  public void sampleTest2() {
    String input =
        "d^f^f ^cee|de^f g^fe|d^f^f ^cee|d^fe dBA|\n"
            + "d^f^f ^cee|de^f g^fe|^fa^f g^fe|[1 d^fe dBA:|[2 d^fe d^cB|]\n"
            + "A3 B3|g^fe ^fdB|A^FA B2^c|d^fe d^cB|\n"
            + "A3 B3|e^fe e^fg|^fa^f g^fe|[1 d^fe d^cB:|[2 d^fe dBA|]\n"
            + "^fAA eAA| de^f g^fe|^fAA eAA|d^fe dBA|\n"
            + "^fAA eAA| de^f g^fe|^fa^f g^fe|d^fe dBA:|\n";

    CharStream stream = new ANTLRInputStream(RepeatHandler.handleRepeats(input));
    MusicGrammarLexer lexer = new MusicGrammarLexer(stream);
    lexer.reportErrorsAsExceptions();
    TokenStream tokens = new CommonTokenStream(lexer);
    MusicGrammarParser parser = new MusicGrammarParser(tokens);
    parser.reportErrorsAsExceptions();
    ParseTree tree = parser.root();
    ParseTreeWalker walker = new ParseTreeWalker();
    ConstructMusic listener = new ConstructMusic(new HeaderInfo());
    walker.walk(listener, tree);
    Music sample = listener.getExpression();
    try {
      SequencePlayer player = new SequencePlayer(200, 12);
      sample.play(player, 0);
      player.play();
      Thread.sleep((long) sample.duration() * 1000);
    } catch (MidiUnavailableException | InvalidMidiDataException e) {
      e.printStackTrace();
    } catch (InterruptedException uie) {
      uie.printStackTrace();
    }
  }
Beispiel #4
0
 public static void main(String[] args) throws IOException {
   if (args.length < 2) {
     usage();
   }
   boolean useTimeoutChecker = false;
   for (int i = 2; i < args.length; ++i) {
     String o = args[i];
     if (o.equals("-timeout")) {
       useTimeoutChecker = true;
     } else {
       usage();
     }
   }
   final Map<Integer, String> incOfMethod;
   final List<String> counters;
   try (FileInputStream fis = new FileInputStream(args[0])) {
     ANTLRInputStream input = new ANTLRInputStream(fis);
     Java7Lexer lexer = new Java7Lexer(input);
     CommonTokenStream tokens = new CommonTokenStream(lexer);
     Java7Parser parser = new Java7Parser(tokens);
     ParseTree tree = parser.compilationUnit();
     Java7Profiler listener = new Java7Profiler(tokens, useTimeoutChecker);
     ParseTreeWalker walker = new ParseTreeWalker();
     walker.walk(listener, tree);
     incOfMethod = listener.incOfMethod;
     counters = listener.counters;
   }
   try (BufferedReader input = new BufferedReader(new FileReader(args[0]));
       PrintWriter output = new PrintWriter(args[1])) {
     new Reader(useTimeoutChecker)
         .run(
             incOfMethod, counters, input, output, classNameFrom(args[0]), classNameFrom(args[1]));
   }
 }
 @Override
 protected void parse(File sourceFile) throws ParseException {
   try {
     GrammarConfiguration configuration =
         LanguageBase.createGrammarConfiguration(
             sourceFile, PHPLexer.class, PHPParser.class, new DefaultErrorListener(sourceFile));
     ParseTree tree = ((PHPParser) configuration.parser).htmlDocument();
     ParseTreeWalker walker = new ParseTreeWalker();
     walker.walk(new PHPParseTreeListener(this), tree);
   } catch (Exception e) {
     throw new ParseException(e);
   }
 }
Beispiel #6
0
  int listenerWay() {
    CharStream stream = new ANTLRInputStream(EXPRESSION);

    CalculatorLexer lexer = new CalculatorLexer(stream);
    CommonTokenStream tokenStream = new CommonTokenStream(lexer);
    CalculatorParser parser = new CalculatorParser(tokenStream);

    // System.out.println(parser.c());

    ParseTree tree = parser.c();
    ParseTreeWalker walker = new ParseTreeWalker();
    CalculatorWalker calcWalker = new CalculatorWalker();
    walker.walk(calcWalker, tree);
    return calcWalker.getSum();
  }
Beispiel #7
0
  public static void main(String[] args) throws IOException {
    System.err.close();

    // ANTLRInputStream input = new ANTLRInputStream( new FileInputStream( new File(args[0])) );
    ANTLRInputStream input = new ANTLRInputStream(new InputStreamReader(System.in));

    XsLogLexer lexer = new XsLogLexer(input);

    CommonTokenStream tokens = new CommonTokenStream(lexer);

    XsLogParser parser = new XsLogParser(tokens);

    ParseTree tree = parser.xsLog();

    ParseTreeWalker walker = new ParseTreeWalker();
    walker.walk(new SipPrinterListener(), tree);
  }
  @Test
  public void shouldDetectQueryWithOrderAscClause() {
    InfluxQueryParser parser = parserFactory.newInstanceForQuery("select a from b order asc");
    parseTreeWalker.walk(definitionsParser, parser.selectQuery());
    SelectQueryDefinitions definitions = definitionsParser.getSelectQueryDefinitions();

    assertFalse(definitions.isOrderDesc());
  }
 /** Test of AST Generation of class IntegerComparisonNode. */
 @Test
 public void testAstGeneration() {
   System.out.println("BooleanLiteralNode - testAstGeneration");
   final BooleanLiteralNode literal = new BooleanLiteralNode(true);
   final String inputRuleset = literal.toRulenode();
   System.out.printf("Input Ruleset: %s\n", inputRuleset);
   final ANTLRInputStream inStream = new ANTLRInputStream(inputRuleset);
   final HCleanLexer lexer = new HCleanLexer(inStream);
   final CommonTokenStream tokenStream = new CommonTokenStream(lexer);
   final HCleanParser parser = new HCleanParser(tokenStream);
   final ParseTreeWalker walker = new ParseTreeWalker();
   final RuleListener listener = new RuleListener();
   final ParseTree parseTree = parser.booleanLiteral();
   walker.walk(listener, parseTree);
   final String outputRuleSet = listener.peekStack().toRulenode();
   System.out.printf("Expected: %s\n", inputRuleset);
   System.out.printf("  Result: %s\n", outputRuleSet);
   assertEquals(inputRuleset, outputRuleSet);
 }
Beispiel #10
0
  /**
   * Return a complete RulePacket describing the definition expressed in the given string. Event
   * attributes, even for different event types, with the same name must have the same ValType (INT,
   * FLOAT, BOOL or STRING)
   *
   * @param rule: a String expressing the definition of the rule in TESLA
   * @return a RulePacket
   */
  public static RulePkt parse(String rule, int index) {
    ANTLRInputStream input = new ANTLRInputStream(rule);
    TESLALexer lexer = new TESLALexer(input);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    TESLAParser parser = new TESLAParser(tokens);
    ParseTree t = parser.trex_rule();
    ParseTreeWalker walker = new ParseTreeWalker(); // create standard walker

    TRexRuleParser extractor = new TRexRuleParser();
    walker.walk(extractor, t); // initiate walk of tree with listener
    RulePkt res = extractor.getRule();
    // debug
    Map<String, Integer> evt_ids = extractor.getPredicatesMap();
    System.out.println("### NAMES -> IDS mapping ###\n");
    for (String evtName : evt_ids.keySet()) {
      System.out.println(evtName + " -> " + evt_ids.get(evtName));
    }
    System.out.println("\n### END ###");
    return res;
  }
Beispiel #11
0
  public static void main(String[] args) throws Exception {
    String inputFile = null;
    if (args.length > 0) inputFile = args[0];
    InputStream is = System.in;
    if (inputFile != null) {
      is = new FileInputStream(inputFile);
    }
    ANTLRInputStream input = new ANTLRInputStream(is);
    PropertyFileLexer lexer = new PropertyFileLexer(input);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    PropertyFileParser parser = new PropertyFileParser(tokens);
    ParseTree tree = parser.file();

    // create a standard ANTLR parse tree walker
    ParseTreeWalker walker = new ParseTreeWalker();
    // create listener then feed to walker
    PropertyFileLoader loader = new PropertyFileLoader();
    walker.walk(loader, tree); // walk parse tree
    System.out.println(loader.props); // print results
  }
  @Test
  public void shouldDetectTimespan() {
    InfluxQueryParser parser = parserFactory.newInstanceForQuery(queryText);
    parseTreeWalker.walk(definitionsParser, parser.selectQuery());
    SelectQueryDefinitions definitions = definitionsParser.getSelectQueryDefinitions();
    GroupByClause groupByClause = definitions.getGroupByClause();

    assertEquals("time", groupByClause.getBucketType());
    assertEquals(bucketSize, groupByClause.getBucketSize());
    assertEquals(bucketSizeUnit, groupByClause.getBucketSizeUnit());
  }
Beispiel #13
0
  /**
   * Parst einen Text und erstellt eine Liste mit Eigenschaften
   *
   * @param text - zu parsender Text
   * @return Liste mit Texteigenschaften
   */
  public List<Integer> parseText(File text) {
    CharStream input = null;
    BufferedReader reader;

    try {
      reader = new BufferedReader(new InputStreamReader(new FileInputStream(text)));
      input = new ANTLRInputStream(reader);
    } catch (IOException e) {
      e.printStackTrace();
    }
    SyntaxLexer lexer = new SyntaxLexer(input);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    SyntaxParser parser = new SyntaxParser(tokens);
    ParseTree tree = parser.stat();

    ParseTreeWalker walker = new ParseTreeWalker(); // create standard walker
    List<Integer> list = new LinkedList<>();
    AnalysisListener extractor = new AnalysisListener(list);
    walker.walk(extractor, tree);

    return list;
  }
Beispiel #14
0
  /**
   * Mit dieser Methode werden die Merkmale aus den Texte herausgelesen und in eine CSV-Datei
   * gespeichert.
   *
   * @param texte - Eine Liste von Texten die Analysiert werden sollen.
   * @param name - Name der CSV-Datei.
   */
  private static void texteEinlesen(File[] texte, String name) {
    CharStream input = null;
    BufferedReader reader;
    for (int i = 0; i < texte.length; i++) {
      try {
        reader = new BufferedReader(new InputStreamReader(new FileInputStream(texte[i])));
        input = new ANTLRInputStream(reader);
      } catch (IOException e) {
        e.printStackTrace();
      }
      SyntaxLexer lexer = new SyntaxLexer(input);
      CommonTokenStream tokens = new CommonTokenStream(lexer);
      SyntaxParser parser = new SyntaxParser(tokens);
      ParseTree tree = parser.stat();

      ParseTreeWalker walker = new ParseTreeWalker(); // create standard
      // walker
      List<Integer> list = new LinkedList<>();
      AnalysisListener extractor = new AnalysisListener(list);
      walker.walk(extractor, tree);

      saveToCSV(name, list);
    }
  }
  /**
   * This function is supposed to be called directly only from PydmlSyntacticValidator when it
   * encounters 'import'
   *
   * @param fileName
   * @return null if atleast one error
   */
  public DMLProgram doParse(String fileName, String dmlScript, HashMap<String, String> argVals)
      throws ParseException {
    DMLProgram dmlPgm = null;

    ANTLRInputStream in;
    try {
      if (dmlScript == null) {
        dmlScript = DMLParserWrapper.readDMLScript(fileName);
      }

      InputStream stream = new ByteArrayInputStream(dmlScript.getBytes());
      in = new org.antlr.v4.runtime.ANTLRInputStream(stream);
      //			else {
      //				if(!(new File(fileName)).exists()) {
      //					throw new ParseException("ERROR: Cannot open file:" + fileName);
      //				}
      //				in = new ANTLRInputStream(new FileInputStream(fileName));
      //			}
    } catch (FileNotFoundException e) {
      throw new ParseException("ERROR: Cannot find file:" + fileName);
    } catch (IOException e) {
      throw new ParseException("ERROR: Cannot open file:" + fileName);
    } catch (LanguageException e) {
      throw new ParseException("ERROR: " + e.getMessage());
    }

    PmlprogramContext ast = null;
    CustomDmlErrorListener errorListener = new CustomDmlErrorListener();

    try {
      PydmlLexer lexer = new PydmlLexer(in);
      CommonTokenStream tokens = new CommonTokenStream(lexer);
      PydmlParser antlr4Parser = new PydmlParser(tokens);

      boolean tryOptimizedParsing =
          false; // For now no optimization, since it is not able to parse integer value.

      if (tryOptimizedParsing) {
        // Try faster and simpler SLL
        antlr4Parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
        antlr4Parser.removeErrorListeners();
        antlr4Parser.setErrorHandler(new BailErrorStrategy());
        try {
          ast = antlr4Parser.pmlprogram();
          // If successful, no need to try out full LL(*) ... SLL was enough
        } catch (ParseCancellationException ex) {
          // Error occurred, so now try full LL(*) for better error messages
          tokens.reset();
          antlr4Parser.reset();
          if (fileName != null) {
            errorListener.pushCurrentFileName(fileName);
          } else {
            errorListener.pushCurrentFileName("MAIN_SCRIPT");
          }
          // Set our custom error listener
          antlr4Parser.addErrorListener(errorListener);
          antlr4Parser.setErrorHandler(new DefaultErrorStrategy());
          antlr4Parser.getInterpreter().setPredictionMode(PredictionMode.LL);
          ast = antlr4Parser.pmlprogram();
        }
      } else {
        // Set our custom error listener
        antlr4Parser.removeErrorListeners();
        antlr4Parser.addErrorListener(errorListener);
        errorListener.pushCurrentFileName(fileName);

        // Now do the parsing
        ast = antlr4Parser.pmlprogram();
      }
    } catch (Exception e) {
      throw new ParseException("ERROR: Cannot parse the program:" + fileName);
    }

    try {
      // Now convert the parse tree into DMLProgram
      // Do syntactic validation while converting
      ParseTree tree = ast;
      // And also do syntactic validation
      ParseTreeWalker walker = new ParseTreeWalker();
      PydmlSyntacticValidatorHelper helper = new PydmlSyntacticValidatorHelper(errorListener);
      PydmlSyntacticValidator validator = new PydmlSyntacticValidator(helper, fileName, argVals);
      walker.walk(validator, tree);
      errorListener.popFileName();
      if (errorListener.isAtleastOneError()) {
        return null;
      }
      dmlPgm = createDMLProgram(ast);
    } catch (Exception e) {
      throw new ParseException(
          "ERROR: Cannot translate the parse tree into DMLProgram:" + e.getMessage());
    }

    return dmlPgm;
  }