导航:首页 > 源码编译 > 词法语法分析源码

词法语法分析源码

发布时间:2022-05-01 08:45:36

① 求一个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]!='')
cout<<a[count++];
}

voidfenxi()
{
char*sentence;
sentence=newchar[50];//储存输入的句子
inti=0;
charinput;
while(cin>>input&&input!='#'&&i<50)
{
sentence[i]=input;
i++;
}

char**id;
id=newchar*[20];
for(intm=0;m<20;m++)
{
id[m]=newchar[10];
}
for(intx=0;x<20;x++)
{
for(inty=0;y<10;y++)
id[x][y]='';
}

char**num;
num=newchar*[20];
for(m=0;m<20;m++)
{
num[m]=newchar[10];
}
for(x=0;x<20;x++)
{
for(inty=0;y<10;y++)
num[x][y]='';
}

int*token;
token=newint[30];
intt=0;//标记token中的词法记号个数


intdanci=-1;//记录字母串的单词个数
intshuzichuan=-1;
intdancilong=0;//记录单词中字母个数

intk=0;//记录sentence中的遍历数位
for(k=0;k<i;)
{
if(zimu(sentence[k]))
{
danci++;
id[danci][dancilong]=sentence[k];
k++;
while(!biaodian(sentence[k])&&k<i)
{
dancilong++;
id[danci][dancilong]=sentence[k];
k++;
}
dancilong=0;
if(strcmp("for",id[danci])==0)
{
token[t]=1;
}
if(strcmp("if",id[danci])==0)
{
token[t]=2;
}
if(strcmp("then",id[danci])==0)
{
token[t]=3;
}
if(strcmp("else",id[danci])==0)
{
token[t]=4;
}
if(strcmp("while",id[danci])==0)
{
token[t]=5;
}
if(strcmp("do",id[danci])==0)
{
token[t]=6;
}
else
token[t]=10;
t++;
}

if(biaodian(sentence[k]))
{
danci++;
id[danci][dancilong]=sentence[k];
k++;
while(biaodian(sentence[k])&&k<i)
{
dancilong++;
id[danci][dancilong]=sentence[k];
k++;
}
dancilong=0;
if(strcmp("+",id[danci])==0)
{
token[t]=13;
}
if(strcmp("-",id[danci])==0)
{
token[t]=14;
}
if(strcmp("*",id[danci])==0)
{
token[t]=15;
}
if(strcmp("/",id[danci])==0)
{
token[t]=16;
}
if(strcmp(":",id[danci])==0)
{
token[t]=17;
}
if(strcmp(":=",id[danci])==0)
{
token[t]=18;
}
if(strcmp("<",id[danci])==0)
{
token[t]=20;
}
if(strcmp("<>",id[danci])==0)
{
token[t]=21;
}
if(strcmp("<=",id[danci])==0)
{
token[t]=22;
}
if(strcmp(">",id[danci])==0)
{
token[t]=23;
}
if(strcmp(">=",id[danci])==0)
{
token[t]=24;
}
if(strcmp("=",id[danci])==0)
{
token[t]=25;
}
if(strcmp(";",id[danci])==0)
{
token[t]=26;
}
if(strcmp("(",id[danci])==0)
{
token[t]=27;
}
if(strcmp(")",id[danci])==0)
{
token[t]=28;
}
if(strcmp("#",id[danci])==0)
{
token[t]=0;
}
t++;
}

if(shuzi(sentence[k]))
{
shuzichuan++;
num[shuzichuan][dancilong]=sentence[k];
k++;
while(shuzi(sentence[k])&&k<i)
{
dancilong++;
num[shuzichuan][dancilong]=sentence[k];
k++;
}
dancilong=0;
token[t++]=11;
}
}

intn=0;
x=0;//id计数
inty=0;//num计数
while(n<t)
{
if(token[n]==11)
{
cout<<"("<<token[n]<<",";
showstring(num[y++]);
cout<<")";
//y++;
}
else
{
cout<<"("<<token[n]<<",";
showstring(id[x++]);
cout<<")";
}
n++;
}


}

voidmain()
{
fenxi();
}

⑥ 怎么用java写一个词法分析器

首先看下我们要分析的代码段如下:

输出结果(c).PNG

括号里是一个二元式:(单词类别编码,单词位置编号)

代码如下:

?

1234567891011121314package Yue.LexicalAnalyzer;import java.io.*;/** 主程序*/public class Main {public static void main(String[] args) throws IOException {Lexer lexer = new Lexer();lexer.printToken();lexer.printSymbolsTable();}}

?

