简体   繁体   中英

Ragel Java outputs [nulla,b] instead of [a,b,c]

I would like to code a CSVReader State Machine in ragel, since I've doned mine in Java with Enums already. The returned list should be [a,b,c] but I get [nulla,b]. I'm using Ragel 6.8 on Fedora 22, I really hope any one could help me

This is the source:

%%{

machine csv_reader_java;
seperator = (';'|',');
letter = [A-Za-z0-9]*;

main := |*
seperator => { putToList(tokens, string); };
letter => { emit(data, tokens, ts, te); };
space;
*|;

}%%

import java.util.*;

public class CSVReader {

private String string;

public void emit(char[] data, List<String> tokens, int ts, int te) {
     char output = data[ts];
     string += output;
}
public void putToList(List<String> tokens, String data){
tokens.add(data);
string = "";
}

%% write data;

public List<String> split(char[] data) {
    int cs; /* state number */
    int p = 0, /* start of input */
    pe = data.length, /* end of input */
    eof = pe,
    ts, /* token start */
    te, /* token end */
    act /* used for scanner backtracking */;

    List<String> tokens = new ArrayList<String>();

    %% write init;
    %% write exec;

    return tokens;
}

public static void main(String[] args) {
    System.out.println(new CSVReader().split("a,b,c".toCharArray()));
}
}

And this is what it returns me: [nulla, b]

Looking over this, I see two problems. The first is the null at the start of your output, which I think is caused by not initializing string when parsing starts, which leaves it null . When the emit call reaches string += output; , string is null , so it appends the current token ("a") to the string representation of null , resulting in "nulla". Initializing string with "" would fix this.

The second problem, the one of not adding "c" to the list, is even simpler. The tokens only get added to the list when a separator is found, and since there is no separator after "c", that token doesn't get added. You could solve this by calling an action on end-of-file to emit the current token string if it isn't empty.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM