Académique Documents
Professionnel Documents
Culture Documents
#include <iostream>
#include <fstream>
#include <string.h>
#include "myStream.h"
#include "lexer.h"
float a = 1.e5;
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);
}
system("Pause");
}
myStream.h
#include <iostream>
#include <fstream>
#include <string.h>
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;
}
#include "tokenizer.h"
int scanner(void) {
char ch;
int zTokType=0, i=0;
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';
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"};
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>
void putBack(char);
struct SymbolTableID
{
string name;
string type;
int scope;
string size;
};
struct SymbolTableFunc{
string name;
string retType;
string paramList;
int scope;
};
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"};
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 putBackToken()
{
for(int i = restInd; i >= 0; i--){
putBack(restBuff[i]);
cout << " Im showing restBuff "
<< restBuff[i]
<<endl
<<endl;
}
#include<iostream>
#include<fstream>
#include <map>
#include<string>
#include "readString.h"
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;
void main(void)
{
int status;
status = readFile();
mapping();
if(status)
{
readCh();
compCh();
}
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;
}
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
| .//////////////////////////
////////////////////////////////////////////////////////////////////////
//////////////
case 30:
dim(dt);
break;
default:
return true ;
}
}
////////////////////////////////////////////////////////////////////////
//////////////
//////////////////////dim -> osqbracket int_const csqbracket
DIM1 ////////////////////
////////////////////////////////////////////////////////////////////////
//////////////
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 //////////
////////////////////////////////////////////////////////////////////////
//////////////
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.////////////////////////
////////////////////////////////////////////////////////////////////////
switch(lookahead)
{
case 4:
match(4);
id_list1(dt);
break;
case 7:
match(7);
flagDec = false;
declaration();
break;
}
return 0;
}
/*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//////////////
////////////////////////////////////////////////////
switch(lookahead)
{
case 36:
dataType1=X(dt);
dataType2=NExpr(dt);
// dataType3=compare(dataType1,dataType2);
return dataType3;
break;
default:
return dt;
}
}
////////////////////////////////////////////////////
/////////////// X---> RelOp expr | null //////////
////////////////////////////////////////////////////
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 //////////////////////////////////
////////////////////////////////////////////////////////////////////////
/
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 //////////////////////////////////
////////////////////////////////////////////////////////////////////////
/
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 //////////
//////////////////// | . ///////////////////////////
/////////////////////////////////////////////////////
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;
if(flagDim)
{
}
}
return 0;
}
if(lookahead == curCP)
{
cout << " Succeeded"
<<endl;
readCh();
compCh();
return 1;
}
else
{
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 ///////////////////////////////
stid[++Aindex].name = n;
stid[Aindex].type = t;
stid[Aindex].scope = s;
stid[Aindex].size = sz;
return true;
}