Vous êtes sur la page 1sur 51

GRAMMAR

FUNCTION -> RETURN_TYPE FUNC


|.
FUNC -> identifier obracket LIST FUNCTION_DEF
| main FUNCTION2
| FUNCTION_DEF.
FUNCTION_DEF -> obrace STATEMENT cbrace
|.
FUNCTION2 -> obracket LIST cbracket obrace STATEMENT cbrace.
RETURN_TYPE -> void
| data_type.
FC -> equal identifier obracket ARG cbracket
| obracket ARG cbracket semi.
FUNCCALL_EXPR -> identifier OPIF.
OPIF -> equal EXPR semi
| FC.
ARG -> identifier ARG
| CONST ARG
| comma ARG
|.

LIST -> void


| data_type identifier LIST
| cbracket
| comma LIST.

STATEMENT -> BREAK_STMT STATEMENT


| CONTINUE_STMT STATEMENT
| DECLARATION STATEMENT
| RETURN_STMT STATEMENT
| IF_ELSE STATEMENT
| FOR STATEMENT
| SWITCH_CASE STATEMENT
| WHILE_LOOP STATEMENT
| FUNCCALL_EXPR STATEMENT
|.

RETURN_STMT -> return RTN.


RTN -> semi
| obrace EXP cbrace semi.
EXP -> CONST
| identifier
|.
CONST -> interger_const
| char_const
| float_const.
CONTINUE_STMT -> continue semi.
BREAK_STMT -> break semi.
DECLARATION -> data_type identifier CONST_LIST ID_LIST.
CONST_LIST -> equal EXP
| DIM
|.
DIM -> osqbracket int_const csqbracket DIM1.
DIM1 -> osqbracket int_const csqbracket DIM1
|.
ID_LIST -> semi DECLARATION
| comma ID_LIST1.

ID_LIST1 -> identifier CONST_LIST ID_LIST.


EQUALTO -> identifier equal EXPR semi.

EXPR -> TERM EXPR1 NEXPR.


NEXPR -> X NEXPR
|.
X -> relop EXPR
|.
EXPR1 -> add TERM EXPR1
| sub TERM EXPR1
|.
TERM -> FACTOR TERM1.
TERM1 -> mul FACTOR TERM1
| div FACTOR TERM1
|.
FACTOR -> POW FACTOR1.
FACTOR1 -> carrot POW FACTOR1
|.
POW -> identifier
| const
| obracket EXP cbracket
| DIM.
IF_ELSE -> if obracket EXPR cbracket IE.
IE -> semi
| obrace STATEMENT cbrace ELSE_STMT.
ELSE_STMT -> ELSE
|.
ELSE -> else EL.
EL -> obrace STATEMENT cbrace.
WHILE_LOOP -> while obrace EXPR cbrace WHILE.
WHILE -> obrace STATEMENT cbrace
| semi.

SWITCH_CASE -> switch obracket EXP cbracket obrace CASE_LIST cbrace.


CASE_LIST -> CASE CASE_LIST.
CASE -> CASE_LABEL STATEMENT.
CASE_LABEL -> case CONST colon
| default colon.

FOR -> for obracket INITIALIZATION semi CONTROL_STATEMENT semi


ITERATION cbracket FOR1.
FOR1 -> semi
| obrace STATEMENT cbrace.
INITIALIZATION -> DECLARATION
|identifier INIT.
INIT -> equal EXPR
|.
CONTROL_STATEMENT -> EXPR
|.
ITERATION -> EXPR
|.
First and Follow Set of Grammar

Nonterminal First set Follow set


FUNCTION void data_type ∅
FUNC identifier main obrace ∅
FUNCTION_DEF obrace ∅
FUNCTION2 obracket ∅
RETURN_TYPE void data_type identifier main obrace
case default break continue
OPIF equal obracket data_type return if for switch
while identifier cbrace
case default break continue
FC equal obracket data_type return if for switch
while identifier cbrace
identifier comma
ARG interger_const char_const cbracket
float_const
void data_type cbracket
LIST obrace cbracket
comma
case default break continue
FUNCCALL_EXPR identifier data_type return if for switch
while identifier cbrace
case default break continue
RETURN_STMT return data_type return if for switch
while identifier cbrace
case default break continue
RTN semi obrace data_type return if for switch
while identifier cbrace
case default break continue
CONTINUE_STMT continue data_type return if for switch
while identifier cbrace
case default break continue
BREAK_STMT break data_type return if for switch
while identifier cbrace
carrot mul div relop add sub
DIM1 osqbracket
comma semi cbracket cbrace
semi case default break continue
ID_LIST1 identifier data_type return if for switch
while identifier cbrace
CONST_LIST equal osqbracket semi comma
semi case default break continue
ID_LIST semi comma data_type return if for switch
while identifier cbrace
EQUALTO identifier ∅
NEXPR relop relop semi cbracket cbrace
X relop relop semi cbracket cbrace
EXPR1 add sub relop semi cbracket cbrace
identifier const obracket relop add sub semi cbracket
TERM
osqbracket cbrace
relop add sub semi cbracket
TERM1 mul div
cbrace
identifier const obracket mul div relop add sub semi
FACTOR
osqbracket cbracket cbrace
mul div relop add sub semi
FACTOR1 carrot
cbracket cbrace
identifier const obracket carrot mul div relop add sub semi
POW
osqbracket cbracket cbrace
carrot mul div relop add sub
DIM osqbracket
comma semi cbracket cbrace
case default break continue
IF_ELSE if data_type return if for switch
while identifier cbrace
case default break continue
IE semi obrace data_type return if for switch
while identifier cbrace
case default break continue
ELSE_STMT else data_type return if for switch
while identifier cbrace
case default break continue
ELSE else data_type return if for switch
while identifier cbrace
case default break continue
EL obrace data_type return if for switch
while identifier cbrace
WHILE_LOOP while case default break continue
data_type return if for switch
while identifier cbrace
case default break continue
WHILE obrace semi data_type return if for switch
while identifier cbrace
case default break continue
SWITCH_CASE switch data_type return if for switch
while identifier cbrace
identifier interger_const
EXP semi comma cbrace cbracket
char_const float_const
CASE_LIST case default cbrace
CASE case default case default
break continue data_type return if
CASE_LABEL case default for switch while identifier case
default
semi cbrace identifier comma
interger_const char_const
CONST interger_const char_const
float_const
float_const colon cbracket
case default break continue
FOR for data_type return if for switch
while identifier cbrace
case default break continue
FOR1 semi obrace data_type return if for switch
while identifier cbrace
break continue data_type
STATEMENT return if for switch while case default cbrace
identifier
INITIALIZATION identifier data_type semi
semi case default break continue
DECLARATION data_type data_type return if for switch
while identifier cbrace
INIT equal semi
identifier const obracket
CONTROL_STATEMENT semi
osqbracket
identifier const obracket
ITERATION cbracket
osqbracket
identifier const obracket
EXPR relop semi cbracket cbrace
osqbracket
LEXICAL ANALYZER
LexicalAnlyzer.cpp

