I want to know why this error in the function in lexical analysis code [closed]

import 'dart:async';
import 'dart:io';
import 'package:validated/validated.dart';
//import '';
enum Token  {Identifier, Constant, Operator, Keyword, Error}
void main() async {
  var f = new File('data.txt');
  var fdata ;
  if(await f.exists()){
    fdata= await f.readAsString();

  }
  bool isConstant(var str){
    if(isNumeric(str))
      return true;
    else if(str[0]=='"' && str.length-1=='"')
      return true;
    else if(str[0]==''' && str.length-1==''')
      return true;
    else
      return false;
  }
  bool isOperator(var ch)                   //check if the given character is a punctuator or not
  {
    if (ch == ':' || ch == ',' || ch == ';' ||  ch == '(' || ch == ')' ||
        ch == '[' || ch == ']' || ch == '{' || ch == '}'  || ch == '//' ||
        ch == '#' || ch == ',' ||ch == '+' || ch == '-' || ch == '*' ||
        ch == '/' || ch == '>' || ch == '<' || ch == '=' || ch == '|' || ch == '&' ||
        ch == '%' ||ch == '$')
    {
      return true;
    }
    else
      return false;
  }
  bool isIdentifier( var str)                       //check if the given identifier is valid or not
  {
    if (str[0] == '0' || str[0] == '1' || str[0] == '2' ||
        str[0] == '3' || str[0] == '4' || str[0] == '5' ||
        str[0] == '6' || str[0] == '7' || str[0] == '8' ||
        str[0] == '9' || isOperator(str[0]) == true)
    {
      return false;
    }                                   //if first character of string is a digit or a special character, identifier is not valid
    int i, len = str.length;/*The strlen() function returns the length of the null terminated byte string.
 It takes a null terminated byte string str as its argument and returns its length.The length does not include the null character.
 If there is no null character in the string, the behaviour of the function is undefined.*/
    if (len == 1)
    {
      return true;
    }                                       //if length is one, validation is already completed, hence return true
    else
    {
      for (i = 1; i < len; i++)                     //identifier cannot contain special characters
          {
        if (isOperator(str[i]) == true)
        {
          return false;
        }
      }
    }
    return true;
  }
  bool isKeyword(String str){
    if (str == "if" || str == "else" || str == "while" || str == "do" || str == "break" || str == "continue" || str == "int"
        || str == "double" || str == "float" || str == "return" || str == "char" || str == "case" || str == "long" || str == "short"
        || str == "typedef" || str == "switch" || str == "unsigned" || str == "void" || str == "static" || str == "struct" || str == "sizeof"
        || str == "long" || str == "volatile" || str == "enum" || str == "const" || str == "union" || str == "extern" || str == "bool"
        || str == "alignas" || str == "alignof" || str == "and" || str == "and_eq" || str == "asm" || str == "atomic_cancel" || str == "atomic_commit"
        || str == "atomic_noexcept" || str == "auto" || str == "bitor" || str == "bitand" || str == "catch" || str == "char16_t" || str == "char32_t"
        ||str == "class" || str == "compl" || str == "concept" || str == "constexpr" || str == "const_cast" || str == "co_await" || str == "co_return"
        || str == "co_yield" || str == "bool" || str == "decltype" || str == "default" || str == "delete" || str == "double"  || str == "dynamic_cast"
        || str == "else" || str == "if" || str == "explicit" || str == "export" || str == "implements" || str == "false" || str == "float" || str == "for"
        || str == "friend" || str == "goto" || str == "import" || str == "inline" || str == "module" || str == "mutable" || str == "namespace" || str == "new"
        || str == "noexcept" || str == "not" || str == "not_eq" || str == "nullptr" || str == "operator" || str == "or" || str == "or_eq" || str == "private"
        || str == "protected" || str == "public" || str == "register" || str == "reinterpret_cast" || str == "requires" || str == "return" || str == "short"
        || str == "signed" || str == "static" || str == "static_assert" || str == "static_cast" || str == "synchronized" || str == "template"
        ||str == "this" || str == "thread_local")

      return true;
    else
      return false;
  }
  Token read_token(var token) {
    String s;
    var ch = s[0];
    //fdata >> ch;
    while(ch==' ') fdata.trim();
    if (token == '#' || token == ':' || token == ',' || token == ';' || token == '(' ||
        token == ')' || token == '[' || token == ']' || token == '{' || token == '}'
        || token == '//' || token == '#' || token == ',' || token == '+' || token == '-' ||
        token == '*' || token == '/' || token == '>' || token == '<' || token == '=' || token == '|' || token == '&'
        || token == '%' || token == '$')
      print("Operator");
    else if (token == '<') {
      fdata.readAsBytesSync(ch);
      if (token == '=' || token == '>')
        print("Operator");
      else
        print("Error");
    }
    else if (token == '>') {
      fdata.readAsBytesSync(ch);
      if (token == '=')
        print("Operator");
      else
        print("Error");

    }
    else if (isAlpha(ch)) {
      s = ch;
      while (isNumeric(ch)) {
        s += ch;
        fdata.readAsBytesSync(ch);
      }
      isKeyword(s);
    }
    else {
      if (isNumeric(ch)){
        s=ch; fdata.readAsBytesSync(ch);
        while(isNumeric(ch)){
          s+=ch; fdata.readAsBytesSync(ch);
        }
         print("Identifier");
      }
      else print("Error");
    }
  }
  String Scanner(var str) {
    Token strng = read_token(str);
    switch (strng) {
      case Token.Keyword:
        {
          print("if");
        }
        break;
      case Token.Keyword:
        {
          print("else");
        }
        break;
      case Token.Keyword:
        {
          print("alignas");
        }
        break;
      case Token.Keyword:
        {
          print("alignof");
        }
        break;
      case Token.Keyword:
        {
          print("and");
        }
        break;
      case Token.Keyword:
        {
          print("and_eq");
        }
        break;
      case Token.Keyword:
        {
          print("asm");
        }
        break;
      case Token.Keyword:
        {
          print("atomic_cancel");
        }
        break;
      case Token.Keyword:
        {
          print("if");
        }
        break;
      case Token.Keyword:
        {
          print("atomic_commit");
        }
        break;
      case Token.Keyword:
        {
          print("atomic_noexcept");
        }
        break;
      case Token.Keyword:
        {
          print("auto");
        }
        break;
      case Token.Keyword:
        {
          print("bitand");
        }
        break;
      case Token.Keyword:
        {
          print("bitor");
        }
        break;
      case Token.Keyword:
        {
          print("bool");
        }
        break;
      case Token.Keyword:
        {
          print("break");
        }
        break;
      case Token.Keyword:
        {
          print("case");
        }
        break;
      case Token.Keyword:
        {
          print("catch");
        }
        break;
      case Token.Keyword:
        {
          print("char");
        }
        break;
      case Token.Keyword:
        {
          print("char16_t");
        }
        break;
      case Token.Keyword:
        {
          print("char32_t");
        }
        break;
      case Token.Keyword:
        {
          print("class");
        }
        break;
      case Token.Keyword:
        {
          print("compl");
        }
        break;
      case Token.Keyword:
        {
          print("concept");
        }
        break;
      case Token.Keyword:
        {
          print("const");
        }
        break;
      case Token.Keyword:
        {
          print("constexpr");
        }
        break;
      case Token.Keyword:
        {
          print("co_await");
        }
        break;
      case Token.Keyword:
        {
          print("continue");
        }
        break;
      case Token.Keyword:
        {
          print("const_cast");
        }
        break;
      case Token.Keyword:
        {
          print("co_return");
        }
        break;
      case Token.Keyword:
        {
          print("co_yield");
        }
        break;
      case Token.Keyword:
        {
          print("decltype");
        }
        break;
      case Token.Keyword:
        {
          print("default");
        }
        break;
      case Token.Keyword:
        {
          print("delete");
        }
        break;
      case Token.Keyword:
        {
          print("do");
        }
        break;
      case Token.Keyword:
        {
          print("double");
        }
        break;
      case Token.Keyword:
        {
          print("dynamic_cast");
        }
        break;
      case Token.Keyword:
        {
          print("enum");
        }
        break;
      case Token.Keyword:
        {
          print("explicit");
        }
        break;
      case Token.Keyword:
        {
          print("export");
        }
        break;
      case Token.Keyword:
        {
          print("extern");
        }
        break;
      case Token.Keyword:
        {
          print("false");
        }
        break;
      case Token.Keyword:
        {
          print("float");
        }
        break;
      case Token.Keyword:
        {
          print("for");
        }
        break;
      case Token.Keyword:
        {
          print("friend");
        }
        break;
      case Token.Keyword:
        {
          print("goto");
        }
        break;
      case Token.Keyword:
        {
          print("int");
        }
        break;
      case Token.Keyword:
        {
          print("if");
        }
        break;
      case Token.Keyword:
        {
          print("long");
        }
        break;
      case Token.Keyword:
        {
          print("module");
        }
        break;
      case Token.Keyword:
        {
          print("mutable");
        }
        break;
      case Token.Keyword:
        {
          print("namespace");
        }
        break;
      case Token.Keyword:
        {
          print("new");
        }
        break;
      case Token.Keyword:
        {
          print("noexcept");
        }
        break;
      case Token.Keyword:
        {
          print("not");
        }
        break;
      case Token.Keyword:
        {
          print("not_eq");
        }
        break;
      case Token.Keyword:
        {
          print("nullptr");
        }
        break;
      case Token.Keyword:
        {
          print("operator");
        }
        break;
      case Token.Keyword:
        {
          print("or");
        }
        break;
      case Token.Keyword:
        {
          print("or_eq");
        }
        break;
      case Token.Keyword:
        {
          print("private");
        }
        break;
      case Token.Keyword:
        {
          print("protected");
        }
        break;
      case Token.Keyword:
        {
          print("public");
        }
        break;
      case Token.Keyword:
        {
          print("register");
        }
        break;
      case Token.Keyword:
        {
          print("reinterpret_cast");
        }
        break;
      case Token.Keyword:
        {
          print("requires");
        }
        break;
      case Token.Keyword:
        {
          print("return");
        }
        break;
      case Token.Keyword:
        {
          print("short");
        }
        break;
      case Token.Keyword:
        {
          print("signed");
        }
        break;
      case Token.Keyword:
        {
          print("sizeof");
        }
        break;
      case Token.Keyword:
        {
          print("static");
        }
        break;
      case Token.Keyword:
        {
          print("static_assert");
        }
        break;
      case Token.Keyword:
        {
          print("static_cast");
        }
        break;
      case Token.Keyword:
        {
          print("struct");
        }
        break;
      case Token.Keyword:
        {
          print("switch");
        }
        break;
      case Token.Keyword:
        {
          print("synchronized");
        }
        break;
      case Token.Keyword:
        {
          print("template");
        }
        break;
      case Token.Keyword:
        {
          print("this");
        }
        break;
      case Token.Keyword:
        {
          print("thread_local");
        }
        break;
      case Token.Keyword:
        {
          print("throw");
        }
        break;
      case Token.Keyword:
        {
          print("typedef");
        }
        break;
      case Token.Keyword:
        {
          print("try");
        }
        break;
      case Token.Keyword:
        {
          print("true");
        }
        break;
      case Token.Keyword:
        {
          print("typeid");
        }
        break;
      case Token.Keyword:
        {
          print("typename");
        }
        break;
      case Token.Keyword:
        {
          print("union");
        }
        break;
      case Token.Keyword:
        {
          print("unsigned");
        }
        break;
      case Token.Keyword:
        {
          print("using");
        }
        break;
      case Token.Keyword:
        {
          print("virtual");
        }
        break;
      case Token.Keyword:
        {
          print("wchar_t");
        }
        break;
      case Token.Keyword:
        {
          print("volatile");
        }
        break;
      case Token.Keyword:
        {
          print("void");
        }
        break;
      case Token.Keyword:
        {
          print("while");
        }
        break;
      case Token.Keyword:
        {
          print("xor");
        }
        break;
      case Token.Keyword:
        {
          print("xor_eq");
        }
        break;

      case Token.Operator:
        {
          print(":");
        }
        break;
      case Token.Operator:
        {
          print(",");
        }
        break;
      case Token.Operator:
        {
          print(";");
        }
        break;
      case Token.Operator:
        {
          print("(");
        }
        break;
      case Token.Operator:
        {
          print(")");
        }
        break;
      case Token.Operator:
        {
          print("[");
        }
        break;
      case Token.Operator:
        {
          print("]");
        }
        break;
      case Token.Operator:
        {
          print("{");
        }
        break;
      case Token.Operator:
        {
          print("}");
        }
        break;
      case Token.Operator:
        {
          print("//");
        }
        break;
      case Token.Operator:
        {
          print("$");
        }
        break;
      case Token.Operator:
        {
          print("%");
        }
        break;
      case Token.Operator:
        {
          print("&");
        }
        break;
      case Token.Operator:
        {
          print("|");
        }
        break;
      case Token.Operator:
        {
          print("=");
        }
        break;
      case Token.Operator:
        {
          print("<");
        }
        break;
      case Token.Operator:
        {
          print(">");
        }
        break;
      case Token.Operator:
        {
          print("/");
        }
        break;
      case Token.Operator:
        {
          print("*");
        }
        break;
      case Token.Operator:
        {
          print("-");
        }
        break;
      case Token.Operator:
        {
          print("+");
        }
        break;
      case Token.Operator:
        {
          print("#");
        }
        break;
      case Token.Constant:
        {
          print(isNumeric(strng.toString()));
        }
        break;
      case Token.Constant:
        {
          print(strng.toString()[0] == '"' && strng
              .toString()
              .length - 1 == '"');
        }
        break;
      case Token.Constant:
        {
          print(strng.toString()[0] == ''' && strng
              .toString()
              .length - 1 == ''');
        }
        break;
      case Token.Error:
        {
          print(strng.toString()[0] == '0');
        }
        break;
      case Token.Error:
        {
          print(strng.toString()[0] == '1');
        }
        break;
      case Token.Error:
        {
          print(strng.toString()[0] == '2');
        }
        break;
      case Token.Error:
        {
          print(strng.toString()[0] == '3');
        }
        break;
      case Token.Error:
        {
          print(strng.toString()[0] == '4');
        }
        break;
      case Token.Error:
        {
          print(strng.toString()[0] == '5');
        }
        break;
      case Token.Error:
        {
          print(strng.toString()[0] == '6');
        }
        break;
      case Token.Error:
        {
          print(strng.toString()[0] == '7');
        }
        break;
      case Token.Error:
        {
          print(strng.toString()[0] == '8');
        }
        break;
      case Token.Error:
        {
          print(strng.toString()[0] == '9');
        }
        break;
      case Token.Error:
        {
          print(isOperator(strng.toString()[0]) == true);
        }
        break;
      case Token.Identifier:
        {
          print(strng.toString()[0] == 'a' || strng.toString()[0] == 'A');
        }
        break;
      case Token.Identifier:
        {
          print(strng.toString()[0] == 'b' || strng.toString()[0] == 'B');
        }
        break;
      case Token.Identifier:
        {
          print(strng.toString()[0] == 'c' || strng.toString()[0] == 'C');
        }
        break;
      case Token.Identifier:
        {
          print(strng.toString()[0] == 'd' || strng.toString()[0] == 'D');
        }
        break;
      case Token.Identifier:
        {
          print(strng.toString()[0] == 'e' || strng.toString()[0] == 'E');
        }
        break;
      case Token.Identifier:
        {
          print(strng.toString()[0] == 'f' || strng.toString()[0] == 'F');
        }
        break;
      case Token.Identifier:
        {
          print(strng.toString()[0] == 'g' || strng.toString()[0] == 'G');
        }
        break;
      case Token.Identifier:
        {
          print(strng.toString()[0] == 'h' || strng.toString()[0] == 'H');
        }
        break;
      case Token.Identifier:
        {
          print(strng.toString()[0] == 'i' || strng.toString()[0] == 'I');
        }
        break;
      case Token.Identifier:
        {
          print(strng.toString()[0] == 'j' || strng.toString()[0] == 'J');
        }
        break;
      case Token.Identifier:
        {
          print(strng.toString()[0] == 'k' || strng.toString()[0] == 'K');
        }
        break;
      case Token.Identifier:
        {
          print(strng.toString()[0] == 'l' || strng.toString()[0] == 'L');
        }
        break;
      case Token.Identifier:
        {
          print(strng.toString()[0] == 'm' || strng.toString()[0] == 'M');
        }
        break;
      case Token.Identifier:
        {
          print(strng.toString()[0] == 'n' || strng.toString()[0] == 'N');
        }
        break;
      case Token.Identifier:
        {
          print(strng.toString()[0] == 'o' || strng.toString()[0] == 'O');
        }
        break;
      case Token.Identifier:
        {
          print(strng.toString()[0] == 'p' || strng.toString()[0] == 'P');
        }
        break;
      case Token.Identifier:
        {
          print(strng.toString()[0] == 'q' || strng.toString()[0] == 'Q');
        }
        break;
      case Token.Identifier:
        {
          print(strng.toString()[0] == 'r' || strng.toString()[0] == 'R');
        }
        break;
      case Token.Identifier:
        {
          print(strng.toString()[0] == 's' || strng.toString()[0] == 'S');
        }
        break;
      case Token.Identifier:
        {
          print(strng.toString()[0] == 't' || strng.toString()[0] == 'T');
        }
        break;
      case Token.Identifier:
        {
          print(strng.toString()[0] == 'v' || strng.toString()[0] == 'V');
        }
        break;
      case Token.Identifier:
        {
          print(strng.toString()[0] == 'w' || strng.toString()[0] == 'W');
        }
        break;
      case Token.Identifier:
        {
          print(strng.toString()[0] == 'x' || strng.toString()[0] == 'X');
        }
        break;
      case Token.Identifier:
        {
          print(strng.toString()[0] == 'y' || strng.toString()[0] == 'Y');
        }
        break;
      case Token.Identifier:
        {
          print(strng.toString()[0] == 'z' || strng.toString()[0] == 'Z');
        }
        break;
      case Token.Identifier:
        {
          print(strng.toString()[0] == '_');
        }
        break;
    }
  }
  Scanner(fdata);
}

/*the error is in read_token function in var ch = s[0] he told me NoSuchMethodError: The method ‘[]’ was called on null.
Receiver: null
Tried calling:
the code is about Lexical analysis is the first phase of a compiler. It takes the modified source code from language preprocessors that are written in the form of sentences. The lexical analyzer breaks these syntaxes into a series of tokens, by removing any whitespace or comments in the source code.

If the lexical analyzer finds a token invalid, it generates an error. The lexical analyzer works closely with the syntax analyzer. It reads character streams from the source code, checks for legal tokens, and passes the data to the syntax analyzer when it demands. I want to know the error of this code*/

Source: Windows Questions

LEAVE A COMMENT