Javascript : Regex for variable name does not work as it meant

105 Views Asked by At

We're making a LolCode Interpreter for our school project. We're having trouble in coding the lexical analyzer of our interpreter.

// I HAS A <variable>
if(/(I\sHAS\sA)\s[a-zA-Z_$][0-9a-zA-Z_$]*$/i.test(entry)){
 if(/(I\sHAS\sA)\s[a-zA-Z_$][0-9a-zA-Z_$]*/i.test(entry)){
  var desc = "Variable Declaration";
  var lex = /I\sHAS\sA/i.exec(entry);
  $('#lexemes tbody').append('<tr><td>' + lex + '</td><td>' + desc + '</td></tr>');
 }
 if(/(I\sHAS\sA)\s[a-zA-Z_$][0-9a-zA-Z_$]*/i.test(entry)){
  var string = /(I\sHAS\sA)\s[a-zA-Z_$][0-9a-zA-Z_$]*/i.exec(entry);
  var desc = "Variable name";
  var lex = /[^(I\sHAS\sA)\s]+/.exec(string);
  $('#lexemes tbody').append('<tr><td>' + lex + '</td><td>' + desc + '</td></tr>');
 }
}

// I HAS A <variable> ITZ <value>
if(/(I\sHAS\sA)\s[a-zA-Z_$][0-9a-zA-Z_$]*\sITZ\s.*/i.test(entry)){
 if(/(I\sHAS\sA)\s[a-zA-Z_$][0-9a-zA-Z_$]*\sITZ\s.*/i.test(entry)){
  var desc = "Variable Declaration";
  var lex = /I\sHAS\sA/i.exec(entry);
  $('#lexemes tbody').append('<tr><td>' + lex + '</td><td>' + desc + '</td></tr>');
 }
 if(/(I\sHAS\sA)\s[a-zA-Z_$][0-9a-zA-Z_$]*\sITZ\s.*/i.test(entry)){
  var string = /(I\sHAS\sA)\s[a-zA-Z_$][0-9a-zA-Z_$]*\sITZ\s.*/i.exec(entry);
  var desc = "Variable name";
  var lex = /[^(I\sHAS\sA)\s]+/.exec(string);
  $('#lexemes tbody').append('<tr><td>' + lex + '</td><td>' + desc + '</td></tr>');
 }

 if(/.+ITZ\s.*/i.test(entry)){
  var string = /.+ITZ\s.*/i.exec(entry);
  var desc = "Initialization";
  var lex = /ITZ/i.exec(string);
  $('#lexemes tbody').append('<tr><td>' + lex + '</td><td>' + desc + '</td></tr>');
 }
}

It is working fine but my regex for variable name is wrong. I am new to regex so any help will be appreciated.

If the variable name has 'I' or 'HAS' or 'A', it's not caught by the regex.

I know it's because of the [^ ] but if i remove that part, the keyword I HAS A will be included in the variable name. Please help me improve our regex. Thank you very much!

0

There are 0 best solutions below