导航:首页 > 源码编译 > 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语言词法分析生成器源码相关的资料

热点内容
模拟实现进程调度算法 浏览:386
现在的压缩包都是加密 浏览:329
施工员找工作去哪个app 浏览:630
安卓手机的游戏怎么打开 浏览:198
pdf扫描转文字 浏览:532
微机室里面的云服务器 浏览:108
excel能编程吗 浏览:931
android系统框架的介绍 浏览:947
无盘系统服务器如何配置 浏览:836
背负贷款如何缓解压力 浏览:82
linux获取日期时间 浏览:881
搬砖问题最合适的算法 浏览:446
小米安卓机密码忘记了如何解锁 浏览:910
产电plc编程手册 浏览:761
vscodephp 浏览:535
阿里云linux桌面 浏览:754
php二维数组搜索 浏览:116
ps快捷命令工具箱 浏览:253
c4d教程pdf 浏览:462
linux集群安装配置 浏览:154