Vous êtes sur la page 1sur 14

#include <stdio.

h>
#include <stdlib.h>
#include <string.h>

//#define debug(n) printf("Here %d\n",n);


#define np flag=1;printf("Parsing Failure");exit(0);

int flag=0;

int check_data_type(char s[]){


if(strcmp(s,"int")==0 || strcmp(s,"char")==0)
return 1;
return 0;
}

int check_main(char s[]){


if(strcmp(s,"main")==0)
return 1;
return 0;
}

int check_special_symbol(char ch){


if(ch=='('||ch==')'||ch=='{'||ch=='}'||ch==';'||ch==','||ch=='['||c
h==']')
return 1;
return 0;
}
FILE *f;
char c;

int is_digit(char c){


int x=(int)c-48;

if((int)c>=48&&(int)c<=57)
return 1;
return 0;
}

int is_number(char z[]){

for (int i = 0; i < strlen(z); ++i)


{
if(!is_digit(z[i]))
return 0;
//printf("\ndigit is %d",(int)z[i]);
}
return 1;
}

int check_relop(char s[]){


if(strcmp(s,"==")==0||strcmp(s,"!=")==0||strcmp(s,"<=")==0||strcmp(
s,">=")==0||strcmp(s,"<")==0||strcmp(s,">")==0)
return 1;
return 0;
}
int check_addop(char s[]){
if(strcmp(s,"+")==0||strcmp(s,"-")==0)
return 1;
return 0;
}
int check_mulop(char s[]){
if(strcmp(s,"*")==0||strcmp(s,"/")==0||strcmp(s,"%")==0)
return 1;
return 0;
}

int check_loop_op(char s[]){


if(strcmp(s,"for")==0||strcmp(s,"while")==0)
return 1;
return 0;
}

int check_decision(char s[]){


if(strcmp(s,"if")==0||strcmp(s,"else")==0)
return 1;
return 0;
}

char* getvalid(char s[]){


if(check_main(s)){
return s;
}
else if(is_number(s)){
return "num";
}else if(check_data_type(s)){
return s;
}else if(check_loop_op(s)){
return s;
}else if(check_decision(s)){
return s;
}else{
return "id";
}
}

char *s;
int k;

char* getNextToken(){
c=getc(f);
while(c!=EOF){
if(isalnum(c)){
k=0;
while(isalnum(c)){

s[k++]=c;
c=getc(f);
}
s[k]='\0';
fseek(f,-1L,SEEK_CUR);
return getvalid(s);
}else if(c=='='){
k=0;
s[k++]=c;
c=getc(f);
if(c=='='){
s[k++]=c;
}else{
fseek(f,-1L,SEEK_CUR);
}
s[k]='\0';
return s;
}else if(c=='<'){
k=0;
s[k++]=c;
c=getc(f);
if(c=='='){
s[k++]=c;
}else{
fseek(f,-1L,SEEK_CUR);
}
s[k]='\0';
return s;
}else if(c=='>'){
k=0;
s[k++]=c;
c=getc(f);
if(c=='='){
s[k++]=c;
}else{
fseek(f,-1L,SEEK_CUR);
}
s[k]='\0';
return s;
}else if(c=='!'){
k=0;
s[k++]=c;
c=getc(f);
if(c=='='){
s[k++]=c;
}else{
fseek(f,-1L,SEEK_CUR);
}
s[k]='\0';
return s;
}else if(c=='+'){
k=0;
s[k++]=c;
c=getc(f);
if(c=='+'){
s[k++]=c;
}else{
fseek(f,-1L,SEEK_CUR);
}
s[k]='\0';
return s;
}else if(c=='-'){
k=0;
s[k++]=c;
c=getc(f);
if(c=='-'){
s[k++]=c;
}else{
fseek(f,-1L,SEEK_CUR);
}
s[k]='\0';
return s;
}else if(c=='*'||c=='/'||c=='%'){
k=0;
s[k++]=c;
s[k]='\0';
return s;
}else if(check_special_symbol(c)){
k=0;
s[k++]=c;
s[k]='\0';
return s;
}
c=getc(f);
}
}

void else_dprime();
void program();
void declaration();
void stmt_list();
void stmt();
void identifiers();
void assign();
void expn();
void eprime();
void simple_exp();
void seprime();
void term();
void tprime();
void factor();
void loop_stat();
void decision();

void program(){
if(strcmp(getNextToken(),"main")==0 &&
strcmp(getNextToken(),"(")==0 && strcmp(getNextToken(),")")==0
&&strcmp(getNextToken(),"{")==0){
declaration();
stmt_list();
char *p = getNextToken();
if(strcmp(p,"}")!=0){
//debug(1)
np
}
}else{
//debug(2)
np
}
}

void declaration(){
char *p = getNextToken();
//printf("%s\n",p );
if(check_data_type(p)){
identifiers();
if(strcmp(getNextToken(),";")==0){
declaration();
}else{
//debug(3)
np
}
}else{
long x = strlen(p);
fseek(f,-1*x,SEEK_CUR);
}
}

void identifiers(){
if(strcmp(getNextToken(),"id")==0){
char *p = getNextToken();
if(strcmp(p,",")==0){
identifiers();
}else{
//trace back
long x = strlen(p);
fseek(f,-1*x,SEEK_CUR);
}

p=getNextToken();
if(strcmp(p,"[")==0){
if(strcmp(getNextToken(),"num")==0 &&
strcmp(getNextToken(),"]")==0){
char *q = getNextToken();
if(strcmp(q,",")==0){
identifiers();
}else{
long x = strlen(q);
fseek(f,-1*x,SEEK_CUR);
}
}else{
//debug(14)
np
}
}else{
long x = strlen(p);
fseek(f,-1*x,SEEK_CUR);
}
}else{
//debug(4)
np
}
}

void stmt_list(){
char *p = getNextToken();
long x = strlen(p);
//printf("stmt list %s\n",p);
if(strcmp(p,"id")==0){
fseek(f,-1*x,SEEK_CUR);
assign();
char *q = getNextToken();
//printf("\n%s\n",q );
if(strcmp(q,";")==0){
stmt_list();
}else{
//debug(19)
np
}
}else if(check_loop_op(p)){
fseek(f,-1*x,SEEK_CUR);
loop_stat();
}else if(check_decision(p)){
fseek(f,-1*x,SEEK_CUR);
decision();
}
else{
fseek(f,-1*x,SEEK_CUR);
}
}
void assign(){
char *p = getNextToken();
//printf("assign %s\n", p);
if(strcmp(p,"id")==0){
char *q = getNextToken();
//printf("assign2 %s\n",q);
if(strcmp(q,"=")==0){
expn();
}else{
//debug(7)
np
}
}else{
//debug(30)
np
}
}

void expn(){
simple_exp();
eprime();
}

void else_dprime(){

if(strcmp(getNextToken(),"else")==0){
if(strcmp(getNextToken(),"{")==0){
stmt_list();
char *q = getNextToken();
//printf("else dprime is %s\n",q);
if(strcmp(q,"}")==0){
//correct
return;
}else{
//debug(38)
np
}
}else{
//debug(37)
np
}
}else{

}
}

void decision(){
if(strcmp(getNextToken(),"if")==0){
if(strcmp(getNextToken(),"(")==0){
expn();
if(strcmp(getNextToken(),")")==0){
if(strcmp(getNextToken(),"{")==0){
stmt_list();
if(strcmp(getNextToken(),"}")==0){
else_dprime();
}else{
//debug(36)
np
}
}else{
//debug(35)
np
}
}else{
//debug(34)
np
}
}else{
//debug(33)
np
}
}else{
//debug(32)
np
}
}

void loop_stat(){
char *p=getNextToken();
//printf("loop %s\n",p);
if(strcmp(p,"while")==0){
if(strcmp(getNextToken(),"(")==0){
expn();
if(strcmp(getNextToken(),")")==0){
if(strcmp(getNextToken(),"{")==0){
stmt_list();
if(strcmp(getNextToken(),"}")==0){

}else{
//debug(25)
np
}
}else{
//debug(24)
np
}
}else{
//debug(23)
np
}
}else{
//debug(22)
np
}
}else if(strcmp(p,"for")==0){
if(strcmp(getNextToken(),"(")==0){
assign();
if(strcmp(getNextToken(),";")==0){
expn();
if(strcmp(getNextToken(),";")==0){
assign();
if(strcmp(getNextToken(),")")==0){
if(strcmp(getNextToken(),"{")==0){
stmt_list();

if(strcmp(getNextToken(),"}")==0){
return;
}else{
//debug(31)
np
}
}else{
//debug(30)
np
}
}else{
//debug(29)
np
}
}else{
//debug(28)
np
}
}else{
//debug(27)
np
}
}else{
//debug(26)
np
}

}else{
//debug(21)
np
}
}

void eprime(){
char *p = getNextToken();
//printf("eprime %s\n", p);
if(check_relop(p)){
simple_exp();
}else{
long x = strlen(p);
fseek(f,-1*x,SEEK_CUR);
}
}

void simple_exp(){
term();
seprime();
}

void seprime(){
char *p = getNextToken();
//printf("seprime %s\n",p );
if(check_addop(p)){
term();
seprime();
}else{
long x = strlen(p);
fseek(f,-1*x,SEEK_CUR);
}
}

void term(){
factor();
tprime();
}

void tprime(){
char *p = getNextToken();
//printf("%s\n",p );
if(check_mulop(p)){
factor();
tprime();
}else{
long x = strlen(p);
fseek(f,-1*x,SEEK_CUR);
}
}

