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, 8596🔥, 0💬
Popular Posts:
SLF4J API is a simple API that allows to plug in any desired logging library at deployment time. Her...
Xalan-Java, Version 2.7.1, is an XSLT processor for transforming XML documents into HTML, text, or o...
iText is an ideal library for developers looking to enhance web- and other applications with dynamic...
Jackson is "the Java JSON library" or "the best JSON parser for Java". Or simply as "JSON for Java"....
commons-lang-1.0.1.jar is the JAR file for Apache Commons Lang 1.0.1, which provides a host of helpe...