(change) programming experiment a lexical analysis program

Source: Internet
Author: User
Tags strcmp

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


typedef struct node{
Char words[255];
struct node *next;
}node;

Char word[255];
Char *word_default[6]={' begin ', ' if ', ' then ', ' while ', ' Do ', ' end '};


void Splitter_node_add (char *str,node *node);

Logical judgment
int Acceptation_logic (char *ch) {

Puts (CH);
int key=0;
if (strcmp (CH, "begin") ==0) {
printf ("<%s \ t 1 >\n", ch);
}

else if (strcmp (CH, "if") ==0) {
printf ("<%s \ t 2 >\n", ch);
}
else if (strcmp (CH, "then") ==0) {
printf ("<%s \ t 3 >\n", ch);
}
else if (strcmp (CH, "while") ==0) {
printf ("<%s \ t 4 >\n", ch);
}
else if (strcmp (CH, "do") ==0) {
printf ("<%s \ t 5 >\n", ch);
}
else if (strcmp (CH, "end") ==0) {
printf ("<%s \ t 6 >\n", ch);
}
else if (strcmp (CH, "+") ==0) {
printf ("<%s \ t 13>\n", CH);
}
else if (strcmp (CH, "-") ==0) {
printf ("<%s \ t 14>\n", CH);
}
else if (strcmp (CH, "*") ==0) {
printf ("<%s \ t 15>\n", CH);
}
else if (strcmp (CH, "/") ==0) {
printf ("<%s \ t 16>\n", CH);
}
else if (strcmp (CH, ":") ==0) {
printf ("<%s \ t 17>\n", CH);
}
else if (strcmp (ch, ": =") ==0) {
printf ("<%s \ t 18>\n", CH);
}
else if (strcmp (CH, "<") ==0) {
printf ("<%s \ t 20>\n", CH);
}
else if (strcmp (CH, "<=") ==0) {
printf ("<%s \ t 21>\n", CH);
}
else if (strcmp (CH, "<>") ==0) {
printf ("<%s \ t 22>\n", CH);
}
else if (strcmp (ch, ">") ==0) {
printf ("<%s \ t 23>\n", CH);
}
else if (strcmp (CH, ">=") ==0) {
printf ("<%s \ t 24>\n", CH);
}
else if (strcmp (ch, "=") ==0) {
printf ("<%s \ t 25>\n", CH);
}
else if (strcmp (CH, ";") ==0) {
printf ("<%s \ t 26>\n", CH);
}
else if (strcmp (CH, "(") ==0) {
printf ("<%s \ t 27>\n", CH);
}
else if (strcmp (CH, ")") ==0) {
printf ("<%s \ t 28>\n", CH);
}
else if (strcmp (CH, "#") ==0) {
printf ("<%s \ t 0>\n", CH);
}
else{

Key=1;
}
Return key;

}
int Acceptation_string_key (char ch);

int acceptation_string (char *str) {
int Key=1;
Char *ch= NULL;
CH=STR;
if (Acceptation_string_key (str[0]) ==0) {
printf ("<%s \ 11>\n", str);
key=0;
}
Return key;

}
int Acceptation_number_key (char ch) {
int key=0;

if (ch<= ' 9 ' &&ch>= ' 0 ') {
key=0;
}
Else
Key=1;

Return key;
}

int Acceptation_number (char *str) {
int key=0;
Char *ch=null;
CH=STR;
while (*ch!= ') {

Key+=acceptation_number_key (*CH);
printf ("keynumber=%d,ch=%c\n", key,*ch);
*ch++;

}
if (key==0)
printf ("<%s \ 10>\n", str);

Return key;

}

int Acceptation_string_key (char ch) {
int key=0;
if (ch<= ' z ' &&ch>= ' a ') {
key=0;
}
Else
Key=1;
Return key;
}

void Node_in (char *str,node *node) {

}

