當前位置:首頁 » 編程語言 » c語言詞法分析器怎麼編
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言詞法分析器怎麼編

發布時間: 2022-09-22 04:38:56

c語言詞法分析器

任務1:識別小型語言所有單詞的詞法分析程序設計
源程序設計語言 G[<程序>]
<程序>→<變數說明><BEGIN> <語句表> <END>.
<變數說明>→VAR<變數表>:<類型>;|<空>
<變數表>→<變數表>,<變數>|<變數>
<類型>→INTEGER
<語句表>→<語句> | <語句>;<語句表>
<語句>→<賦值語句>|<條件語句>|<WHILE語句>|<復合語句>
<賦值語句>→<變數>:=<算術表達式>
<條件語句>→IF<關系表達式>THEN<語句>ELSE<語句>
<WHILE語句>→WHILE<關系表達式>DO<語句>
<復合語句>→BEGIN<語句表>END
<算術表達式>→<項>|<算術表達式>+<項>|<算術表達式>-<項>
<項>→<因式>|<項>*<因式>|<項>/<因式>
<因式>→<變數>|<整數>|(<算術表達式>)
<關系表達式>→<算術表達式><關系符><算術表達式>
<變數>→<標識符>
<標識符>→<標識符><字母>|<標識符><數字>|<字母>
<整數>→0|<非零數字><泛整數>
<泛整數>→<數字>|<數字><泛整數>|ε
<關系符>→<|<=|==|>|>=|<>
<字母>
→A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z
<非零數字>→1|2|3|4|5|6|7|8|9
<數字>→<非零數字>|0
<空>→
要求和提示:
詞法分析階段,可以打開任意位置和名稱的源文件進行詞法分析,可以進行非法字元和數字後邊跟字母的錯誤判斷,如果沒有錯誤則提示「詞法分析正確完成!」,並且可以選擇輸出token.txt(token文件)string.txt(符號表)兩個文件;
1.詞法分析程序的主要任務如下:
① 組織源程序的輸入,識別出源程序中的各個基本語法單位(也稱為單詞或語法符號),按規則轉換成二元式的形式;
② 刪除無用的空白字元、回車符、及其它非實質性符號;
③ 刪除註解行;
④ 為後面的語法和語義分析提供二元式鏈表;
單詞 編碼 單詞 編碼
標識符 1 < 15
正整數 2 <= 16
BEGIN 3 > 17
END 4 >= 18
IF 5 <> 19
THEN 6 == 20
ELSE 7 ; 21
WHILE 8 . 22
DO 9 := 23
INTEGER 10 , 24
+ 11 ( 25
- 12 ) 26
* 13
/ 14
1) 對標識符的長度控制在8個字元(包括8個)以內,超過的做截斷處理;
2) 數字不大於65535,否則報錯;
3) 能跳過源程序中的空白格:兩個單詞之間的任何空格,製表符,回車,換行都是白空格,除了用來分隔單詞以外,沒有意義;
4) 能跳過注釋:
a) 接連出現的/*到下一次接連出現的*/之間的任何文字都是注釋(多行);
b) 從某行接連出現的//到該行的結尾的任何文字都是注釋(單行)。
3.怎樣編寫詞法分析程序:
1) 預處理:把源文件一個字元一個字元的讀入詞法分析程序設置的輸入字元結構體數組中(輸入緩沖區),讀入過程要刪除注釋,刪除多餘的白空格;
2) 從源程序字元數組中獲得單詞, 編碼為二元式.:
二元式採用結構體數組存儲, 把單詞類型和詞元記錄下來。
分解單詞的方法:
1) Case多路轉換語句根據單詞的特點直接編寫;
2) 通過描述單詞的正規文法得到相應的有窮自動機,通過case多路轉換語句完成有窮自動機的處理流程。
3.編寫詞法分析程序要注意的問題:
1) 檢查詞法是否有錯誤
檢查是否有非法字元:如 @, &, !
檢查標志符和數字是否滿足限制條件
檢查注釋符號是否配對
2) 符分隔單詞
能夠區分兩個單詞的符號為界符
有些界符不是單詞:如白空格
有些界符僅僅用來分隔:如;
有些界符本身還是源程序不可缺少的單詞,如(, ), +, /, 等等
有些界符包含兩個字元:如<>, >=等等
3) 輸出詞法錯誤
如果有錯誤,需要報告詞法錯誤的原因。並且要能夠越過錯誤,分解下一個單詞,直到源程序結束。
4) 輸出的二元式流保存在二元式結構體數組中。

