Nothing Special   »   [go: up one dir, main page]

LAB 5 Compiler Construction

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Spring 2021 Session 2018-22

LAB # 05
Statement Purpose:
Lexical analysis is the first phase of a compiler. It takes the modified source code from language
preprocessors that are written in the form of sentences. The lexical analyzer breaks these
syntaxes into a series of tokens, by removing any whitespace or comments in the source code. If
the lexical analyzer finds a token invalid, it generates an error. The lexical analyzer works
closely with the syntax analyzer. It reads character streams from the source code, checks for
legal tokens, and passes the data to the syntax analyzer when it demands.

In programming language, keywords, constants, identifiers, strings, numbers, operators and


punctuations symbols can be considered as tokens.

Activity Outcomes:
This lab teaches you
 How to recognize keywords from a source program written in a high level language

Instructor Note:

Students should know how to write regular expressions in C#


Introduction

A regular expression is a pattern that describes a set of strings. Regular expressions are
constructed analogously to arithmetic expressions by using various operators to combine smaller
expressions.
The fundamental building blocks are the regular expressions that match a single character. Most
characters, including all letters and digits, are regular expressions that match themselves. Any
meta character with special meaning may be quoted by preceding it with a backslash. In basic
regular expressions the metacharacters "?", "+", "{", "|", "(", and ")" lose their special meaning;
instead use the backslashed versions "\?", "\+", "\{", "\|", "\(", and "\)".

Regular Expression for keywords

[int | float | double | char]


Lab Activities:
Activity 1:
Design a regular expression for keywords

Solution:
This example replaces extra white space:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;
namespace Sessional1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

}
private void button1_Click(object sender, EventArgs e)
{
// take input from a richtextbox/textbox
String var = richTextBox1.Text;
// split the input on the basis of space
String[] words = var.Split(' ');
// Regular Expression for variables
Regex regex1 = new Regex(@"^[int | float | char] $");
for (int i = 0; i < words.Length; i++)
{
Match match1 = regex1.Match(words[i]);
if (match1.Success)
{
richTextBox2.Text += words[i] + " ";
}
else {
MessageBox.Show("invalid "+words[i]);
}
}
}
}
Home Activities:

Activity 1:
Design a regular expression for finding all the words starting with ‘t’ and ‘m’ in the following
document.

You might also like