ANTLR "Arithmetic" Grammar

Q

Where to get an "Arithmetic" grammar for ANTLR?

✍: FYIcenter

A

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

ANTLR TestRig Graphical Output

ANTLR Commands and Examples

⇑⇑ FAQ for ANTLR Parser Generator

2021-01-09, 8596🔥, 0💬