0% found this document useful (0 votes)
7 views

Lab 4

Uploaded by

Qaiser Khan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Lab 4

Uploaded by

Qaiser Khan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Compiler Construction

Lab #4

Huzaifa Jameel
RC 28
CS 34 A

Department of Computer Science


NUML, Rawalpindi
Activity
using System.Collections;

using System.Text.RegularExpressions;

namespace WinFormsApp4

public partial class Form1 : Form

public Form1()

InitializeComponent();

private void button1_Click(object sender, EventArgs e)

//taking user input from rich textbox

String userInput = tfInput.Text;

//List of keywords which will be used to seperate keywords from


variables

List<String> keywordList = new List<String>();

keywordList.Add("int");

keywordList.Add("float");

keywordList.Add("double");

keywordList.Add("string");

keywordList.Add("char");

keywordList.Add("while");
keywordList.Add("main");

keywordList.Add("if");

keywordList.Add("else");

keywordList.Add("new");

//row is an index counter for symbol table

int row = 1;

//count is a variable to increment variable id in tokens

int count = 1;

//line_num is a counter for lines in user input

int line_num = 0;

//SymbolTable is a 2D array that has the following structure

//[Index][Variable Name] [type][value][line#]

//rows are incremented with each variable information entry

String[,] symbolTable = new String[20, 6];

List<String> varlistinSymbolTable = new List<String>();

//Input Buffering

ArrayList finalArray = new ArrayList();

ArrayList finalArrayc = new ArrayList();

ArrayList tempArray = new ArrayList();

char[] charinput = userInput.ToCharArray();

//Regular Expression for Variables

Regex variable_Reg = new Regex(@"^[A-Za-z_][A-Za-z0-9]*$");

//Regular Expression for Constants


Regex constants_Reg = new Regex(@"^[0-9]+([.][0-9]+)?([e]([+-])?[0-
9]+)?$");

//Regular Expression for Operators

Regex operators_Reg = new Regex(@"^[-*+/><&&||=]$");

//Regular Expression for Special_Characters

Regex Special_Reg = new Regex(@"^[.,'\[\]{}();:?]$");

for (int itr = 0; itr < charinput.Length; itr++)

if (charinput[itr].Equals('\n'))

if (tempArray.Count != 0)

int j = 0;

String fin = "";

for (; j < tempArray.Count; j++)

fin += tempArray[j];

finalArray.Add(fin);

tempArray.Clear();

else

tempArray.Add(charinput[itr]);

}
if (tempArray.Count != 0)

int j = 0;

String fin = "";

for (; j < tempArray.Count; j++)

fin += tempArray[j];

finalArray.Add(fin);

tempArray.Clear();

// Final Array SO far correct

tfTokens.Clear();

symbolTable = new String[20, 6];

//looping on all lines in user input

for (int i = 0; i < finalArray.Count; i++)

String line = finalArray[i].ToString();

char[] lineChar = line.ToCharArray();

line_num++;

//taking current line and splitting it into lexemes by space

for (int itr = 0; itr < lineChar.Length; itr++)

Match Match_Variable = variable_Reg.Match(lineChar[itr] +


"");
Match Match_Constant = constants_Reg.Match(lineChar[itr] +
"");

Match Match_Operator = operators_Reg.Match(lineChar[itr] +


"");

Match Match_Special = Special_Reg.Match(lineChar[itr] + "");

if (Match_Variable.Success || Match_Constant.Success)

tempArray.Add(lineChar[itr]);

if (lineChar[itr].Equals(' '))

if (tempArray.Count != 0)

int j = 0;

String fins = "";

for (; j < tempArray.Count; j++)

fins += tempArray[j];

finalArrayc.Add(fins);

tempArray.Clear();

if (Match_Operator.Success || Match_Special.Success)

{
if (tempArray.Count != 0)

int j = 0;

String fins = "";

for (; j < tempArray.Count; j++)

fins += tempArray[j];

finalArrayc.Add(fins);

tempArray.Clear();

finalArrayc.Add(lineChar[itr]);

if (tempArray.Count != 0)

String fina = "";

for (int j = 0; j < tempArray.Count; j++)

fina += tempArray[j];

finalArrayc.Add(fina);

tempArray.Clear();
}

// we have a splitted line here

for (int x = 0; x < finalArrayc.Count; x++)

Match operators =
operators_Reg.Match(finalArrayc[x].ToString());

Match variables =
variable_Reg.Match(finalArrayc[x].ToString());

Match digits =
constants_Reg.Match(finalArrayc[x].ToString());

Match punctuations =
Special_Reg.Match(finalArrayc[x].ToString());

if (operators.Success)

// if a current lexeme is an operator then make a token


e.g. < op, = >

tfTokens.AppendText("<op, " + finalArrayc[x].ToString()


+ "> ");

else if (digits.Success)