package Yue.LexicalAnalyzer;import java.io.*;import java.util.*;/** 词法分析并输出*/public class Lexer {/*记录行号*/public static int line = 1;/*存放最新读入的字符*/char character = ' ';/*保留字*/Hashtable<String, KeyWord> keywords = new Hashtable<String, KeyWord>();/*token序列*/private ArrayList<Token> tokens = new ArrayList<Token>();/*符号表*/private ArrayList<Symbol> symtable = new ArrayList<Symbol>();/*读取文件变量*/BufferedReader reader = null;/*保存当前是否读取到了文件的结尾*/private Boolean isEnd = false;/* 是否读取到文件的结尾 */public Boolean getReaderState() {return this.isEnd;}/*打印tokens序列*/public void printToken() throws IOException {FileWriter writer = new FileWriter("E:\lex.txt");System.out.println("词法分析结果如下:");System.out.print("杜悦-2015220201031 ");writer.write("杜悦-2015220201031 ");while (getReaderState() == false) {Token tok = scan();String str = "line " + tok.line + " (" + tok.tag + "," + tok.pos + ") "+ tok.name + ": " + tok.toString() + " ";writer.write(str);System.out.print(str);}writer.flush();}/*打印符号表*/public void printSymbolsTable() throws IOException {FileWriter writer = new FileWriter("E:\symtab1.txt");System.out.print(" 符号表 ");System.out.print("编号 行号 名称 ");writer.write("符号表 ");writer.write("编号 " + " 行号 " + " 名称 ");Iterator<Symbol> e = symtable.iterator();while (e.hasNext()) {Symbol symbol = e.next();String desc = symbol.pos + " " + symbol.line + " " + symbol.toString();System.out.print(desc + " ");writer.write(desc + " ");}writer.flush();}/*打印错误*/public void printError(Token tok) throws IOException{FileWriter writer = new FileWriter("E:\error.txt");System.out.print(" 错误词法如下: ");writer.write("错误词法如下: ");String str = "line " + tok.line + " (" + tok.tag + "," + tok.pos + ") "+ tok.name + ": " + tok.toString() + " ";writer.write(str);}/*添加保留字*/void reserve(KeyWord w) {keywords.put(w.lexme, w);}public Lexer() {/*初始化读取文件变量*/try {reader = new BufferedReader(new FileReader("E:\输入.txt"));} catch (IOException e) {System.out.print(e);}/*添加保留字*/this.reserve(KeyWord.begin);this.reserve(KeyWord.end);this.reserve(KeyWord.integer);this.reserve(KeyWord.function);this.reserve(KeyWord.read);this.reserve(KeyWord.write);this.reserve(KeyWord.aIf);this.reserve(KeyWord.aThen);this.reserve(KeyWord.aElse);}/*按字符读*/public void readch() throws IOException {character = (char) reader.read();if ((int) character == 0xffff) {this.isEnd = true;}}/*判断是否匹配*/public Boolean readch(char ch) throws IOException {readch();if (this.character != ch) {return false;}this.character = ' ';return true;}/*数字的识别*/public Boolean isDigit() throws IOException {if (Character.isDigit(character)) {int value = 0;while (Character.isDigit(character)) {value = 10 * value + Character.digit(character, 10);readch();}Num n = new Num(value);n.line = line;tokens.add(n);return true;} elsereturn false;}/*保留字、标识符的识别*/public Boolean isLetter() throws IOException {if (Character.isLetter(character)) {StringBuffer sb = new StringBuffer();/*首先得到整个的一个分割*/while (Character.isLetterOrDigit(character)) {sb.append(character);readch();}/*判断是保留字还是标识符*/String s = sb.toString();KeyWord w = keywords.get(s);/*如果是保留字的话,w不应该是空的*/if (w != null) {w.line = line;tokens.add(w);} else {/*否则就是标识符,此处多出记录标识符编号的语句*/Symbol sy = new Symbol(s);Symbol mark = sy; //用于标记已存在标识符Boolean isRepeat = false;sy.line = line;for (Symbol i : symtable) {if (sy.toString().equals(i.toString())) {mark = i;isRepeat = true;}}if (!isRepeat) {sy.pos = symtable.size() + 1;symtable.add(sy);} else if (isRepeat) {sy.pos = mark.pos;}tokens.add(sy);}return true;} elsereturn false;}/*符号的识别*/public Boolean isSign() throws IOException {switch (character) {case '#':readch();AllEnd.allEnd.line = line;tokens.add(AllEnd.allEnd);return true;case ' ':if (readch(' ')) {readch();LineEnd.lineEnd.line = line;tokens.add(LineEnd.lineEnd);line++;return true;}case '(':readch();Delimiter.lpar.line = line;tokens.add(Delimiter.lpar);return true;case ')':readch();Delimiter.rpar.line = line;tokens.add(Delimiter.rpar);return true;case ';':readch();Delimiter.sem.line = line;tokens.add(Delimiter.sem);return true;case '+':readch();CalcWord.add.line = line;tokens.add(CalcWord.add);return true;case '-':readch();CalcWord.sub.line = line;tokens.add(CalcWord.sub);return true;case '*':readch();CalcWord.mul.line = line;tokens.add(CalcWord.mul);return true;case '/':readch();CalcWord.div.line = line;tokens.add(CalcWord.div);return true;case ':':if (readch('=')) {readch();CalcWord.assign.line = line;tokens.add(CalcWord.assign);return true;}break;case '>':if (readch('=')) {readch();CalcWord.ge.line = line;tokens.add(CalcWord.ge);return true;}break;case '<':if (readch('=')) {readch();CalcWord.le.line = line;tokens.add(CalcWord.le);return true;}break;case '!':if (readch('=')) {readch();CalcWord.ne.line = line;tokens.add(CalcWord.ne);return true;}break;}return false;}/*下面开始分割关键字,标识符等信息*/public Token scan() throws IOException {Token tok;while (character == ' ')readch();if (isDigit() || isSign() || isLetter()) {tok = tokens.get(tokens.size() - 1);} else {tok = new Token(character);printError(tok);}return tok;}}

⑦ 急求C/C++做的编译原理的词法语法分析程序!

这是我以前学习编译原理时定的词法分析程序,可以运行的,供你参考
#include <stdio.h>
#include <string.h>
char prog[80],token[8],ch;
int syn,p,m,n,sum;
char *rwtab[4]={"if","else","while","do"};
scaner();
main()
{p=0;
printf("\n please input a string(end with '#'):");
do{
scanf("%c",&ch);
prog[p++]=ch;
}while(ch!='#');
p=0;
do{
scaner();
switch(syn)
{case 11:printf("( %-10d%5d )\n",sum,syn);
break;
case -1:printf("you have input a wrong string\n");
getch();
exit(0);
default: printf("( %-10s%5d )\n",token,syn);
break;
}
}while(syn!=0);
getch();
}

scaner()
{ sum=0;
for(m=0;m<8;m++)token[m++]=NULL;
ch=prog[p++];
m=0;
while((ch==' ')||(ch=='\n'))ch=prog[p++];
if(((ch<='z')&&(ch>='a'))||((ch<='Z')&&(ch>='A')))
{ while(((ch<='z')&&(ch>='a'))||((ch<='Z')&&(ch>='A'))||((ch>='0')&&(ch<='9')))
{token[m++]=ch;
ch=prog[p++];
}
p--;
syn=10;
for(n=0;n<6;n++)
if(strcmp(token,rwtab[n])==0)
{ syn=n+1;
break;
}
}
else if((ch>='0')&&(ch<='9'))
{ while((ch>='0')&&(ch<='9'))
{ sum=sum*10+ch-'0';
ch=prog[p++];
}
p--;
syn=11;
}
else switch(ch)
{ case '<':token[m++]=ch;
ch=prog[p++];
if(ch=='=')
{ syn=22;
token[m++]=ch;
}
else
{ syn=20;
p--;
}
break;
case '>':token[m++]=ch;
ch=prog[p++];
if(ch=='=')
{ syn=24;
token[m++]=ch;
}
else
{ syn=23;
p--;
}
break;
case '+': token[m++]=ch;
ch=prog[p++];
if(ch=='+')
{ syn=17;
token[m++]=ch;
}
else
{ syn=13;
p--;
}
break;

case '-':token[m++]=ch;
ch=prog[p++];
if(ch=='-')
{ syn=29;
token[m++]=ch;
}
else
{ syn=14;
p--;
}
break;

case '!':ch=prog[p++];
if(ch=='=')
{ syn=21;
token[m++]=ch;
}
else
{ syn=31;
p--;
}
break;

case '=':token[m++]=ch;
ch=prog[p++];
if(ch=='=')
{ syn=25;
token[m++]=ch;
}
else
{ syn=18;
p--;
}
break;
case '*': syn=15;
token[m++]=ch;
break;
case '/': syn=16;
token[m++]=ch;
break;
case '(': syn=27;
token[m++]=ch;
break;
case ')': syn=28;
token[m++]=ch;
break;
case '{': syn=5;
token[m++]=ch;
break;
case '}': syn=6;
token[m++]=ch;
break;
case ';': syn=26;
token[m++]=ch;
break;
case '\"': syn=30;
token[m++]=ch;
break;
case '#': syn=0;
token[m++]=ch;
break;
case ':':syn=17;
token[m++]=ch;
break;
default: syn=-1;
break;
}
token[m++]='\0';
}

⑧ 编译原理 词法分析 C 版 老师要求由编译原理课后PL0完整源代码改编

用LEX和YACC可以自动生成词法分析和语法分析。
你要分析什么语法,没有明确讲啊。

⑨ 求一个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;
}

还有运行效果图,和实验报告 ,你要的话留下邮箱

⑩ 关于java编译器中词法分析,语法分析,语义分析

请采纳

阅读全文

与词法语法分析源码相关的资料

热点内容
软件怎么加密华为 浏览:220
扫地机怎么安装app 浏览:317
考研结合特征值计算法 浏览:514
操作系统算法综合题 浏览:150
华为程序员待遇 浏览:545
程序员带娃的图片 浏览:77
迷你云服务器怎么下载 浏览:813
福州溯源码即食燕窝 浏览:232
当乐服务器怎么样 浏览:713
nc编程软件下载 浏览:382
如何限制手机app的使用 浏览:307
安卓华为手机怎么恢复桌面图标 浏览:956
我的世界电脑版服务器地址在哪找 浏览:533
违抗了命令 浏览:256
安卓如何实现拖拽放置 浏览:91
净资产收益率选股指标源码 浏览:599
血压力传感器计算公式单片机 浏览:466
全网接口vip影视解析源码 浏览:916
如何破解服务器远程密码错误 浏览:377
平安深圳app如何实名认证 浏览:500