Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Lab_Final
- {
- struct wt
- {
- public string Word;
- public string Type;
- public wt(string w, string t)
- {
- Word = w;
- Type = t;
- }
- }
- class Lexical
- {
- public static ArrayList ALLex = new ArrayList();
- public static void Lex(string st)
- {
- int i = 0;
- char c;
- char[] ch = st.ToCharArray(0, st.Length);
- string w = "";
- c = getchar(ref i, ch);
- while (c != '$')
- {
- w = "";
- if ((c == ' ') || (c == '\r') || (c == '\n') || (c == '\t'))
- {
- c = getchar(ref i, ch);
- continue;
- }
- else if (char.IsLetter(c))
- {
- w += c;
- c = getchar(ref i, ch);
- while (char.IsLetter(c))
- {
- w += c;
- c = getchar(ref i, ch);
- }
- if (RW(w))
- {
- ALLex.Add(new wt(w, "RW"));
- w = "";
- }
- else
- {
- ALLex.Add(new wt(w, "ID"));
- }
- unget(ref i, ch);
- }
- else if (char.IsDigit(c))
- {
- w += c;
- c = getchar(ref i, ch);
- while (char.IsDigit(c))
- {
- w += c;
- c = getchar(ref i, ch);
- }
- if (c == '.')
- {
- w += c;
- c = getchar(ref i, ch);
- while (char.IsDigit(c))
- {
- w += c;
- c = getchar(ref i, ch);
- }
- unget(ref i, ch);
- ALLex.Add(new wt(w, "float_const"));
- }
- else
- {
- ALLex.Add(new wt(w, "int_const"));
- unget(ref i, ch);
- }
- }
- else if (OP(c))
- {
- if (c == '=')
- {
- w += c;
- c = getchar(ref i, ch);
- if (c == '=')
- {
- w += c;
- ALLex.Add(new wt(w, "OP"));
- }
- else
- {
- ALLex.Add(new wt(w, "OP"));
- unget(ref i, ch);
- }
- }
- else if (c == '!')
- {
- w += c;
- c = getchar(ref i, ch);
- if (c == '=')
- {
- w += c;
- ALLex.Add(new wt(w, "OP"));
- }
- else
- {
- ALLex.Add(new wt(w, "OP"));
- unget(ref i, ch);
- }
- }
- else if (c == '<')
- {
- w += c;
- c = getchar(ref i, ch);
- if (c == '=')
- {
- w += c;
- ALLex.Add(new wt(w, "OP"));
- }
- else
- {
- ALLex.Add(new wt(w, "OP"));
- unget(ref i, ch);
- }
- }
- else if (c == '>')
- {
- w += c;
- c = getchar(ref i, ch);
- if (c == '=')
- {
- w += c;
- ALLex.Add(new wt(w, "OP"));
- }
- else
- {
- ALLex.Add(new wt(w, "OP"));
- unget(ref i, ch);
- }
- }
- else if (c == '|')
- {
- w += c;
- c = getchar(ref i, ch);
- if (c == '|')
- {
- w += c;
- ALLex.Add(new wt(w, "OP"));
- }
- else
- {
- }
- unget(ref i, ch);
- }
- else if (c == '&')
- {
- w += c;
- c = getchar(ref i, ch);
- if (c == '&')
- {
- w += c;
- ALLex.Add(new wt(w, "OP"));
- }
- else
- {
- }
- unget(ref i, ch);
- }
- else
- {
- w += c;
- ALLex.Add(new wt(w, "OP"));
- }
- }
- else if (c == '"')
- {
- w += c;
- c = getchar(ref i, ch);
- while (c != '"')
- {
- w += c;
- c = getchar(ref i, ch);
- }
- w += c;
- ALLex.Add(new wt(w, "string_const"));
- }
- else if (c == '@')
- {
- w += c;
- c = getchar(ref i, ch);
- while (c != '@')
- {
- w += c;
- c = getchar(ref i, ch);
- }
- w += c;
- }
- c = getchar(ref i, ch);
- }
- ALLex.Add(new wt("$", "$"));
- }
- public static char getchar(ref int i, char[] ch)
- {
- char c;
- c = ch[i];
- i++;
- return c;
- }
- public static char unget(ref int i, char[] ch)
- {
- i--;
- char c;
- c = ch[i];
- return c;
- }
- public static void print()
- {
- foreach (wt tk in ALLex)
- {
- Console.WriteLine("\t" + tk.Word + "\t" + tk.Type);
- }
- }
- public static bool RW(string s)
- {
- switch (s)
- {
- case "int":
- case "float":
- case "string":
- case "Program":
- case "begin":
- case "end":
- case "fun":
- case "call":
- case "while":
- return true;
- default:
- return false;
- }
- }
- public static bool OP(char c)
- {
- switch (c)
- {
- case '=':
- case '+':
- case '-':
- case '*':
- case '/':
- case '(':
- case ')':
- case ';':
- case ',':
- case '<':
- case '>':
- case '!':
- case '|':
- case '&':
- return true;
- default:
- return false;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement