Parser

Published on February 2017 | Categories: Documents | Downloads: 31 | Comments: 0 | Views: 248
of 7
Download PDF   Embed   Report

Comments

Content

/*
GRAMAR FOR SYNATX CHECKING OF FUNCTION PROTOTYPE

fun_pro -> ret_type fun_name ( arg_list );
ret_type -> int | char | float
fun_name -> [a-zA-Z]+
arg_list -> ret_type fun_name
| ret_type fun_name , arg_list

*/

#include "stdafx.h"
#include <stdio.h>
#include <string.h>
void getToken(FILE *read);
char word[50];
FILE *ptr;
void fun_pro();
void ret_type();
void fun_name();
void arg_list();

int main(int argc, char* argv[])
{

ptr = fopen("sample.txt","r");

if (ptr==NULL)
{
printf("Not able to load file\n");
return(0);
}
fun_pro();

return 0;
}

void getToken(FILE *read)
{
char ch;
int i=0;

while ((ch = fgetc(read))!=EOF)
{
if (ch != ' ' && ch !='\t')
{
fseek(read,-1,SEEK_CUR);
break;
}
}

while ((ch=fgetc(read))!=EOF)

{
if (ch != ' ' && ch != '\t' && ch != '\n' && ch != '(' &&
ch != ')' && ch !=',' && ch !=';' )
word[i++] = ch;
else
{
if (ch == '(' || ch == ')' || ch == ',' || ch == ';')
{
if (i==0)
word[i++] = ch;
else
fseek(read,-1,SEEK_CUR);
}
break;
}
}
word[i] = '\0';
}

void fun_pro()
{
getToken(ptr);

ret_type();
fun_name();

if (strcmp("(",word) == 0)

getToken(ptr);

else
{
printf (" open paranthesis is missing\n");
return;
}
arg_list();

if (strcmp(")",word) == 0)

getToken(ptr);

else
{
printf (" close paranthesis is missing\n");
return;
}

if (strcmp(";",word) == 0)
{
getToken(ptr);

}

else
{

printf (" semicolon is missing\n");
return;
}

printf("Valid function prototype\n");
}

void ret_type()
{

if (strcmp("int",word) == 0 || strcmp("void",word) == 0 ||
strcmp("char",word) == 0 || strcmp("float",word) == 0)
getToken(ptr);
else
printf("Invalid return type\n");
}

void fun_name()
{
int i;

for (i =0; word[i]!='\0';i++)
{
if ( (word[i] >= 97 && word[i] <= 122 ) || (word[i] >= 65
&& word[i] <= 90 ) )

continue;
else
{
printf ("Not a valid identifier\n");
break;
}

}
getToken(ptr);
}

void arg_list()
{

if ((strcmp(")",word))==0)
{
printf ("No arguments\n");
fseek(ptr,-1,SEEK_CUR);

}
else
{
ret_type();
fun_name();

if (strcmp(",",word)==0)
{
getToken(ptr);

arg_list();
}
}

}

Sponsor Documents

Or use your account on DocShare.tips

Hide

Forgot your password?

Or register your new account on DocShare.tips

Hide

Lost your password? Please enter your email address. You will receive a link to create a new password.

Back to log-in

Close