導航:首頁 > 源碼編譯 > c語言詞法分析生成器源碼

c語言詞法分析生成器源碼

發布時間:2022-09-27 02:07:00

① 怎麼用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;
}

② 求一個C語言詞法分析器源代碼

我有,這是這學期剛做的,
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

bool isLetter(char ch){
if ((ch>='A' && ch<='Z') || (ch>='a' && ch<='z')) return true;
else return false;
}

bool isDigit(char ch){
if (ch>='0' && ch<='9') return true;
else return false;
}

bool isP(char ch){
if(ch=='+'||ch=='*'||ch=='-'||ch=='/') return true;
//ch==':'||ch==','||ch=='='||ch==';'||ch=='('||ch==')'
else return false;
}
bool isJ(char ch){
if(ch==','||ch==';'||ch=='.'||ch=='('||ch==')'||ch=='['||ch==']'||ch=='='||ch==':'||ch=='<'||ch=='>'||ch=='{'||ch=='}'||ch=='#') return true;
//
else return false;
}
bool isBlank(char ch){
if(ch==' '||ch=='\t') return true;
else return false;
}

int main(){
string src,ste,s;
char ch0,ch,ch1[2];
char ktt[48][20]={"and","begin","const","div","do","else","end","function","if","integer",
"not","or","procere","program","read","real","then","type","var","while","write","標識符","無符號數",
",",";",":",".","(",")","[","]","..","++","--","+","-","*","/","=","<",">","<>","<="
,">=",":=","{","}","#"};
int pos=0;
FILE *fp;
fp=fopen("d:\\in.txt","r");
ch0=fgetc(fp);
while(ch0!=EOF)
{
//if(ch0!='\t'){src+=ch0;}
src+=ch0;
ch0=fgetc(fp);
}
src+='#';
cout<<src<<endl;
ch=src[pos++];
ste=" ";
for(int j=0;j<47;j++){cout<<j<<ktt[j]<<endl;}
cout<<"詞法分析:\n";
while(ch!='#')
{
char str[20];
if(ch!='\n')
{
if(isDigit(ch))
{ //判斷常數
int i=0;
while(isDigit(ch)||ch=='.')
{
str[i++]=ch;
//i++;
ch=src[pos++];
}
str[i]='\0';
ste=ste+"|"+"22";
cout<<str;
continue;
}
else if(isLetter(ch))
{ //判斷字元
int i=0,j;
while(isLetter(ch)||isDigit(ch))
{
str[i++]=ch;
//i++;
ch=src[pos++];
}
str[i]='\0';
for(j=0;j<21;j++){ //判斷是否關鍵字
int t=strcmp(str,ktt[j]);
if(t==0) {
stringstream ss;
ste+="|";
ss<<ste;ss<<j;
ss>>ste;
break;
}
}
if(j==21){ste=ste+"|"+"21";}
// cout<<" ";
cout<<str;
continue;
}
else if(isP(ch)){ ///判斷是否運算符
int i=0,j;
str[i++]=ch;
str[i]='\0';
for(j=34;j<38;j++){
int t=strcmp(str,ktt[j]);
if(t==0) {
stringstream ss;
ste+="|";
ss<<ste;ss<<j;
ss>>ste;
break;
}
}
cout<<str;
ch=src[pos++];
continue;
}
else if(isJ(ch)) //判斷是否界符
{
int i=0,j;
while(isJ(ch))
{
str[i++]=ch;
ch=src[pos++];
}
str[i]='\0';
for(j=23;j<47;j++){
int t=strcmp(str,ktt[j]);
if(t==0) {
stringstream ss;
ste+="|";
ss<<ste;ss<<j;
ss>>ste;
break;
}
}
cout<<str;
continue;
}
else if(isBlank(ch))
{
cout<<ch;
ch=src[pos++];
continue;
}
}
else{
cout<<ste<<endl;
ste=" ";
}
ch=src[pos++];
}
return 0;
}

還有運行效果圖,和實驗報告 ,你要的話留下郵箱

③ 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"stdio.h"/*定義I/O庫所用的某些宏和變數*/

#include"string.h"/*定義字元串庫函數*/

#include"conio.h"/*提供有關屏幕窗口操作函數*/

#include"ctype.h"/*分類函數*/

charprog[80]={''},

token[8];/*存放構成單詞符號的字元串*/

charch;

intsyn,/*存放單詞字元的種別碼*/

n,

sum,/*存放整數型單詞*/

m,p;/*p是緩沖區prog的指針,m是token的指針*/

char*rwtab[6]={"begin","if","then","while","do","end"};