㈡ c語言的詞法分析器

任務1:識別小型語言所有單詞的詞法分析程序設計
源程序設計語言
G[<程序>]
<程序>→<變數說明><BEGIN>
<語句表>
<END>.
<變數說明>→VAR<變數表>:<類型>;|<空>
<變數表>→<變數表>,<變數>|<變數>
<類型>→INTEGER
<語句表>→<語句>
|
<語句>;<語句表>
<語句>→<賦值語句>|<條件語句>|<WHILE語句>|<復合語句>
<賦值語句>→<變數>:=<算術表達式>
<條件語句>→IF<關系表達式>THEN<語句>ELSE<語句>
<WHILE語句>→WHILE<關系表達式>DO<語句>
<復合語句>→BEGIN<語句表>END
<算術表達式>→<項>|<算術表達式>+<項>|<算術表達式>-<項>
<項>→<因式>|<項>*<因式>|<項>/<因式>
<因式>→<變數>|<整數>|(<算術表達式>)
<關系表達式>→<算術表達式><關系符><算術表達式>
<變數>→<標識符>
<標識符>→<標識符><字母>|<標識符><數字>|<字母>
<整數>→0|<非零數字><泛整數>
<泛整數>→<數字>|<數字><泛整數>|ε
<關系符>→<|<=|==|>|>=|<>
<字母>
→A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z
<非零數字>→1|2|3|4|5|6|7|8|9
<數字>→<非零數字>|0
<空>→
要求和提示:
詞法分析階段,可以打開任意位置和名稱的源文件進行詞法分析,可以進行非法字元和數字後邊跟字母的錯誤判斷,如果沒有錯誤則提示「詞法分析正確完成!」,並且可以選擇輸出token.txt(token文件)string.txt(符號表)兩個文件;
1.詞法分析程序的主要任務如下:

組織源程序的輸入,識別出源程序中的各個基本語法單位(也稱為單詞或語法符號),按規則轉換成二元式的形式;

刪除無用的空白字元、回車符、及其它非實質性符號;

刪除註解行;

為後面的語法和語義分析提供二元式鏈表;
單詞
編碼
單詞
編碼
標識符
1
<
15
正整數
2
<=
16
BEGIN
3
>
17
END
4
>=
18
IF
5
<>
19
THEN
6
==
20
ELSE
7

21
WHILE
8

22
DO
9
:=
23
INTEGER
10

24
+
11
(
25
-
12

26
*
13
/
14
1)
對標識符的長度控制在8個字元(包括8個)以內,超過的做截斷處理;
2)
數字不大於65535,否則報錯;
3)
能跳過源程序中的空白格:兩個單詞之間的任何空格,製表符,回車,換行都是白空格,除了用來分隔單詞以外,沒有意義;
4)
能跳過注釋:
a)
接連出現的/*到下一次接連出現的*/之間的任何文字都是注釋(多行);
b)
從某行接連出現的//到該行的結尾的任何文字都是注釋(單行)。
3.怎樣編寫詞法分析程序:
1)
預處理:把源文件一個字元一個字元的讀入詞法分析程序設置的輸入字元結構體數組中(輸入緩沖區),讀入過程要刪除注釋,刪除多餘的白空格;
2)
從源程序字元數組中獲得單詞,
編碼為二元式.:
二元式採用結構體數組存儲,
把單詞類型和詞元記錄下來。
分解單詞的方法:
1)
Case多路轉換語句根據單詞的特點直接編寫;
2)
通過描述單詞的正規文法得到相應的有窮自動機,通過case多路轉換語句完成有窮自動機的處理流程。
3.編寫詞法分析程序要注意的問題:
1)
檢查詞法是否有錯誤
檢查是否有非法字元:如
@,
&,
!
檢查標志符和數字是否滿足限制條件
檢查注釋符號是否配對
2)
符分隔單詞
能夠區分兩個單詞的符號為界符
有些界符不是單詞:如白空格
有些界符僅僅用來分隔:如;
有些界符本身還是源程序不可缺少的單詞,如(,
),
+,
/,
等等
有些界符包含兩個字元:如<>,
>=等等
3)
輸出詞法錯誤
如果有錯誤,需要報告詞法錯誤的原因。並且要能夠越過錯誤,分解下一個單詞,直到源程序結束。
4)
輸出的二元式流保存在二元式結構體數組中。

㈢ 編制C語言子集的詞法分析程序

#include <iostream>

#include <string>

using namespace std;

string key[6] = {"begin", "if", "then", "while", "do", "end"};

//關鍵字

bool isKey( string str, int &syn) //判斷是否為關鍵字,若是傳回相

應關鍵碼的種別名

{

int i;

for(i=0; i<6; i++)

{

if(str == key[i])

{

syn = i + 1;

return true;

}

}

return false;

}

bool isLetter(char c) //是否為字母

{

if((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))

return true;

else

return false;

}

bool isDigit(char c) //是否為數字

{

if(c >= '0' && c <= '9')

return true;

else

return false;

}

void analyse(FILE *fileP)

{

int n;

char c;

string str = "";

while((c = fgetc(fileP)) != EOF)

{

if(c == ' ' || c == '\n' || c == '\t')

continue;

else if(isDigit(c)) //數字

{

while(isDigit(c))

{

str += c;

c = fgetc(fileP);

}

fseek(fileP, -1, SEEK_CUR);

cout << "(11, " << str << ")" << endl;

str = "";

}

else if(isLetter(c)) //字母開頭的

{

while(isDigit(c) || isLetter(c))

{

str += c;

c = fgetc(fileP);

}

fseek(fileP, -1, SEEK_CUR);

if(isKey(str, n))

cout << "(" << n << ", " << str << ")" << endl; //關鍵碼

else

cout << "(10, " << "\'"<< str << "\'" << ")" << endl; //標志符

str = "";

}

else //操作符等

{

switch(c)

{

case '+':

cout << "(13, +)" << endl;

break;

case '-':

cout << "(14, -)" << endl;

break;

case '*':

cout << "(15, *)" << endl;

break;

case '/':

cout << "(16, /)" << endl;

break;

case ':':

{

if(c=fgetc(fileP) == '=')

cout << "(18, :=)" << endl;

else

{

cout << "(17, :)" << endl;

fseek(fileP, -1, SEEK_CUR);

}

break;

}

case '<':

{

c=fgetc(fileP);

if(c == '=')

cout << "(22, <=)" << endl;

else if(c == '>')

cout << "(21, <>)" << endl;

else

{

cout << "(20, <)" << endl;

fseek(fileP, -1, SEEK_CUR);

}

break;

}

case '>':

{

c=fgetc(fileP);

if(c == '=')

cout << "(24, >=)" << endl;

else

{

cout << "(23, >)" << endl;

fseek(fileP, -1, SEEK_CUR);

}

break;

}

case '=':

cout << "(25, =)" << endl;

break;

case ';':

cout << "(26, ;)" << endl;

break;

case '(':

cout << "(27, ()" << endl;

break;

case ')':

cout << "(28, ))" << endl;

break;

case '#':

cout << "(0, #)" << endl;

break;

}

}

}

}

int main()

{

FILE *fileP;

fileP = fopen("test.txt", "r");

cout << "------詞法分析如下------" << endl;

analyse(fileP);

return 0;

}

㈣ 重謝!請高人用c語言編寫個詞法分析器

#include "stdio.h" /*定義I/O庫所用的某些宏和變數*/
#include "string.h" /*定義字元串庫函數*/
#include "conio.h" /*提供有關屏幕窗口操作函數*/
#include "ctype.h" /*分類函數*/
char prog[80]={'\0'},
token[8]; /*存放構成單詞符號的字元串*/
char ch;
int syn, /*存放單詞字元的種別碼*/
n,
sum, /*存放整數型單詞*/
m,p; /*p是緩沖區prog的指針,m是token的指針*/
char *rwtab[5]={"while","if","else","switch","case"};
void scaner(){
m=0;
sum=0;
for(n=0;n<8;n++)
token[n]='\0';
ch=prog[p++];
while(ch==' ')
ch=prog[p++];
if(isalpha(ch)) /*ch為字母字元*/{
while(isalpha(ch)||isdigit(ch)) /*ch 為字母字元或者數字字元*/{
token[m++]=ch;
ch=prog[p++];}
token[m++]='\0';
ch=prog[p--];
syn=6;
for(n=0;n<5;n++)
if(strcmp(token,rwtab[n])==0) /*字元串的比較*/{
syn=n+1;
break;}}
else if(isdigit(ch)) /*ch是數字字元*/{
while(isdigit(ch)) /*ch是數字字元*/{
sum=sum*10+ch-'0';
ch=prog[p++];}
ch=prog[p--];
syn=7;}
else
switch(ch){
case'<':m=0;token[m++]=ch;ch=prog[p++];
if(ch=='='){ //判斷是小於號,還是小於等於號
syn=11;
token[m++]=ch;}
else{
syn=11;
ch=prog[p--];}
break;

case'+':syn=8;token[0]=ch;break;
case'-':syn=9;token[0]=ch;break;
case'*':syn=10;token[0]=ch;break;

case'=':m=0;token[m++]=ch;ch=prog[p++];
if(ch=='='){
syn=11;
token[m++]=ch;}
else{syn=12;ch=prog[p--];token[0]=ch;}break;
case';':syn=13;token[0]=ch;break;

case'#':syn=0;token[0]=ch;break;
default:syn=-1;}}
int main()
{
printf("\n\nThe significance of the figures:\n"
"1.figures 1 to 5 said Keyword\n"
"2.figures 6 to 7 said Other indicators\n"
"3.figures 8 to 13 said Operators\n");

p=0;

printf("\nplease input string:\n");
do {
ch=getchar();
prog[p++]=ch;
}while(ch!='#');

p=0;

do{
scaner();
switch(syn){
case 7: printf("(%d,%d)\n",syn,sum);break;
case -1: printf("\n ERROR;\n");break;
default: printf("(%d,%s)\n",syn,token);
}
}while(syn!=0);

return 0;
}

