📄 yufa.txt
字号:
www.pudn.com > 语法分析器.zip > 语法分析器.cpp
#include <iostream.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <iomanip.h>
#define ID 256
#define NUM 257
#define ADDOP 258
#define MULOP 259
#define ASSIGNOP 260
#define RELOP 261
#define LSBRACKET 262
#define RSBRACKET 263
#define LMBRACKET 264
#define RMBRACKET 265
#define COMMA 266
#define COLON 267
#define DOT 268
#define DDOT 269
#define SEMICOLON 270
#define IF 271
#define ELSE 272
#define THEN 273
#define WHILE 274
#define DO 275
#define NOT 276
#define PROGRAM 277
#define PROCEDURE 278
#define BEGIN 279
#define END 280
#define OF 281
#define VAR 282
#define INTEGER 283
#define REAL 284
#define ARRAY 285
#define FUNCTION 286
#define LE 288
#define NE 289
#define LT 290
#define EQ 291
#define GE 292
#define GT 293
#define match(name,l) doit(#name,name,l)
struct token{
char label[20];
int id;
int attribute;
};
int next[10];
char num[100][20],string[20],c;
int Line=1,length=0,len=0,index=0,sleft=0,mleft=0,lleft=0;
char addop[3][3]={"+","-","or"};
char mulop[5][5]={"*","/","and","mod","div"};
char key[16][16]={"if","else","then","while","do","not","program","procedure","begin",
"end","of","var","integer","real","array","function"};
char id[256][20];
FILE *fp;
int state,start,havelooked=0;
struct token Scanner();
void Loadkeyword();
token lookahead,looka;
void simple_expression();
void expr();
void simple_expression();
void state_t();
void optional_statements();
void doit(char *s,int t,int l){
int i,tag,tok;
if(lookahead.id==t){
if(havelooked==0) lookahead=Scanner();
else {lookahead=looka;havelooked=0;}
}
else {
tag=0;tok=0;
cout<<"line:"<<Line<<" syntax error:";
for(i=0;i<l;i++)
if(lookahead.id==next[i]) {cout<<"possible lost "<<s<<endl;tag=1;tok=1;}
if(tag==0){
looka=Scanner(); havelooked=1;
if(t==looka.id) {cout<<"possible unwanted "<<lookahead.label<<endl;tok=1;
lookahead=Scanner(); havelooked=0;}
}
if(tok==0&amt;&amt;tag==0){
cout<<"possible error "<<lookahead.label<<endl;
if(havelooked==0) lookahead=Scanner();
else {lookahead=looka;havelooked=0;}
}
}
}
void identifier_table(){
if(lookahead.id==COMMA){
next[0]=ID;
match(COMMA,1);
next[0]=COMMA;next[1]=SEMICOLON;
match(ID,2);
identifier_table();
}
else {
}
}
void identifier_list(){
next[0]=COMMA;next[1]=SEMICOLON;
match(ID,2);
identifier_table();
}
void standard_type(){
if(lookahead.id==REAL){
next[0]=COMMA;next[1]=SEMICOLON;
match(REAL,2);
}
else {
next[0]=COMMA;next[1]=SEMICOLON;
match(INTEGER,2);
}
}
void type(){
if(lookahead.id==ARRAY){
next[0]=LMBRACKET;match(ARRAY,1); next[0]=NUM;match(LMBRACKET,1);
next[0]=DDOT;match(NUM,1); next[0]=NUM; match(DDOT,1);
next[0]=RMBRACKET;match(NUM,1); next[0]=OF; match(RMBRACKET,1);
next[0]=INTEGER;next[1]=REAL;match(OF,2);
standard_type();
}
else {
standard_type();
}
}
void declaration(){
if(lookahead.id==VAR){
next[0]=ID; match(VAR,1);
identifier_list();
next[0]=INTEGER;next[1]=REAL;next[2]=ARRAY; match(COLON,3);
type();
next[0]=VAR;next[1]=FUNCTION;next[2]=PROCEDURE;next[3]=BEGIN; match(SEMICOLON,4);
declaration();
}
else {
}
}
void declarations(){
declaration();
}
void parameter_table(){
if(lookahead.id==SEMICOLON){
next[0]=ID; match(SEMICOLON,1);
identifier_list();
next[0]=REAL;next[1]=INTEGER;next[2]=ARRAY; match(COLON,3);
type();
parameter_table();
}
else {
}
}
void parameter_list(){
identifier_list();
next[0]=REAL;next[1]=INTEGER;next[2]=ARRAY; match(COLON,3);
type();
parameter_table();
}
void arguments(){
if(lookahead.id==LSBRACKET){
next[0]=ID;match(LSBRACKET,1);
parameter_list();
next[0]=COLON;next[1]=SEMICOLON; match(RSBRACKET,2);
}
else {
}
}
void subprogram_head(){
if(lookahead.id==FUNCTION){
next[0]=ID;match(FUNCTION,1);
next[0]=COLON;next[1]=LSBRACKET;match(ID,2);
arguments();
next[0]=INTEGER;next[1]=REAL;match(COLON,2);
standard_type();
next[0]=VAR;next[1]=BEGIN; match(SEMICOLON,2);
}
else {
next[0]=ID;match(PROCEDURE,1);
next[0]=LSBRACKET;next[1]=SEMICOLON;match(ID,2);
arguments();
}
}
void sign(){
}
void expression(){
simple_expression();
expr();
}
void expression_table(){
if(lookahead.id==COMMA){
next[0]=ID;next[1]=NUM;next[2]=LSBRACKET;next[3]=NOT;match(COMMA,4);
expression();
expression_table();
}
else{
}
}
void expression_list(){
expression();
expression_table();
}
void factor_t(){
if(lookahead.id==LSBRACKET){
next[0]=ID;next[1]=NUM;next[2]=LSBRACKET;next[3]=NOT; match(LSBRACKET,4);
expression_list();
next[0]=DO;next[1]=THEN;next[2]=RSBRACKET;next[3]=RELOP;next[4]=RMBRACKET;
next[5]=COMMA; match(RSBRACKET,6);
}
else{
}
}
void factor(){
if(lookahead.id==ID){
next[0]=DO;next[1]=THEN;next[2]=RSBRACKET;next[3]=RELOP;next[4]=RMBRACKET;
next[5]=COMMA; match(ID,6);
factor_t();
}
else if(lookahead.id==NUM){
next[0]=DO;next[1]=THEN;next[2]=RSBRACKET;next[3]=RELOP;next[4]=RMBRACKET;
next[5]=COMMA; match(NUM,6);
}
else if(lookahead.id==LSBRACKET){
next[0]=ID;next[1]=NUM;next[2]=LSBRACKET;next[3]=NOT; match(LSBRACKET,4);
expression();
next[0]=DO;next[1]=THEN;next[2]=RSBRACKET;next[3]=RELOP;next[4]=RMBRACKET;
next[5]=COMMA;match(RSBRACKET,6);
}
else{
next[0]=ID;next[1]=NUM;next[2]=LSBRACKET;next[3]=NOT;match(NOT,4);
factor();
}
}
void term_t(){
if(lookahead.id==MULOP){
next[0]=ID;next[1]=NUM;next[2]=LSBRACKET;next[3]=NOT;match(MULOP,4);
factor();
term_t();
}
else{
}
}
void term(){
factor();
term_t();
}
void simple_expr(){
if(lookahead.id==ADDOP){
next[0]=ID;next[1]=NUM;next[2]=LSBRACKET;next[3]=NOT;match(ADDOP,4);
term();
simple_expr();
}
else {
}
}
void simple_expression(){
if(lookahead.id==ID||lookahead.id==NUM||lookahead.id==LSBRACKET||lookahead.id==NOT){
term();
simple_expr();
}
else {
sign();
term();
simple_expr();
}
}
void expr(){
if(lookahead.id==RELOP){
next[0]=ID;next[1]=NUM;next[2]=LSBRACKET;next[3]=NOT;match(RELOP,4);
simple_expression();
}
else{
}
}
void procedure_state(){
if(lookahead.id==LSBRACKET){
next[0]=ID;next[1]=NUM;next[2]=LSBRACKET;next[3]=NOT;match(LSBRACKET,4);
expression_list();
next[0]=ELSE;next[1]=SEMICOLON;match(RSBRACKET,2);
}
else{
}
}
void procedure_statement(){
next[0]=LSBRACKET;next[1]=ELSE;next[2]=SEMICOLON;match(ID,3);
procedure_state();
}
void variable_t(){
if(lookahead.id==LMBRACKET){
next[0]=ID;next[1]=NUM;next[2]=LSBRACKET;next[3]=NOT;match(LMBRACKET,4);
expression();
next[0]=ASSIGNOP;match(RMBRACKET,1);
}
else{
}
}
void variable(){
next[0]=LMBRACKET;next[1]=ASSIGNOP;match(ID,2);
variable_t();
}
void state_t(){
if(lookahead.id==LMBRACKET||lookahead.id==ASSIGNOP){
variable_t();
next[0]=ID;next[1]=NUM;next[2]=LSBRACKET;next[3]=NOT;match(ASSIGNOP,4);
expression();
}
else {
procedure_state();
}
}
void compound_statement(){
next[0]=ID;next[1]=END;next[2]=BEGIN;next[3]=IF;next[4]=WHILE;match(BEGIN,5);
optional_statements();
next[0]=SEMICOLON;next[1]=DOT; match(END,2);
}
void statement(){
if(lookahead.id==ID){
next[0]=LSBRACKET;next[1]=LMBRACKET;next[2]=ASSIGNOP;next[3]=SEMICOLON;
next[4]=END;match(ID,5);
state_t();
}
else if(lookahead.id==BEGIN){
compound_statement();
}
else if(lookahead.id==IF){
next[0]=ID;next[1]=NUM;next[2]=LSBRACKET;next[3]=NOT;match(IF,4);
expression();
next[0]=ID;next[1]=BEGIN;next[2]=IF;next[3]=WHILE;match(THEN,4);
statement();
next[0]=ID;next[1]=BEGIN;next[2]=IF;next[3]=WHILE;match(ELSE,4);
statement();
}
else{
next[0]=ID;next[1]=NUM;next[2]=LSBRACKET;next[3]=NOT;match(WHILE,4);
expression();
next[0]=ID;next[1]=BEGIN;next[2]=IF;next[3]=WHILE;match(DO,4);
statement();
}
}
void statement_table(){
if(lookahead.id==SEMICOLON){
next[0]=ID;next[1]=BEGIN;next[2]=IF;next[3]=WHILE;match(SEMICOLON,4);
statement();
statement_table();
}
else{
}
}
void statement_list(){
statement();
statement_table();
}
void optional_statements(){
if(lookahead.id==ID||lookahead.id==BEGIN||lookahead.id==IF||lookahead.id==WHILE){
statement_list();
}
else{
}
}
void subprogram_declaration(){
subprogram_head();
declarations();
compound_statement();
}
void sub_declarations(){
if(lookahead.id==FUNCTION||lookahead.id==PROCEDURE){
subprogram_declaration();
next[0]=FUNCTION;next[1]=PROCEDURE;next[2]=BEGIN;match(SEMICOLON,3);
sub_declarations();
}
else {
}
}
void subprogram_declarations(){
sub_declarations();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -