導航:首頁 > 源碼編譯 > 詞法語法分析源碼

詞法語法分析源碼

發布時間: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編譯器中詞法分析,語法分析,語義分析

請採納

閱讀全文

與詞法語法分析源碼相關的資料

熱點內容
武漢理工大學伺服器ip地址 瀏覽:139
亞馬遜雲伺服器登錄 瀏覽:515
安卓手機如何進行文件處理 瀏覽:62
mysql執行系統命令 瀏覽:920
php支持curlhttps 瀏覽:134
新預演算法責任 瀏覽:435
伺服器如何處理5萬人同時在線 瀏覽:242
哈夫曼編碼數據壓縮 瀏覽:415
鎖定伺服器是什麼意思 瀏覽:376
場景檢測演算法 瀏覽:608
解壓手機軟體觸屏 瀏覽:339
方舟pv怎麼轉伺服器 瀏覽:100
數據挖掘中誤差值演算法函數 瀏覽:119
php開發套件 瀏覽:191
伺服器的spi板是什麼 瀏覽:897
解壓縮全能王中文密碼是什麼 瀏覽:80
javaftp伺服器上傳文件 瀏覽:104
演算法設計中文版pdf 瀏覽:82
視頻壓縮形式怎麼改 瀏覽:369
perl程序員 瀏覽:791