Categories:
Audio (13)
Biotech (29)
Bytecode (36)
Database (77)
Framework (7)
Game (7)
General (507)
Graphics (53)
I/O (35)
IDE (2)
JAR Tools (101)
JavaBeans (21)
JDBC (121)
JDK (426)
JSP (20)
Logging (108)
Mail (58)
Messaging (8)
Network (84)
PDF (97)
Report (7)
Scripting (84)
Security (32)
Server (121)
Servlet (26)
SOAP (24)
Testing (54)
Web (15)
XML (309)
Collections:
Other Resources:
ANTLR "Arithmeic" Grammar Java Test
How to write a Java program to test the lexer and parser generated from ANTLR?
✍: FYIcenter
If you have the lexer and parser generated from the
arithmetic.g4 grammar file by ANTLR as shown in the previous tutorial,
you use the following Java program to test it:
// Copyright (c) FYIcenter.com import org.antlr.v4.runtime.*; import org.antlr.v4.runtime.tree.*; public class ArithmeticTest { public static void main(String [] args) throws Exception { String source = args[0]; CharStream stream = CharStreams.fromString(source); arithmeticLexer lexer = new arithmeticLexer(stream); CommonTokenStream tokens = new CommonTokenStream(lexer); arithmeticParser parser = new arithmeticParser(tokens); ParseTree tree = parser.equation(); // ParseTree tree = parser.expression(); System.out.println(tree.toStringTree(parser)); } }
Note that the above program tests the "equation" grammar rule. You un-comment the expression() line to test the "expression grammar rule.
Make sure the arithmeticLexer.java arithmeticParser.java are compiled in the current directory as shown in the previous tutorial. Then compile and run ArithmeticTest.java:
\fyicenter>javac -cp .;antlr-4.10.1-complete.jar ArithmeticTest.java \fyicenter>java -cp .;antlr-4.10.1-complete.jar ArithmeticTest "x=a" (equation (expression (term (factor (signedAtom (atom (variable x)))))) (relop = ) (expression (term (factor (signedAtom (atom (variable a))))))) \fyicenter>java -cp .;antlr-4.10.1-complete.jar ArithmeticTest \ "x = (-b + (b^2 -4*a*c)^0.50) / 4*a*c" (equation (expression (term (factor (signedAtom (atom (variable x)))))) (relop = ) (expression (term (factor (signedAtom (atom ( (expression (term (factor ( signedAtom - (signedAtom (atom (variable b)))))) + (term (factor (signedAtom (atom ( (expression (term (factor (signedAtom (atom (variable b))) ^ (signedAtom (atom ( scientific 2))))) - (term (factor (signedAtom (atom (scientific 4)))) * (factor (signedAtom (atom (variable a)))) * (factor (signedAtom (atom (variable c)))))) ))) ^ (signedAtom (atom (scientific 0.50)))))) )))) / (factor (signedAtom (atom (scientific 4)))) * (factor (signedAtom (atom (variable a)))) * (factor ( signedAtom (atom (variable c)))))))
If the source code has any syntax error, ANTLR will print out errors:
\fyicenter>java -cp .;antlr-4.10.1-complete.jar ArithmeticTest "x = (a+b" line 1:9 missing ')' at '<EOF>' (equation (expression (term (factor (signedAtom (atom (variable x)))))) (relop = ) (expression (term (factor (signedAtom (atom ( (expression (term (factor ( signedAtom (atom (variable a))))) + (term (factor (signedAtom (atom (variable b)))))) <missing ')'>))))))
2020-12-26, 6793🔥, 0💬
Popular Posts:
What Is jniwrap.jar in JNIWrapper 3.7.1? winpack.jar in JNIWrapper 3.7.1 is an add-on JAR file for J...
Commons VFS provides a single API for accessing various different file systems. It presents a unifor...
JLayer is a library that decodes/plays/converts MPEG 1/2/2.5 Layer 1/2/3 (i.e. MP3) in real time for...
What JAR files are required to run dom\Writer.java provided in the Apache Xerces package? 3 JAR file...
How to display types defined in an XML Schema file with the xs\QueryXS.java provided in the Apache X...