3
votes

Have a look at my grammar

grammar protocol;

options {  
  language = Java; 
  output = AST;
}                     
//imaginary tokens
tokens{ 
BOOL;
CHAR;
STRING;
}
parse
    : declaration
    ;

declaration
    :   variable
    ;
variable
    :   locals
    ;
locals
  :  (bool
  |  char
  |  string)+
  ;
bool
    :'bool' ID -> ^(BOOL ID)
    ;
char
    : 'char' ID -> ^(CHAR ID)
    ;
string  
    :'string' ID -> ^(STRING ID)
    ;

ID  
    : (('a'..'z' | 'A'..'Z'|'_')('a'..'z' | 'A'..'Z'|'0'..'9'|'_'))*
    ;
INT 
    : ('0'..'9')+
    ;
WHITESPACE
    : ('\t' | ' ' | '\r' | '\n' | '\u000C')+ {$channel = HIDDEN;}
    ;  

For the following input,

bool boolVariable
char charVariable
string stringVariable  

My grammar creates the following AST
AST For variables' grammar

I can't declare a variable more than once. I don't want to declare variables of the same type at once separated by commas but I want like this

bool boolVariable1
bool boolVariable2
bool boolVariable3
string stringVariable1
string stringVariable2

After doing this, I want all variables to be of two main types. Shared and local. In Java, a shared variable (static) is the one who has single copy for all the objects whereas local variable has separate copy for each object. I want user to explicitly specify the scope of the variable before defining the variable set. Like,

locals:
    bool boolVariable1
    bool boolVariable2
    bool boolVariable3
    string stringVariable1
    string stringVariable2
shared:
    bool boolVariable4
    bool boolVariable5
    bool boolVariable6
    string stringVariable3
    string stringVariable4
    char charVariable1

Moreover, is there any way that I can check user cann't have two variables of the same name? Like,

bool boolVariable
bool boolVariable  

should give some sort of error or like that. Any thoughts/help?
Thank you

EDIT - SOLUTION

grammar protocol;

options {  
  language = Java; 
  output = AST;
}                     
//imaginary tokens
tokens{ 
BOOL;
CHAR;
STRING;
SBOOL;
SCHAR;
SSTRING;
}
parse
    : declaration
    ;

declaration
    :   variable
    ;
variable
    :   (locals 
    |   shared)*
    ;
locals
  : 'locals:' (bool| char| string)*
  ;
bool
    :'bool' ID -> ^(BOOL ID)
    ;
char
    : 'char' ID -> ^(CHAR ID)
    ;
string  
    :'string' ID -> ^(STRING ID)
    ;
shared
  : 'shared:' (sbool| schar| sstring)*
  ;

sbool
    :'bool' ID -> ^(SBOOL ID)
    ;
schar
    : 'char' ID -> ^(SCHAR ID)
    ;
sstring 
    :'string' ID -> ^(SSTRING ID)
    ;
ID  
    : (('a'..'z' | 'A'..'Z'|'_')('a'..'z' | 'A'..'Z'|'0'..'9'|'_'))*
    ;
INT 
    : ('0'..'9')+
    ;
WHITESPACE
    : ('\t' | ' ' | '\r' | '\n' | '\u000C')+ {$channel = HIDDEN;}
    ;
2
You can post your solution as answer and self accept it.nhahtdh

2 Answers

1
votes

You can try this one:

  locals
      :  bool* char* string*
      ;

It should permit you declaration of different variables with the same type. The best (I think) way to forbid declaration of different variables with the same name is to keep collection (in Java) with declared previously variables. Just call Java functions, like here

With best regards

1
votes

As Bahdan mentioned in his answer, you'll want to maintain a collection of names that have already been used. Here's a simple example based on your updated grammar (with a couple of other changes, described at the bottom of this answer). New rule var is where the new member code gets used. Note that there is no real error handling being done here, just a name check.

grammar protocol;

options {  
  language = Java; 
  output = AST;
}                     
//imaginary tokens
tokens{ 
BOOL;
CHAR;
STRING;
SBOOL;
SCHAR;
SSTRING;
}

@parser::header { 
    import java.util.ArrayList;
}

@members {
    private ArrayList<String> variableNames = new ArrayList<String>();

    private boolean variableDefined(String name){
        return variableNames.contains(name);
    }

    private void defineVariable(String name){
        variableNames.add(name);
    }
}

parse
    : declaration
    ;

declaration
    :   variable
    ;
variable
    :   (locals | shared)*
    ;
locals
    : 'locals:' (bool| char_ | string)*
    ;
bool
    :'bool' var -> ^(BOOL var)
    ;
char_
    : 'char' var -> ^(CHAR var)
    ;
string  
    :'string' var -> ^(STRING var)
    ;
shared
    : 'shared:' (sbool| schar| sstring)*
    ;
sbool
    :'bool' var -> ^(SBOOL var)
    ;
schar
    : 'char' var -> ^(SCHAR var)
    ;
sstring 
    :'string' var -> ^(SSTRING var)
    ;   
var
    : ID 
      {!variableDefined($ID.text)}? //This rule is only satisfied if the variable is new. 
      {defineVariable($ID.text);}  //we made it here, so it's new. Add it for future reference.
    ;    
ID  
    : ('a'..'z' | 'A'..'Z'|'_')('a'..'z' | 'A'..'Z'|'0'..'9'|'_')*
    ;
INT 
    : ('0'..'9')+
    ;
WHITESPACE
    : ('\t' | ' ' | '\r' | '\n' | '\u000C')+ {$channel = HIDDEN;}
    ;

I can declare two variables like bool boolVariable bool boolVariable but I can't declare two variables like bool boolVariable bool boolVariable12

See my changes to ID above. There was an extra set of parentheses that messed up the rule. I also renamed char to char_ to get the parser to compile correctly for me.