#include <iostream>
#include <fstream>
#include <string.h>
#include "myStream.h"
#include "lexer.h"

using namespace std;

int main (void) {


char ch;
int zTokType;

float a = 1.e5;

cout << "Starting Lexical Analysis..." << endl;


cout << "Opening your file..........." << endl << endl;

if (!readFile()){
cout << "Error Opening Input File" << endl
<< "Program Terminated Unsuccessfully...." << endl <<
endl;

system("Pause");
return 0;
}

// Until EOF
while ((ch = read_ch())!= EOF && ch > 0) {
put_back(ch);
zTokType = scanner();
generateToken(zTokType, zbuffer);
}

cout << "End of Lexical Analysis" << endl


<<".......................Thank
You........................" << endl;

system("Pause");
}
myStream.h

#include <iostream>
#include <fstream>
#include <string.h>

using namespace std;


int cur_line, cur_col, err_line, err_col;
int buffLen;
char zbuffer[50];

ofstream outfile("token.txt");
ifstream infile;

int readFile(void) {
infile.open("MyFile.txt");
if(infile.good())
return 1;
else
return 0;
}

int read_ch(void) {
char ch;

infile.get(ch);
cur_col++;
if (ch == '\n') {
cur_line++;
cur_col = 0;
}
return ch;
}

void put_back(char ch) {


infile.putback(ch);
cur_col--;
if (ch == '\n') cur_line--;
}
Lexer.h

#include "tokenizer.h"

int scanner(void) {
char ch;
int zTokType=0, i=0;

// Read Until valid character


while ((ch = read_ch())!= EOF && ch <= ' ');

buffLen = 0;
zbuffer[buffLen++] = (char)ch;
zbuffer[buffLen] = '\0';
err_line = cur_line;
err_col = cur_col;

switch (ch) {
case EOF:
zTokType = EOFile;
break;

case ',':
zTokType = comma;
break;

case '^':
zTokType = carrot;
break;

case ';':
zTokType = semi;
break;

case '(':
zTokType = obracket;
break;

case ')':
zTokType = cbracket;
break;

case '[':
zTokType = osqBracket;
break;

case '{':
zTokType = obrace;
break;

case '}':
zTokType = cbrace;
break;
case ']':
zTokType = csqBracket;
break;

case ':':
zTokType = colon;
break;

case '\'':
ch = read_ch();
if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch
<= 'Z') || (ch >= '0' && ch <= '9'))
{
zbuffer[buffLen++] = (char)ch;
cur_col++;
ch = read_ch();
if( ch == '\'')
{
zbuffer[buffLen++] = (char)ch;
cur_col++;
zTokType = ConstChar;
}
else
put_back(ch);
}
else{
put_back(ch);
zTokType = myError;
}
break;

case '+':
ch = read_ch();
if (ch == '+') {
zbuffer[buffLen++] = (char)ch;
cur_col++;
zTokType = IncDec;
}
else {
put_back(ch);
zTokType = add;
}
break;

case '-':
ch = read_ch();
if (ch == '-') {
zbuffer[buffLen++] = (char)ch;
cur_col++;
zTokType = IncDec; }
else {
put_back(ch);
zTokType = sub; }
break;

case '*':
zTokType = mul;
break;

case '/':
ch = read_ch();
if (ch == '/'){
do {
zbuffer[buffLen++] = (char)ch;
ch = read_ch();
if (ch == '\n') cur_line++;
} while ( ch != '\n');
cur_col = 0;
put_back(ch);
zTokType = Remarks; }
else {
put_back(ch);
zTokType = divide; }
break;

case '=':
ch = read_ch();
if (ch == '='){
zbuffer[buffLen++] = (char)ch;
cur_col++;
zTokType = isEqual; }
else {
put_back(ch);
zTokType = assign; }
break;

case '<':
ch = read_ch();
if (ch == '<'){
zbuffer[buffLen++] = (char)ch;
cur_col++;
zTokType = OpBit; }
else if (ch == '>'){
cur_col++;
zTokType = OpRel; }
else if (ch == '='){
zbuffer[buffLen++] = (char)ch;
cur_col++;
zTokType = OpRel; }
else {
put_back(ch);
zTokType = OpRel; }
break;

case '>':
ch = read_ch();
if (ch == '>'){
zbuffer[buffLen++] = (char)ch;
cur_col++;
zTokType = OpBit; }
else if (ch == '='){
zbuffer[buffLen++] = (char)ch;
cur_col++;
zTokType = OpRel; }
else {
put_back(ch);
zTokType = OpRel; }
break;

default:
if (isdigit(ch)) {
do {
zbuffer[buffLen++] = (char)ch;
ch = read_ch();
} while ( ch != EOF && isdigit(ch));
put_back(ch);
zbuffer[--buffLen] = '\0';
zTokType = ConstInt;
break;
}

if (isalpha(ch)) {
do {
ch = read_ch();
zbuffer[buffLen++] = (char)ch;
} while ( ch != EOF && isalnum(ch));
put_back(ch);
zbuffer[--buffLen] = '\0';

for(i = 0; i < 13; i++) {


if(strcmp(zbuffer, &keywordTable[i]
[0]) == 0){
zTokType = Keyword;
break; }
}

if (zTokType==0)
for(i = 0; i < 5; i++) {
if(strcmp(zbuffer,
&DataTypeTable[i][0]) == 0){
zTokType = DataType;
break; }
}

if (zTokType==0)
zTokType = identifier;
}
else {
zTokType = myError; }
}

zbuffer[buffLen] = '\0';
return zTokType;
}
Tokenizer.h

enum eType
{ Null, myError, Keyword, DataType, identifier,
assign, add, sub, equal, mul, divide, obracket, cbracket,
obrace, cbrace,
IncDec, colon, osqBracket, csqBracket, carrot, OpRel, OpBit,
ConstInt, ConstFloat, ConstChar,
Remarks, comma, semi, isEqual, EOFile};

char keywordTable[13][10] =
{ "If", "Else", "For", "While",
"Do", "Switch", "Case", "Break", "Continue",
"Default", "Void", "Return", "Main"};

char DataTypeTable[5][10] =
{ "Int", "Char", "Float", "Double", "Long"};