// if a current lexeme is a digit then make a token e.g.


< digit, 12.33 >

tfTokens.AppendText("<digit, " +
finalArrayc[x].ToString() + "> ");

else if (punctuations.Success)

{
// if a current lexeme is a punctuation then make a token
e.g. < punc, ; >

tfTokens.AppendText("<punc, " + finalArrayc[x].ToString()


+ "> ");

else if (variables.Success)

// if a current lexeme is a variable and not a keyword

if (!keywordList.Contains(finalArrayc[x].ToString()))

// if it is not a keyword

// check what is the category of varaible, handling


only two cases here

//Category1- Variable initialization of type digit


e.g. int count = 10 ;

//Category2- Variable initialization of type String


e.g. String var = ' Hello ' ;

Regex reg1 = new Regex(@"^(int|float|double)\s([A-Za-


z|_][A-Za-z|0-9]{0,10})\s?(=)\s?([0-9]+([.][0-9]+)?([e][+|-]?[0-9]+)?)\s?(;)$");

// line of type int alpha = 2 ;

Match category1 = reg1.Match(line);

Regex reg2 = new Regex(@"^(string|char)\s([A-Za-z|_]


[A-Za-z|0-9]{0,10})\s?(=)\s?[']\s?([A-Za-z|_][A-Za-z|0-9]{0,30})\s?[']\s?(;)$");

// line of type String alpha = ' Hello ' ;

Match category2 = reg2.Match(line);

//if it is a category 1 then add a row in symbol


table containing the information related to that variable

if (category1.Success)

{
symbolTable[row, 1] = row.ToString();

//index

symbolTable[row, 2] = finalArrayc[x].ToString();

//variable name

symbolTable[row, 3] = finalArrayc[x -
1].ToString();

//type

symbolTable[row, 4] = finalArrayc[x +
2].ToString();

//value

symbolTable[row, 5] = line_num.ToString();

// line number

tfTokens.AppendText("<var" + count + ", " + row


+ "> ");

tfSymbolTable.AppendText(symbolTable[row,
1].ToString() + " \t ");

tfSymbolTable.AppendText(symbolTable[row,
2].ToString() + " \t ");

tfSymbolTable.AppendText(symbolTable[row,
3].ToString() + " \t ");

tfSymbolTable.AppendText(symbolTable[row,
4].ToString() + " \t ");

tfSymbolTable.AppendText(symbolTable[row,
5].ToString() + " \n ");

row++;

count++;

}
else if (category2.Success)

// if a line such as String var = 'Hello ; comes


and the loop moves to index of array containing Hel

//then this if condition prevents addition of


Hello in symbol Table because it is not a variable it is j

if (!(finalArrayc[x - 1].ToString().Equals("'")
&& finalArrayc[x + 1].ToString().Equals("'")))

symbolTable[row, 1] = row.ToString(); //
index

symbolTable[row, 2] =
finalArrayc[x].ToString(); //varname

symbolTable[row, 3] = finalArrayc[x -
1].ToString(); //type

symbolTable[row, 4] = finalArrayc[x +
3].ToString(); //value

symbolTable[row, 5] = line_num.ToString(); //
line number

tfTokens.AppendText("<var" + count + "," +


row + "> ");

tfTokens.AppendText("<var" + count + "," +


row + "> ");

tfSymbolTable.AppendText(symbolTable[row,
1].ToString() + " \t ");

tfSymbolTable.AppendText(symbolTable[row,
2].ToString() + " \t ");

tfSymbolTable.AppendText(symbolTable[row,
3].ToString() + " \t ");
tfSymbolTable.AppendText(symbolTable[row,
4].ToString() + " \t ");

tfSymbolTable.AppendText(symbolTable[row,
5].ToString() + " \n ");

row++;

count++;

else

tfTokens.AppendText("<String" + count + ", "


+ finalArrayc[x].ToString() + "> ");

else

// if any other category line comes in we check


if we have initialized that varaible before,

// if we have initiazed it before then we put the


index of that variable in symbol table,

//in its token

String ind = "Default";

String ty = "Default";

String val = "Default";

String lin = "Default";

for (int r = 1; r <= symbolTable.GetLength(0);


r++)

{ //search in the symbol table if variable entry


already exists
if (symbolTable[r,
2].Equals(finalArrayc[x].ToString()))

ind = symbolTable[r, 1];

ty = symbolTable[r, 3];

val = symbolTable[r, 4];

lin = symbolTable[r, 5];

tfTokens.AppendText("<var" + ind + ", " +


ind + "> ");

break;

// if a current lexeme is not a variable but a keyword


then make a token such as: <keyword, int>

else

tfTokens.AppendText("<keyword, " +
finalArrayc[x].ToString() + "> ");

finalArrayc.Clear();

tfTokens.AppendText("\n");

finalArrayc.Clear();

}
Output

You might also like