Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
对象已成功添加到Java ArrayList,但在尝试检索该ArrayList时,它返回空列表_Java_Arraylist_Jflex - Fatal编程技术网

对象已成功添加到Java ArrayList,但在尝试检索该ArrayList时,它返回空列表

对象已成功添加到Java ArrayList,但在尝试检索该ArrayList时,它返回空列表,java,arraylist,jflex,Java,Arraylist,Jflex,我正在为Jflex编写一个lexer规范,它应该读取一个输入文件并返回一个令牌。我确实成功地完成了此部分,如下所示: /*Super-FORTAN language lexer specification*/ %% %public %class LexicalAnalyser %unicode %line %column %type Symbol %standalone %{ private ArrayList<Symbol> tokens = new ArrayLis

我正在为Jflex编写一个lexer规范,它应该读取一个输入文件并返回一个令牌。我确实成功地完成了此部分,如下所示:

    /*Super-FORTAN language lexer specification*/
%%
%public
%class LexicalAnalyser
%unicode
%line
%column
%type Symbol
%standalone

%{
  private ArrayList<Symbol> tokens = new ArrayList<Symbol>();

  public LexicalAnalyser() {
    //ArrayList<Symbol> tokens = new ArrayList<Symbol>();
  }

  /**
  * This method will be called as action to be taken in the rules and actions section
  * of the lexer.
  * @para  m unit the lexical unit of the lexer
  * @param value the matched input characters
  * @return return an object of type symbol
  *
  */

  public Symbol symbol(LexicalUnit unit, Object value){
    Symbol token = new Symbol(unit, yyline+1, yycolumn+1, value);

    if(token != null) {
      tokens.add(token);  //add a token to the token list
      //System.out.println("Token added && the size is: " + tokens.size()); //Checking whether a token has been successfully added
    } else{
      System.out.println("Failed to add token");
    }

    System.out.println(token); //print out the list of token to standard output
    return  token;
  }

  public ArrayList<Symbol> getTokens(){
    System.out.println("In total " + tokens.size() + " tokens have been found");
    return tokens;
  }

  public boolean isZzAtEOF() {
    return zzAtEOF;
  }
  %}

  %eofval{
    return new Symbol(LexicalUnit.EOS, yyline, yycolumn);
    %eofval}

    /*Program Name */
    ProgramName = [:uppercase:][:jletterdigit:]*[:lowercase:][:jletterdigit:]*

    /*Variables names*/
    VarName = [:lowercase:][a-z0-9]*

    /*Carriage Return*/
    EndLine = \r|\n|\r\n

    /*Number*/
    Number = [1-9][0-9]*

    EOS = {EndLine} | [\t\f\b]

    /*Input character*/
    InputCharacter = [^\r|\n]

    /*Comments*/
    Shortcomment ="//"{InputCharacter}*{EndLine}?
    Longcomment = "/*"[^'*']~"*/"
    Comment = {Shortcomment}|{Longcomment}
    FileMetaData ="rtf1"~"cf0 "

    %state STRING, CHARLITERAL
    %%

    <YYINITIAL> {
      /*Program Name*/
      {ProgramName} {return symbol(LexicalUnit.PROGNAME, yytext());}

      /* keywords */
      "BEGINPROG" {return symbol(LexicalUnit.BEGINPROG, yytext());
      }
      "DO" {return symbol(LexicalUnit.DO, yytext());}
      "ENDPROG"   {return symbol(LexicalUnit.ENDPROG, yytext());}
      "ENDIF"  {return symbol(LexicalUnit.ENDIF, yytext());}
      "ENDFOR" {return symbol(LexicalUnit.ENDFOR, yytext());}
      "ENDWHILE" {return symbol(LexicalUnit.ENDWHILE, yytext());}
      "ELSE" {return symbol(LexicalUnit.ELSE, yytext());}
      "FOR" {return symbol(LexicalUnit.FOR, yytext());}
      "IF" {return symbol(LexicalUnit.IF, yytext());}
      "PRINT" {return symbol(LexicalUnit.PRINT, yytext());}
      "THEN" {return symbol(LexicalUnit.THEN, yytext());}
      "TO" {return symbol(LexicalUnit.TO, yytext());}
      "READ"  {return symbol(LexicalUnit.READ, yytext());}
      "VARIABLES" {return  symbol(LexicalUnit.VARIABLES, yytext());}

      /*Binary operators */
      "AND"   {return symbol(LexicalUnit.AND, yytext());}
      "OR"    {return symbol(LexicalUnit.OR, yytext());}

      /*operators */
      "+"  {return symbol(LexicalUnit.PLUS, yytext());}
      "-"  {return symbol(LexicalUnit.MINUS, yytext());}
      "*"  {return symbol(LexicalUnit.TIMES, yytext());}
      "/"  {return symbol(LexicalUnit.DIVIDE, yytext());}

      /*Comparator */
      "="  {return symbol(LexicalUnit.EQ, yytext());}
      ">=" {return symbol(LexicalUnit.GEQ, yytext());}
      ">"  {return symbol(LexicalUnit.GT, yytext());}
      "<=" {return symbol(LexicalUnit.LEQ, yytext());}
      "<"  {return symbol(LexicalUnit.LT, yytext());}
      "NOT" {return symbol(LexicalUnit.NOT, yytext());}
      "<>" {return symbol(LexicalUnit.NEQ, yytext());}

      /* separators */
      {EndLine} {return new Symbol(LexicalUnit.ENDLINE, yyline, yycolumn);}
      "(" {return symbol(LexicalUnit.LPAREN, yytext());}
      ")" {return symbol(LexicalUnit.RPAREN, yytext());}
      "," {return symbol(LexicalUnit.COMMA, yytext());}

      /*Assignment */
      ":=" {return symbol(LexicalUnit.ASSIGN, yytext());}

      /*identifiers*/
      {VarName} {return symbol(LexicalUnit.VARNAME, yytext());}

      /*numbers */
      {Number} {return symbol(LexicalUnit.NUMBER, yytext());}

      {Comment} {}
        {FileMetaData} {}
    }
使用该方法中的print语句,我确认已成功添加每个对象。 但是,当我从主类调用getTokens()getter时(如下面的代码中所示)。我得到一个空列表:

public class Main{
    /**
    * Runs the scanner on input files.
    *
    * This is a standalone scanner, it will print any unmatched
    * text to System.out unchanged.
    *
    * @param argv   the command line, contains the filenames to run
    *               the scanner on.
    */

    public static void main(String argv[]){
    LexicalAnalyser lexer = new LexicalAnalyser();

    lexer.main(argv);

    System.out.println("\nIdentifiers");

    ArrayList<Symbol> tokenList = lexer.getTokens(); //Retrieving the token list
    System.out.println("Token added && the size is: " + tokenList.size());

    for(Symbol tk: tokenList){
        if(tk !=null){
            if (tk.getType() == LexicalUnit.VARIABLES){
                System.out.println(tk.getValue() + " " + tk.getLine() );
            }
        }
    }
}
公共类主{
/**
*在输入文件上运行扫描仪。
*
*这是一个独立的扫描仪,它将打印任何不匹配的
*文本到System.out不变。
*
*@param argv命令行,包含要运行的文件名
*扫描仪打开了。
*/
公共静态void main(字符串argv[]){
LexicalAnalyser lexer=新的LexicalAnalyser();
lexer.main(argv);
System.out.println(“\n标识符”);
ArrayList tokenList=lexer.getTokens();//检索令牌列表
System.out.println(“添加了令牌&大小为:“+tokenList.size()”);
用于(符号tk:tokenList){
如果(tk!=null){
if(tk.getType()==LexicalUnit.VARIABLES){
System.out.println(tk.getValue()+“”+tk.getLine());
}
}
}
}
}

我错过了什么吗?这里我没有提供的唯一一段代码是Symbol对象类。 我想知道我错过了哪一点。
非常感谢。

大小为0,因为您在主类中将
TokenList
的一个新实例初始化,并在该空实例上执行for循环。
您将令牌添加到
LexicalAnalyzer
类中的令牌列表中,该类是私有的,因此您应该提供一个getter方法并检索它

在您的
LexicalAnalyzer
类中添加以下内容:

public ArrayList<Symbol> getTokenList() {
    return tokens.getTokenList();
}
publicArrayList getTokenList(){
return tokens.getTokenList();
}

在主类中,change
tokenList=tokens.getTokenList()
to
tokenList=lexer.getTokenList()

感谢您抽出时间回复并帮助我解决问题。我在LexerAnalyser类中做了如下更改:private ArrayList tokens=new ArrayList();令牌。添加(令牌);public ArrayList getTokens(){return tokens;}但是我在主类中仍然得到相同的输出,我已经更改了初始化,如下所示:ArrayList tokenList=lexer.getTokens()@DeclerusYvesKerbens能否请你编辑你的问题,更新你的代码?这篇文章已经更新,包含了我代码的最新版本
public ArrayList<Symbol> getTokenList() {
    return tokens.getTokenList();
}