GithubHelp home page GithubHelp logo

antlr4-csv-demo's Introduction

ANTLR 4 CSV Demo

A small demo project that demonstrates how to use ANTLR 4 to parse a CSV file and return the contents as a 2 dimension list of strings (in Java) as demonstrated in the ANTLR 3 tutorial posted here.

To run the Main class:

public class Main { 
 
  public static void main(String[] args) throws Exception {
    // the input source
    String source =
        "aaa,bbb,ccc" + "\n" +   
        "\"d,\"\"d\",eee,fff";  
      
    // create an instance of the lexer
    CSVLexer lexer = new CSVLexer(new ANTLRInputStream(source));
         
    // wrap a token-stream around the lexer
    CommonTokenStream tokens = new CommonTokenStream(lexer);
          
    // create the parser
    CSVParser parser = new CSVParser(tokens);
    
    // invoke the entry point of our grammar
    List<List<String>> data = parser.file().data;

    // display the contents of the CSV source
    for(int r = 0; r < data.size(); r++) {
      List<String> row = data.get(r);
      for(int c = 0; c < row.size(); c++) {
        System.out.println("(row=" + (r+1) + ",col=" + (c+1) + ") = " + row.get(c));  
      }
    }
  }
} 

do the following:

git clone https://github.com/bkiers/antlr4-csv-demo.git
cd antlr4-csv-demo
ant run

which would produce the following output:

Buildfile: .../csv/build.xml

init:

generate:
     [echo] generating parser source files...
     [move] Moving 4 files to .../csv/src/gen/csv

compile:
    [javac] Compiling 4 source files to .../csv/build/main

run:
     [java] (row=1,col=1) = aaa
     [java] (row=1,col=2) = bbb
     [java] (row=1,col=3) = ccc
     [java] (row=2,col=1) = d,"d
     [java] (row=2,col=2) = eee
     [java] (row=2,col=3) = fff

BUILD SUCCESSFUL
Total time: 1 second

antlr4-csv-demo's People

Contributors

bkiers avatar

Stargazers

 avatar

Watchers

 avatar  avatar

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.