㈤ 跪求C語言編寫的簡單詞法分析器

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int i,j,k,flag,number,status;
/*status which is use to judge the string is keywords or not!*/
char ch;
char words[10] = {" "};
char program[500];
int Scan(char program[])
{
char *keywords[13] = {"void","main","if","then","break","int",
"char","float","include","for","while","printf",
"scanf"};
number = 0;
status = 0;
j = 0;
ch = program[i++];
/* To handle the lettle space ands tab*/

/*handle letters*/
if ((ch >= 'a') && (ch <= 'z' ))
{
while ((ch >= 'a') && (ch <= 'z' ))
{
words[j++]=ch;
ch=program[i++];
}
i--;
words[j++] = '\0';
for (k = 0; k < 13; k++)
if (strcmp (words,keywords[k]) == 0)
switch(k)
{
case 0:{
flag = 1;
status = 1;
break;
}
case 1:{
flag = 2;
status = 1;
break;
}
case 2:{
flag = 3;
status = 1;
break;
}
case 3:{
flag = 4;
status = 1;
break;
}
case 4:{
flag = 5;
status = 1;
break;
}
case 5:{
flag = 6;
status = 1;
break;
}
case 6:{
flag = 7;
status = 1;
break;
}
case 7:{
flag = 8;
status = 1;
break;
}
case 8:{
flag = 9;
status = 1;
break;
}
case 9:{
flag = 10;
status = 1;
break;
}
case 10:{
flag = 11;
status = 1;
break;
}
case 11:{
flag = 12;
status = 1;
break;
}
case 12:{
flag = 13;
status = 1;
break;
}
}
if (status == 0)
{
flag = 100;
}
}
/*handle digits*/
else if ((ch >= '0') && (ch <= '9'))
{
number = 0;
while ((ch >= '0' ) && (ch <= '9' ))
{
number = number*10+(ch-'0');
ch = program[i++];
}
flag = 200;
i--;
}
/*opereation and edge handle*/
else switch (ch)
{
case '=':{
if (ch == '=')
words[j++] = ch;
words[j] = '\0';
ch = program[i++];
if (ch == '=')
{
words[j++] = ch;
words[j] = '\0';
flag = 401;
}
else
{
i--;
flag = 402;
}
break;
}
case'>':{
if (ch == '>')
words[j++] = ch;
words[j] = '\0';
ch = program[i++];
if (ch == '=')
{
words[j++] = ch;
words[j] = '\0';
flag = 403;
}
else
{
i--;
flag = 404;
}
break;
}
case'<':{
if (ch == '<')
words[j++] = ch;
words[j] = '\0';
ch = program[i++];
if (ch == '=')
{
words[j++] = ch;
words[j] = '\0';
flag = 405;
}
else
{
i--;
flag = 406;
}
break;
}
case'!':{
if (ch == '!')
words[j++] = ch;
words[j] = '\0';
ch = program[i++];
if (ch == '=')
{
words[j++] = ch;
words[j] = '\0';
flag = 407;
}
else
{
i--;
flag = 408;
}
break;
}
case'+':{
if (ch == '+')
words[j++] = ch;
words[j] = '\0';
ch = program[i++];
if (ch == '=')
{
words[j++] = ch;
words[j] = '\0';
flag = 409;
}
else if (ch == '+')
{
words[j++] = ch;
words[j] = '\0';
flag = 410;
}
else
{
i--;
flag = 411;
}
break;
}
case'-':{
if (ch == '-')
words[j++] = ch;
words[j] = '\0';
ch = program[i++];
if (ch == '=')
{
words[j++] = ch;
words[j] = '\0';
flag = 412;
}
else if( ch == '-')
{
words[j++] = ch;
words[j] = '\0';
flag = 413;
}
else
{
i--;
flag = 414;
}
break;
}
case'*':{
if (ch == '*')
words[j++] = ch;
words[j] = '\0';
ch = program[i++];
if (ch == '=')
{
words[j++] = ch;
words[j] = '\0';
flag = 415;
}
else
{
i--;
flag = 416;
}
break;
}
case'/':{
if (ch == '/')
words[j++] = ch;
words[j] = '\0';
ch = program[i++];
if (ch == '=')
{
words[j++] = ch;
words[j] = '\0';
flag = 417;
}
else
{
i--;
flag = 418;
}
break;
}
case';':{
words[j] = ch;
words[j+1] = '\0';
flag = 501;
break;
}
case'(':{
words[j] = ch;
words[j+1] = '\0';
flag = 502;
break;
}
case')':{
words[j] = ch;
words[j+1] = '\0';
flag = 503;
break;
}
case'[':{
words[j] = ch;
words[j+1] = '\0';
flag = 504;
break;
}
case']':{
words[j] = ch;
words[j+1] = '\0';
flag = 505;
break;
}
case'{':{
words[j] = ch;
words[j+1] = '\0';
flag = 506;
break;
}
case'}':{
words[j] = ch;
words[j+1] = '\0';
flag = 507;
break;
}
case':':{
words[j] = ch;
words[j+1] = '\0';
flag = 508;
break;
}
case'"':{
words[j] = ch;
words[j+1] = '\0';
flag = 509;
break;
}
case'%':{
if (ch == '%')
words[j++] = ch;
words[j] = '\0';
ch = program[i++];
if (ch == '=')
{
words[j++] = ch;
words[j] = '\0';
flag = 510;
}
else
{
i--;
flag = 511;
}
break;
}
case',':{
words[j] = ch;
words[j+1] = '\0';
flag = 512;
break;
}
case'#':{
words[j] = ch;
words[j+1] = '\0';
flag = 513;
break;
}
case'@':{
words[j] = '#';
flag = 0;
break;
}
default:{
flag = -1;
break;
}
}
return flag;
}
main()
{
i=0;
printf("please input a program end with @");
do
{
ch = getchar();
program[i++] = ch;
}while(ch != '@');
i = 0;
do{
flag = Scan(program);
if (flag == 200)
{
printf("(%2d,%4d)",flag,number);
}
else if (flag == -1)
{
printf("(%d,error)",flag);
}
else
{
printf("(%2d,%4s)",flag,words);
}
}while (flag != 0);
system("pause");
}