voidscaner(){

m=0;

sum=0;

for(n=0;n<8;n++)

token[n]='';

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++]='';

ch=prog[p--];

syn=10;

for(n=0;n<6;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=11;}

else

switch(ch){

case'<':m=0;token[m++]=ch;ch=prog[p++];

if(ch=='>'){

syn=21;

token[m++]=ch;}

elseif(ch=='='){

syn=22;

token[m++]=ch;}

else{

syn=20;

ch=prog[p--];}

break;

case'>':m=0;token[m++]=ch;ch=prog[p++];

if(ch=='='){

syn=24;

token[m++]=ch;}

else{

syn=23;

ch=prog[p--];}

break;

case':':m=0;token[m++]=ch;ch=prog[p++];

if(ch=='='){

syn=18;

token[m++]=ch;}

else{

syn=17;

ch=prog[p--];}

break;

case'+':syn=13;token[0]=ch;break;

case'-':syn=14;token[0]=ch;break;

case'*':syn=15;token[0]=ch;break;

case'/':syn=16;token[0]=ch;break;

case'=':syn=25;token[0]=ch;break;

case';':syn=26;token[0]=ch;break;

case'(':syn=27;token[0]=ch;break;

case')':syn=28;token[0]=ch;break;

case'#':syn=0;token[0]=ch;break;

default:syn=-1;}}

main()

{

printf(" Thesignificanceofthefigures: "

"1.figures1to6saidKeyword "

"2. "

"3.figures13to28saidOperators ");

p=0;

printf(" pleaseinputstring: ");

do{

ch=getchar();

prog[p++]=ch;

}while(ch!='#');

p=0;

do{

scaner();

switch(syn){

case11:printf("(%d,%d) ",syn,sum);break;

case-1:printf(" ERROR; ");break;

default:printf("(%d,%s) ",syn,token);

}

}while(syn!=0);

getch();

}

程序測試結果

對源程序beginx:=9:ifx>9thenx:=2*x+1/3;end#的源文件,經過詞法分析後輸出如下圖5-1所示:

具體的你在修改修改吧

⑤ 求一個C語言詞法分析器源代碼。要求:輸入一個.c的源程序,輸出該程序中所有變數。

首先做一個字元串數組
char *keyword[] 裡面放入所有數據類型關鍵字,int,double什麼的。
然後一行一行處理,找裡面的關鍵字,找到以後順序往後找,將空格,逗號,等號作為間隔符。將分號作為結束標志。
等號後面到下一個逗號或者分號之間的都忽略掉,如果有括弧(大中小),到下一個括弧之間的都忽略掉。
如果是long,unsigned,繼續分析後面是不是int。
基本就ok了。你要我幫你寫源碼的話,沒那時間。

⑥ 跪求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" /*定義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;
}

⑧ 求編譯原理的詞法分析器源碼

/* 我上編譯原理課時的第一次作業就是這個,flex源碼. */
%{
#include<math.h>
int num_lines=0;
%}
DIGIT [0-9]
ID [a-zA-Z_][a-zA-Z0-9]*
%%
"#include" {
printf("<包含頭文件,請手動合並文件\\>\n");
fprintf(yyout,"<包含頭文件,請手動合並文件\\>\n");
}
{DIGIT}+ {
printf("(3整數, \"%s\")\n", yytext);
fprintf(yyout,"(3整數, \"%s\")\n", yytext);
}
{DIGIT}+"."{DIGIT}* {
printf("(3浮點數, \" %s\")\n",yytext);
fprintf(yyout,"(3浮點數, \" %s\")\n",yytext);
}
auto |
break |
case |
char |
const |
continue |
default |
do |
double |
else |
enum |
extern |
float |
for |
goto |
if |
int |
long |
register |
return |
short |
signed |
sizeof |
static |
struct |
switch |
typedef |
union |
unsigned |
void |
volatile |
while {
fprintf(yyout,"(1, \"%s\")\n",yytext);
fprintf(yyout,"(1, \"%s\")\n",yytext);
}
{ID} {
printf("(2, \"%s\")\n",yytext);
fprintf(yyout,"(2, \"%s\")\n",yytext);
}
"+" |
"++" |
"+=" |
"-" |
"--" |
"-=" |
"->" |
"*" |
"**" |
"*=" |
"/" |
"/=" |
"=" |
"==" |
">" |
">>" |
">=" |
">>=" |
"<" |
"<<" |
"<=" |
"<<=" |
"!" |
"!=" |
"%" |
"%=" |
"&" |
"&&" |
"&=" |
"|" |
"||" |
"|=" |
"^" |
"^=" {
printf("(4, \"%s\")\n",yytext);
fprintf(yyout,"(4, \"%s\")\n",yytext);
}
"{" |
"}" |
"(" |
")" |
";" |
"," |
"'" |
"\"" |
"." |
"?" |
"[" |
"]" |
"\\" |
":" {
printf("(5, \"%s\")\n",yytext);
fprintf(yyout,"(5, \"%s\")\n",yytext);
}
\n {
++num_lines;
}
"/*"[^(*/)\n]*"*/"
(" ")+
[\t]+
. {
printf("(不能識別字元, \"%s\")\n",yytext);
fprintf(yyout,"(不能識別字元, \"%s\")\n",yytext);
}
%%
main(argc,argv)
int argc;
char **argv;
{
++argv,--argc;
if(argc>0)
yyin=fopen(argv[0],"r");
else
yyin=stdin;
yyout=fopen("output.txt","w");
yylex();
fclose(yyout);
}
int yywrap()
{
return 1;
}

