① 求一个C语言词法分析器源代码。要求:输入一个.c的源程序,输出该程序中所有变量。
首先做一个字符串数组
char *keyword[] 里面放入所有数据类型关键字,int,double什么的。
然后一行一行处理,找里面的关键字,找到以后顺序往后找,将空格,逗号,等号作为间隔符。将分号作为结束标志。
等号后面到下一个逗号或者分号之间的都忽略掉,如果有括号(大中小),到下一个括号之间的都忽略掉。
如果是long,unsigned,继续分析后面是不是int。
基本就ok了。你要我帮你写源码的话,没那时间。
② 求编译原理的词法分析器源码
/* 我上编译原理课时的第一次作业就是这个,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语言词法分析器、语法分析器、语义分析器源码
bison 网上搜以下, 开源的
④ 谁有词法分析器的源代码谢谢
// 456.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<iostream>
#include<fstream>
#include<string.h>
using namespace std;
bool Isnoshow(char ch){ //判断是不是空格、回车、换行符
if(ch=='\n'||ch=='\t'||ch==' ')
return true;
return false;
}
bool Isletter(char ch){ //判断是不是字母
if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
return true;
return false;
}
bool Isdigital(char ch){ //判断是不是数字
if(ch>='0'&&ch<='9')
return true;
return false;
}
bool Isunline(char ch){ //判断是不是下划线
if(ch=='_')
return true;
return false;
}
bool Iscacus(char ch){ //判断是不是运算符
if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='%'||
ch=='<'||ch=='>'||ch=='&'||ch=='|'||ch=='!'||ch=='=')
return true;
return false;
}
bool Issplits(char ch){ //判断是不是分界符
if(ch=='{'||ch=='}'||ch=='['||ch==']'||ch=='('||
ch==')'||ch==';'||ch==','||ch=='.'||ch==':'||ch=='"')
return true;
return false;
}
int _tmain(int argc, _TCHAR* argv[])
{
char b[1000];
ifstream ifile;
ifile.open("d:\\1.txt");
int i=0;
while(ifile.get(b[i])){
{
int a=i+1;
if(ifile.eof()==1) break;
if(Isletter(b[i])||Isunline(b[i]))
cout<<b[i];
else if(Isnoshow(b[i]))
{
if(Isletter(b[i-1])||Isunline(b[i-1]))
cout<<"是标识符"<<endl;
else if( Isdigital(b[i-1]))
cout<<"是数字"<<endl;
else if(Issplits(b[i-1]))
cout<<"是分界符"<<endl;
else if(Iscacus(b[i-1]))
cout<<"是运算符"<<endl;
}
else if(Isdigital(b[i]))
{
if(Isletter(b[i-1])||Isunline(b[i-1]))
cout<<"是标识符"<<endl;
else if(Issplits(b[i-1]))
cout<<b[i-1]<<"是分界符"<<endl;
else if(Iscacus(b[i-1]))
cout<<"是运算符"<<endl;
cout<<b[i];
}
else if(Iscacus(b[i]))//运算符
{
if(Isletter(b[i-1])||Isunline(b[i-1]))
cout<<"是标识符"<<endl;
else if( Isdigital(b[i-1]))
cout<<"是数字"<<endl;
else if(Issplits(b[i-1]))
cout<<"是分界符"<<endl;
cout<<b[i];
}
else if(Issplits(b[i]))//分界符
{
if(Isletter(b[i-1])||Isunline(b[i-1]))
cout<<"是标识符"<<endl;
else if( Isdigital(b[i-1]))
cout<<"是数字"<<endl;
else if(Iscacus(b[i-1]))
cout<<"是运算符"<<endl;
cout<<b[i];
}
i++;
}
}
if(b[i]='/0')
{
if(Isletter(b[i-1])||Isunline(b[i-1]))
cout<<"是标识符"<<endl;
else if( Isdigital(b[i-1]))
cout<<"是数字"<<endl;
else if(Issplits(b[i-1]))
cout<<"是分界符"<<endl;
else if(Iscacus(b[i-1]))
cout<<"是运算符"<<endl;
}
ifile.close();
return 0;
}
⑤ VS2010环境在一个程序中实现语法分析和词法分析,求代码
//一个词法分析程序,输入源程序,输出二元组(词法记号,属性值/其在符号表中的位置)构成的序列
#include<iostream>
#include<cstring>
usingnamespacestd;
//char*keywords[6]={"for","if","then","else","while","do"};
intzimu(chara)//分析是否为字母
{
if((a>='a')&&(a<='z'))
return1;
else
return0;
}
intshuzi(chara)
{
if((a>='0')&&(a<='9'))
return1;
else
return0;
}
intbiaodian(chara)
{
if(!zimu(a)&&!shuzi(a))
return1;
else
return0;
}
voidshowstring(char*a)
{
intcount=0;
while(a[count]!='