當前位置:首頁 » 編程軟體 » 編譯原理詞法分析器java代碼

編譯原理詞法分析器java代碼

發布時間: 2022-08-18 00:56:48

A. 急求!!!編譯原理詞法分析器代碼

#include <stdio.h>
#include <string.h>
#include <conio.h>
#define norw 13 /* 關鍵字個數 */
#define nmax 14 /* number的最大位數 */
#define al 10 /* 符號的最大長度 */
#define cn 11
#define IDENT "ident"
#define NUM "number"
#define SYM "sym"
#define ISLETTER(c) ((c)>='A'&&(c)<='Z'||(c)>='a'&&(c)<='z')
#define ISNUMBER(c) ((c)>='0'&&(c)<='9')
void letter();void number();
char ch; /* 獲取字元的緩沖區,getch使用 */
char a[al+1]; /* 臨時符號,多出的一個位元組用於存放0 */
FILE *fin;
char fname[al];
char *word[] = {
"begin", "call" , "const" , "do" , "end" ,
"if" , "odd" , "procere" , "read" , "then" ,
"var" , "while" , "write"
};
int k=0,i=0;
char ssym[][10]={"plus","minus","times","slash","lparen", "rparen","eql","comma","neq","semicolon"};
char csym[]={'+','-','*','/','(',')','=',',','#',';'};

void main() {
do{
printf("Input pl/0 filename:");
gets(fname);
fin=fopen(fname,"r");
}while(!fin);
ch=fgetc(fin);
while(ch!='.'){
for(;' '==ch;ch=fgetc(fin));
if(ISLETTER(ch)){
k=0;
letter();
}else if(ISNUMBER(ch)){
k=0;
number();
}else{
switch(ch){
case ':':
if('='==(ch=fgetc(fin))){
printf("(\":=\",eql)\n");
ch=fgetc(fin);
}break;
case '>':
if('='==(ch=fgetc(fin))){
printf("(\">=\",gtr)\n");
ch=fgetc(fin);
break;
}else
printf("(\">\",geq)\n");break;
case '<':
if('='==(ch=fgetc(fin))){
printf("(\"<=\",leq)\n");
ch=fgetc(fin);break;
}
else
printf("(\"<\",lss)\n");break;
default:
for(i=0;i<norw && ch!=csym[i];i++);
if(i<norw)
printf("(\"%c\",%s)\n",ch,ssym[i]);
ch=fgetc(fin);
}
}
}printf("(\"%c\",period)\n",ch);
}

void letter(){
do{
if(al>k) {
a[k]=ch;
k+=1;
}
ch=fgetc(fin);
}while(ISLETTER(ch));
a[k]='\0';
for(i=0;i<norw && strcmp(a,word[i]);i++);
if(i<norw)
printf("(\"%s\",%s%s)\n",a,a,SYM);
else
printf("(\"%s\",%s)\n",a,IDENT);
}

void number(){
do{
if(nmax>k) {
a[k]=ch;
k+=1;
}
ch=fgetc(fin);
}while(ISNUMBER(ch));
a[k]='\0';
printf("(\"%s\",%s)\n",a,NUM);
}

B. 求詞法分析器java語言實現的代碼,求解

/*
*詞法分析
*
*/
importjava.io.File;
importjava.io.FileReader;

publicclassCompiler{
privatestaticStringstring;
privatestaticStringstr;
privatestaticcharch;
/*
*讀取文件
*/
publicstaticvoidgetChar()throwsException{
Filef=newFile("C:\","test.txt");
if(!f.exists()){
System.out.println("文件不存在,請輸入正確的文件路徑");
}
FileReaderfr=newFileReader(f);
intrs=0;
char[]data=newchar[256];
System.out.print("");

while((rs=fr.read(data))>0){
string=newString(data,0,rs).trim();
}
}
/*
*判斷讀入的字元是否為字母
*/
publicstaticbooleanisLetter(charc){
if((ch>='a'&&ch<='z')||(ch>+'A'&&ch<='Z')){
returntrue;
}
else
returnfalse;
}
/*
*判斷讀入的字元是否為數字
*/
publicstaticbooleanisDigit(charc){
if(ch>='0'&&ch<='9'){
returntrue;
}
else
returnfalse;
}
/*
*判斷是否為關鍵字
*/
publicstaticbooleanisKey(Stringstring){
if(string.equals("void")||string.equals("if")||string.equals("for")||string.equals("while")
||string.equals("do")||string.equals("return")||string.equals("break")
||string.equals("main"))
{
returntrue;
}
elsereturnfalse;
}
/*
*判斷輸入的字元並輸出單詞符號
*/
publicstaticvoidjudgement()throwsException{
Compiler.getChar();
intm=0;
string+='';
for(inti=0;i<string.length();i++){
switch(m)
{
case0:
ch=string.charAt(i);
if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='='
||ch=='>'||ch=='<')
{
m=4;
}
elseif(ch==','||ch==';'||ch=='{'||ch=='}'||ch=='('||ch==')')
{
m=5;
}
elseif(isDigit((ch=string.charAt(i))))
{
str="";
str+=ch;
m=3;
}
elseif(isLetter(ch=string.charAt(i)))
{
str="";
str+=ch;
m=2;
}else{}
break;

case4:
i--;
System.out.println(("(4"+"「"+ch+"」)"));
m=0;
break;

case5:
i--;
System.out.println(("(5"+"「"+ch+"」)"));
m=0;
break;

case2:
if(isLetter(ch=string.charAt(i)))
{
str+=ch;
}
else
{
if(isKey(str))
{
System.out.println("(1"+"「"+str+"」)");
}else{
System.out.println(("(2"+"「"+str+"」)"));
}
i--;
m=0;
}
break;

case3:
if(isDigit((ch=string.charAt(i))))
{
str+=ch;
}
else
{
System.out.println(("(3"+"「"+str+"」)"));
i--;
m=0;
}
break;
}
}
}

publicstaticvoidmain(String[]args)throwsException{
Compiler.judgement();
// System.out.print(b)

}
}

C. 速求編譯原理詞法分析器,要求說明語言有完整代碼能運行。急!!!

不好意思 發成語法的了
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <malloc.h>
#include <conio.h>
#define NULL 0
#define true1 1
#define false1 0
FILE *fp;
char ch;
char *keyword[17]={"break","char","continue","do","double",
"else","extern","float","for","if","int","long",
"short","static","switch","void","while"};
char *operatornum[3]={"+","-","*"};
char *comparison[6]={"<","<=","=",">",">=","<>"};
char *interpunction[5]={",",";",":=","(",")"};
char *biaoshifu[6]={"%","$","^","&","_","#"};//
char *zhushifu[3]={"//","/*","*/"};//
char *luoji[3]={"&&","||","!"};//
char search(char searchstr[],int wordtype) {
int i; switch (wordtype) {
case 1:for(i=0;i<=16;i++) {
if(strcmp(keyword[i],searchstr)==0)
return(true1);
} case 2:{
for(i=0;i<=2;i++)
{
if(strcmp(operatornum[i],searchstr)==0)
return(true1);
} break;
} case 3: {
for(i=0;i<=5;i++)
{
if(strcmp(comparison[i],searchstr)==0)
return(true1);
}
break;
}
case 4: for(i=0;i<=4;i++)
{
if(strcmp(interpunction[i],searchstr)==0)
return(true1); }
break; case 5: for(i=0;i<=5;i++)
{
if(strcmp(biaoshifu[i],searchstr)==0)
return(true1);
}break; case 6: for(i=0;i<=2;i++)
{
if(strcmp(zhushifu[i],searchstr)==0)
return(true1);
}break; case 7: for(i=0;i<=2;i++)
{ if(strcmp(luoji[i],searchstr)==0)
return(true1);
}break; }
return(false1);
}
char letterprocess (char ch)
{
int i=-1; char letter[20];
while (isalnum(ch)!=0) {
letter[++i]=ch;
ch=fgetc(fp);
};
letter[i+1]='\0';
if (search(letter,1))
{ if(strcmp(letter,"main"))//
printf("<1,關鍵字,%s>\n",letter);//
else printf("<2,自定義標識符,%s>\n",letter);//
} else { printf("<2,自定義標識符,%s>\n",letter);//
} return(ch); }
char numberprocess(char ch)//
{ int i=-1; char num[20];
while (isdigit(ch)!=0) {
num[++i]=ch;
ch=fgetc(fp); }
if(isalpha(ch)!=0) {
while(isspace(ch)==0)
{
num[++i]=ch;
ch=fgetc(fp);
} num[i+1]='\0';
printf("3,數字,%s\n",num);
goto u; }
num[i+1]='\0'; printf("<3,數字,%s>\n",num);//
u: return(ch); }
char otherprocess(char ch) {
int i=-1;
char other[20];
if (isspace(ch)!=0)
{
ch=fgetc(fp);
goto u; }
while ((isspace(ch)==0)&&(isalnum(ch)==0)) {
other[++i]=ch;
ch=fgetc(fp); }
other[i+1]='\0';
if (search(other,2))
printf("<4,運算符,%s>\n",other);//
else if (search(other,3))
printf("<4,運算符,%s>\n",other);//
else if (search(other,4)) printf("<5,分隔符號,%s>\n",other);//
else if (search(other,5)) printf("<%s,特殊標識符號>\n",other);
else if (search(other,6)) printf("<%s,注釋符號>\n",other);
else if (search(other,7)) printf("<%s,邏輯運算符號>\n",other);
else
printf("錯誤! 非法字元:%s\n",other);
u: return (ch); }
void main () {
char str,c; printf("**********************************詞法分析器***********************************\n");
if((fp=fopen("源程序.txt","r"))==NULL)
printf("源程序無法打開!\n");
else { str =fgetc(fp);

while (str!=EOF) {
if (isalpha(str)!=0)
str=letterprocess(str);
else {
if (isdigit(str)!=0)
str=numberprocess(str);
else
str=otherprocess(str);
}
};
printf("詞法分析結束,謝謝使用!\n");
printf("點任意鍵退出\n");
}
c=getch();
}

這個

D. Java代碼到底是如何編譯成機器指令的

編譯器把一種語言規范轉化為另一種語言規范的這個過程需要哪些步驟?回答這個問題需要參照《編譯原理》,總結過程如下:

        1)詞法分析:讀取源代碼,一個位元組一個位元組的讀進來,找出這些詞法中我們定義的語言關鍵詞如:if、else、while等,識別哪些if是合法的哪些是不合法的。這個步驟就是詞法分析過程。

        詞法分析的結果:就是從源代碼中找出了一些規范化的token流,就像人類語言中,給你一句話你要分辨出哪些是一個詞語,哪些是標點符號,哪些是動詞,哪些是名詞。

        2)語法分析:就是對詞法分析中得到的token流進行語法分析,這一步就是檢查這些關鍵片語合在一起是不是符合Java語言規范。如if的後面是不是緊跟著一個布爾型判斷表達式。

        語法分析的結果:就是形成一個符合Java語言規定的抽象語法樹,抽象語法樹是一個結構化的語法表達形式,它的作用是把語言的主要詞法用一個結構化的形式組織在一起。這棵語法樹可以被後面按照新的規則再重新組織。

        3)語義分析:語法分析完成之後也就不存在語法問題了,語義分析的主要工作就是把一些難懂的,復雜的語法轉化成更簡單的語法。就如難懂的文言文轉化為大家都懂的百話文,或者是注釋一下一些不懂的成語。

        語義分析結果:就是將復雜的語法轉化為簡單的語法,對應到Java就是將foreach轉化為for循環,還有一些注釋等。最後生成一棵抽象的語法樹,這棵語法樹也就更接近目標語言的語法規則。

        4)位元組碼生成:將會根據經過注釋的抽象語法樹生成位元組碼,也就是將一個數據結構轉化為另外一個數據結構。就像將所有的中文詞語翻譯成英文單詞後按照英文語法組裝文英文語句。代碼生成器的結果就是生成符合java虛擬機規范的位元組碼。

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

/* 我上編譯原理課時的第一次作業就是這個,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)實驗總結:你在編程過程中花時多少?多少時間在紙上設計?多少時間上機輸入和調試?多少時間在思考問題?遇到了哪些難題?你是怎麼克服的?你對你的程序的評價?你的收獲有哪些?

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

F. 請用JAVA編程輸入一個語句進行詞法分析

我最近正在學編譯原理,我有c語言實現的詞法分析程序,不知可不可以,識別的是TEST語言的單詞。

#include<stdio.h>

#include<ctype.h>

#include<string.h>


#define keywordSum 8


char * keyword[keywordSum] = {"do", "else", "for", "if", "int", "read", "while", "write"};

char singleword[50] = "+-*(){};,:";

char doubleword[10] = "><=!|&";


char Scanin[300], Scanout[300];

FILE * fin, * fout;


int binaryFind(int low, int high, char * c1, char ** c2) {


int mid;

if(low > high) return -1;

mid = (low+high)/2;

if(strcmp(c1, c2[mid]) == 0) return mid;

else if(strcmp(c1, c2[mid]) > 0) return binaryFind(mid+1, high, c1, c2);

else return binaryFind(low, mid-1, c1, c2);

}


int TESTscan() {


char ch, token[40];

int es = 0, j, n;


printf("請輸入源文件名(包括路徑):");

scanf("%s", Scanin);

printf("請輸入詞法分析輸出文件名(包括路徑):");

scanf("%s", Scanout);


if((fin=fopen(Scanin, "r")) == NULL) {

printf(" 打開詞法分析輸入文件出錯! ");

return 1;

}

if((fout=fopen(Scanout, "w")) == NULL) {

printf(" 創建詞法分析輸出文件出錯! ");

return 2;

}


// printf("%c", getc(fin));


ch = getc(fin);


while(ch != EOF) {


while(ch==' ' || ch==' ' || ch==' ') {

ch = getc(fin);

}


if(isalpha(ch)) { //標識符


token[0] = ch;

j = 1;

ch = getc(fin);

while(isalnum(ch)) { //判斷當前字元是否是字母或數字

token[j++] = ch;

ch = getc(fin);

}

token[j] = '';

// printf("%s", token);

n = binaryFind(0, keywordSum-1, token, keyword);

if(n < 0 ) {

fprintf(fout, "%s %s ", "ID", token);

} else {

fprintf(fout, "%s %s ", token, token);

}


} else if(isdigit(ch)) { //數字


token[0] = ch;

j = 1;

ch = getc(fin);

while(isdigit(ch)) {

token[j++] = ch;

ch = getc(fin);

}

token[j] = '';


fprintf(fout, "%s %s ", "NUM", token);


} else if(strchr(singleword, ch) > 0) { //singleword


token[0] = ch;

token[1] = '';

ch = getc(fin);


fprintf(fout, "%s %s ", token, token);


} else if(strchr(doubleword, ch) > 0) { //doubleword


token[0] = ch;

ch = getc(fin);


if(ch=='=' && (token[0]=='>'||token[0]=='<' || token[0] == '!')) {

token[1] = ch;

token[2] = '';

ch = getc(fin);

} else if((ch=='&')||(ch=='|')||(ch=='=') && ch==token[0]) {

token[1] = ch;

token[2] = '';

ch = getc(fin);

} else {

token[1] = '';

}


fprintf(fout, "%s %s ", token, token);


} else if(ch == '/') { //注釋


ch = getc(fin);


if(ch == '*') {

char ch1;

ch1 = getc(fin);

do {

ch = ch1;

ch1 = getc(fin);

} while((ch!='*'||ch1!='/') && ch1!=EOF);


ch = getc(fin);


} else {

token[0] = '/';

token[1] = '';

fprintf(fout, "%s %s ", token, token);

}


} else {


token[0] = ch;

token[1] = '';

ch = getc(fin);

es = 3;


fprintf(fout, "%s %s ", "ERROR", token);

}

}


fclose(fin);

fclose(fout);


return es;


}


void main() {


int es = 0;

es = TESTscan();

if(es > 0) {

printf("詞法分析有錯, 編譯停止! ");

} else {

printf("詞法分析成功! ");

}


}

G. 編譯原理 語義分析 算術表達式求值代碼

Java字元串算術表達式求值:importjava.util.ArrayList;importjava.util.Stack;/****@authoryhh**/publicclassCalculate{/***將字元串轉化成List*@paramstr*@return*/publicArrayListgetStringList(Stringstr){ArrayListresult=newArrayList();Stringnum="";for(inti=0;igetPostOrder(ArrayListinOrderList){ArrayListresult=newArrayList();Stackstack=newStack();for(inti=0;ipostOrder){Stackstack=newStack();for(inti=0;i

H. 編譯原理課程設計詞法分析器設計(java實現)

參考答案 永遠對生活充滿希望,對於困境與磨難,微笑面對。

熱點內容
機架式伺服器怎麼操作 發布:2024-04-27 05:19:02 瀏覽:815
我的世界minez網易伺服器 發布:2024-04-27 05:09:26 瀏覽:384
易網頁源碼 發布:2024-04-27 04:51:06 瀏覽:864
攜程伺服器是什麼牌子 發布:2024-04-27 04:31:50 瀏覽:745
醫院新冠肺炎疫情防控演練腳本 發布:2024-04-27 04:04:45 瀏覽:652
天津智慧網關伺服器雲伺服器 發布:2024-04-27 03:56:51 瀏覽:422
移門製作下料尺寸演算法 發布:2024-04-27 03:15:02 瀏覽:641
c語言5常量 發布:2024-04-27 02:38:49 瀏覽:991
源碼怎麼搭建 發布:2024-04-27 02:33:44 瀏覽:97
java獲取參數 發布:2024-04-27 02:22:21 瀏覽:501