简体   繁体   中英

How to use java_cup step by step?

I am totally new to java_cup and parsing and I'd like to generate parser for simple grammar using bnfc and javacup. I've specified grammar rules and used bnfc:

bnfc -java -filename.cf

which generated me some files (ia filename.cup)

then I tried to build it with java_cup:

java java_cup.Main filename.cup

which also ends successfully. Finally I got files parser.java and sym.java.

I have some inputfiles which I'd like to check if they parse. I have no idea how to use this generated parser. I also compiled parser.java but when I tried to run it, it throws

Exception in thread "main" java.lang.NoClassDefFoundError: parser (wrong name: Attempt1/parser)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:788)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:447)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482

The parser generated by bnfc is not meant to be called as an execucable. BNFC generates a test program in filename/Test.java that you can use.

The easiest way to get started is to ask BNFC to generate a makefile for you:

bnfc -java -m filename.cf
make

This should compile the test program which you can then test with:

echo "1 + 3 = 4" | java filename.Test

(replace 1 + 3 = 4 with something your grammar can parse)

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