Sie sind auf Seite 1von 31

Laxical Analyzer

Ayeen muhammad (15497) Faran ahmer khan(15242) mirza wakalat baig(14243)

shery baig

Assignme
nt #

Task

page #

Due
Date

01

SourceCode

2 To-8

9/3/201
4

01

Tokens

8-To-10

9/3/201
4

Group Members:
01

Regular

10-to-13

9/3/201
4

13-t0-30

9/3/201
4

Ayeen MuhammadExp
khan (15497)
Faran01
ahmer khanFA
(15242)
Mirza wakalat baig (14243)
01

Algo

Sub: Compiler Construction

30-TO-32

Date : 9/3/14
Submitted to: Sir Aihab Khan

Remarks

Source Code:LexicalAnalysis.h
// LexicalAnalysis.h: interface for the LexicalAnalysis class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(_LA_)
#define _LA_
#include <iostream>
#include <string>
using namespace std;
enum { DIGIT, LETTER, IDENT, INT_LIT, ERROR, SPACE, STOP, Logics,Rel_op };
class LexicalAnalysis
{
public:
char nextChar;
int charClass;
string lexeme;
private:
string input;

public:
int lex();
void setNewInput(string in);
LexicalAnalysis(string in = "");

virtual ~LexicalAnalysis();
private:
void addChar();
void getChar();
};
#endif