void generateToken(int zTokType, char TValue[]){

switch (zTokType) {
case EOFile:
outfile << "<"
<< " EOF," << cur_line << ">" <<endl;
cout << "<EOF," << cur_line << ">" <<endl;
break;

case carrot:
outfile << "<"
<< "carrot" <<"," << zbuffer << ", " <<
cur_line << ">" <<endl;
cout << "<" << "carrot" <<"," << zbuffer << "," <<
cur_line << ">" <<endl;
break;

case colon:
outfile << "<"
<< "colon" <<"," << zbuffer << ", " <<
cur_line << ">" <<endl;
cout << "<" << "colon " <<"," << zbuffer << ", " <<
cur_line << ">" <<endl;
break;

case semi:
outfile << "<"
<< "semi" <<"," << zbuffer << ", " <<
cur_line << ">" <<endl;
cout << "<" << "semi" <<"," << zbuffer << ", " <<
cur_line << ">" <<endl;
break;

case comma:
outfile << "<"
<< "comma" <<"," << zbuffer << ", " <<
cur_line << ">" <<endl;
cout << "<" << "comma " <<"," << zbuffer << ", " <<
cur_line << ">" <<endl;
break;

case osqBracket:
outfile << "<"
<< "osqBracket" <<"," << zbuffer << ", "
<< cur_line << ">" <<endl;
cout << "<" << "osqBracket" <<"," << zbuffer << ", "
<< cur_line << ">" <<endl;
break;

case csqBracket:
outfile << "<"
<< "csqBracket" <<"," << zbuffer << ", "
<< cur_line << ">" <<endl;
cout << "<" << "csqBracket " <<"," << zbuffer << ", "
<< cur_line << ">" <<endl;
break;

case obrace:
outfile << "<"
<< "obrace" <<"," << zbuffer << ", " <<
cur_line << ">" <<endl;
cout << "<" << "obrace " <<"," << zbuffer << ", " <<
cur_line << ">" <<endl;
break;

case cbrace:
outfile << "<"
<< "cbrace" <<"," << zbuffer << ", " <<
cur_line << ">" <<endl;
cout << "<" << "cbrace " <<"," << zbuffer << ", " <<
cur_line << ">" <<endl;
break;

case cbracket:
outfile << "<"
<< "cbracket" <<"," << zbuffer << ", " <<
cur_line << ">" <<endl;
cout << "<" << "cbracket " <<"," << zbuffer << ", " <<
cur_line << ">" <<endl;
break;

case obracket:
outfile << "<"
<< "obracket" <<"," << zbuffer << ", " <<
cur_line << ">" <<endl;
cout << "<" << "obracket " <<"," << zbuffer << ", " <<
cur_line << ">" <<endl;
break;

case Keyword:
outfile << "<"
<< zbuffer << "," << "NULL, " <<
cur_line << ">" <<endl;
cout << "<" << zbuffer << "," << "NULL, " << cur_line
<< ">" <<endl;
break;

case DataType:
outfile << "<"
<< "data_type" <<"," << zbuffer
<< ", " << cur_line << ">" <<endl;
cout << "<" << "data_type" <<"," << zbuffer << ", "
<< cur_line << ">" <<endl;
break;

case identifier:
outfile << "<"
<< "identifier" <<"," << zbuffer << ", "
<< cur_line << ">" <<endl;
cout << "<" << "identifier" <<"," << zbuffer << ", "
<< cur_line << ">" <<endl;
break;

case assign:
outfile << "<"
<< "equal" <<"," << zbuffer << ", "
<< cur_line << ">" <<endl;
cout << "<" << "equal" <<"," << zbuffer << ", " <<
cur_line << ">" <<endl;
break;

case add:
outfile << "<"
<< "add" <<"," << zbuffer << ", " <<
cur_line << ">" <<endl;
cout << "<" << "add " <<"," << zbuffer << ", " <<
cur_line << ">" <<endl;
break;

case sub:
outfile << "<"
<< "sub" <<"," << zbuffer << ", " <<
cur_line << ">" <<endl;
cout << "<" << "sub " <<"," << zbuffer << ", " <<
cur_line << ">" <<endl;
break;

case mul:
outfile << "<"
<< "mul" <<"," << zbuffer << ", " <<
cur_line << " > " <<endl;
cout << "<" << "mul" <<"," << zbuffer << ", " <<
cur_line << " > " <<endl;
break;

case divide:
outfile << "<"
<< "divide" <<"," << zbuffer << ", " <<
cur_line << ">" <<endl;
cout << "<" << "divide" <<"," << zbuffer << ", " <<
cur_line << ">" <<endl;
break;

case IncDec:
outfile << "<"
<< "IncDec " <<"," << zbuffer << ",
" << cur_line << ">" <<endl;
cout << "<" << "IncDec " <<"," << zbuffer << ", " <<
cur_line << ">" <<endl;
break;

case OpRel:
outfile << "<"
<< "OpRel" <<"," << zbuffer << ", "
<< cur_line << ">" <<endl;
cout << "<" << "OpRel" <<"," << zbuffer << ", " <<
cur_line << ">" <<endl;
break;

case OpBit:
outfile << "<"
<< "OpBit " <<"," << zbuffer << ", "
<< cur_line << ">" <<endl;
cout << "<" << "OpBit " <<"," << zbuffer << ", " <<
cur_line << ">" <<endl;
break;

case ConstInt:
outfile << "<" << "int_const" <<"," << zbuffer << ", " << cur_line <<
">" <<endl;
cout << "<" << "int_const" <<"," << zbuffer << ", " <<
cur_line << ">" <<endl;
break;

case ConstChar:
outfile << "<" << "char_const" <<"," << zbuffer << ", " << cur_line
<< ">" <<endl;
cout << "<" << "char_const" <<"," << zbuffer << ", "
<< cur_line << ">" <<endl;
break;

case Remarks:
outfile << "<" << "Remarks" <<"," << zbuffer << ", " << cur_line << ">"
<<endl;
cout << "<" << "Remarks" <<"," << zbuffer << ", " <<
cur_line << ">" <<endl;
break;

case myError:
outfile << "<" << "Error" <<"," << zbuffer << ", " << cur_line << ">"
<<endl;
cout << "<" << "Error" <<"," << zbuffer << ", " <<
cur_line << ">" <<endl;
break;
}
}
PARSER
readString.h

#include <iostream>
#include <fstream>
#include <string.h>

using namespace std;

void putBack(char);

struct SymbolTableID
{
string name;
string type;
int scope;
string size;
};

struct SymbolTableFunc{
string name;
string retType;
string paramList;
int scope;
};

map<int, char*, less<int> > mapTer;

int cur_line = 0, cur_col = 0;


int lookahead;
char buffer[30];
int index = 0, restInd= 0,lineInd = 0;
char restBuff[30];
char lineNum[3];

