Creating AST for shared and local variables

Posted by Rizwan Abbasi on Stack Overflow See other posts from Stack Overflow or by Rizwan Abbasi
Published on 2012-11-16T19:59:14Z Indexed on 2012/11/16 23:00 UTC
Read the original article Hit count: 259

Filed under:
|
|
|

Here is my grammar

grammar simulator;

options {  
  language = Java; 
  output = AST;
  ASTLabelType=CommonTree;
}                     
//imaginary tokens
tokens{ 
SHARED;
LOCALS;
BOOL;
RANGE;
ARRAY;
}
parse
    : declaration+
    ;

declaration
    :variables
    ;
variables
  :  locals
  ;

locals
  :  (bool
  |   range
  |   array)
  ;
bool
    :ID 'in' '[' ID ',' ID ']' ('init' ID)? -> ^(BOOL ID ID ID ID?)
    ;
range
    : ID 'in' '[' INT '..' INT ']' ('init' INT)? -> ^(RANGE ID INT INT INT?)
    ;
array   
    :ID 'in' 'array' 'of' '[' INT '..' INT ']' ('init' INT)? -> ^(ARRAY ID INT INT INT?)
    ;
ID  
    : (('a'..'z' | 'A'..'Z'|'_')('a'..'z' | 'A'..'Z'|'0'..'9'|'_'))*
    ;
INT 
    : ('0'..'9')+
    ;
WHITESPACE
    : ('\t' | ' ' | '\r' | '\n' | '\u000C')+ {$channel = HIDDEN;}
    ;

INPUT

flag in [down, up] init down
pc in [0..7] init 0 
CA   in array of [0..5]   init 0

AST enter image description here

It is having a small problem. Variables (bool, range or array) can be of two abstract types 1. locals (each object will have it's own variable) 2. shared (think of static in java, same for all object)

Now the requirements are changed. I want the user to input like this
NEW INPUT

domains:
upDown [up,down]
possibleStates [0-7]
booleans [true,false]

locals:
pc in possibleStates  init 0
flag in upDown init down
flag1 in upDown init down
map in array of booleans init false

shared:
pcs in possibleStates  init 0
flag in upDown init down
flag1 in upDown init down
maps in array of booleans init false  

Again, all the variables can be of two types (of any domain sepecified)
1. Local
2. Shared
In Domains:

upDown [up,down]
possibleStates [0-7]

upDown, up, down and possibleStates are of type ID (ID is defined in my above grammar), 0 and 7 are of type INT
Can any body help me how to convert my current grammar to meet new specifications.

© Stack Overflow or respective owner

Related posts about variables

Related posts about antlr3