I'm trying to implement arrays in antlr4 and I'm lost as to how I can get the multiple elements of the array when it is initialized like so:
int array[] = {1, 2};
I was thinking of placing them in a HashMap like this, the key being the index:
public Map<Integer, Value> array_memory = new HashMap<Integer, Value>();
Below is the grammar I'm following:
grammar GaleugParserNew;
/*
* PARSER RULES
*/
declare_var
: INTEGER ID '[' (INT)? ']' (ASSIGN '{' array_init '}')? SCOL
;
array_init
: INT ',' array_init
| INT
;
/*
* LEXER RULES
*/
SCOL : ';';
ASSIGN : '=';
INTEGER : 'int';
INT : [0-9]+;
I have a variable that would count how many times declare_var has visited array_init for the index. But I don't know how to visit array_init with multiple elements.
This is my declare_var visitor:
@Override
public Value visitDeclareArray(GaleugParserNewParser.DeclareArrayContext ctx){
String id = ctx.ID().getText(); //gets array name
String size = ctx.INT().getText(); //get string version of array size
int x = Integer.parseInt(size); //convert size(String) to int
Value elem = this.visit(ctx.array_init());
return Value.VOID;
}
And this is my array_init visitor:
@Override
public Value visitArray_init(GaleugParserNewParser.Array_initContext ctx){
index += 1;
return new Value(Double.valueOf(ctx.getText()));
}
If you have any suggestions as to how I can visit array_init in reference to the number of variables I'd like to hear them. Thank you!
I was thinking of placing them in a HashMap like this, the key being the index:
public Map<Integer, Value> array_memory = new HashMap<Integer, Value>();
Why? A List<Value>
would do as well, right? No need to keep track of the index yourself.
You're making thinks more complicated by recursively calling the array_init
rule:
array_init
: INT ',' array_init
| INT
;
I would do it like this instead:
array_init
: INT ( ',' INT )*
;
You can then do something like this:
import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.tree.TerminalNode;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
String source = "int array[] = {1, 2};";
GaleugParserNewLexer lexer = new GaleugParserNewLexer(CharStreams.fromString(source));
GaleugParserNewParser parser = new GaleugParserNewParser(new CommonTokenStream(lexer));
Value value = new EvalVisitor().visit(parser.declare_var());
System.out.println(value);
}
}
class Value {
final Object value;
public Value(Object value) {
this.value = value;
}
@Override
public String toString() {
return String.valueOf(this.value);
}
}
class EvalVisitor extends GaleugParserNewBaseVisitor<Value> {
@Override
public Value visitDeclare_var(GaleugParserNewParser.Declare_varContext ctx) {
List<Value> numbers = new ArrayList<>();
if (ctx.array_init() != null) {
for (TerminalNode tokenNode : ctx.array_init().INT()) {
numbers.add(new Value(Integer.valueOf(tokenNode.getText())));
}
}
return new Value(numbers);
}
}
And if you run this Main
class, the following will be printed to your console:
[1, 2]
EDIT
But what if I'm not specifically looking for INT, rather I would like to look for any tokens inside a grammar containing all of the data types in my language? what should i use instead of TerminalNode?
Easy as 1-2-3, define you grammar like this:
array_init
: expr ( ',' expr )*
;
expr
: '(' expr ')' #nestedExpr
| lhs=expr '+' rhs=expr #addExpr
| INT #intExpr
| ID #idExpr
;
and then do something like this:
import org.antlr.v4.runtime.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Main {
public static void main(String[] args) {
String source = "int array[] = { 123456789, (1 + 2), Q };";
GaleugParserNewLexer lexer = new GaleugParserNewLexer(CharStreams.fromString(source));
GaleugParserNewParser parser = new GaleugParserNewParser(new CommonTokenStream(lexer));
Map<String, Value> memory = new HashMap<String, Value>(){{
put("Q", new Value(42));
}};
Value value = new EvalVisitor(memory).visit(parser.declare_var());
System.out.println(value);
}
}
class Value {
final Object value;
public Value(Object value) {
this.value = value;
}
int asInt() {
return (Integer) value;
}
@Override
public String toString() {
return String.valueOf(this.value);
}
}
class EvalVisitor extends GaleugParserNewBaseVisitor<Value> {
final Map<String, Value> memory;
EvalVisitor(Map<String, Value> memory) {
this.memory = memory;
}
@Override
public Value visitDeclare_var(GaleugParserNewParser.Declare_varContext ctx) {
List<Value> numbers = new ArrayList<>();
if (ctx.array_init() != null) {
for (GaleugParserNewParser.ExprContext expr : ctx.array_init().expr()) {
numbers.add(super.visit(expr));
}
}
return new Value(numbers);
}
@Override
public Value visitIntExpr(GaleugParserNewParser.IntExprContext ctx) {
return new Value(Integer.valueOf(ctx.getText()));
}
@Override
public Value visitAddExpr(GaleugParserNewParser.AddExprContext ctx) {
return new Value(super.visit(ctx.lhs).asInt() + super.visit(ctx.rhs).asInt());
}
@Override
public Value visitNestedExpr(GaleugParserNewParser.NestedExprContext ctx) {
return super.visit(ctx.expr());
}
@Override
public Value visitIdExpr(GaleugParserNewParser.IdExprContext ctx) {
return this.memory.get(ctx.getText());
}
}
which will print:
[123456789, 3, 42]