Source.Cpp
#include "LexicalAnalysis.h"
LexicalAnalysis::LexicalAnalysis(string in) : input(in), charClass(ERROR), nextChar('
'), lexeme("")
{
getChar();
}
LexicalAnalysis::~LexicalAnalysis()
{
}
void LexicalAnalysis::setNewInput(string in)
{
input = in;
getChar();
}

void LexicalAnalysis::getChar()
{
if (input.size()>0) {
nextChar = input[0];

input.erase(0, 1);
}
else nextChar = '$';

charClass = ERROR;

if ((nextChar > 64 && nextChar <91) || (nextChar > 96 && nextChar <123))
charClass = LETTER;

if (nextChar > 47 && nextChar <58)


charClass = DIGIT;

if (nextChar == ' ') charClass = SPACE;


if (nextChar == '$') charClass = STOP;
if (nextChar == '+') charClass = Rel_op;
if (nextChar == '=') charClass = Logics;
if (nextChar == '-') charClass = Rel_op;
if (nextChar == '*') charClass = Rel_op;
if (nextChar == '/') charClass = Logics;
if (nextChar == '>') charClass = Logics;
if (nextChar == '<') charClass = Logics;
if (nextChar == '<=') charClass = Logics;
if (nextChar == '=>') charClass = Logics;
}
void LexicalAnalysis::addChar()
{

lexeme += nextChar;
}
int LexicalAnalysis::lex()
{

lexeme = "";

while (charClass == SPACE) getChar();

if (charClass == ERROR) { addChar(); getChar(); return ERROR; }


if (charClass == STOP) { return STOP; }
if (charClass == Rel_op) { addChar(); getChar(); return Rel_op; }
switch (charClass) {
case LETTER:
addChar();
getChar();
while (charClass == LETTER || charClass == DIGIT)
{
addChar();
getChar();
}
return IDENT;
break;
case DIGIT:
addChar();
getChar();

while (charClass == DIGIT) {


addChar();
getChar();
}
return INT_LIT;
break;
case Logics:
addChar();
getChar();
return Logics;
break;
case Rel_op:
addChar();
getChar();
return Rel_op;
break;
}
}
Test.Cpp
#include <iostream>
#include <string>
#include<conio.h>
#include "LexicalAnalysis.h"
//using namespace std;
string convertToken(int t);
void main (void)

{
cout<<"========== Testing lexical analyzer"<<endl;
int token=SPACE;
//Testing LexicalAnalysis
LexicalAnalysis LA("#include <iostream> using namespac std; int main{int
num,Factorial=1;cout<<""Enter number to finds its factorial:"";cin>>num;for(int
i=1;i<=num;i++){factorail=factoraial*i;}cout<<""factorial of given number is
=""<<factorail<<endl;return 0;} ");

while (token!=STOP) {
token = LA.lex();
cout<<"lexeme:
"<<LA.lexeme<<"
convertToken(token)<<endl;
}
int a;
cin>>a;
//getch();
}
string convertToken(int t) {
switch (t) {
case SPACE: return "SPACE";
case LETTER: return "LETTER";
case DIGIT: return "DIGIT";
case IDENT: return "IDENT";
case INT_LIT:return "INT_LIT";
case ERROR: return "Delimator";
case STOP: return "STOP";
case PLUS_CODE: return "Relational";

token:

"<<

default: return "Logical";


}
}

a. List of tokens supported by your lexical analyzer

Lexeme
#
Include
<
Iostream
>

Token
Identifier
Keyword
Relational Operator
Keyword
Relational Operator

Pattern
#
include
<
iostream
>

Using
Namespace
Std
;
Int
Main
(
)
{
Cout
<<

String

;
Cin
>>
Num
;
For
(
Int
I
=
1
;
I
<=
Num
;
i
++
{
Factorial
=
Factorial
*
I
;
}
Cout
<<

String

<<
Factorial
<<
Endl
;
Return
0
}

Key word
Key word
Key word
Delimiter
Key word
Key word
Brackets
Brackets
Brackets
Key word
Relational operators
Double quotes
String
Double quotes
Delimiter
Keyword
Relational operator
Identifier
Delimiter
Keyword
Brackets
Keyword
Identifier
Assignment operator
Integer
Delimiter
Identifier
Relational operator
Identifier
Delimiter
Identifier
Arithmetic operator
Brackets
Identifier
Assignment operator
Identifier
Arithmetic operator
Identifier
Delimiter
Brackets
Keywords
Relational operator
Double quotes
String
Double brackets
Relational operator
Identifier
Relational operator
Keyword
Delimiter
Keyword
Integer
Brackets

Using
namespace
std
;
Int
Main
(
)
{
Cout
<<

Enter to find fact

;
Cin
>>
L(l/D)*
;
For
(
Int
L(l/d)*
=
d(d)*
;
I
<=
L(l/d)*
;
L(l/d)*
++
{
L(l/d)*
=
L(l/d)*
*
L(l/d)*
;
}
Cout
<<

L(l/d)*

<<
L(l/d)*
<<
Endl
;
Return
D*
}

b. Regular expression for token specification


Token
#
Include
<
Iostream
>
Using
Namespace
Std
;
Int
Main
(
)
{
Cout
<<

String

;
Cin
>>
Num
;
For
(
Int
I
=
1
;
I
<=
Num
;
i
++
{
Factorial

Regular Expression
Identifier
L(l)*
Relational Operator
L(l)*
Relational Operator
L(l)*
L(L)*
L(L)*
Delimiter
L(L)*
L(L)*
Brackets
Brackets
Brackets
L(L)*
Relational operators
Double quotes
L(l/d)*
Double quotes
Delimiter
L(l)*
Relational operator
L(L)*
Delimiter
L(L)*
Brackets
L(L)*
L(L)*
Assignment operator
D(D)*
Delimiter
Identifier
Relational operator
L(L)*
Delimiter
Identifier
Arithmetic operator
Brackets
L(L)*

=
Factorial
*
I
;
}
Cout
<<

String

<<
Factorial
<<
Endl
;
Return
0
}

Assignment operator
L(L)*
Arithmetic operator
L(L)*
Delimiter
Brackets
L(L)*
Relational operator
Double quotes
L(L)*
Double brackets
Relational operator
L(L)*
Relational operator
L(L)*
Delimiter
L(L)*
D(D)*
Brackets

#
#

Except #
+
Include
i
-

n
A

c
B

l
C

u
D

d
E

e
F

Except

Except

Except

Except

Except

Except
Except
e

+
<
<
Except <

Iostream

i
a

o
A

s
m
B

r
E

ExceptI,o,s,t,r,e,a,m

e
F

>
>
-

+
Except >

+
Using

I
C

Exceptu,

s,

I,
+

Namespace
(l)*
l
-

Except l
+

n,

Std

d
B

Except s,t,d

;
;
-

Except ;

int
(l)*
l
-

Except l
+

Main

m
-

i
B

+
Except m,a,I,n

(
+
(
-

+
Except (

)
-

+
Except )

{
-

+
Except {

Int

num

(l)*
l
-

Except l
+

factorial
(l)*
l
-

Except l
+

Except I,n,t

=
-

+
Except =
+

1
-

+
Except 1
+

;
-

+
Except ;
+

cout
(l)*
l
-

Except l
+

<<
<

<
+

Except <,<
+

+
Except
+
String
(l/d)*
l

+
Except

;
;
-

Except ; +
+

cin

(l)*
l
-

>>
>

>
+

Except >,>
+

num

(l)*
l
-

;
;
-

Except; +
+

for
(l)*
l
-

+
Except l
+

(
(
-

+
Except (
+

int

(l)*
l
-

+
Except l
+
i
(l)*
l

+
Except l
+

=
=
-

+
Except =
+
1
(d)*
d

+
Except d
+

;
;
-

Except ;
+

i
(l)*
l
-

+
Except l
+

<=
<
-

=
+
Except <,=

+
num
(l)*
l
-

+
Except l
+

;
;
-

Except ;
+
i
(l)*
l
-

+
Except l
+

++
+
-

A
Except +

+
Except +

+
)
)
-

+
Except )
+
{
{

+
Except {
+

factorial

(l)*
l
-

+
Except l
+
=
=

+
Except =
+

factorial
(l)*
l
-

+
Except l
+
*
*

+
Except *
+

i
(l)*
l
-

Except l
+

;
;
-

+
Except ;
+
}
}

+
Except }
+
cout
(l)*
l

Except l
+

<<
<
-

<

Except <

+
Except <

Except +
+
String
(l/d)*
l

Except +
+

<<
<
-

<

Except <

+
Except <

+
factorial
(l)*

l
-

+
Except l
+

<<
<
-

<

Except <

+
Except <

+
endl
(l)*
l
-

Except l
+
;
;
-

Except ; +
+
return
(l)*
l

Except l
+
(d)*

0
d
-

+
Except d

}
}
-

+
Except }
+

ALGORITHM
Main.cpp

token-->space
while(token=not stop)
token->lA.tex()
output->LA.lexeme.
laxical analysis.h

enum(.....)
class laxical analysis

character->nextchar
integer charclass
string lexeme
string lexeme
string input;

function
int lex()
setnewinput(string in)
lexical analysis(string in="")

===============================================
===
source.cpp

laxical analysis->laxial analysis(string in)


getchar->function

laxical analysis->setnewinput(string in)


input<-in
getcahr -> functio

laxical analysis ; get char()

Das könnte Ihnen auch gefallen