[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Could someone give me a hand, re: uninitialised variables



Hi, I am currentely altering an existing language such that unitialised
variables are no longer allowed.  The language is called Kenya and I had
orignally intended to make the change by just hacking the typechecker,
but im interested to know complex it would be to alter the sablecc
grammar (attached) and regenerate the code.  Any assistance would be
greatly appreciated.

Chris Gale.

--- begin grammar.sablecc ---

Package minijava;

Helpers
  letter = ['A'..'Z'] | ['a'..'z'];
  digit = ['0'..'9'];
  cr = 13;
  lf = 10;
  tab = 9;
  point = '.';
  not_cr_lf = [[32..127] - [cr + lf]];
  escape = '\b' | '\t' | '\n' | '\f' | '\r' | '\"' | '\' ''' | '\\';
  space = ' ';

Tokens

  new_line = cr | lf | cr lf;
  blank = ( tab | space )+;
  comment = '//' not_cr_lf* ( cr | lf | cr lf )?;

  boolean = 'boolean';
  char = 'char';
  int = 'int';
  real = 'double';
  string = 'String';
  void = 'void';

  klass = 'class';
  const = 'const';
  print = 'print';
  println = 'println';
  if = 'if';
  else = 'else';
  while = 'while';
  return = 'return';
  switch = 'switch';
  case = 'case';
  break = 'break';
  default = 'default';
  for = 'for';
  to = 'to';
  step = 'step';
  decreasing = 'decreasing';
  assert = 'assert';
  arraycopy = 'arraycopy';

  true = 'true';
  false = 'false';
  null = 'null';

  and = 'and' | '&&';
  or = 'or' | '||';
  xor = 'xor';
  not = 'not' | '!';

  sin  = 'sin';
  cos  = 'cos';
  tan  = 'tan';
  asin = 'asin';
  acos = 'acos';
  atan = 'atan';
  sqrt = 'sqrt';
  log  = 'log';
  exp = 'exp';
  random = 'random';
  round = 'round';
  abs = 'abs';
  floor = 'floor';
  ceil = 'ceil';

  read = 'read()';
  readint = 'readInt()';
  readreal = 'readDouble()';
  readstring = 'readString()';
  readchar = 'readChar()';
  endoffile = 'isEOF()';

  drawbox = 'drawBox';
  drawtriangle = 'drawTriangle';
  drawellipse = 'drawEllipse';
  drawline = 'drawLine';
  drawstring = 'drawString';
  clear = 'clear()';
  setcolour = 'setColour';
  setbgcolour = 'setBGColour';
  fill = 'fill';

  identifier = letter (letter | digit)*;
  stringliteral = '"' [not_cr_lf - '"']* '"';
  charliteral = ''' [not_cr_lf  - ''' ] ''' | ''' escape ''';

  l_parenthese = '(';
  r_parenthese = ')';
  l_brace = '{';
  r_brace = '}';
  l_bracket = '[';
  r_bracket = ']';
  bracket_pair = '[ ]';
  semicolon = ';';
  colon = ':';
  comma = ',';
  dot = point;

  plus = '+';
  plusplus = '++';
  minus = '-';
  minusminus = '--';
  times = '*';
  divide = '/';
  power = '^';
  mod = '%';
  less = '<';
  lessequal = '<=';
  greater = '>';
  greaterequal = '>=';
  equal = '==';
  notequal = '!=';
  assign = '=';

  intnumber = digit+ ;
  fpnumber = digit+ point digit+;


Ignored Tokens
  blank , new_line;

Productions

 statements =
    {list}  statement statements |
    {empty} ;

  statement =
    {comment} comment |
    {dec} declaration |
    {functioncall} function_application semicolon |
    {assignment} field_access assign expression semicolon |
    {if} if l_parenthese bool_expression r_parenthese [block1]:block else? [block2]:block? |
    {while} while l_parenthese bool_expression r_parenthese block |
    {return} return expression? semicolon |
    {switch} switch l_parenthese expression r_parenthese switch_block |
    {for} for for_control block |
    {break} break semicolon |
    {print_str} print l_parenthese expression r_parenthese semicolon |
    {println_str} println l_parenthese expression r_parenthese semicolon |
    {assert} assert bool_expression colon_string? semicolon |
    {arraycopy} arraycopy l_parenthese [src]:name comma [dest]:name r_parenthese semicolon |

    {clear} clear semicolon |
    {drawline} drawline l_parenthese four_numbers r_parenthese semicolon |
    {drawbox} drawbox l_parenthese four_numbers r_parenthese semicolon |
    {drawtriangle} drawtriangle l_parenthese six_numbers r_parenthese semicolon |
    {drawellipse} drawellipse l_parenthese four_numbers r_parenthese semicolon |
    {drawstring} drawstring l_parenthese [string]:expression [c1]:comma [x]:expression [c2]:comma [y]:expression r_parenthese semicolon |
    {setcolour} setcolour l_parenthese rgb r_parenthese semicolon |
    {setbgcolour} setbgcolour l_parenthese rgb r_parenthese semicolon |
    {fill} fill l_parenthese bool_expression r_parenthese semicolon ;

  for_control =
    {java}   l_parenthese name assign expression [s1]:semicolon bool_expression [s2]:semicolon unary_exp r_parenthese |
    {simple} decreasing? name assign [exp1]:expression to [exp2]:expression step_bit?;

  step_bit = step expression ;

  block = l_brace statements r_brace;

  function_application = name l_parenthese actual_param_list r_parenthese;

  actual_param_list = {list} actual_param_list comma expression |
                      {exp} expression |
                      {empty};

  four_numbers = [x1]:math_expression [c1]:comma [y1]:math_expression [c2]:comma [x2]:math_expression [c3]:comma [y2]:math_expression;

  six_numbers = four_numbers [c1]:comma [x]:math_expression [c2]:comma [y]:math_expression;

  rgb = [r]:math_expression [c1]:comma [g]:math_expression [c2]:comma [b]:math_expression;

  switch_block = l_brace possible_case* r_brace;

  possible_case = switch_label block;

  switch_label =
    {case} case expression colon |
    {default} default colon;

  declaration =
    {class_dec} klass identifier l_brace declaration* r_brace |
    {func_dec}  type bracket_pair* identifier l_parenthese formal_param_list? r_parenthese block |
    {const_dec} const type identifier initialiser? semicolon |
    {var_dec}   type bracket_pair* type_param? identifier array_access* initialiser? semicolon;

  formal_param_list = type_name comma_type_name*;

  type_name = type bracket_pair* identifier;

  comma_type_name = comma type_name;

  initialiser =
    {scalar} assign expression |
    {array} assign array_init;

  array_init = l_brace init_list r_brace;

  init_list =
    {scalar} expression comma_exp* |
    {array}  array_init comma_array_init*;

  comma_exp = comma expression;

  comma_array_init = comma array_init;

  colon_string = colon expression;

  booleanliteral =
    {true} true |
    {false} false;

/********************
       Types
********************/

  type =

    {basic_type}
        basic_type |

    {reference_type}
        reference_type;

   basic_type =

    {char}
        char |

    {int}
	int |

    {real}
	real |

    {string}
        string |

    {boolean}
        boolean |

    {void}
        void;

  reference_type =
    {class_type}
        class_type;

  class_type =
    name;

/********************
Variable Declarations
********************/

  type_param = less type_param_list greater;

  type_param_list = type comma_type*;

  comma_type = comma type;

/********************
  Expressions
********************/

  expression =
    {boolexp} bool_expression;

  math_expression =
    {term} term |
    {plus} math_expression plus term |
    {minus} math_expression minus term ;

  term =
    {unary} unary_exp |
    {mult} term times unary_exp |
    {div} term divide unary_exp |
    {mod} term mod unary_exp |
    {power} term power unary_exp;

  unary_exp =
    {preincr} preincr |
    {predecr} predecr |
    {minus} minus unary_exp |
    {plus} plus unary_exp |
    {negate} not unary_exp |
    {postfix_exp} postfix_exp;

  postfix_exp =
    {factor} factor |
    {postincr} postincr |
    {postdecr} postdecr;

  postdecr = postfix_exp minusminus;

  postincr = postfix_exp plusplus;

  predecr = minusminus postfix_exp;

  preincr = plusplus postfix_exp;

  factor =
    {null} null |
    {fieldaccess} field_access |
    {function} function_application |
    {number} number |
    {stringliteral} stringliteral |
    {charliteral} charliteral |
    {boolliteral} booleanliteral |
    {readint} readint |
    {readreal} readreal |
    {readstring} readstring |
    {readchar} readchar |
    {read} read |
    {endoffile} endoffile |
    {sin} sin l_parenthese math_expression r_parenthese |
    {cos} cos l_parenthese math_expression r_parenthese |
    {tan} tan l_parenthese math_expression r_parenthese |
    {asin} asin l_parenthese math_expression r_parenthese |
    {acos} acos l_parenthese math_expression r_parenthese |
    {atan} atan l_parenthese math_expression r_parenthese |
    {sqrt} sqrt l_parenthese math_expression r_parenthese |
    {log} log l_parenthese math_expression r_parenthese |
    {exp} exp l_parenthese math_expression r_parenthese |
    {abs} abs l_parenthese math_expression r_parenthese |
    {floor} floor l_parenthese math_expression r_parenthese |
    {ceil} ceil l_parenthese math_expression r_parenthese |
    {round} round l_parenthese math_expression r_parenthese |
    {random} random l_parenthese r_parenthese |
    {expression} l_parenthese bool_expression r_parenthese;

  number = {i} intnumber |
           {f} fpnumber;

  bool_expression =
    {term} bool_term |
    {or} bool_expression or bool_term |
    {xor} bool_expression xor bool_term;

  bool_term =
    {equality} equality |
    {and} bool_term and equality;

  equality =
    {relational} relational |
    {eq} equality equal relational |
    {neq} equality notequal relational;

  relational =
    {math} math_expression |
    {lt} relational less math_expression |
    {gt} relational greater math_expression |
    {lteq} relational lessequal math_expression |
    {gteq} relational greaterequal math_expression;


/********************
       Names
********************/

  field_access =
    {array} name array_access+ |
    {scalar} name;

  name =

    {simple_name}
        simple_name |

    {qualified_name}
        qualified_name;

  simple_name =
    identifier;

  qualified_name =
    field_access dot identifier;

  array_access = l_bracket math_expression r_bracket;