Reset
int Acceptation_str (char ch,char *str,int i,char *sch,node *tnode) {
*sch++;
printf ("%d\n", Acceptation_string_key (CH));
if (Acceptation_logic (str) ==0) {

Splitter_node_add (Str,tnode);
i=0;
memset (str, 0,i);

}
if (Acceptation_string_key (CH) ==0) {
if (Acceptation_string_key (*sch)!=0&&acceptation_number_key (*sch)!=0) {
printf ("str=%s", str);
Acceptation_string (str);

Splitter_node_add (str,tnode); i=0;
memset (str, 0,i);

}

}

if (Acceptation_number_key (CH) ==0) {
if (Acceptation_number_key (*sch)!=0) {
Acceptation_number (str);
Splitter_node_add (str,tnode); i=0;
memset (str, 0,i);

}
}


return i;
}


Judging the meaning of words
void Word_acceptation (char *ch,node *tnode) {
Char *str=null;
Char **sstr=null;
int I=1,k=0,key=1;

sstr=&ch;
Str= (char *) malloc (1);
Str[0]=*ch;
str[1]= ' + ';

deposited in Str
while (*ch!= ') {
*ch++;
k++;
Str[i]=*ch;
i++;
str[i]= ' + ';
**sstr++;
sstr=&ch;
I=acceptation_str (*ch,str,i,*sstr,tnode);


Puts (str);

}

}


Deposit node
void Splitter_node_add (char *str,node *node) {
NODE *p,*q;
P=node;
while (P->next!=null) {
p=p->next;
}
q= (node *) malloc (sizeof (node));
q->next=null;
strcpy (Q->WORDS,STR);
p->next=q;
p=q;


}

Determine the space carriage return position
int Splitter_start (char *str) {
int i=0;
while (*str!= ') {
*str++;
i++;
if (*str== ')
Break
if (*str== ' \ n ')
Break
}
return i;
}


Divide each letter with a space or carriage return into a word
void Word_splitter (char* str,node *node) {

Char *aword=null;
Char **sword=null;

sword=&str;

printf ("splitter_start=%d\n", Splitter_start (*sword));

Intercept the first paragraph
aword= (char *) malloc (Splitter_start (*sword));
strncpy (Aword,*sword,splitter_start (*sword));
Aword[splitter_start (*sword)]= ' + ';
Puts (Aword);
Splitter_node_add (Aword,node);
memset (aword, 0, Splitter_start (*sword));

while (*str!= ') {

printf ("%c\n", *str);
*str++;
*aword++;

if (*str== ' | | | *str== ' \ n ') {
aword= (char *) malloc (Splitter_start (*sword));

*str++;//Intercept spaces or enter
**sword++;
sword=&str;

printf ("splitter_start=%d\n", Splitter_start (*sword));
Puts (*sword);

strncpy (Aword,*sword,splitter_start (*sword));
Aword[splitter_start (*sword)]= ' + ';

Splitter_node_add (Aword,node);
Puts (Aword);

memset (aword, 0, Splitter_start (*sword));
}

}
}

void Word_node (char *ch,node *node) {

NODE *p,*tnode;
Puts (word);

Tnode= (node *) malloc (sizeof (node));
tnode->next=null;

Save Word to Node
Splitter_node_add ("If", node);
Word_splitter (Word,node);

P=node;
while (P->next!=null) {
p=p->next;
Word_acceptation (P->words,tnode);
Word_acceptation (p->words);
Acceptation_logic (p->words);
}

printf ("\ n");

P=tnode;
while (P->next!=null) {

p=p->next;

if (Acceptation_logic (p->words) ==0);
else if (acceptation_string (p->words) ==0);
else if (Acceptation_number (p->words) ==0);
else{
printf ("< error >\n");
}

Puts (p->words);
}

}


int main () {
NODE *node;

Node= (node *) malloc (sizeof (node));
node->next=null;

printf ("Please enter: \ n");
scanf ("%[^#]s", word);

Word_node (Word,node);

Puts (word);

return 0;

}

(change) programming experiment a lexical analysis program

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.