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.7.1-complete.jar org.antlr.v4.Tool arithmetic.g4

\fyicenter>dir 
    1,298 arithmetic.g4
    2,239 arithmetic.interp
      212 arithmetic.tokens
    4,079 arithmeticBaseListener.java
    3,802 arithmeticLexer.interp
    5,391 arithmeticLexer.java
      212 arithmeticLexer.tokens
    3,357 arithmeticListener.java
   19,677 arithmeticParser.java

3. Compile the lexer, parser and supporting Java classes

\fyicenter>javac -cp .;antlr-4.7.1-complete.jar arithmetic*.java

\fyicenter>dir *.class
    2,528 arithmeticBaseListener.class
    5,000 arithmeticLexer.class
    1,537 arithmeticListener.class
    1,562 arithmeticParser$AtomContext.class
    1,390 arithmeticParser$EquationContext.class
    1,651 arithmeticParser$ExpressionContext.class
    1,555 arithmeticParser$FactorContext.class
    1,018 arithmeticParser$RelopContext.class
      955 arithmeticParser$ScientificContext.class
    1,335 arithmeticParser$SignedAtomContext.class
    1,632 arithmeticParser$TermContext.class
      936 arithmeticParser$VariableContext.class
   10,333 arithmeticParser.class

4. Testing the lexer and parser with ANTLR TestRig:

\fyicenter>java -cp .;antlr-4.7.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, 7020👍, 0💬