在文件上测试JavACC?

发布于 2025-01-25 08:08:01 字数 252 浏览 2 评论 0原文

因此,在编译代码后,我希望它从文件而不是命令行读取输入。

因此,我不想这样做:

javacc Ex.jj
javac *.java
java Ex "x+2"

我想这样做:

javacc Ex.jj
javac *.java
java test.txt

test.txt在其中有一个:

"x+4"

So after I compile my code, I want it to read input from a file instead of the command line.

So instead of doing this:

javacc Ex.jj
javac *.java
java Ex "x+2"

I want to do this:

javacc Ex.jj
javac *.java
java test.txt

Where test.txt has this in it:

"x+4"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

箹锭⒈辈孓 2025-02-01 08:08:01

您可以在语法文件中声明具有MAIM方法的类:

options {
    STATIC = false;
    IGNORE_CASE = false;
}

PARSER_BEGIN(Ex)
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.IOException;

public class Ex {
    public static void main(String[] args) {
        try (InputStream in = new FileInputStream(args[0])) {
            Ex parser = new Ex(in, "UTF-8");
            double r = parser.expr();
            System.out.println("Result: " + r);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}
PARSER_END(Ex)

...


SKIP : { " " | "\r" | "\n" | "\t" }

TOKEN: {
...
}

double expr(): {
}
{
    ...
}

You can declare a class with a main method in your syntax file:

options {
    STATIC = false;
    IGNORE_CASE = false;
}

PARSER_BEGIN(Ex)
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.IOException;

public class Ex {
    public static void main(String[] args) {
        try (InputStream in = new FileInputStream(args[0])) {
            Ex parser = new Ex(in, "UTF-8");
            double r = parser.expr();
            System.out.println("Result: " + r);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}
PARSER_END(Ex)

...


SKIP : { " " | "\r" | "\n" | "\t" }

TOKEN: {
...
}

double expr(): {
}
{
    ...
}
香草可樂 2025-02-01 08:08:01

为您的JavACC项目尝试此基本解析器:

options
{
  // Your options
}

PARSER_BEGIN(XML_lexer)
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

public class XML_lexer{} //De momento no construimos el parser, solo nos interesa el Scanner/Lexer

PARSER_END(XML_lexer)

TOKEN_MGR_DECLS : {

static int commentNesting = 0;


public static void main(String args [])  
{
    FileInputStream myfile = null;
    XML_lexer lexer=null;
    int option=0;
    try {
      InputStreamReader isr = new InputStreamReader(System.in);
      BufferedReader bf = new BufferedReader(isr);
      System.out.println("Configuracion de codigo fuente a analizar");
      System.out.println("\t[1] Reading by command line");
      System.out.println("\t[2] Reading by file");
      
      option = Integer.parseInt(bf.readLine());
      if (option == 1)
      {
        System.out.println("By command line. Enter the text: ");
        lexer = new XML_lexer( System.in ) ;
      }
      else if (option == 2)
      {
        System.out.println("Enter name of file");
        myfile = new FileInputStream(bf.readLine());
        lexer = new XML_lexer ( myfile ) ;
      }
    } catch (Exception e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
    }

    while (true)
    {
      try
      {
        Token myToken=lexer.getNextToken();
        if (myToken.kind==lexer.EOF)
        {
          System.out.println("End of file");
          System.exit(0);
        }
        System.out.println("Leido lexema: " + myToken.image + "\t-->Token:  " + lexer.tokenImage[myToken.kind]);
      } catch (TokenMgrError e) {
        System.out.println(e.getMessage());
        if (option == 1) lexer.ReInit(System.in);
        else if (option==2) lexer.getNextToken();
      }
    }
  }
}

SKIP:
{
      < "\r" >
    | < "\t" >
    | < "\n" >
} 

TOKEN:
{
  // Your tokens definition
}

Try this basic parser for your JavaCC projects:

options
{
  // Your options
}

PARSER_BEGIN(XML_lexer)
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

public class XML_lexer{} //De momento no construimos el parser, solo nos interesa el Scanner/Lexer

PARSER_END(XML_lexer)

TOKEN_MGR_DECLS : {

static int commentNesting = 0;


public static void main(String args [])  
{
    FileInputStream myfile = null;
    XML_lexer lexer=null;
    int option=0;
    try {
      InputStreamReader isr = new InputStreamReader(System.in);
      BufferedReader bf = new BufferedReader(isr);
      System.out.println("Configuracion de codigo fuente a analizar");
      System.out.println("\t[1] Reading by command line");
      System.out.println("\t[2] Reading by file");
      
      option = Integer.parseInt(bf.readLine());
      if (option == 1)
      {
        System.out.println("By command line. Enter the text: ");
        lexer = new XML_lexer( System.in ) ;
      }
      else if (option == 2)
      {
        System.out.println("Enter name of file");
        myfile = new FileInputStream(bf.readLine());
        lexer = new XML_lexer ( myfile ) ;
      }
    } catch (Exception e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
    }

    while (true)
    {
      try
      {
        Token myToken=lexer.getNextToken();
        if (myToken.kind==lexer.EOF)
        {
          System.out.println("End of file");
          System.exit(0);
        }
        System.out.println("Leido lexema: " + myToken.image + "\t-->Token:  " + lexer.tokenImage[myToken.kind]);
      } catch (TokenMgrError e) {
        System.out.println(e.getMessage());
        if (option == 1) lexer.ReInit(System.in);
        else if (option==2) lexer.getNextToken();
      }
    }
  }
}

SKIP:
{
      < "\r" >
    | < "\t" >
    | < "\n" >
} 

TOKEN:
{
  // Your tokens definition
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文