| 1: | /* Created by SharpDevelop. | |
| 2: | * User: Eduardo Rocha Monteiro ([email protected]) | |
| 3: | * Date: 25/11/2004 | |
| 4: | * Time: 23:51 | |
| 5: | */ | |
| 6: | ||
| 7: | using System; | |
| 8: | using System.IO; | |
| 9: | using System.Text.RegularExpressions; | |
| 10: | using Compilador.Util; | |
| 11: | ||
| 12: | ||
| 13: | namespace Compilador.AnalisadorLexicoN.AutomatoN | |
| 14: | { | |
| 15: | ||
| 16: | ||
| 17: | /// <summary> | |
| 18: | /// Contera o automato definido em um arquivo Txt. | |
| 19: | /// Eh um lista encadeada. | |
| 20: | /// </summary> | |
| 21: | public class Automato | |
| 22: | { | |
| 23: | ||
| 24: | public int _index; | |
| 25: | public TipoCaracter _caracterLido; | |
| 26: | public int _estadoAtual; | |
| 27: | public int _estadoSeguinte; | |
| 28: | public int _acao; | |
| 29: | public int _proximo; | |
| 30: | public Automato first; | |
| 31: | public Automato next; | |
| 32: | ||
| 33: | public Automato () | |
| 34: | { | |
| 35: | } | |
| 36: | ||
| 37: | public Automato(string automatoTxt) | |
| 38: | { | |
| 39: | first = Load(automatoTxt); | |
| 40: | } | |
| 41: | ||
| 42: | public Automato Load(string automatoTxt) | |
| 43: | { | |
| 44: | Automato a = new Automato(); | |
| 45: | StreamReader sr = new StreamReader(automatoTxt); | |
| 46: | string automatoStr; | |
| 47: | a.first=a; | |
| 48: | while ((automatoStr = sr.ReadLine()) != null) | |
| 49: | { | |
| 50: | a.Add(LoadLine(automatoStr)); | |
| 51: | a=a.next; | |
| 52: | } | |
| 53: | return a.first; | |
| 54: | } | |
| 55: | ||
| 56: | public void Add(Automato automato) | |
| 57: | { | |
| 58: | next = new Automato(); | |
| 59: | next = automato; | |
| 60: | next.first= first; | |
| 61: | } | |
| 62: | ||
| 63: | public Automato LoadLine(string line) | |
| 64: | { | |
| 65: | Automato automato = new Automato(); | |
| 66: | string aux=""; | |
| 67: | int i = 0; | |
| 68: | while(line.Substring(i,1)!=" ") | |
| 69: | { | |
| 70: | aux+=line.Substring(i,1); | |
| 71: | i++; | |
| 72: | } | |
| 73: | automato._index = Convert.ToInt32(aux); | |
| 74: | i++; | |
| 75: | aux=""; | |
| 76: | while(line.Substring(i,1)!=" ") | |
| 77: | { | |
| 78: | aux+=line.Substring(i,1); | |
| 79: | i++; | |
| 80: | } | |
| 81: | automato._estadoAtual = Convert.ToInt32(aux); | |
| 82: | i++; | |
| 83: | aux=""; | |
| 84: | while(line.Substring(i,1)!=" ") | |
| 85: | { | |
| 86: | aux+=line.Substring(i,1); | |
| 87: | i++; | |
| 88: | } | |
| 89: | automato._caracterLido=DefineCaracterLido(aux); | |
| 90: | i++; | |
| 91: | aux=""; | |
| 92: | while(line.Substring(i,1)!=" ") | |
| 93: | { | |
| 94: | aux+=line.Substring(i,1); | |
| 95: | i++; | |
| 96: | } | |
| 97: | automato._estadoSeguinte = Convert.ToInt32(aux); | |
| 98: | i++; | |
| 99: | aux=""; | |
| 100: | while(line.Substring(i,1)!=" ") | |
| 101: | { | |
| 102: | aux+=line.Substring(i,1); | |
| 103: | i++; | |
| 104: | } | |
| 105: | automato._acao = Convert.ToInt32(aux); | |
| 106: | i++; | |
| 107: | aux=""; | |
| 108: | while(i < line.Length) | |
| 109: | { | |
| 110: | ||
| 111: | aux+=line.Substring(i,1); | |
| 112: | i++; | |
| 113: | } | |
| 114: | automato._proximo = Convert.ToInt32(aux); | |
| 115: | return automato; | |
| 116: | } | |
| 117: | ||
| 118: | public Automato Posiciona(int estado) | |
| 119: | { | |
| 120: | Automato a = this.first; | |
| 121: | while (a!=null) | |
| 122: | { | |
| 123: | if (estado == a._estadoAtual) | |
| 124: | { | |
| 125: | return a; | |
| 126: | } | |
| 127: | a = a.next; | |
| 128: | } | |
| 129: | return null; | |
| 130: | } | |
| 131: | ||
| 132: | public static TipoCaracter DefineCaracterLido(string aux) | |
| 133: | { | |
| 134: | switch(aux) { | |
| 135: | ||
| 136: | case "d": | |
| 137: | return TipoCaracter.Digito; | |
| 138: | ||
| 139: | case "l": | |
| 140: | return TipoCaracter.Letra; | |
| 141: | ||
| 142: | case "-": | |
| 143: | return TipoCaracter.SimbMenos; | |
| 144: | ||
| 145: | case "e": | |
| 146: | return TipoCaracter.Finais; | |
| 147: | ||
| 148: | case "f": | |
| 149: | return TipoCaracter.LetraOuEspeciais; | |
| 150: | ||
| 151: | case "<": | |
| 152: | return TipoCaracter.SimbMenor; | |
| 153: | ||
| 154: | case "h": | |
| 155: | return TipoCaracter.Especiais; | |
| 156: | ||
| 157: | case "g": | |
| 158: | return TipoCaracter.LetraOuDigito; | |
| 159: | ||
| 160: | default: | |
| 161: | Console.WriteLine("Erro no automato"); | |
| 162: | return 0; | |
| 163: | } | |
| 164: | ||
| 165: | } | |
| 166: | ||
| 167: | public void Imprime () | |
| 168: | { | |
| 169: | Automato b = this; | |
| 170: | while(b!=null) | |
| 171: | { | |
| 172: | Console.WriteLine(b._index); | |
| 173: | b = b.next; | |
| 174: | } | |
| 175: | } | |
| 176: | ||
| 177: | } | |
| 178: | } |
This page was automatically generated by SharpDevelop.