void factor(){
char *p = getNextToken();
//printf("factor %s\n",p );
if(strcmp(p,"id")==0 || strcmp(p,"num")==0){
//printf("yes\n");
}else{
//debug(16)
np
}
}

int main(){
f=fopen("test.c","r");
//getNextToken();
s=(char *)malloc(sizeof(char)*100);

program();

if(flag==0){
printf("\nParsing Success\n");
}
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define debug(n) printf("Here %d\n",n);


#define np flag=1;printf("not possible");exit(0);

int flag=0;

int check_if_data_type(char s[]){


if(strcmp(s,"int")==0 || strcmp(s,"char")==0)
return 1;
return 0;
}

int check_if_main(char s[]){


if(strcmp(s,"main")==0)
return 1;
return 0;
}

int check_if_special_symbol(char ch){


if(ch=='('||ch==')'||ch=='{'||ch=='}'||ch==';'||ch==','||ch=='['||c
h==']')
return 1;
return 0;
}
FILE *f;
char c;

int is_digit(char c){


int x=(int)c-48;

if((int)c>=48&&(int)c<=57)
return 1;
return 0;
}

int is_number(char z[]){

for (int i = 0; i < strlen(z); ++i)


{
if(!is_digit(z[i]))
return 0;
//printf("\ndigit is %d",(int)z[i]);
}
return 1;
}

int check_if_relop(char s[]){


if(strcmp(s,"==")||strcmp(s,"!=")||strcmp(s,"<=")||strcmp(s,">=")||
strcmp(s,"<")||strcmp(s,">"))
return 1;
return 0;
}
int check_if_addop(char s[]){
if(strcmp(s,"+")||strcmp(s,"-"))
return 1;
return 0;
}
int check_if_mulop(char s[]){
if(strcmp(s,"*")||strcmp(s,"/")||strcmp(s,"%"))
return 1;
return 0;
}
int check_if_loop_op(char s[]){
if(strcmp(s,"for")==0||strcmp(s,"while")==0)
return 1;
return 0;
}

int check_if_decision(char s[]){


if(strcmp(s,"if")==0||strcmp(s,"else")==0)
return 1;
return 0;
}

char* getvalid(char s[]){


if(check_if_main(s)){
return s;
}
else if(is_number(s)){
return "num";
}else if(check_if_data_type(s)){
return s;
}else if(check_if_loop_op(s)){
return s;
}else if(check_if_decision(s)){
return s;
}else{
return "id";
}
}

char *s;
int k;

void getNextToken(){
c=getc(f);
while(c!=EOF){
if(isalnum(c)){
k=0;
while(isalnum(c)){

s[k++]=c;
c=getc(f);
}
s[k]='\0';
fseek(f,-1L,SEEK_CUR);
printf("%s\n",getvalid(s));
}else if(c=='='){
k=0;
s[k++]=c;
c=getc(f);
if(c=='='){
s[k++]=c;
}else{
fseek(f,-1L,SEEK_CUR);
}
s[k]='\0';
printf("%s\n",s );

}else if(c=='<'){
k=0;
s[k++]=c;
c=getc(f);
if(c=='='){
s[k++]=c;
}else{
fseek(f,-1L,SEEK_CUR);
}
s[k]='\0';
printf("%s\n",s );
}else if(c=='>'){
k=0;
s[k++]=c;
c=getc(f);
if(c=='='){
s[k++]=c;
}else{
fseek(f,-1L,SEEK_CUR);
}
s[k]='\0';
printf("%s\n",s );
}else if(c=='!'){
k=0;
s[k++]=c;
c=getc(f);
if(c=='='){
s[k++]=c;
}else{
fseek(f,-1L,SEEK_CUR);
}
s[k]='\0';
printf("%s\n",s );
}else if(c=='+'){
k=0;
s[k++]=c;
c=getc(f);
if(c=='+'){
s[k++]=c;
}else{
fseek(f,-1L,SEEK_CUR);
}
s[k]='\0';
printf("%s\n",s );
}else if(c=='-'){
k=0;
s[k++]=c;
c=getc(f);
if(c=='-'){
s[k++]=c;
}else{
fseek(f,-1L,SEEK_CUR);
}
s[k]='\0';
printf("%s\n",s );
}else if(c=='*'||c=='/'||c=='%'){
k=0;
s[k++]=c;
s[k]='\0';
printf("%s\n",s );
}else if(check_if_special_symbol(c)){
k=0;
s[k++]=c;
s[k]='\0';
printf("%s\n",s );
}
c=getc(f);
}
}

int main(){
f=fopen("test.c","r");
//getNextToken();
s=(char *)malloc(sizeof(char)*100);
getNextToken();

if(flag==0){
printf("\nsuccess\n");
}
}
main(){
int a;
b=c;
while(i<5){
b=c;
}
}

Vous aimerez peut-être aussi