char *terminal[37] = {
"Null", "Void", "data_type", "identifier", "comma",
"assign", "constant", "semi", "add", "sub", "equal",
"mul", "divide", "Main", "obracket", "cbracket",
"obrace", "cbrace", "int_const", "char_const",
"float_const",
"IncDec", "For", "While", "Switch", "Case",
"colon", "Default", "If", "Else", "osqBracket",
"csqBracket", "Break", "Continue", "Return", "carrot",
"OpRel"};

/*///////////////////// Map Table LookUp //////////////////////////////

1. Null = 0
2. Void = 1
3. data type = 2
4. identifier = 3
5. comma, = 4
6. assign = 5
7. constant = 6
8. semi; = 7
9. add = 8
10. sub = 9
11. equal = 10
12. mul = 11
13. divide = 12
14. Main = 13
15. obracket( = 14
16. cbracket) = 15
17. obrace{ = 16
18. cbrace} = 17
19. int_const = 18
20. char const = 19
21. float const = 20
22. IncDec = 21
23. For = 22
24. While = 23
25. Switch = 24
26. Case = 25
27. Colon: = 26
28. Default = 27
29. If = 28
30. Else = 29
31. osqBracket[ = 30
32. csqBracket] = 31
33. Break = 32
34. Continue = 33
35. Return = 34
36. carrot = 35
37. OpRel = 36

////////////////////////////////////////////////////////////////////////
/////*/

ifstream infile;

int readFile(void)
{
infile.open("token.txt");
if(infile.good())
return 1;
else
return 0;
}