㈥ C語言詞法分析器

這是我前陣子寫的:

#include<stdio.h>

#include<string.h>

#defineMAX_SIZE128

//關鍵字表

charkey[][128]={"const","if","while","for","static"};

//連接函數

char*Concat(char*strToken,charch)

{

chartemp[2];

temp[0]=ch;

temp[1]='';

strcat(strToken,temp);

returnstrToken;

}

//是否為字母

intIsLetter(charch)

{

if(((ch>='A')&&(ch<='Z'))||((ch>='a')&&(ch<='z')))

return1;

return0;

}

//是否為數字

intIsDigit(charch)

{

if(ch>='0'&&ch<='9')

return1;

return0;

}

//是否是關鍵字

intReserve(char*strToken)

{

for(inti=0;i<5;i++)

{

if(0==strcmp(strToken,key[i]))

return1;

}

return0;

}

//詞法分析函數

voidanalyse(FILE*file)

{

charstrToken[MAX_SIZE];

charch;

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

{

//將字元串清空

memset(strToken,0,MAX_SIZE);

if((ch==' ')||(ch==' ')||(ch==''))

{

//printf("kong ");

}

elseif(IsLetter(ch))

{

while(IsLetter(ch)||IsDigit(ch))

{

Concat(strToken,ch);

ch=fgetc(file);

}

//讓文件流指針後退一個位元組

fseek(file,-1L,SEEK_CUR);

//判斷是否為關鍵字

if(Reserve(strToken))

{

printf("%s:為關鍵字 ",strToken);

}

else

printf("%s:為標識符 ",strToken);

}

elseif(IsDigit(ch))

{

while(IsDigit(ch))

{

Concat(strToken,ch);

ch=fgetc(file);

}

//讓文件流指針後退一個位元組

fseek(file,-1L,SEEK_CUR);

printf("%s:為屬性值 ",strToken);

}

elseif(ch=='=')

printf("%c:為賦值符 ",ch);

elseif(ch=='(')

printf("%c:為左括弧 ",ch);

elseif(ch==')')

printf("%c:為右括弧 ",ch);

elseif(ch=='{')

printf("%c:為左大括弧 ",ch);

elseif(ch=='}')

printf("%c:為右大括弧 ",ch);

elseif(ch==';')

printf("%c:為分號 ",ch);

else

printf("詞法錯誤! ");

}

}

//測試詞法分析器

intmain()

{

charfile_name[]="test.cpp";

FILE*file=fopen(file_name,"r");

if(file==NULL)

{

printf("打開(%s)源文件錯誤! ",file_name);

return1;

}

analyse(file);

fclose(file);

return0;

}

㈦ 用C語言編寫一個詞法分析器

建議用VC6.0自己就有語法分析及提示

㈧ 用C語言編寫簡單的詞法分析器

學編譯原理給老師交作業呢?我以前也做過,自己好好做吧,求作業是不對的

㈨ 怎麼用c語言編一個詞法分析器

