Skip to content

Instantly share code, notes, and snippets.

@DavideDunne
Created March 13, 2023 21:01
Show Gist options
  • Save DavideDunne/f10f4dbe30ce13535b53668606d6b688 to your computer and use it in GitHub Desktop.
Save DavideDunne/f10f4dbe30ce13535b53668606d6b688 to your computer and use it in GitHub Desktop.
C# Algebraic Expression Automaton: TC2037.603 Project 1
//Davide Dunne Sanchez A01642355@tec.mx
//Raúl Antonio Castillejos Santillán A01174919@tec.mx
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
// Listado de estados tanto en cada char como cada conjunto de chars en la cadena
enum State{
Empty,
Entero,
Flotante,
Suma,
Resta,
Multiplicacion,
Division,
Asignacion,
Variable,
Punto,
AbreParentesis,
CierraParentesis,
Death
}
class Program{
static public State getCharState(char c){
State whatIsNow=State.Empty;
if((int)c==32) whatIsNow=State.Empty;
else if((int)c==40) whatIsNow=State.AbreParentesis;
else if((int)c==41) whatIsNow=State.CierraParentesis;
else if((int)c==42) whatIsNow=State.Multiplicacion;
else if((int)c==43) whatIsNow=State.Suma;
else if((int)c==45) whatIsNow=State.Resta;
else if((int)c==46) whatIsNow=State.Punto;
else if((int)c==49) whatIsNow=State.Division;
else if((int)c>=48 && (int)c<=57) whatIsNow=State.Entero;
else if((int)c==61) whatIsNow=State.Asignacion;
else if((int)c>=65 && (int)c<=90) whatIsNow=State.Variable;
else if((int)c>=97 && (int)c<=122) whatIsNow=State.Variable;
else whatIsNow=State.Death;
return whatIsNow;
}
static public void lexerAritmetico(string linea){
List<Tuple<string,State>>myTable=new List<Tuple<String,State>>();
State myState=getCharState(linea[0]);
State whatIsNow=State.Empty;
string currentContinuous=Char.ToString(linea[0]);
if(linea.Length==1) myTable.Add(new Tuple<String,State>(currentContinuous,myState));
else{
for(int i=1;i<linea.Length;i++){
whatIsNow=getCharState(linea[i]);
if(whatIsNow==State.Punto && myState==State.Entero){
currentContinuous+=linea[i];
myState=State.Flotante;
}
else if(whatIsNow==State.Entero && myState==State.Flotante){
currentContinuous+=linea[i];
}
else if(whatIsNow==State.Empty) continue;
else if(whatIsNow==State.Death){
myTable.Add(new Tuple<string,State>(currentContinuous,myState));
myState=State.Death;
currentContinuous=Char.ToString(linea[i]);
myTable.Add(new Tuple<string,State>(currentContinuous,myState));
break;
}
else if(whatIsNow==State.Entero && myState==State.Resta){
currentContinuous+=linea[i];
myState=State.Entero;
}
else if(whatIsNow==myState){
currentContinuous+=linea[i];
}
else if(whatIsNow!=myState){
myTable.Add(new Tuple<string,State>(currentContinuous,myState));
currentContinuous=Char.ToString(linea[i]);
myState=whatIsNow;
}
}
myTable.Add(new Tuple<string,State>(currentContinuous,myState));
}
foreach(Tuple<string,State>i in myTable) Console.WriteLine(i.Item1+" "+i.Item2);
}
static void Main(){
foreach(string line in File.ReadLines("file.txt")){
Console.WriteLine(line);
lexerAritmetico(line);
Console.WriteLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment