當前位置:首頁 » 編程軟體 » 編譯原理類c語言詞法分析程序

編譯原理類c語言詞法分析程序

發布時間: 2022-08-01 12:04:56

編譯原理課設實現C/C++語言詞法分析器

既然是c語言詞法分析器,那就是用c/c++對一段c語言文本進行詞法分析,c語言中的for語句、while語句、switch語句、if語句等等的進行分析並將其提取出來的一個設計和實現過程而矣
這是大學專門有一門《編譯原理》的課程而矣

㈡ 編譯原理課程設計-詞法分析器設計(C語言)

#include"stdio.h"/*定義I/O庫所用的某些宏和變數*/

#include"string.h"/*定義字元串庫函數*/

#include"conio.h"/*提供有關屏幕窗口操作函數*/

#include"ctype.h"/*分類函數*/

charprog[80]={''},

token[8];/*存放構成單詞符號的字元串*/

charch;

intsyn,/*存放單詞字元的種別碼*/

n,

sum,/*存放整數型單詞*/

m,p;/*p是緩沖區prog的指針,m是token的指針*/

char*rwtab[6]={"begin","if","then","while","do","end"};

voidscaner(){

m=0;

sum=0;

for(n=0;n<8;n++)

token[n]='';

ch=prog[p++];

while(ch=='')

ch=prog[p++];

if(isalpha(ch))/*ch為字母字元*/{

while(isalpha(ch)||isdigit(ch))/*ch為字母字元或者數字字元*/{

token[m++]=ch;

ch=prog[p++];}

token[m++]='';

ch=prog[p--];

syn=10;

for(n=0;n<6;n++)

if(strcmp(token,rwtab[n])==0)/*字元串的比較*/{

syn=n+1;

break;}}

else

if(isdigit(ch))/*ch是數字字元*/{

while(isdigit(ch))/*ch是數字字元*/{

sum=sum*10+ch-'0';

ch=prog[p++];}

ch=prog[p--];

syn=11;}

else

switch(ch){

case'<':m=0;token[m++]=ch;ch=prog[p++];

if(ch=='>'){

syn=21;

token[m++]=ch;}

elseif(ch=='='){

syn=22;

token[m++]=ch;}

else{

syn=20;

ch=prog[p--];}

break;

case'>':m=0;token[m++]=ch;ch=prog[p++];

if(ch=='='){

syn=24;

token[m++]=ch;}

else{

syn=23;

ch=prog[p--];}

break;

case':':m=0;token[m++]=ch;ch=prog[p++];

if(ch=='='){

syn=18;

token[m++]=ch;}

else{

syn=17;

ch=prog[p--];}

break;

case'+':syn=13;token[0]=ch;break;

case'-':syn=14;token[0]=ch;break;

case'*':syn=15;token[0]=ch;break;

case'/':syn=16;token[0]=ch;break;

case'=':syn=25;token[0]=ch;break;

case';':syn=26;token[0]=ch;break;

case'(':syn=27;token[0]=ch;break;

case')':syn=28;token[0]=ch;break;

case'#':syn=0;token[0]=ch;break;

default:syn=-1;}}

main()

{

printf(" Thesignificanceofthefigures: "

"1.figures1to6saidKeyword "

"2. "

"3.figures13to28saidOperators ");

p=0;

printf(" pleaseinputstring: ");

do{

ch=getchar();

prog[p++]=ch;

}while(ch!='#');

p=0;

do{

scaner();

switch(syn){

case11:printf("(%d,%d) ",syn,sum);break;

case-1:printf(" ERROR; ");break;

default:printf("(%d,%s) ",syn,token);

}

}while(syn!=0);

getch();

}

程序測試結果

對源程序beginx:=9:ifx>9thenx:=2*x+1/3;end#的源文件,經過詞法分析後輸出如下圖5-1所示:

具體的你在修改修改吧

㈢ 請問編譯原理的詞法分析用C語言編寫的演算法是怎樣的

#include <iostream>
using namespace std;
#define m 45
#define n 100
#define t 10
int main()
{
FILE *fp;
char filename[20],c[n];
printf("Type the file name which you want to open:");
scanf("%s",&filename);
fp=fopen(filename,"r+"); /*以r-只讀方式打開指定文件*/
if((fp=fopen(filename,"r"))==NULL) /*文件不存在輸出錯誤*/
{cout<<"文件不存在!"<<endl;exit(-1);}
cout<<"文件中內容如下:"<<endl;
for(int j=0;!feof(fp);j++){
c[j]=fgetc(fp);//從流中讀取字元
}
char keyword[m][t]={"include","int","string","cout","cin","auto","break","case","char","class","const",
"continue","default","delete","do","double","else","enum","extern","float","for","friend","if","inline",
"int","long","new","operator","private","protected","public","register","return","short","sizeof","static",
"struct","switch","template","this","typedef","union","virtual","void","while"};//關鍵字數組
char a[t],*p=c,*q=a,*s=a;
bool w=0,r=0;
int i=0;
for(i=0;i<10;i++)a[i]=NULL;//初始化臨時數組
while (*p !=NULL){
q=s=a;
if((*p>='a'&&*p<='z')||(*p>='A'&&*p<='Z')||*p=='_'){//識別標識符
*q=*p;p++;q++;
while ((*p>='a'&&*p<='z')||(*p>='A'&&*p<='Z')||(*p>='0'&&*p<='9')||*p=='_'){
*q=*p;p++;q++;
}
for(i=0;i<m;i++)if(strcmp(keyword[i],a)==0){r=1;break;}
if(r==1){cout<<"關鍵字為:";r=0;}
else cout<<"標識符為:";
while(s!=q){
cout<<*s;
s++;
}
cout<<endl;
for(i=0;i<t;i++)a[i]=NULL;
}
else if(*p=='\''){//識別字元常量
p++;
while(*p!='\''){

*q=*p;
p++;
q++;
}
cout<<"字元常量為:";
while(s!=q){
cout<<*s;
s++;
}
cout<<endl;
for(i=0;i<t;i++)a[i]=NULL;
p++;
}
else if(*p=='\"'){//識別字元串常量
p++;
while(*p!='\"'){
*q=*p;
p++;
q++;
}
cout<<"字元串常量為:";
while(s!=q){
cout<<*s;
s++;
}
cout<<endl;
for(i=0;i<t;i++)a[i]=NULL;
p++;
}
else if(*p=='+'||*p=='-'||*p=='*'||*p=='/'||*p=='='||*p=='%'||*p=='/'){//識別運算符
cout<<"運算符為:"<<*p;
cout<<endl;
p++;
}
else if(*p==';'||*p==','){//識別分解符
cout<<"分界符為:"<<*p;
cout<<endl;
p++;
}
else if(*p>='0'&&*p<='9'){
s=q=a;
*q=*p;p++;q++;
while(*p>='0'&&*p<='9'||*p=='.'){
*q=*p;p++;q++;
}
while(s!=q){
if(*s=='.'){w=1;break;}//識別實型常量
s++;
}
s=a;
if(w ==1){
cout<<"實型常量為:";
while(s!=q){
cout<<*s;
s++;
}
for(i=0;i<t;i++)a[i]=NULL;
}
else {
cout<<"整型常量為:";
while(s!=q){//識別整型常量
cout<<*s;
s++;
}
for(i=0;i<t;i++)a[i]=NULL;
}
cout<<endl;
}
else p++;
}
return 0;
}

㈣ 急求!!!用C語言編寫一個編譯原理實驗的簡單優先分析法程序

編譯原理IF條件語句的翻譯程序設計—簡單優先法、輸出四元式通過設計、編制、調試一個條件語句的語法及語義分析程序,加深對語法及語義分析原理的理解,並實現詞法分析程序對單詞序列的詞法檢查和分析。具體做到以下幾點:①對輸入語句進行詞法分析。將輸入的字元串進行掃描和分解,識別出一個個合法的單詞。單詞種類包括:關鍵字,標識符,運算符,常數和界限符②進行語法分析。編寫條件語句的相應文法,按照語法分析方法中的簡單優先分析法為文法設計簡單優先表,對詞法分析得到的單詞序列進行語法分析,以判別輸入的語句是否屬於該文法的條件語句。③語法制導翻譯。設計中間代碼(四元式)序列的結構及屬性文法,運用語法制導翻譯,在進行語法分析的同時,執行相應的語義規則描述的動作,從而實現語義處理,生成中間代碼以四元式的形式輸出。④錯誤提示。對不同的錯誤給出簡略描述,並終止程序的繼續執行。下載地址如下,有你要的東西!pile.rar

㈤ 急求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語言子集的詞法分析程序

#include <iostream>

#include <string>

using namespace std;

string key[6] = {"begin", "if", "then", "while", "do", "end"};

//關鍵字

bool isKey( string str, int &syn) //判斷是否為關鍵字,若是傳回相

應關鍵碼的種別名

{

int i;

for(i=0; i<6; i++)

{

if(str == key[i])

{

syn = i + 1;

return true;

}

}

return false;

}

bool isLetter(char c) //是否為字母

{

if((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))

return true;

else

return false;

}

bool isDigit(char c) //是否為數字

{

if(c >= '0' && c <= '9')

return true;

else

return false;

}

void analyse(FILE *fileP)

{

int n;

char c;

string str = "";

while((c = fgetc(fileP)) != EOF)

{

if(c == ' ' || c == '\n' || c == '\t')

continue;

else if(isDigit(c)) //數字

{

while(isDigit(c))

{

str += c;

c = fgetc(fileP);

}

fseek(fileP, -1, SEEK_CUR);

cout << "(11, " << str << ")" << endl;

str = "";

}

else if(isLetter(c)) //字母開頭的

{

while(isDigit(c) || isLetter(c))

{

str += c;

c = fgetc(fileP);

}

fseek(fileP, -1, SEEK_CUR);

if(isKey(str, n))

cout << "(" << n << ", " << str << ")" << endl; //關鍵碼

else

cout << "(10, " << "\'"<< str << "\'" << ")" << endl; //標志符

str = "";

}

else //操作符等

{

switch(c)

{

case '+':

cout << "(13, +)" << endl;

break;

case '-':

cout << "(14, -)" << endl;

break;

case '*':

cout << "(15, *)" << endl;

break;

case '/':

cout << "(16, /)" << endl;

break;

case ':':

{

if(c=fgetc(fileP) == '=')

cout << "(18, :=)" << endl;

else

{

cout << "(17, :)" << endl;

fseek(fileP, -1, SEEK_CUR);

}

break;

}

case '<':

{

c=fgetc(fileP);

if(c == '=')

cout << "(22, <=)" << endl;

else if(c == '>')

cout << "(21, <>)" << endl;

else

{

cout << "(20, <)" << endl;

fseek(fileP, -1, SEEK_CUR);

}

break;

}

case '>':

{

c=fgetc(fileP);

if(c == '=')

cout << "(24, >=)" << endl;

else

{

cout << "(23, >)" << endl;

fseek(fileP, -1, SEEK_CUR);

}

break;

}

case '=':

cout << "(25, =)" << endl;

break;

case ';':

cout << "(26, ;)" << endl;

break;

case '(':

cout << "(27, ()" << endl;

break;

case ')':

cout << "(28, ))" << endl;

break;

case '#':

cout << "(0, #)" << endl;

break;

}

}

}

}

int main()

{

FILE *fileP;

fileP = fopen("test.txt", "r");

cout << "------詞法分析如下------" << endl;

analyse(fileP);

return 0;

}

㈦ 求C語言編譯原理語法分析程序

一繼承的詞法來自

http://blog.sina.com.cn/s/blog_67c9fc300100srad.html
二語法

用擴充的BNF表示如下:

⑴<程序>::=begin<語句串>end

⑵<語句串>::=<語句>{;<語句>}

⑶<語句>::=<賦值語句>

⑷<賦值語句>::=ID:=<表達式>

⑸<表達式>::=<項>{+<項> | -<項>}

