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 "Arithmetic" Grammar
Where to get an "Arithmetic" grammar for ANTLR?
✍: FYIcenter
You can download a copy of the "Arithmetic" grammar for ANTLR at GitHub > antlr > grammars-v4 > arithmetic".
You can follow these steps to play with arithmetic.g4.
1. View the grammar file, arithmetic.g4 (modified to reduce space):
/* BSD License Copyright (c) 2013, Tom Everett All rights reserved. */ grammar arithmetic; equation : expression relop expression ; expression : term ((PLUS | MINUS) term)* ; term : factor ((TIMES | DIV) factor)* ; factor : signedAtom (POW signedAtom)* ; signedAtom : PLUS signedAtom | MINUS signedAtom | atom ; atom : scientific | variable | LPAREN expression RPAREN ; scientific : SCIENTIFIC_NUMBER ; variable : VARIABLE ; relop : EQ | GT | LT; VARIABLE : VALID_ID_START VALID_ID_CHAR* ; fragment VALID_ID_START: ('a' .. 'z') | ('A' .. 'Z') | '_' ; fragment VALID_ID_CHAR: VALID_ID_START | ('0' .. '9') ; SCIENTIFIC_NUMBER: NUMBER (E SIGN? NUMBER)? ; //The integer part gets its potential sign from the signedAtom rule fragment NUMBER: ('0' .. '9') + ('.' ('0' .. '9') +)? ; fragment E: 'E' | 'e' ; fragment SIGN: ('+' | '-') ; LPAREN : '(' ; RPAREN : ')' ; PLUS : '+' ; MINUS : '-' ; TIMES : '*' ; DIV : '/' ; GT : '>' ; LT : '<' ; EQ : '=' ; POINT : '.' ; POW : '^' ; WS : [ \r\n\t] + -> skip ;
1. Generate the lexer and parser in Java:
\fyicenter>java -cp antlr-4.10.1-complete.jar org.antlr.v4.Tool arithmetic.g4 \fyicenter>dir 2156 arithmetic.interp 187 arithmetic.tokens 3935 arithmeticBaseListener.java 3714 arithmeticLexer.interp 8364 arithmeticLexer.java 187 arithmeticLexer.tokens 3260 arithmeticListener.java 21062 arithmeticParser.java 1223 arithmetic.g4
3. Compile the lexer, parser and supporting Java classes
\fyicenter>javac -cp .;antlr-4.10.1-complete.jar arithmetic*.java \fyicenter>dir *.class 2528 arithmeticBaseListener.class 5525 arithmeticLexer.class 1537 arithmeticListener.class 1581 arithmeticParser$AtomContext.class 1409 arithmeticParser$EquationContext.class 1670 arithmeticParser$ExpressionContext.class 1574 arithmeticParser$FactorContext.class 1037 arithmeticParser$RelopContext.class 974 arithmeticParser$ScientificContext.class 1354 arithmeticParser$SignedAtomContext.class 1651 arithmeticParser$TermContext.class 955 arithmeticParser$VariableContext.class 10733 arithmeticParser.class
4. Testing the lexer and parser with ANTLR TestRig:
\fyicenter>java -cp .;antlr-4.10.1-complete.jar \ org.antlr.v4.gui.TestRig arithmetic equation -tree x = (-b + (b^2 -4*a*c)^0.50) / 4*a*c ^Z (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)))))))
The above output shows that the Arithmetic lexer and parser is working.
⇒ ANTLR "Arithmetic" Grammar - Graphical Output
2021-01-09, 8832🔥, 0💬
Popular Posts:
Java Architecture for XML Binding (JAXB) is a Java API that allows Java developers to map Java class...
maven-model-builder-3.8. 6.jaris the JAR file for Apache Maven 3.8.6 Model Builder module. Apache Ma...
JDK 11 jdk.jdi.jmod is the JMOD file for JDK 11 JDI (Java Debug Interface) tool. JDK 11 JDI tool com...
How to download and install Apache XMLBeans-2.6.0.zip? If you want to try the XMLBeans Java library,...
How to read XML document with XML Schema validation from socket connections with the socket\DelayedI...