編譯原理實驗代碼
❶ 編譯原理 詞法分析程序的設計與實現實驗題
說他像蒼蠅,是罵蒼蠅呢還是罵他呢?
❷ 編譯原理用C語言實現基於LR(1)或SLR(1)語法分析程序代碼,最好還有報告,急。。。
這個是精簡的語法分析程序,如果符合的話,hi我
給你實驗報告
#include <stdio.h>
#include<dos.h>
#include<stdlib.h>
#include<string.h>
char a[50] ,b[50];
char ch;
int n1,i1=0,n=5;
int E();int T();int E1();int T1();int F();
void main() /*遞歸分析*/
{
int f,j=0;
printf("請輸入字元串(長度<50,以#號結束)\n");
do{
scanf("%c",&ch);
a[j]=ch;
j++;
}while(ch!='#');
n1=j;
ch=b[0]=a[0];
f=E();
if (f==0) return;
if (ch=='#') printf("accept\n");
else printf("error\n");
}
int E() // E→TE'
{ int f,t;
f=T();
if (f==0) return(0);
t=E1();
if (t==0) return(0);
else return(1);
}
int T() // T→FT'
{ int f,t;
f=F();
if (f==0) return(0);
t=T1();
if (t==0) return(0);
else return(1);
}
int E1()/*E』*/ // E'→+TE'
{ int f;
if(ch=='+') {
b[i1]=ch;
ch=a[++i1];
f=T();
if (f==0) return(0);
E1();
return(1);
}
return(1);
}
int T1()/*T』*/ // T'→*FT'
{
int f,t;
if(ch=='*') {
b[i1]=ch;
ch=a[++i1];
f=F();
if (f==0) return(0);
t=T1();
if (t==0) return(0);
else return(1);}
a[i1]=ch;
return(1);
}
int F() // F→(E)
{ int f;
if(ch=='(') {
b[i1]=ch;
ch=a[++i1];
f=E();
if (f==0) return(0);
if(ch==')') {
b[i1]=ch;
ch=a[++i1];
}
else {
printf("error\n");
return(0);
}
}
else if(ch=='i') {
b[i1]=ch;
ch=a[++i1];
}
else {printf("error\n");return(0);}
return(1);
}
❸ 求編譯原理的詞法分析器源碼
/* 我上編譯原理課時的第一次作業就是這個,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)實驗總結:你在編程過程中花時多少?多少時間在紙上設計?多少時間上機輸入和調試?多少時間在思考問題?遇到了哪些難題?你是怎麼克服的?你對你的程序的評價?你的收獲有哪些?
另可附加:關鍵字 有符號數 符號表填寫 行號記錄,等
*/
❹ 編譯原理 語義分析 算術表達式求值代碼
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
❺ 編譯原理實驗二 算術表達式擴充
你是需要解釋分析過程,還是需要用代碼寫出你的程序的分析過程?
❻ 有人知道編譯原理實驗之詞法分析器用C++怎麼做嗎
#include "globals.h"
#include "util.h"
#include "scan.h"
#include "parse.h"
static TokenType token; /* holds current token */
/* function prototypes for recursive calls */
static TreeNode * stmt_sequence(void);
static TreeNode * statement(void);
static TreeNode * if_stmt(void);
static TreeNode * repeat_stmt(void);
static TreeNode * assign_stmt(void);
static TreeNode * read_stmt(void);
static TreeNode * write_stmt(void);
static TreeNode * exp(void);
static TreeNode * simple_exp(void);
static TreeNode * term(void);
static TreeNode * factor(void);
static void syntaxError(char * message)
{ fprintf(listing,"\n>>> ");
fprintf(listing,"Syntax error at line %d: %s",lineno,message);
Error = TRUE;
}
static void match(TokenType expected)
{ if (token == expected) token = getToken();
else {
syntaxError("unexpected token -> ");
printToken(token,tokenString);
fprintf(listing," ");
}
}
TreeNode * stmt_sequence(void)
{ TreeNode * t = statement();
TreeNode * p = t;
while ((token!=ENDFILE) && (token!=END) &&
(token!=ELSE) && (token!=UNTIL))
{ TreeNode * q;
match(SEMI);
q = statement();
if (q!=NULL) {
if (t==NULL) t = p = q;
else /* now p cannot be NULL either */
{ p->sibling = q;
p = q;
}
}
}
return t;
}
TreeNode * statement(void)
{ TreeNode * t = NULL;
switch (token) {
case IF : t = if_stmt(); break;
case REPEAT : t = repeat_stmt(); break;
case ID : t = assign_stmt(); break;
case READ : t = read_stmt(); break;
case WRITE : t = write_stmt(); break;
default : syntaxError("unexpected token -> ");
printToken(token,tokenString);
token = getToken();
break;
} /* end case */
return t;
}
TreeNode * if_stmt(void)
{ TreeNode * t = newStmtNode(IfK);
match(IF);
if (t!=NULL) t->child[0] = exp();
match(THEN);
if (t!=NULL) t->child[1] = stmt_sequence();
if (token==ELSE) {
match(ELSE);
if (t!=NULL) t->child[2] = stmt_sequence();
}
match(END);
return t;
}
TreeNode * repeat_stmt(void)
{ TreeNode * t = newStmtNode(RepeatK);
match(REPEAT);
if (t!=NULL) t->child[0] = stmt_sequence();
match(UNTIL);
if (t!=NULL) t->child[1] = exp();
return t;
}
TreeNode * assign_stmt(void)
{ TreeNode * t = newStmtNode(AssignK);
if ((t!=NULL) && (token==ID))
t->attr.name = String(tokenString);
match(ID);
match(ASSIGN);
if (t!=NULL) t->child[0] = exp();
return t;
}
TreeNode * read_stmt(void)
{ TreeNode * t = newStmtNode(ReadK);
match(READ);
if ((t!=NULL) && (token==ID))
t->attr.name = String(tokenString);
match(ID);
return t;
}
TreeNode * write_stmt(void)
{ TreeNode * t = newStmtNode(WriteK);
match(WRITE);
if (t!=NULL) t->child[0] = exp();
return t;
}
TreeNode * exp(void)
{ TreeNode * t = simple_exp();
if ((token==LT)||(token==EQ)) {
TreeNode * p = newExpNode(OpK);
if (p!=NULL) {
p->child[0] = t;
p->attr.op = token;
t = p;
}
match(token);
if (t!=NULL)
t->child[1] = simple_exp();
}
return t;
}
TreeNode * simple_exp(void)
{ TreeNode * t = term();
while ((token==PLUS)||(token==MINUS))
{ TreeNode * p = newExpNode(OpK);
if (p!=NULL) {
p->child[0] = t;
p->attr.op = token;
t = p;
match(token);
t->child[1] = term();
}
}
return t;
}
TreeNode * term(void)
{ TreeNode * t = factor();
while ((token==TIMES)||(token==OVER))
{ TreeNode * p = newExpNode(OpK);
if (p!=NULL) {
p->child[0] = t;
p->attr.op = token;
t = p;
match(token);
p->child[1] = factor();
}
}
return t;
}
TreeNode * factor(void)
{ TreeNode * t = NULL;
switch (token) {
case NUM :
t = newExpNode(ConstK);
if ((t!=NULL) && (token==NUM))
t->attr.val = atoi(tokenString);
match(NUM);
break;
case ID :
t = newExpNode(IdK);
if ((t!=NULL) && (token==ID))
t->attr.name = String(tokenString);
match(ID);
break;
case LPAREN :
match(LPAREN);
t = exp();
match(RPAREN);
break;
default:
syntaxError("unexpected token -> ");
printToken(token,tokenString);
token = getToken();
break;
}
return t;
}
/****************************************/
/* the primary function of the parser */
/****************************************/
/* Function parse returns the newly
* constructed syntax tree
*/
TreeNode * parse(void)
{ TreeNode * t;
token = getToken();
t = stmt_sequence();
if (token!=ENDFILE)
syntaxError("Code ends before file\n");
return t;
}
上面是一個語法分析器的主代碼部分它可以識別類似下面的代碼,但是由於篇幅有限,上面的代碼不是完整代碼,完整代碼太長,還有好幾個文件。
read x; { input an integer }
if 0 < x then { don't compute if x <= 0 }
fact := 1;
repeat
fact := fact * x;
x := x - 1
until x = 0;
write fact { output factorial of x }
end
❼ 急求:編譯原理判斷文法類型的C語言源代碼!!!!!!
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/**//*全局變數定義*/
char inputString[10]; /**//*用來存儲用戶輸入的字元串,最長為20個字元*/
char stack[10]; /**//*用來進行語法分析的棧結構*/
int base=0; /**//*棧底指針*/
int top=1; /**//*棧頂指針*/
char VT[4]={'a','d','b','e'}; /**//*用來存放5個終結符*/
char chanShengShi[10]; /**//*用來存放預測分析表M[A,a]中的一條產生式*/
int firstCharIntex=0; /**//*如果a匹配產生式,則每次firstCharIntex 自增 1 */
/**//*firstCharIntex用來存放用戶輸入串的第一個元素的下標*/
/**//*自定義函數聲明*/
char pop() ; /**//*彈出棧頂元素*/
int push(char) ; /**//*向棧內添加一個元素,成功返回1,若棧已滿則返回0*/
int search(char temp) ; /**//*查找非終結符集合VT中是否存在變數temp,存在返回1,不存在返回0*/
int M(char A, char a) ; /**//* 若預測分析表M[A,a]中存在產生式,
則將該產生式賦給字元數組chanShengShi[10],並返回 1,
若M[A,a]中無定義產生式則返回 0
*/
void init() ; /**//*初始化數組inputString[10] 、棧 stack[10] 和 chanShengShi[10]*/
int yuCeFenXi() ; /**//* 進行輸入串的預測分析的主功能函數,
若輸入串滿足文法則返回 1,不滿足則返回0
*/
void printStack(); /**//*列印棧內元素 */
void printinputString(); /**//*列印用戶輸入串 */
/**//*進入主函數*/
void main()
{
system("cls");
yuCeFenXi(); /**//*調用語法預測分析函數*/
system("pause");
}
/**//*函數的定義*/
int yuCeFenXi()
{
char X; /**//*X變數存儲每次彈出的棧頂元素*/
char a; /**//*a變數存儲用戶輸入串的第一個元素*/
int i;
int counter=1; /**//*該變數記錄語法分析的步驟數*/
init(); /**//*初始化數組*/
printf("wen fa : \n"); /**//*輸出文法做為提示*/
printf("S -> aH \n");
printf("H -> aMd | d \n");
printf("M -> Ab | \n");
printf("A -> aM | e \n");
printf("\ninput string ,'#' is a end sign !!(aaabd#) \n"); /**//*提示用戶輸入將要測試的字元串*/
scanf("%s",inputString);
push('#');
push('S');
printf("\nCounter-----Stack---------------Input string \n"); /**//*輸出結果提示語句*/
while(1) /**//*while循環為語法分析主功能語句塊*/
{
printf(" ");
printf(" %d",counter); /**//*輸出分析步驟數*/
printf(" "); /**//*輸出格式控制語句*/
printStack(); /**//*輸出當前棧內所有元素*/
X=pop(); /**//*彈出棧頂元素賦給變數X*/
printinputString(); /**//*輸出當前用戶輸入的字元串*/
if( search(X)==0 ) /**//*在終結符集合VT中查找變數X的值,存在返回 1,否則返回 0*/
{
if(X == '#') /**//*棧已經彈空,語法分析結果正確,返回 1*/
{
printf("success \n"); /**//*語法分析結束,輸入字元串符合文法定義*/
return 1;
}
else
{
a = inputString[firstCharIntex];
if( M(X,a)==1 ) /**//*查看預測分析表M[A,a]是否存在產生式,存在返回1,不存在返回0*/
{
for(i=0;i<10;i++) /**//* '$'為產生式的結束符,for循環找出該產生式的最後一個元素的下標*/
{
if( chanShengShi[i]=='$' ) break;
}
i-- ; /**//*因為 '$' 不是產生式,只是一個產生式的結束標志,所以i自減1*/
while(i>=0)
{
push( chanShengShi[i] ); /**//*將當前產生式逆序壓入棧內*/
i-- ;
}
}
else
{
printf(" error(1) !!\n"); /**//*若預測分析表M[A,a]不存在產生式,說明語法錯誤*/
return 0;
}
}
}
else /**//*說明X為終結符*/
{
if( X==inputString[firstCharIntex] ) /**//*如果X等於a,說明a匹配*/
{
firstCharIntex++; /**//*輸入串的第一個元素被約去,下一個元素成為新的頭元素*/
}
else
{
printf(" error(2) !! \n");
return 0;
}
}
counter++;
}
}
void init()
{
int i;
for(i=0;i<10;i++)
{
inputString[i]=NULL; /**//*初始化數組inputString[10] */
stack[i]=NULL; /**//*初始化棧stack[10] */
chanShengShi[i]=NULL; /**//*初始化數組chanShengShi[10]*/
}
}
int M(char A, char a) /**//*文法定義因實際情況而定,該文法為課本例題的文法*/
{ /**//*該函數模擬預測分析表中的二維數組 */
if( A=='S'&& a=='a' ) { strcpy(&chanShengShi[0],"aH$"); return 1; }
if( A=='H'&& a=='a' ) { strcpy(&chanShengShi[0],"aMd$"); return 1; }
if( A=='H'&& a=='d' ) { strcpy(&chanShengShi[0],"d$"); return 1; }
if( A=='M'&& a=='a' ) { strcpy(&chanShengShi[0],"Ab$"); return 1; }
if( A=='M'&& a=='d' ) { strcpy(&chanShengShi[0],"$"); return 1; }
if( A=='M'&& a=='b' ) { strcpy(&chanShengShi[0],"$"); return 1; }
if( A=='M'&& a=='e' ) { strcpy(&chanShengShi[0],"Ab$"); return 1; }
if( A=='A'&& a=='a' ) { strcpy(&chanShengShi[0],"aM$"); return 1; }
if( A=='A'&& a=='e' ) { strcpy(&chanShengShi[0],"e$"); return 1; }
else return 0; /**//*沒有定義產生式則返回0*/
}
char pop() /**//*彈出棧頂元素,用topChar返回*/
{
char topChar;
topChar=stack[--top];
return topChar;
}
int push(char ch)
{
if( top>9 )
{
printf(" error : stack overflow "); /**//*棧空間溢出*/
return 0;
}
else
{
stack[top]=ch; /**//*給棧頂空間賦值*/
top++;
return 1;
}
}
int search(char temp)
{
int i,flag=0; /**//*flag變數做為標志,若找到temp則賦1,否則賦0*/
for(i=0;i<4;i++)
{
if( temp==VT[i] ) /**//*終結符集合中存在temp*/
{
flag=1;
break;
}
}
if(flag==1) return 1; /**//*flag==1說明已找到等於temp的元素*/
else return 0;
}
void printStack() /**//*輸出棧內內容*/
{
int temp;
for(temp=1;temp<top;temp++)
{
printf("%c",stack[temp]);
}
}
void printinputString() /**//*輸出用戶輸入的字元串*/
{
int temp=firstCharIntex ;
printf(" "); /**//*該句控制輸出格式*/
do{
printf("%c",inputString[temp]);
temp++;
}while(inputString[temp-1]!='#');
printf(" \n");
}
❽ 編譯原理課程設計-詞法分析器設計(C語言)
#include"stdio.h"/*定義I/O庫所用的某些宏和變數*/
#include"string.h"/*定義字元串庫函數*/
#include"conio.h"/*提供有關屏幕窗口操作函數*/
#include"ctype.h"/*分類函數*/
charprog[80]={'