I need to know what is error of this code this is a code in dart language in android studio code

    import 'dart:collection';//queue
    import 'dart:io';
    import 'package:flutter/material.dart';
    /*******SCANNER*******/
    bool isPunctuator(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 == '|')
      {
        return true;
      }
      return false;
    }
    bool validIdentifier( 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' || isPunctuator(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 (isPunctuator(str[i]) == true)
          {
            return false;
          }
        }
      }
      return true;
    }
    bool isOperator(var ch)                         //check if the given character is an operator or not
    {
      if (ch == '+' || ch == '-' || ch == '*' ||
          ch == '/' || ch == '>' || ch == '<' ||
          ch == '=' || ch == '|' || ch == '&')
      {
        return true;
      }
      return false;
    }
    bool isKeyword(var str)                     //check if the given substring is a keyword or not         char*str: str pointer points to adress of char in memory and we made this to pass an array in function ande this is made only in parameters in function
    {
      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;
      }
    }
    bool isNumber(var str)                          //check if the given substring is a number or not
    {
      int i, len = str.size(), numOfDecimal = 0;
      if (len == 0)
      {
        return false;
      }
      for (i = 0; i < len; i++)
      {
        if (numOfDecimal > 1 && str[i] == '.') //numOfDecimal > 1 this means it must pass on number 1
            {
          return false;
        }
        else if (numOfDecimal <= 1)
        {
          numOfDecimal++;
        }
        if (str[i] != '0' && str[i] != '1' && str[i] != '2'
            && str[i] != '3' && str[i] != '4' && str[i] != '5'
            && str[i] != '6' && str[i] != '7' && str[i] != '8'
            && str[i] != '9' || (str[i] == '.' && i > 0))
        {
          return false;
        }
      }
      return true;
    }
    String newString(String oldString, int n) {
      if (oldString.length >= n) {
        return oldString.substring(oldString.length - n);
      } else {
        print("It's a newString");
      }
    }
    void scanner(var str)                       //make a scanner to the expression
    {
      int left = 0, right = 0;
      int len = str.length;
      while (right <= len && left <= right) {
        if (isPunctuator(str[right]) == false)          //if character is a digit or an alphabet
            {
          right++;
        }
    
        if (isPunctuator(str[right]) == true && left == right)      //if character is a punctuator
            {
          if (isOperator(str[right]) == true)
          {
            print("$str[right] IS AN OPERATORn");
    
            right++;
            left = right;
          }
        }
        else if (isPunctuator(str[right]) == true && left != right
            || (right == len && left != right))             //check if scanned substring is a keyword or identifier or number
            {
          var sub = newString(str, n); //extract substring
    
          if (isKeyword(sub) == true)
          {
            print( "$sub IS A KEYWORDn");
          }
          else if (isNumber(sub) == true)
          {
            print( "$sub IS A NUMBERn");
          }
          else if (validIdentifier(sub) == true
              && isPunctuator(str[right - 1]) == false)
          {
            print( "$sub IS A VALID IDENTIFIERn");
          }
          else if (validIdentifier(sub) == false
              && isPunctuator(str[right - 1]) == false)
          {
            print( "$sub ISn't A KEYWORDn");
          }
    
          left = right;
        }
      }
      return;
    }
    
    /*==============================================*/
    /*******PARSER*******/
    
    int n, n1, n2;
    int getPosition(String arr, String q, int size)
    {
      for(int j = 0;j<size;j++)
      {
        if (q == arr[j])
          return j;
      }
      return -1 ;
    }
    
    void main()
    {
      print("|++++++|COMPILER|++++++|n");
      print("n|**|LEXICAL ANALYSER|**|n");
      var l;[100];
      print("Enter a C++ statement to make a scanner n");
      l= stdin.readLineSync();
      scanner(l);
      print("n===============================================n");
      print("n|**|SYNTAX ANALYSER|**|n");
      var prods;[10];
      var first;[10];
      var follow;[10];
      var nonterms;[10];
      var terms;[10];
      var pp_table;[20][20] ;
      print("Enter the number of productions : ");
      n = stdin.readLineSync() as int;
      print( "Enter the productionsn");
      for (int j = 0;j < n;j++)
      {
        prods = stdin.readLineSync();
        print( "Enter first for ");
        print (prods.size(3));
        print(" : ");
        first=stdin.readLineSync();
      }
      print("Enter the number of Terminals : ");
      n2 = stdin.readLineSync() as int;
      print("Enter the Terminalsn");
      for (int j = 0;j < n2;j++)
      {
        terms = stdin.readLineSync();
      }
      terms = "$";
      n2++;
      print("Enter the number of Non-Terminals : ");
      n1 = stdin.readLineSync() as int;
      for (int j = 0;j < n1;j++)
      {
        print("Enter Non-Terminal : ");
        nonterms = stdin.readLineSync();
        print("Enter follow of  $nonterms[j]  : ");
        follow = stdin.readLineSync();
      }
    
    
      print("n");
      print("Grammarn");
      for (int j = 0;j < n;j++)
      {
        print("$prods[j]n");
      }
    
    
      for (int d = 0;d < n;d++)
      {
        int row = getPosition(nonterms, prods[d].size[1], n1);/*substr():This function contains two parameters: pos and len.
            The pos parameter specifies the start position of the substring and len denotes the number of characters in a substring.*/
        if (prods[d].at(3) != '#')//at(): function is used reference the element present at the position given as the parameter to the function.
            {
          for (int j = 0;j < first[d].length;j++)
          {
            int col = getPosition(terms, first[d].substring(0,2), n2);
            pp_table[row][col] = prods[d];
          }
        }
        else
        {
          for (int j = 0;j < follow[row].length;j++)
          {
            int col = getPosition(terms, follow[row].substring(j,2), n2);
            pp_table[row][col] = prods[d];
          }
        }
      }
    //Display Table
      for (int d = 0;d < n2;d++)
        print("t $terms[d]");
      print("n");
      for (int j = 0;j < n1;j++)
      {
        print("$nonterms[j] t");
    //Display Table
        for (int d = 0;d < n2;d++)
        {
          print("$pp_table[j][d] t");
        }
        print("n");
      }
    //Parsing String
      var c;
      do {
        var ip;/* IP address is a unique address that identifies a device on the internet or a local network. IP stands for "Internet Protocol,"
            which is the set of rules governing the format of data sent via the internet or local network.*/
        Queue pp_stack = new Queue();
        pp_stack.addLast("$");
        pp_stack.addLast(prods[0].substring(0,2));
        print("Enter the string to be parsed : ");
        ip=stdin.readLineSync();
        ip.addFirst('$');
        print( "StacktInputtActionn" );
        while (true)
        {
          for (int j = 0;j < pp_stack.length;j++)//size(): function is used to return the size of the deque container or the number of elements in the deque container.
            print( "$pp_stack[j]");
          print( "t  $ip  t");
          int row1 = getPosition(nonterms, pp_stack.first(), n1);
          int row2 = getPosition(terms, pp_stack.first(), n2);
          int column = getPosition(terms, ip.substring(0,2), n2);
          if (row1 != -1 && column != -1)
          {
            var p = pp_table[row1][column];
            if (p.isEmpty)//empty() function is used to check if the deque container is empty or not.
                {
              print(  "nString cannot be Parsed.n" );
              break;
            }
            pp_stack.removeFirst();
            if (p[3] != '#')
            {
              for (int x = p.length - 1;x > 2;x--)
              {
                pp_stack.removeLast();
              }
            }
            print(p);
          }
          else
          {
            if (ip.size(1) == pp_stack.first())
            {
              if (pp_stack.first() == "$")// front() function is used to reference the first element of the deque container.
                  {
                print("nString Parsed.n");
                break;
              }
              print("Match $ip[0]");
              pp_stack.removeLast();
              ip = ip.size(1);
            }
            else
            {
              print("nString cannot be Parsed.n");
              break;
            }
          }
          print("n");
        }
        print("Continue?(Y/N)");
        c=stdin.readLineSync();
    
      } while (c == 'y' || c == 'Y');
    }
    /*This is a code about compiler that is a conversion of code from high level language to low level machine language understood by computer and there are phases of which 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.
Syntax analysis or parsing is the second phase of a compiler. In this chapter, we shall learn the basic concepts used in the construction of a parser. We have seen that a lexical analyzer can identify tokens with the help of regular expressions and pattern rules. But a lexical analyzer cannot check the syntax of a given sentence due to the limitations of the regular expressions. Regular expressions cannot check balancing tokens, such as parenthesis. Therefore, this phase uses context-free grammar (CFG), which is recognized by push-down automata. It implies that every Regular Grammar is also context-free, but there exists some problems, which are beyond the scope of Regular Grammar. CFG is a helpful tool in describing the syntax of programming languages. In this section, we will first see the definition of context-free grammar and introduce terminologies used in parsing technology.

A context-free grammar has four components:

A set of non-terminals (V). Non-terminals are syntactic variables that denote sets of strings. The non-terminals define sets of strings that help define the language generated by the grammar.

A set of tokens, known as terminal symbols (Σ). Terminals are the basic symbols from which strings are formed.

A set of productions (P). The productions of a grammar specify the manner in which the terminals and non-terminals can be combined to form strings. Each production consists of a non-terminal called the left side of the production, an arrow, and a sequence of tokens and/or on- terminals, called the right side of the production.

One of the non-terminals is designated as the start symbol (S); from where the production begins.

The strings are derived from the start symbol by repeatedly replacing a non-terminal (initially the start symbol) by the right side of a production, for that non-terminal.

Example
We take the problem of palindrome language, which cannot be described by means of Regular Expression. That is, L = { w | w = wR } is not a regular language. But it can be described by means of CFG
*/

Source: Windows Questions

LEAVE A COMMENT