int readCh(void)
{
char ch;
index = 0;
restInd= 0;
lineInd = 0;
infile.get(ch);
cur_col++;
if(ch == '<')
{
do{
infile.get(ch);
buffer[index++] = ch;
}while(ch != ',');

do{
infile.get(ch);
restBuff[restInd++] = ch;

}while(ch != ',');
do{
infile.get(ch);
lineNum[lineInd++] = ch;
}while( ch != '>' );
do{
infile.get(ch);
}while(ch != '\n');

lineNum[--lineInd] = '\0';
restBuff[--restInd] = '\0';
buffer[--index] = '\0';

return 0;
}

void putBack(char ch)


{
infile.putback(ch);
cur_col--;
if(ch == '\n')
cur_line--;
}

void putBackToken()
{
for(int i = restInd; i >= 0; i--){
putBack(restBuff[i]);
cout << " Im showing restBuff "
<< restBuff[i]
<<endl
<<endl;
}

for(int i = index; i >= 0; i--){


putBack(buffer[i]);
cout << " Im showing Buff "
<< buffer[i]
<<endl
<<endl;
}
}
Semantic.cpp

#include<iostream>
#include<fstream>
#include <map>
#include<string>
#include "readString.h"

using namespace std;

bool flagDim = false;


bool flagFunc = false;
bool flagDec = false;
bool flagScope = false;
bool flagDecVar = false;

void mapping(void);
bool arg(void);
void compCh(void);
int match(int);
bool function(void);
bool func(void);
int return_type(void);
bool function2(void);
bool function_call(void);
bool fc(void);
bool list(void);
bool list2(void);
bool list3(void);
bool function_def(void);
bool exp(void);
bool declaration(void);
bool const_list(string);
bool dim(string);
bool dim1(string);
bool id_list(string);
bool id_list1(string);
bool statement(void);
bool if_else(void);
bool ie(void);
bool else_stmt(void);
bool Else(void);
bool el(void);
bool while_loop(void);
bool while1(void);
bool For(void);
bool for1(void);
bool return_stmt(void);
bool rtn(void);
bool Const(void);
bool continue_stmt(void);
bool break_stmt(void);
string expr(void);
string expr1(string);
string term(void);
string term1(string);
string factor(void);
string factor1(string);
string pow(void);
bool switch_case(void);
bool Case_list(void);
bool CASE(void);
bool case_label(void);
bool initialization(void);
bool init(void);
bool control_st(void);
bool iteration(void);
string NExpr(string);
string X(string);
bool funcCall_Expr(void);
bool Opif(void);
bool equalTo(void);
void start(void);
string compare(string,string);
bool lookup(string, int);
string size (string);
int insertID(string, string, int, string);
int insertFunc(string, string, string, int);

string gblDT,
gblSize ,
gblAsize ,
dataType1,
dataType2,
dataType3,
dataType4;
int gblScope;

static int Aindex = -1;


static int Findex = -1;

struct SymbolTableID stid[50];


struct SymbolTableFunc stf[10];

void main(void)
{
int status;
status = readFile();
mapping();
if(status)
{
readCh();
compCh();
}

////////////// Starting Rule ///////////


function();

system("Pause");
}
////////////////////////////////////////////////////////////////////////
//////////////
/////////////////////////////////FUNCTION -> RETURN_TYPE
FUNC////////////////////////
///////////////////////////////////////// ->
Null ////////////////////////////////////
////////////////////////////////////////////////////////////////////////
//////////////

bool function()
{
cout << " im in function"
<<endl;
switch(lookahead)
{
case 1:
// 1 associates with "void"
case 2:
// 2 associates with "data type"
return_type();
func();
break;

default: //
default used for NULL
return true;
}
}

////////////////////////////////////////////////////////////////////////
//////////////
//////////////// FUNC -> identifier obracket LIST
FUNCTION_DEF/////////////
///////////////////// -> main
FUNCTION2 //////////////////////////////////////////
///////////////////// ->
FUNCTION_DEF ///////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
/////////////

bool func()
{
cout << " im in func"
<<endl;
switch(lookahead)
{
case 3:
// 3 assocaites with "identifier"
match(3);
match(14);
//14 associates with "open round bracket"
list();
function_def();
break;
case 13:
// 13 associates with "main"
match(13);
function2();
break;
case 16:
// 16 associates with "open brace"
function_def();
break;
}
return 0;
}

////////////////////////////////////////////////////////////////////////
//////////////
//////////// FUNCTION2 -> obracket LIST obracket obrace STATEMENT
cbrace.////////////////////
/////////////////////
|. ////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
/////////////

bool function2()
{
cout << " im in function2"
<<endl;
switch(lookahead)
{
case 14:
// 14 asscocites with open round bracket
match(14);
list();
match(15);
match(16);
// 16 associates with open brace
statement();
match(17);
// 17 associates with close brace
break;
}
return 0;
}

////////////////////////////////////////////////////////////////////////
//////////////
//////////////////////////////RETURN_TYPE ->
void ////////////////////////////////////
///////////////////////////////////////// ->
data_type //////////////////////////////
////////////////////////////////////////////////////////////////////////
//////////////

int return_type()
{
cout << " im in return type"
<<endl;
switch(lookahead)
{
case 1:
// 1 assocaites with "void"
flagScope = true;
match(1);
flagScope = false;
break;

case 2:
flagScope = true;
match(2);
// 2 associates with data type
flagScope = false;
break;
}
return true;
}

////////////////////////////////////////////////////////////////////////
//////////////
///////////////////FUNCTION_CALL -> identifier
FC. ///////////////////////////////////
////////////////////////////////////////////////////////////////////////
/////////////

bool function_call()
{
cout << " im in function call"
<<endl;
switch(lookahead)
{
case 3:
// 3 associates with identifier
match(3);
fc();
break;
}
return 0;
}

////////////////////////////////////////////////////////////////////////
//////////////
//////////////////////////////FC -> identifier
Opif//////////////////////////////////
////////////////////////////////////////////////////////////////////////
//////////////

bool funcCall_Expr()
{
cout << " im in funcCall_Expr"
<<endl;

switch(lookahead)
{
case 3:
match(3);
Opif();
// match(7);
break;
}
return 0;
}

//Opif -> equal expr | fc.

bool Opif()
{
cout << " im in Opif"
<<endl;
switch(lookahead)
{
case 10:
match(10);
expr();
match(7);
break;

case 14:
fc();
break;
}
return 0;
}

////////////////////////////////////////////////////////////////////////
//////////////
//////////////////////////////FC -> equal identifier obracket ARG
cbracket ///////////
///////////////////////////////// | obracket ARG cbracket
semi.///////////////////////
////////////////////////////////////////////////////////////////////////
//////////////

bool fc()
{
cout << " im in fc"
<<endl;
switch(lookahead)
{
case 10:
// 3 associates with identifier
match(10);
match(3);
// 14 associates with open round bracket
match(14);
arg();
match(15);
// 15 assocaites with close round bracket
break;

case 14:
match(14);
arg();
match(15);
// 15 associates with close round bracket
match(7);
// 7 associates with semi colon
break;
}
return 0;
}

////////////////////////////////////////////////////////////////////////
//////////////
//////////////////////////////// ARG -> identifier
ARG ///////////////////////////////
/////////////////////////////////// | CONST
ARG //////////////////////////////////
/////////////////////////////////// | comma
ARG //////////////////////////////////
///////////////////////////////////
| . ///////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
/////////////

bool arg()
{
cout << " im in arg"
<<endl;
switch(lookahead)
{
case 3:
match(3);
arg();
break;

case 18:
case 19:
case 20:
Const();
arg();
break;

case 4:
match(4);
arg();

default:
return true;
}
}

///////////////////////////////////////////////////////////////
///////////////////LIST -> void ///////////////////////////////
/////////////////// | data_type identifier LIST /////////
/////////////////// | cbracket /////////////////////////
///////////////////// | comma LIST. ///////////////////////
/////////////////////////////////////////////////////////////

bool list()
{
cout << " im in list"
<<endl;
switch(lookahead)
{
case 1:
// 1 associates with "void"
match(1);
break;

case 2:
// 2 assocites with data type
match(2);
match(3);
// 3 associates with identifier
list();
break;

case 15:
match(15);
break;

case 4:
match(4);
list();
break;
}
return 0;
}

////////////////////////////////////////////////////////////////////////
//////////////
//////////////////////////////FUNCTION_DEF -> obrace STATEMENT
cbrace ////////////////
/////////////////////////////////// ->
Null.//////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
//////////////

bool function_def()
{
cout << " im in function definition"
<<endl;

switch(lookahead)
{
case 16:
// 16 associates with open brace
match(16);
statement();
match(17);
// 17 assocaites with close brace
break;

default:
// defalut shows the presence of Null
return true;
}
}
////////////////////////////////////////////////////////////////////////
/
//////////////////////////////EXP -> CONST |
identifier| .//////////////
////////////////////////////////////////////////////////////////////////
/

bool exp()
{
cout << " im in exp"
<<endl;
switch(lookahead)
{
case 18:
case 19:
case 20: //20 associates with
float_const
Const();
break;

case 3: // 3 associates
with identifier
match(3);
break;

default:
return true;
}
}
////////////////////////////////////////////////////////////////////////
/
//////////DECLARATION -> data_type identifier CONST_LIST
ID_LIST ////////
///////////////////
|. //////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
/

bool declaration()
{
cout << " im in declaration"
<<endl;

switch(lookahead)
{

case 2:
flagDecVar = true;
string name,dt;
flagDec=true;
// gblSize = size(gblDT);
cout<<"Size ="<<gblSize<<endl;
match(2);// 2 associates with data type
match(3);// 3 associates with identifier

const_list(gblDT);
id_list(gblDT);
flagDecVar = false;
break;
}
return true;
}

////////////////////////////////////////////////////////////////////////
//////////////
///////////////////////////CONST_LIST -> equal EXPR | DIM
| .//////////////////////////
////////////////////////////////////////////////////////////////////////
//////////////

bool const_list(string dt)


{
cout << " im in constant list"
<<endl;
switch(lookahead)
{
case 10:
match (10);
//10 associates with equal
expr();
id_list(dt);
break;

case 30:
dim(dt);
break;

default:
return true ;
}
}

////////////////////////////////////////////////////////////////////////
//////////////
//////////////////////dim -> osqbracket int_const csqbracket
DIM1 ////////////////////
////////////////////////////////////////////////////////////////////////
//////////////

bool dim(string dt)


{
cout << " im in dim"
<<endl;

switch(lookahead)
{
case 30:
match(30);
//30 associates with osqbracket
match(18);
//18 associates with int_cons
match(31);
dim1(dt);
break;
}
return 0;
}

////////////////////////////////////////////////////////////////////////
//////////////
////////////DIM1 -> osqbracket int_const csqbracket DIM1 //////////
////////////////////////////////////////////////////////////////////////
//////////////

bool dim1(string dt)


{
cout << " im in dim1"
<<endl;

switch(lookahead)
{
case 30:
flagDim=true;
match(30);
//30 associates with osqbracket
gblAsize=restBuff;
gblSize=gblSize;
cout<<"elements of array = "<<gblAsize<<endl;
match(18);
//18 associates with int_cons
match(31);
//31 associates with csqbracket
dim1(dt);
break;

default:
return true;
}
}

////////////////////////////////////////////////////////////////////////
/
/////////////////////ID_LIST -> semi DECLARATION. /////////////////////
///////////////////// | comma ID_LIST1.////////////////////////
////////////////////////////////////////////////////////////////////////

bool id_list(string dt)


{
cout << " im in id list"
<<endl;

switch(lookahead)
{
case 4:
match(4);
id_list1(dt);

break;
case 7:
match(7);
flagDec = false;
declaration();
break;
}
return 0;
}

// ID_LIST1 -> identifier CONST_LIST ID_LIST


// | CONST_LIST ID_LIST.

bool id_list1(string dt)


{
cout << " im in id list1"
<<endl;
switch(lookahead)
{
case 3:
string name;
gblDT=dt;
name =restBuff;
// gblSize= size(dt);
match(3);
const_list(dt);
id_list(dt);
break;

/*case 10:
case 30:
const_list();
id_list();
break;*/
}
return 0;
}

////////////////////////////////////////////////////////////////////////
/////
//////////////////STATEMENT -> BREAK_STMT
STATEMENT //////////////////////////
/////////////////////////// -> | CONTINUE_STMT
STATEMENT ////////////////////
/////////////////////////// -> | FfuncCall_Expr
STATEMENT ///////////////////
/////////////////////////// -> | DECLARATION
STATEMENT //////////////////////
/////////////////////////// -> | RETURN_STMT
STATEMENT ///////////////////////
/////////////////////////// -> | FOR
STATEMENT /////////////////////////////
/////////////////////////// -> | SWITCH_CASE
STATEMENT //////////////////////
/////////////////////////// -> | WHILE_LOOP
STATEMENT ////////////////////////
/////////////////////////// -> | IF_ELSE
STATEMENT /////////////////////////
/////////////////////////// -> |
Null. //////////////////////////////////////
////////////////////////////////////////////////////////////////////////
////

bool statement()
{
cout << " im in statement"
<<endl;

switch(lookahead)
{
case 32:
break_stmt();
statement();
break;

case 33:
//33 associates with continue
continue_stmt();
statement();
break;
case 2:
declaration();
statement();
break;

case 34:
//34 associates with return
return_stmt();
statement();
break;

case 3:
funcCall_Expr();
break;

case 22:
For();
statement();
break;

case 24:
switch_case();
statement();
break;

case 23:
while_loop();
statement();
break;

case 28:
if_else();
statement();
break;

case 18:
// 18 associates with integer constant
case 19:
// 19 associates with character constant
case 20:
// 20 associates with float constant
case 14:
// 14 assocaites with open round bracket
case 30:
cout << " im in statement expr"
<<endl;// 30 associates with open square bracket
expr();
statement();
break;

default:
return true;
}
}

////////////////////////////////////////////////////////////////////////
//////////////
/////////////////////////IF_ELSE -> if obracket EXPR cbracket
IE. ////////////////////
////////////////////////////////////////////////////////////////////////
//////////////

bool if_else()
{
cout << " im in if-else"
<<endl;

switch(lookahead)
{
case 28:
// 28 associates with "if"
match(28);
match(14);
expr();
match(15);
ie();
break;
}
return 0;
}

/////////////////////////////////////////////////////////////////
///////////////////IE -> semi ////////////////////////////////////
///////////////////// -> | obrace STATEMENT cbrace ELSE_STMT ///
/////////////////////////////////////////////////////////////////

bool ie()
{
cout << " im in ie"
<<endl;
switch(lookahead)
{
case 7: // 7 associates with
semi colon
match(7);
break;

case 16:
match(16);
statement();
match(17);
else_stmt();
break;
}
return 0;
}

//////////////////////////////////////////////////////////////
////////////ELSE_STMT -> ELSE ///////////////////////////////
///////////////////// | . ////////////////////////////////////
///////////////////////////////////////////////////////////////

bool else_stmt()
{
cout << " im in else statement"
<<endl;

switch(lookahead)
{
case 29:
// 29 associates with "else"
Else();
break;

default:
return true;
}
}

//////////////////////////////////////////////////////////
//////////////ELSE -> else EL. /////////////////////////
//////////////////////////////////////////////////////////

bool Else()
{
cout << " im in Else"
<<endl;

switch(lookahead)
{
case 29:
// 29 associates with "else"
match(29);
el();
break;
}
return 0;
}

//////////////////////////////////////////////////////////////
//////////////////EL -> obrace STATEMENT cbrace /////////////
//////////////////////////////////////////////////////////////

bool el()
{
cout << " im in el"
<<endl;

switch(lookahead)
{
case 16:
// 16 associates with open brace
match(16);
statement();
match(17);
break;
}
return 0;
}

////////////////////////////////////////////////////////////////////
///WHILE_LOOP -> while obrace EXPR cbrace WHILE. ////////////////
///////////////////////////////////////////////////////////////////

bool while_loop()
{
cout << " im in while loop"
<<endl;

switch(lookahead)
{
case 23:
match(23); //23 associates with
while
match(14); //14 associates with
osbracket
expr();
match(15); //15 associates with
csbracket
while1();
break;
}
return 0;
}

/////////////////////////////////////////////////////////
/////////WHILE -> obrace STATEMENT cbrace ///////////////
// | semi. //////////////////////////////////////
/////////////////////////////////////////////////////////

bool while1()
{
cout << " im in while1"
<<endl;

switch(lookahead)
{
case 16:
match(16); //16 associates with
obrace
statement();
match(17); //17 associates with
cbrace
break;

case 7:
match(7); //7 associates with
semicolon
break;
}
return 0;
}

////////////////////////////////////////////////////////////////////////
/
///FOR -> for obracket INITIALIZATION semi
CONTROL_STATEMENT ////////////
////// semi ITERATION cbracket FOR1. ////////////////////////////////
////////////////////////////////////////////////////////////////////////

bool For()
{
cout << " im in for"
<<endl;

switch(lookahead)
{
case 22 :
match(22);
match(14);
initialization();
match(7);
control_st();
match(7);
iteration();
match(15);
for1();
break;
}
return 0;
}

////////////////////////////////////////////////////////
////FOR1 -> semi /////////////////////////////////////
// | obrace STATEMENT cbrace. ///////////////////
///////////////////////////////////////////////////////
bool for1()
{
cout << " im in for1"
<<endl;

switch(lookahead)
{
case 7:
match(7);

case 16:
match(16);
statement();
match(17);
break;
}
return 0;
}

//////////////////////////////////////////////////////////
////INITIALIZATION -> DECLARATION | identifier INIT. /////
//////////////////////////////////////////////////////////

bool initialization()
{
cout << " im in initialization"
<<endl;

switch(lookahead)
{
case 2:
declaration();
break;

case 3:
match(3); // 3 associates with identifier
init();
break;
}
return 0;
}

/////////////////////////////////////////////////////////
/////////////INIT -> equal EXPR |. ////////////////////
////////////////////////////////////////////////////////

bool init()
{
cout << " im in init"
<<endl;

switch(lookahead)
{
case 10:
match(10); // 10 associates with equal
expr();
break;

default:
return true;
}
}

///////////////////////////////////////////
////CONTROL_STATEMENT -> EXPR | . /////////
//////////////////////////////////////////

bool control_st()
{
cout << " im in control set"
<<endl;

switch (lookahead)
{
case 3:
case 18:
case 19:
case 20:
case 14:
case 30:
expr();
break;

default:
return true;
}
}

/////////////////////////////////////////
//////ITERATION -> EXPR | . /////////
/////////////////////////////////////////

bool iteration()
{
cout << " im in iteration"
<<endl;

switch (lookahead)
{
case 3:
case 18:
case 19:
case 20:
case 14:
case 30:
expr();

break;
break;

default:
return true;
}
}

////////////////////////////////////////
//////RETURN_STMT -> return RTN. ///////
///////////////////////////////////////

bool return_stmt()
{
cout << " im in return statement"
<<endl;

switch(lookahead)
{
case 34:
match(34); //34 associates with return
rtn();
break;
}
return 0;
}

////////////////////////////////////////////////////////////////
///RTN -> semi_punc | brace_punc EXP brace_punc semi_punc. /////
////////////////////////////////////////////////////////////////

bool rtn()
{
cout << " im in rtn"
<<endl;

switch(lookahead)
{
case 7:
match(7); //7 associates with semicolon
break;

case 16:
match(16); //16 associates with obrace
exp();
match(17); //17 associates with cbrace
match(7); //7 associates with semicolon
break;
}
return 0;
}

///////////////////////////////////////////////////////////////
///CONST -> interger_const | char_const | float_const. ////////
//////////////////////////////////////////////////////////////

bool Const()
{
cout << " im in const"
<<endl;

switch(lookahead)
{
case 18:
match(18); //18 associates with int_const
break;

case 19:
match(19); //19 associates with char_const
break;

case 20:
match(20); //20 associates with float_const
break;
}
return 0;
}

//////////////////////////////////////////////////
///CONTINUE_STMT -> continue semi. ///////////////
///////////////////////////////////////////////////

bool continue_stmt()
{
cout << " im in continue statement"
<<endl;

switch(lookahead)
{
case 33:
match(33); //33 associates with continue
match(7); //7 associates with semi colon
break;
}
return 0;
}

////////////////////////////////////////////////////////
///////////BREAK_STMT -> break semi.///////////////////
///////////////////////////////////////////////////////

bool break_stmt()
{
cout << " im in break statement"
<<endl;

switch(lookahead)
{
case 32:
match(32); //32 associates with break
match(7); //7 associates with semi colon
break;
}
return 0;
}

////////////////////////////////////////////////////////////////////////
//
//SWITCH_CASE -> switch obracket EXP cbracket obrace CASE_LIST
cbrace.////
////////////////////////////////////////////////////////////////////////
//

bool switch_case()
{
cout << " im in switch case"
<<endl;

switch(lookahead)
{
case 24:
match(24);
match(14);
exp();
match(15);
match(16);
Case_list();
match(17);
break;
}
return 0;
}

//////////////////////////////////////////////
////// Case_list -> Case Case_list. //////////
//////////////////////////////////////////////

bool Case_list()
{
cout << " im in Case List"
<<endl;

switch(lookahead)
{
case 25:
case 27:
CASE();
Case_list();
}
return 0;
}

////////////////////////////////////////////////////////////////////////
///////////////////CASE -> CASE_LABEL STATEMENT. ///////////////////////
////////////////////////////////////////////////////////////////////////

bool CASE()
{
cout << " im in case"
<<endl;

switch(lookahead)
{
case 25:
case 27:
case_label();
statement();
break;
}
return 0;
}

////////////////////////////////////////////////////////////////////////
///////////////////CASE_LABEL -> case CONST colon_punc ////////////////
///////////////////////////// -> | default colon_punc. /////////////////
////////////////////////////////////////////////////////////////////////

bool case_label()
{
cout << " im in case label"
<<endl;

switch(lookahead)
{
case 25:
match(25);
Const();
match(26);
break;

case 27:
match(27);
match(26);
break;

}
return 0;
}

////////////////////////////////////////////////////////////////////////
/
/////////////////////// EXPR -> TERM EXPR1
NExpr. /////////////////////////////
////////////////////////////////////////////////////////////////////////
/

string expr()
{
cout << " im in expr"
<<endl;

switch(lookahead)
{
case 3: // 3 associates identifier
case 18: // 18 associates with integer constant
case 19: // 19 associates with character constant
case 20: // 20 associates with float constant
case 14: // 14 assocaites with open round bracket
case 30: // 30 associates with open square bracket

dataType1=term();
dataType2=expr1(dataType1);
dataType3=NExpr(dataType2);
// dataType4= compare(dataType2,dataType3);
return dataType4;
break;
}
return dataType1;

/////////////////////////////////////////////////////////////////////
////////////// equalTo -> identifier equal EXPR semi////////////////
/////////////////////////////////////////////////////////////////////

bool equalTo()
{
cout << " im in equalTo"
<<endl;

switch(lookahead)
{
case 3:
match(3);
match(10);
expr();
match(7);
break;
}
return 0;
}

/////////////////////////////////////////////////////
//////////////// nexpr--> X nexpr| null//////////////
////////////////////////////////////////////////////

string NExpr(string dt)


{
cout << " im in Nexpr"
<<endl;

switch(lookahead)
{
case 36:
dataType1=X(dt);
dataType2=NExpr(dt);
// dataType3=compare(dataType1,dataType2);
return dataType3;
break;

default:
return dt;
}
}

////////////////////////////////////////////////////
/////////////// X---> RelOp expr | null //////////
////////////////////////////////////////////////////

string X(string dt)


{
cout << " im in X"
<<endl;

switch(lookahead)
{
case 36:
match(36);
dataType1=expr();
// dataType2=compare(dataType1,dt);
return dataType2;
break;

default:
return dt;
}
}

////////////////////////////////////////////////////////////////////////
/
///////////////////////EXPR1 -> add TERM EXPR1 ////////////////////////
//////////////////////////// -> | sub TERM
EXPR1 ///////////////////////
//////////////////////////// -> |
Null //////////////////////////////////
////////////////////////////////////////////////////////////////////////
/

string expr1(string dt)


{
cout << " im in expr1"
<<endl;

switch(lookahead)
{
case 8: // 8
associates with +
match(8);
dataType1=term();
// dataType2= compare(dt,dataType1);
dataType3=expr1(dataType2);

return dataType3;
break;

case 9: // 9
associates with -
match(9);
dataType1=term();
// dataType2= compare(dt,dataType1);
dataType3=expr1(dataType2);
return dataType3;
break;

default:
return dt;
}
}

////////////////////////////////////////////////////////////////////////
/
/////////////////////// TERM -> FACTOR
TERM1. /////////////////////////////
////////////////////////////////////////////////////////////////////////
/

string term()
{
cout << " im in term"
<<endl;

switch(lookahead)
{
case 3:
// 3 associates identifier
case 18:
// 18 associates with integer constant
case 19:
// 19 associates with character constant
case 20:
// 20 associates with float constant
case 14:
// 14 assocaites with open round bracket
case 30:
// 30 associates with open square bracket
dataType1=factor();
dataType2=term1(dataType1);
/// dataType3=compare(dataType1,dataType2);
return dataType3;
break;
}
return dataType1;
}

////////////////////////////////////////////////////////////////////////
/
///////////////////////TERM1 -> mul FACTOR
TERM1 ////////////////////////
//////////////////////////// -> | div FACTOR
TERM1 ///////////////////////
//////////////////////////// -> |
Null //////////////////////////////////
////////////////////////////////////////////////////////////////////////
/

string term1(string dt)


{
cout << " im in term1"
<<endl;

switch(lookahead)
{
case 11: // 11
associates with *
match(11);
dataType1=term();
// dataType2= compare(dt,dataType1);
dataType3=expr1(dataType2);
return dataType3;
break;

case 12: // 12
associates with /
match(12);
dataType1=term();
// dataType2= compare(dt,dataType1);
dataType3=expr1(dataType2);
return dataType3;
break;

default:
return dt;
}
}

///////////////////////////////////////////////
///////////POW -> identifier ////////////////
////////////////| const //////////////////////
////////////////| obracket EXPR cbracket /////
////////////////| DIM.////////////////////////

string pow()
{
cout << " im in pow"
<<endl;

switch(lookahead)
{
case 3:
// gblDT=lookup(restBuff);
// gblSize=size(gblDT);
match(3);
break;
case 18:
match(18);
break;

case 19:
match(19);
break;

case 20:
match(20);
break;

case 14:
match(14);
expr();
match(15);
break;

case 30:
dim(gblDT);
break;
}
return gblDT;
}

///////////////////////////////////////////////////////
/////////////FACTOR1 -> carrot POW FACTOR1 //////////
//////////////////// | . ///////////////////////////
/////////////////////////////////////////////////////

string factor1(string dt1)


{
cout << " im in factor1"
<<endl;

switch(lookahead)
{
case 35 :
match(35);
dataType1=pow();
dataType2=factor1(dataType1);
return dataType2;
break;

default:
return dt1;
}
}

////////////////////////////////////////////////////////
////////////////FACTOR -> POW FACTOR1. //////////////////
/////////////////////////////////////////////////////////

string factor()
{
cout << " im in factor"
<<endl;

switch(lookahead)
{

case 3:
// 3 associates identifier
case 18:
// 18 associates with integer constant
case 19:
// 19 associates with character constant
case 20:
// 20 associates with float constant
case 14:
// 14 assocaites with open round bracket
case 30:
dataType1=pow();
dataType2=factor1(dataType1);
// dataType3=compare(dataType1,dataType2);
return dataType3;
break;
}

return dataType1;

int chkIdentifier(int curCP)


{
bool lpStatus;
if(curCP == 3)
{
if(!flagDecVar){
lpStatus = lookup(restBuff, gblScope);
if(lpStatus){
cout << endl
<<"!!!!!! Line "
<< lineNum
<< " : "
<<"Undeclared Identifier \""
<< restBuff
<< "!!!!!!"
<<endl
<<endl;
}
}

if(!flagFunc && flagDecVar)


{
lpStatus = lookup(restBuff, gblScope);
if(lpStatus)
insertID(restBuff, gblDT, gblScope, gblSize);
else
system("Pause");
}

if(flagDim)
{
}
}
return 0;
}

int match(int curCP)


{
cout << " im in match"
<<endl;
if(flagScope)
gblScope++;

if(lookahead == curCP)
{
cout << " Succeeded"
<<endl;

if((curCP == 2) && (flagDecVar))


{
gblDT = restBuff;
gblSize = size(gblDT);
}
chkIdentifier(curCP);

readCh();
compCh();

return 1;
}
else
{

cout<< " "


<<terminal[lookahead]
<< " found "
<< mapTer[curCP]
<< " expected"
<<endl;
cout<< " Failed"
<<endl;
system("Pause");
exit(1);
}
}

void mapping(void)
{
////////////////////////// terminals Mapping //////////////////////////

char *ter;
for( int index = 0 ; index < 37; index++)
{
ter = terminal[index];
mapTer[index] = ter;
}
}

void compCh(void)
{
int status;
char *buffer1 = buffer;

/////////////////////////// Map
comparison ///////////////////////////////

for( int index = 0; index < 37; index++)


{
status = strcmp(buffer1, mapTer[index]);
if(status == 0)
{
cout << mapTer[index]
<< endl;
lookahead = index;
break;
}
}
}

int insertID(string n, string t, int s, string sz)


{
cout<<"//////////////////Symbol Table for
Identifier//////////////////"<< endl <<endl;

stid[++Aindex].name = n;
stid[Aindex].type = t;
stid[Aindex].scope = s;
stid[Aindex].size = sz;

for(int i = 0; i <= Aindex; i++)


{
cout <<"Name: " << stid[i].name
<< "\t\t| "
<< "Type: " << stid[i].type
<< "\t| "
<< "Size: " << stid[i].size
<< "\t| "
<< "Scope: " <<stid[i].scope
<<endl;
}
cout <<endl <<
"//////////////////////////////////////////////////////////////"<<endl;
return true;
}

int insertFunc(string n, string rt, string pl, int s)


{
stf[++Findex].name = n;
stf[Findex].retType = rt;
stf[Findex].paramList = pl;
stf[Findex].scope = s;

return true;
}

string size (string dt)


{
if(dt=="Int")
return "2";
else if(dt=="Char")
return "1";
else if (dt=="Float")
return "4";
}
bool lookup(string n, int s)
{
cout<<"Im in LookUp"<<endl;
for(int i=0; i<=Aindex ; i++)
{
if((stid[i].name == n) && (stid[i].scope == s))
{
if(flagDecVar)
{
cout<<endl;
cout <<"!!!!!! Line "
<< lineNum
<< " : "
<< "Redeclaration of identifier "
<< stid[i].name
<< " of type "
<< stid[i].type
<< "!!!!!"
<< endl
<<endl;
}
return false;
}
}
return true;
}
/*
string compare(string dt1,string dt2)
{
if(dt1=="Float" && dt2=="Int")
return dt1;
else if(dt1=="Int" && dt2=="Float")
return dt2;
else if(dt1=="Float[]" && dt2=="Int[]")
return dt1;
else if(dt1=="Int[]" && dt2=="Float[]")
return dt2;
else if(dt1=="Int[]" && dt2=="Char[]")
return dt1;
else if(dt1=="Char[]" && dt2=="Int[]")
return dt2;
else return dt1;}*/

Vous aimerez peut-être aussi