簡而言之就是先畫一個狀態圖,然後根據圖來編碼就行
一個簡單的xml的詞法分析器供參考
#include
<stdio.h>
#include
<stdlib.h>
#include
<string.h>
typedef
struct
{
char
*p;
int
len;
}
xml_Text;
typedef
enum
{
xml_tt_U,
/*
Unknow
*/
xml_tt_H,
/*
Head
<?xxx?>*/
xml_tt_E,
/*
End
</xxx>
*/
xml_tt_B,
/*
Begin
<xxx>
*/
xml_tt_BE,
/*
Begin
End
<xxx/>
*/
xml_tt_T
/*
Text
xxx
*/
}
xml_TokenType;
typedef
struct
{
xml_Text
text;
xml_TokenType
type;
}
xml_Token;
int
xml_initText(xml_Text
*pText,
char
*s)
{
pText->p
=
s;
pText->len
=
strlen(s);
return
0;
}
int
xml_initToken(xml_Token
*pToken,
xml_Text
*pText)
{
pToken->text.p
=
pText->p;
pToken->text.len
=
0;
pToken->type
=
xml_tt_U;
return
0;
}
int
xml_print(xml_Text
*pText)
{
int
i;
for
(i
=
0;
i
<
pText->len;
i++)
{
putchar(pText->p[i]);
}
return
0;
}
int
xml_println(xml_Text
*pText)
{
xml_print(pText);
putchar('\n');
return
0;
}
int
xml_getToken(xml_Text
*pText,
xml_Token
*pToken)
{
char
*start
=
pToken->text.p
+
pToken->text.len;
char
*p
=
start;
char
*end
=
pText->p
+
pText->len;
int
state
=
0;
pToken->text.p
=
p;
pToken->type
=
xml_tt_U;
for
(;
p
<
end;
p++)
{
switch(state)
{
case
0:
switch(*p)
{
case
'<':
state
=
1;
break;
default:
state
=
7;
break;
}
break;
case
1:
switch(*p)
{
case
'?':
state
=
2;
break;
case
'/':
state
=
4;
break;
default:
state
=
5;
break;
}
break;
case
2:
switch(*p)
{
case
'?':
state
=
3;
break;
default:
state
=
2;
break;
}
break;
case
3:
switch(*p)
{
case
'>':
pToken->text.len
=
p
-
start
+
1;
pToken->type
=
xml_tt_H;
return
1;
default:
state
=
-1;
break;
}
break;
case
4:
switch(*p)
{
case
'>':
pToken->text.len
=
p
-
start
+
1;
pToken->type
=
xml_tt_E;
return
1;
default:
state
=
4;
break;
}
break;
case
5:
switch(*p)
{
case
'>':
pToken->text.len
=
p
-
start
+
1;
pToken->type
=
xml_tt_B;
return
1;
case
'/':
state
=
6;
break;
default:
state
=
5;
break;
}
break;
case
6:
switch(*p)
{
case
'>':
pToken->text.len
=
p
-
start
+
1;
pToken->type
=
xml_tt_BE;
return
1;
default:
state
=
-1;
break;
}
break;
case
7:
switch(*p)
{
case
'<':
p--;
pToken->text.len
=
p
-
start
+
1;
pToken->type
=
xml_tt_T;
return
1;
default:
state
=
7;
break;
}
break;
default:
pToken->text.len
=
p
-
start
+
1;
pToken->type
=
xml_tt_T;
return
1;
}
}
return
0;
}
int
main()
{
int
ret
=
0;
xml_Text
xml;
xml_initText(&xml,
"<?xml?><root>
ss
<haha>hoho</haha></root>");
xml_Token
token;
xml_initToken(&token,
&xml);
ret
=
xml_getToken(&xml,
&token);
printf("ret=%d;text=",ret);
xml_print(&token.text);
printf(";type=%d;\n\n",
token.type);
ret
=
xml_getToken(&xml,
&token);
printf("ret=%d;text=",ret);
xml_print(&token.text);
printf(";type=%d;\n\n",
token.type);
ret
=
xml_getToken(&xml,
&token);
printf("ret=%d;text=",ret);
xml_print(&token.text);
printf(";type=%d;\n\n",
token.type);
ret
=
xml_getToken(&xml,
&token);
printf("ret=%d;text=",ret);
xml_print(&token.text);
printf(";type=%d;\n\n",
token.type);
ret
=
xml_getToken(&xml,
&token);
printf("ret=%d;text=",ret);
xml_print(&token.text);
printf(";type=%d;\n\n",
token.type);
ret
=
xml_getToken(&xml,
&token);
printf("ret=%d;text=",ret);
xml_print(&token.text);
printf(";type=%d;\n\n",
token.type);
ret
=
xml_getToken(&xml,
&token);
printf("ret=%d;text=",ret);
xml_print(&token.text);
printf(";type=%d;\n\n",
token.type);
return
0;
}