Antlr Array Help

╄→尐↘猪︶ㄣ 提交于 2019-12-07 17:58:32

问题


Hey ive started to use Antlr with java and i wanted to know how i can store some values directly into a 2d array and return this array? i cant find any tutorials on this at all, all help is apperciated.


回答1:


Let's say you want to parse a flat text file containing numbers separated by spaces. You'd like to parse this into a 2d array of int's where each line is a "row" in your array.

The ANTLR grammar for such a "language" could look like:

grammar Number;

parse
  :  line* EOF
  ;

line
  :  Number+ (LineBreak | EOF)
  ;

Number
  :  ('0'..'9')+
  ;

Space
  :  (' ' | '\t') {skip();}
  ;

LineBreak
  :  '\r'? '\n'
  |  '\r'
  ;

Now, you'd like to have the parse rule return an List of List<Integer> objects. Do that by adding a returns [List<List<Integer>> numbers] after your parse rule which can be initialized in an @init{ ... } block:

parse returns [List<List<Integer>> numbers]
@init {
  $numbers = new ArrayList<List<Integer>>();
}
  :  line* EOF
  ;

Your line rule looks a bit the same, only it returns a 1 dimensional list of numbers:

line returns [List<Integer> row]
@init {
  $row = new ArrayList<Integer>();
}
  :  Number+ (LineBreak | EOF)
  ;

The next step is to fill the Lists with the actual values that are being parsed. This can be done embedding the code {$row.add(Integer.parseInt($Number.text));} inside the Number+ loop in your line rule:

line returns [List<Integer> row]
@init {
  $row = new ArrayList<Integer>();
}
  :  (Number {$row.add(Integer.parseInt($Number.text));})+ (LineBreak | EOF)
  ;

And lastly, you'll want to add the Lists being returned by your line rule to be actually added to your 2D numbers list from your parse rule:

parse returns [List<List<Integer>> numbers]
@init {
  $numbers = new ArrayList<List<Integer>>();
}
  :  (line {$numbers.add($line.row);})* EOF
  ;

Below is the final grammar:

grammar Number;

parse returns [List<List<Integer>> numbers]
@init {
  $numbers = new ArrayList<List<Integer>>();
}
  :  (line {$numbers.add($line.row);})* EOF
  ;

line returns [List<Integer> row]
@init {
  $row = new ArrayList<Integer>();
}
  :  (Number {$row.add(Integer.parseInt($Number.text));})+ (LineBreak | EOF)
  ;

Number
  :  ('0'..'9')+
  ;

Space
  :  (' ' | '\t') {skip();}
  ;

LineBreak
  :  '\r'? '\n'
  |  '\r'
  ;

which can be tested with the following class:

import org.antlr.runtime.*;
import java.util.List;

public class Main {
    public static void main(String[] args) throws Exception {
        String source = 
                "1 2       \n" +
                "3 4 5 6 7 \n" +
                "      8   \n" +
                "9 10 11     ";
        ANTLRStringStream in = new ANTLRStringStream(source);
        NumberLexer lexer = new NumberLexer(in);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        NumberParser parser = new NumberParser(tokens);
        List<List<Integer>> numbers = parser.parse();
        System.out.println(numbers);
    }
}

Now generate a lexer and parser from the grammar:

java -cp antlr-3.2.jar org.antlr.Tool Number.g

compile all .java source files:

javac -cp antlr-3.2.jar *.java

and run the main class:

// On *nix
java -cp .:antlr-3.2.jar Main

// or Windows
java -cp .;antlr-3.2.jar Main

which produces the following output:

[[1, 2], [3, 4, 5, 6, 7], [8], [9, 10, 11]]

HTH




回答2:


Here's some excerpts from a grammar I made that parses people's names and returns a Name object. Should be enough to show you how it works. Other objects such as arrays are done the same way.

In the grammar:

grammar PersonNames;

fullname returns [Name name]
@init {
    name = new Name();
}
  : (directory_style[name] | standard[name] | title_without_fname[name] |      family_style[name] | proper_initials[name]) EOF;

standard[Name name]
 : (title[name] ' ')* fname[name] ' ' (mname[name] ' ')* (nickname[name] ' ')? lname[name] (sep honorifics[name])*;

 fname[Name name] : (f=NAME | f=INITIAL)  { name.set(Name.Part.FIRST, toNameCase($f.text)); };

in your regular Java code

public static Name parseName(String str) throws RecognitionException {
    System.err.println("parsing `" + str + "`");
    CharStream stream = new ANTLRStringStream(str);
    PersonNamesLexer lexer = new PersonNamesLexer(stream);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    PersonNamesParser parser = new PersonNamesParser(tokens);

    return parser.fullname();
}


来源:https://stackoverflow.com/questions/4354758/antlr-array-help

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!