Categories:
Audio (13)
Biotech (29)
Bytecode (22)
Database (79)
Framework (7)
Game (7)
General (497)
Graphics (53)
I/O (32)
IDE (2)
JAR Tools (70)
JavaBeans (16)
JDBC (86)
JDK (338)
JSP (20)
Logging (90)
Mail (54)
Messaging (8)
Network (106)
PDF (82)
Report (7)
Scripting (75)
Security (67)
Server (112)
Servlet (17)
SOAP (24)
Testing (55)
Web (24)
XML (287)
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.7.1-complete.jar arithmeticTest.java \fyicenter>java -cp .;antlr-4.7.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.7.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.7.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, 6458👍, 0💬
Popular Posts:
JDK 11 jdk.hotspot.agent.jmod is the JMOD file for JDK 11 Hotspot Agent module. JDK 11 Hotspot Agent...
JasperReports, the world's most popular open source business intelligence and reporting engine and J...
Byte Code Generation Library is high level API to generate and transform JAVA byte code. It is used ...
Commons Pool provides an Object-pooling API, with three major aspects: 1. A generic object pool inte...
JAX-WS is an API for building web services and clients. It is the next generation Web Services API r...