⑹<項>::=<因子>{*<因子> | /<因子>

⑺<因子>::=ID | NUM | (<表達式>)

三要求

輸入單詞串,以「#」結束,如果是文法正確的句子,則輸出成功信息,列印「success」,否則輸出「error」。

例如:

輸入 begin a:=9; x:=2*3; b:=a+x end #

輸出 success!

輸入 x:=a+b*c end #

輸出 error!

㈧ 學了編譯原理這門課,要求編一個:詞法分析的程序,要求對詞法分析至少選擇三種不同類型的句子進行單詞識

給你一個toyl語言的
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<windows.h>
#define is_end_of_input(ch) ((ch)=='#')
#define is_letter(ch) ('A'<=(ch)&&(ch)<='Z'||'a'<=(ch)&&(ch)<='z')
#define is_digit(ch) ('0'<=(ch)&&(ch)<='9')
#define is_digit_or_letter(ch) (is_letter(ch)||is_digit(ch))
#define is_operator(ch) ((ch)=='+'||(ch)='-'||(ch)='*')
#define is_layout(ch) (!is_end_of_input(ch)&&(ch)<=' ')
#define Step 10 //字元串每次增長的長度.

typedef struct
{
char * Class;
char seman[];
int len;
int value;
}mytoken;

typedef struct node
{
char words[20];
int length;
int time;
struct node *next;
}mynode;
//全局變數
char ch;
char *fp;
mytoken token;
int TYPE=-1;
int num=1;
int j=0;
int Length=0;
void error()//報錯
{
printf("錯誤\n");
}

char * getstr() //從鍵盤獲取任意長度的輸入函數實現
{
char *temp, *str=(char *)malloc(10);
int c=0, len=0, times=1, number=0;
if(!str)
{
printf("內存不足!");
return (char *)NULL;
}
number+=times*Step;

do //遇到#則輸入結束。
{
c=getchar();
if(len==number)
{
times++;
number=times*Step;
temp=str;
str=(char *)realloc(str,number);
if(str==NULL)
{
printf("內存不足!");
str=temp;
break;
}
}
*(str+len)=c;
len++;
}while(c!='#');
str=(char *)realloc(str,len+1); //字元串的最終長度調整.
*(str+len)='\0';
return str;
}

void next_char(void)//獲取下一個字元
{
ch=*fp;
fp=fp+1;

}

void next_valchar(void)//獲取第一個有效的字元,過濾空格等。
{
next_char();
if(ch=='#')exit(0);//當文件只有空格和#號時
while(is_layout(ch))
{
next_char();
//if(ch=='#')exit(0);
}
}

void back()//指針回走
{
fp=fp-1;
}

void recongnize_name(char chr)//識別字元串
{
char name[10];
int i=0;
name[i++]=ch;
next_char();
while(is_digit_or_letter(ch))
{
name[i++]=ch;
next_char();

}

if((ch!=' ')&&(ch!='\t')&&(ch!='\n')&&(ch!='#')&&(ch!=':')&&(ch!='(')&&(ch!=')')&&(ch!=';'))//轉非法字元串處理
{

do
{
name[i++]=ch;
next_char();

}while((ch!=' ')&&(ch!='\t')&&(ch!='\n')&&(ch!='#'));
if((name[i-1]=='#')||(name[i-1]=='\n'))//去掉結束符#或回車
{
name[i-1]='\0';
}
back();//指針回走
name[i]='\0';
printf("非法字元串\t%s\n",name);

}
else{
name[i]='\0';
if (name== "begin")
{
token.Class="BEGIN";
}
else if (name== "end")
{
token.Class="END";
}
else if (name=="read")
{
token.Class=="READ";
}
else if (name=="write")
{
token.Class="WRITE";
}
else
{
token.Class="IDEN";
int n=0;
Length=0;
while(name[n]!='\0')
{

token.seman[n]=name[n];
n++;
}
Length=n;
token.seman[n]=name[n];

}
back();
}
}

void recongnize_number(char cha)//識別數字
{
int N=0;
int m;
char name[10];//存非法字元串
int i=0;
while((m=is_digit(ch)))
{
N=N*10+(ch-'0');

name[i++]=ch;
next_char();
}
if(ch==' '||ch=='\t'||ch=='\n'||ch=='#'||(ch==';'))
{
token.Class="NUMB";
token.value=N;
back();
}
else//轉非法字元串處理
{

do
{
name[i++]=ch;
next_char();

}while((ch!=' ')&&(ch!='\t')&&(ch!='\n')&&(ch!='#'));
if((name[i-1]=='#')||(name[i-1]=='\n'))//去掉結束符#或回車
{
name[i-1]='\0';
}
back();//指針回走
name[i]='\0';
printf("非法字元串\t%s\n",name);
TYPE=-1;

}

}

int next_token(void)//讀下一個單詞
{
next_valchar();
char name[10];//存首字母非法的字元串
int fg=0;
int i=0;
if('0'<=(ch)&&(ch)<='9')
{
TYPE=0;
}
else if('A'<=(ch)&&(ch)<='Z'||'a'<=(ch)&&(ch)<='z')
{
TYPE=1;
}
else
{
TYPE=2;
}

switch(TYPE)
{
case 0:
recongnize_number(ch);break;
case 1:
recongnize_name(ch);break;
case 2:
switch(ch)
{
case '+' :
token.Class="ADD";
token.seman[0]='+';
token.seman[1]='\0';
TYPE=2;
break;
case '*' :
token.Class="MULT";
token.seman[0]='*';
token.seman[1]='\0';
TYPE=2;
break;
case ':' :
next_char();
if(ch!='=')
{
error();
TYPE=-1;
break;
}
token.Class="ASS";
token.seman[0]=':';
token.seman[1]='=';
token.seman[2]='\0';
TYPE=2;
break;
case ';' :
token.Class="SEMI";
token.seman[0]=';';
token.seman[1]='\0';
TYPE=2;
break;
case '(' :
token.Class="OPEN";
token.seman[0]='(';
token.seman[1]='\0';
TYPE=2;
break;
case ')' :
token.Class="CLOSE";
token.seman[0]=')';
token.seman[1]='\0';
TYPE=2;
break;
default :
fg=1;
break;

}
}
if(fg==1)//非法字元串處理
{
name[i++]=ch;
while((ch!=' ')&&(ch!='\t')&&(ch!='\n')&&(ch!='#'))
{
next_char();
name[i++]=ch;
}
if((name[i-1]=='#')||(name[i-1]=='\n'))//去掉結束符#或回車

{
name[i-1]='\0';
}
back();//指針回走
name[i]='\0';
printf("非法字元串\t%s\n",name);
TYPE=-1;//置TYPE為-1
}

}

int compare(node *head,char words[],int Length)//單詞的比較
{
node *p;
p=head;
if(head==NULL)
{
return 0;
}
else
{

int fg=1;

do
{
int i,j,succ;
i=0;
succ=0;
while((i<=p->length-Length)&&(!succ))
{
j=0;
succ=1;
while((j<Length)&&succ)
{
if(words[j]==(p->words[i+j]))
{
j++;
}
else
{
succ=0;
}

}
i++;
}
if(succ&&(j>=p->length))
{
(p->time)++;
fg=0;

}
if(p->next!=NULL)
{
p=p->next;
}

}while((p->next!=NULL)&&fg);

if(fg==0)
{
return 1;
}
else
{
return 0;
}

}
}

node *insert(node *head) //將讀到的新單詞加入鏈表
{
node *p;
p=(mynode*)malloc(sizeof(mynode));/*分配空間*/
strcpy(p->words,token.seman);
int n=0;
p->length=0;
p->time=1;
while(p->words[n]!='\0')
{
p->length++;
n++;
}
p->next=NULL;
if(head==NULL)
{
head=p;
}
else
{
p->next=head->next;
head->next=p;
}

return head;
}

void display(node *head)//列印鏈表的內容
{
node *p;
p=head;
if(!p) printf("\n無標識符!");
else
{
printf("\n各標識符或保留字及其出現的次數為:\n");
printf("標識符\t出現次數\n");
while(p) { printf("%s\t%d\n",p->words,p->time);p=p->next;}

}
}

int main(int argc, char *argv[])
{
char *str1=NULL;
printf("請輸入程序代碼:\n");
str1=getstr();//獲取用戶程序段的輸入
fp=str1;
mynode *head=NULL;
do{
next_token();

switch(TYPE)
{
case 0:
printf("[%d]\t(%s,\t\"%d\")\n",num++,token.Class,token.value);
break;
case 1:
case 2:
printf("[%d]\t(%s,\t\"%s\")\n",num++,token.Class,token.seman);
int f;
f=0;
f=compare(head,token.seman,Length);
if((TYPE==1)&&(f==0))
{

head=insert(head);
}
break;
default:
break;
}

}while(*fp!='#');

display(head);
return 0;
}

㈨ 編譯原理語法分析器程序設計,用C語言或C++,哪裡有這個程序

1.文法簡略,沒有實現的部分,可以在此文法的基礎上進行擴充,本程序的採用自頂向下的LL(1)文法。
2.可以自動實現求First
集和
Follow
集。
3.處終結符外(有些硬編碼的成分),終結符的文法可以自定義,也就是說讀者可以自定義文法。
4.為方便理解,C語言的文法描述寫成中文。
5.程序將詞法分析和語法分析結合起來,詞法分析的結果作為語法分析的輸入。
6.最終結果在控制台顯示的有:詞法分析、First集、Follow集、Select集,在preciateResult.txt
中寫入了語法分析結果,在preciateTable.txt
中寫入了預測分析表。
7.文法的詞素之間必須有空格分開。

㈩ 編譯原理詞法分析程序

(一)Block子程序分析

procere enter(k: object1); //填寫符號表
begin {enter object into table}
tx := tx + 1; //下標加1,tx的初始值為零,零下標不地址不填寫標志符,用於查找失敗使用
with table[tx] do //填入內容,保存標志符名和類型
begin name := id; kind := k;
case k of //根據類型判斷是否正確
constant: begin if num > amax then //如果是常量,判斷是否大於最大值,若是則報30號錯
begin error(30); num :=0 end;
val := num //否則保存數值
end;
varible: begin level := lev; adr := dx; dx := dx + 1; //如果是變數,填寫變數內部表示,LEVEl是變數的層次,adr為地址
end;
proc: level := lev //如果是過程,保存過程的層次
end
end
end {enter};

//查找符號表的位置
function position(id: alfa): integer;
var i: integer;
begin {find indentifier id in table} //從後向前查找
table[0].name := id; i := tx; //找到保存類型
while table[i].name <> id do i := i-1;
position := i //返回標志符在符號表中的位置
end {position};

procere block(lev,tx: integer; fsys: symset);
var dx: integer; {data allocation index} //數據分配索引
tx0: integer; {initial table index} //初始符號表索引
cx0: integer; {initial code index} //初始代碼索引
procere enter(k: object1); //填寫符號表,下次分析
begin {enter object into table}
tx := tx + 1;
with table[tx] do
begin name := id; kind := k;
case k of
constant: begin if num > amax then
begin error(30); num :=0 end;
val := num
end;
varible: begin level := lev; adr := dx; dx := dx + 1;
end;
proc: level := lev
end
end
end {enter};

function position(id: alfa): integer; //查找符號表,下次分析
var i: integer;
begin {find indentifier id in table}
table[0].name := id; i := tx;
while table[i].name <> id do i := i-1;
position := i
end {position};

procere constdeclaration; //常量聲明
begin if sym = ident then //如果是標志符,讀入一個TOKEN
begin getsym;
if sym in [eql, becomes] then //讀入的是等號或符值號繼續判斷
begin if sym = becomes then error(1); //如果是「=」報1號錯
getsym; //讀入下一個TOKEN
if sym = number then //讀入的是數字,填寫符號表
begin enter(constant); getsym
end
else error(2) //如果不是數字,報2號錯
end else error(3) //不是等號或符值號,報3號錯
end else error(4) //如果不是標志符,報4號錯
end {constdeclaration};

procere vardeclaration; //變數聲明
begin if sym = ident then //讀入的是標志符,填寫符號表
begin enter(varible); getsym
end else error(4) //不是標志符,報4號錯
end {vardeclaration};

procere listcode;
var i: integer;
begin {list code generated for this block}
for i := cx0 to cx-1 do
with code[i] do
writeln(i:5, mnemonic[f]:5, 1:3, a:5)
end {listcode};

procere statement(fsys: symset);
var i, cx1, cx2: integer;
procere expression(fsys: symset); //表達式分析
var addop: symbol;
procere term(fsys: symset); //項分析
var mulop: symbol;
procere factor(fsys: symset); //因子分析
var i: integer;
begin test(facbegsys, fsys, 24); //讀入的是「(」,標志符或數字
while sym in facbegsys do
begin
if sym = ident then //是標志符,查表
begin i:= position(id);
if i = 0 then error(11) else //未找到,報11號錯
with table[i] do //找到,讀入標志符類型
case kind of
constant: gen(lit, 0, val); //寫常量命令
varible: gen(lod, lev-level, adr);//寫變數命令
proc: error(21) //過程名,報21號錯
end;
getsym //讀入下一個TOKEN
end else
if sym = number then //讀入的是數字
begin if num > amax then //如果數字大於最大數,報30號錯誤
begin error(30); num := 0
end;
gen(lit, 0, num); getsym //調用數字命令,讀入下一個TOKEN
end else
if sym = lparen then //讀入的是「(」
begin getsym; expression([rparen]+fsys); //調用表達式分析函數
if sym = rparen then getsym else error(22) //如果「(」後無「)」,報22號錯
end;
test(fsys, [lparen], 23)
end
end {factor};//因子分析結束

//項分析
begin {term} factor(fsys+[times, slash]); //調用因子分析程序
while sym in [times, slash] do //取得是乘、除號循環
begin mulop:=sym;getsym;factor(fsys+[times,slash]); //記錄符號,調用因子分析
if mulop=times then gen(opr,0,4) else gen(opr,0,5) //寫乘除指令
end
end {term};
begin {expression}
if sym in [plus, minus] then //如果是加減號
begin addop := sym; getsym; term(fsys+[plus,minus]); //記錄符號,調用項分析程序
if addop = minus then gen(opr, 0,1) //寫加減指令
end else term(fsys+[plus, minus]);
while sym in [plus, minus] do //如果是加減號循環
begin addop := sym; getsym; term(fsys+[plus,minus]);
if addop=plus then gen(opr,0,2) else gen(opr,0,3)
end
end {expression};

//條件過程
procere condition(fsys: symset);
var relop: symbol;
begin
if sym = oddsym then //如果是判奇符
begin getsym; expression(fsys); gen(opr, 0, 6) //取下一個TOKEN,調用expression,填指令
end else
begin expression([eql, neq, lss, gtr, leq, geq]+fsys);
if not(sym in [eql, neq, lss, leq, gtr, geq]) then //如果不是取到邏輯判斷符號,出錯.20
error(20) else
begin relop := sym; getsym; expression(fsys);
case relop of
eql: gen(opr, 0, 8); // =,相等
neq: gen(opr, 0, 9); // #,不相等
lss: gen(opr, 0, 10); // <,小於
geq: gen(opr, 0, 11); // ],大於等於
gtr: gen(opr, 0, 12); // >,大於
leq: gen(opr, 0, 13); // [,小於等於
end
end
end
end {condition};

begin {statement}
if sym = ident then //如果是標識符
begin i := position(id); //查找符號表
if i = 0 then error(11) else //未找到,標識符未定義,報11號錯
if table[i].kind <> varible then //如果標識符不是變數,報12號錯
begin {assignment to non-varible} error(12); i := 0
end;
getsym; if sym = becomes then getsym else error(13); //如果是變數讀入下一個TOKEN,不是賦值號,報13好錯;是則讀入一個TOKEN
expression(fsys); //調用表達是過程
if i <> 0 then //寫指令
with table[i] do gen(sto, lev-level, adr)
end else
if sym = callsym then //如果是過程調用保留字,讀入下一個TOKEN
begin getsym;
if sym <> ident then error(14) else //不是標識符報14號錯
begin i := position(id);
if i = 0 then error(11) else //是標識符,未定義,報13號錯
with table[i] do // 已定義的標識符讀入類型
if kind=proc then gen(cal, lev-level, adr) //是過程名寫指令
else error(15); //不是過程名,報15號錯
getsym
end
end else
if sym = ifsym then //如果是IF
begin getsym; condition([thensym, dosym]+fsys); //讀入一個TOKEN,調用條件判斷過程
if sym = thensym then getsym else error(16); //如果是THEN,讀入一個TOKEN,不是,報16號錯
cx1 := cx; gen(jpc, 0, 0); //寫指令
statement(fsys); code[cx1].a := cx
end else
if sym = beginsym then //如果是BEGIN
begin getsym; statement([semicolon, endsym]+fsys); //讀入一個TOKEN
while sym in [semicolon]+statbegsys do
begin
if sym = semicolon then getsym else error(10); //如果讀入的是分號
statement([semicolon, endsym]+fsys)
end;
if sym = endsym then getsym else error(17) //如果是END 讀入一個TOKEN,不是,報17號錯
end else
if sym = whilesym then //如果是WHILE
begin cx1 := cx; getsym; condition([dosym]+fsys); //調用條件過程
cx2 := cx; gen(jpc, 0, 0); //寫指令
if sym = dosym then getsym else error(18); //如果是DO讀入下一個TOKEN,不是報18號錯
statement(fsys); gen(jmp, 0, cx1); code[cx2].a := cx
end;
test(fsys, [], 19)
end {statement};

begin {block}
dx:=3;
tx0:=tx;
table[tx].adr:=cx;
gen(jmp,0,0);
if lev > levmax then error(32);
repeat
if sym = constsym then //如果是CONST
begin getsym; //讀入TOKEN
repeat constdeclaration; //常量聲明
while sym = comma do
begin getsym; constdeclaration
end;
if sym = semicolon then getsym else error(5) //如果是分號讀入下一個TOKEN,不是報5號錯
until sym <> ident //不是標志符常量聲明結束
end;
if sym = varsym then 如果是VAR
begin getsym; 讀入下一個TOKEN
repeat vardeclaration; //變數聲明
while sym = comma do
begin getsym; vardeclaration
end;
if sym = semicolon then getsym else error(5) //如果是分號讀入下一個TOKEN,不是報5號錯
until sym <> ident; //不是標志符常量聲明結束
end;
while sym = procsym do //過程聲明
begin getsym;
if sym = ident then
begin enter(proc); getsym
end
else error(4); //不是標志符報4號錯
if sym = semicolon then getsym else error(5); //如果是分號讀入下一個TOKEN,不是報5號錯
block(lev+1, tx, [semicolon]+fsys);
if sym = semicolon then //如果是分號,取下一個TOKEN,不是報5號錯
begin getsym;test(statbegsys+[ident,procsym],fsys,6)
end
else error(5)
end;
test(statbegsys+[ident], declbegsys, 7)
until not(sym in declbegsys); //取到的不是const var proc結束
code[table[tx0].adr].a := cx;
with table[tx0] do
begin adr := cx; {start adr of code}
end;
cx0 := 0{cx}; gen(int, 0, dx);
statement([semicolon, endsym]+fsys);
gen(opr, 0, 0); {return}
test(fsys, [], 8);
listcode;
end {block};

熱點內容
交叉編譯優化 發布:2025-05-14 03:48:52 瀏覽:531
動圖在線壓縮 發布:2025-05-14 03:35:24 瀏覽:132
w7共享無法訪問 發布:2025-05-14 03:35:24 瀏覽:482
為什麼微信會出現賬號密碼錯誤 發布:2025-05-14 03:03:30 瀏覽:692
幻影腳本官網 發布:2025-05-14 03:01:13 瀏覽:826
servlet的webxml怎麼配置 發布:2025-05-14 02:51:46 瀏覽:772
怎麼取消手勢密碼 發布:2025-05-14 02:51:11 瀏覽:639
openvpn搭建vpn伺服器搭建 發布:2025-05-14 02:47:52 瀏覽:998
密碼忘了從哪裡找 發布:2025-05-14 02:39:09 瀏覽:548
我的世界什麼伺服器有前途 發布:2025-05-14 02:30:31 瀏覽:528