Problem with List string

2

Hi, I have a problem trying to modify a code in a lexical analyzer and I want it to identify reserved words. The original code came with List<char> but when I change the char for string I get an error in this part:

List<string> _elementos = txtExpresion.Text.Replace(" ","").ToCharArray().ToList();

and I really do not know how to solve that.

public partial class frmPrincipal : Form
{

    List<string> _numeros = new List<string>(new string[] { "0","1"});
    List<string> _variables = new List<string>(new string[] { "a"});
    List<string> _operadores = new List<string>(new string[] { "+" });
    List<string> _palabras = new List<string>(new string[] { "definir" });
    List<string> _delimitadores = new List<string>(new string[] { "(" });
    DataTable _tblResultados = new DataTable();

    public frmPrincipal()
    {
        InitializeComponent();           
    }

    private void frmPrincipal_Load(object sender, EventArgs e)
    {
        _tblResultados.Columns.Add("Token", typeof(string));
        _tblResultados.Columns.Add("Tipo", typeof(string));
    }      

    private void btnAnalizar_Click(object sender, EventArgs e)
    {
        _tblResultados.Clear();

        List<string> _elementos = txtExpresion.Text.Replace(" ", "").ToCharArray().ToList();

        if (_elementos.Count > 0)
        {
            DataRow _fila;

            foreach (string elemento in _elementos)
            {
                _fila = _tblResultados.NewRow();

                if (_numeros.Contains(elemento))
                {
                    _fila["Token"] = elemento;
                    _fila["Tipo"] = "Número";
                }
                else if (_variables.Contains(elemento))
                {
                    _fila["Token"] = elemento;
                    _fila["Tipo"] = "Variable";
                }
                else if (_operadores.Contains(elemento))
                {
                    _fila["Token"] = elemento;
                    _fila["Tipo"] = "Operador";
                }
                else if (_delimitadores.Contains(elemento))
                {
                    _fila["Token"] = elemento;
                    _fila["Tipo"] = "Delimitador";
                }
                else if (_palabras.Contains(elemento))
                {
                    _fila["Token"] = elemento;
                    _fila["Tipo"] = "Palabras";
                }
                else
                {
                    _fila["Token"] = elemento;
                    _fila["Tipo"] = "Error";
                }

                _tblResultados.Rows.Add(_fila);
            }

            dgvResultados.DataSource = _tblResultados;
            dgvResultados.Refresh();
        }
        else
        {
            dgvResultados.DataSource = null;
            dgvResultados.Refresh();
        }
    }
    
asked by outsider 16.03.2018 в 03:29
source

1 answer

2

you must use the split function (you pass it as a parameter the separator, that is, a blank space) to get the array of words and cast to list

List<string> _elementos = txtExpresion.Split(' ').ToList();
    
answered by 16.03.2018 / 03:52
source