/* 附:我們第一次作業的要求。
實驗一:用高級語言編寫詞法分析器(用lex生成)一、實驗目的:編制一個識別C語言子集的詞法分析器。從輸入的源程序中,識別出各個具有獨立意義的記號,即基本保留字、標識符、常數、運算符、分隔符五大類。並依次輸出各個記號的內部編碼及記號符號自身值。(遇到錯誤時可顯示「Error」,然後跳過錯誤部分繼續顯示)二、實驗過程和指導:(一)准備:1.閱讀課本有關章節,明確語言的詞法,寫出基本保留字、標識符、常數、運算符、分隔符和程序例。2.初步編制好程序。3.准備好多組測試數據。(二)程序要求:程序輸入/輸出示例:如源程序為C語言。輸入如下一段:main(){ int a,b; a = 10; b = a + 20;}要求輸出如下:(2,」main」)(5,」(「)(5,」)「)(5,」{「)(1,」int」)(2,」a」)(5,」,」)(2,」b」)(5,」;」)(2,」a」)(4,」=」)(3,」10」)(5,」;」)(2,」b」)(4,」=」)(2,」a」)(4,」+」)(3,」20」)(5,」;」)(5,」)「}
要求(滿足以下要求可獲得70%該題的得分):識別保留字:if、int、for、while、do、return、break、continue其他的都識別為標識符;常數為無符號整形數;運算符包括:+、-、*、/、=、>、<、>=、<=、!=分隔符包括:,、;、{、}、(、)以上為參考,具體可自行增刪。 三、實驗檢查:1.程序:輸入:測試數據(以文件形式);輸出:二元組(以文件形式)。2.實驗報告:(1)功能描述:該程序具有什麼功能?(2)狀態轉換圖。(2)程序結構描述:函數調用格式、參數含義、返回值描述、函數功能;函數之間的調用關系圖、程序總體執行流程圖。(4)源程序代碼。(5)實驗過程記錄:出錯次數、出錯嚴重程度、解決辦法摘要。(6)實驗總結:你在編程過程中花時多少?多少時間在紙上設計?多少時間上機輸入和調試?多少時間在思考問題?遇到了哪些難題?你是怎麼克服的?你對你的程序的評價?你的收獲有哪些?

另可附加:關鍵字 有符號數 符號表填寫 行號記錄,等
*/

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

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

⑩ 簡易C語言詞法分析器的設計與實現。求源代碼

這個是編譯原理的課程設計吧, 做詞法分析這個題目算是最簡單的了

閱讀全文

與c語言詞法分析生成器源碼相關的資料

熱點內容
外出完整140分鍾版本 瀏覽:728
香港電影走光 瀏覽:326
初級初中生編程 瀏覽:697
在阿姨鬼混還是鬼片 瀏覽:127
老年人愛愛的電影 瀏覽:900
與365dni類似的電影推薦 瀏覽:986
伺服器上的soc什麼意思 瀏覽:884
朱藝彬年輕的姐夫 瀏覽:396
怎麼刪除梅花視頻 瀏覽:360
尋緣app在哪裡下載 瀏覽:300
男同激情電影 瀏覽:391
macpdf轉換成word 瀏覽:582
框架柱尺寸加密 瀏覽:730
韓國電影一男的做鴨 瀏覽:486
女主和爸爸哥哥在一起的小說 瀏覽:729
伺服器什麼時候買最便宜 瀏覽:785
最刺激的床戲電影 瀏覽:750
日本電影女主瞎了一隻眼男主渾身 瀏覽:157
電影演員勞拉是哪個電影里的 瀏覽:537
android緩存sqlite 瀏覽:271