gpt4 book ai didi

c++ - Recursive Decent 解析器 (c++) 的编译问题

转载 作者:太空宇宙 更新时间:2023-11-04 16:18:30 24 4
gpt4 key购买 nike

我的 C++ 程序在编译时遇到问题,它是一个递归的不错的解析器,规则如下:

slist :: = stmt slist | stmt
stmt ::= decl | assign | print
decl ::= INT ID SC | FLOAT ID SC
print ::= PRINT expr SC
assign ::= ID EQUAL expr SC
expr ::=term PLUS expr|term MINUS expr| term
term ::= factor STAR term|factor DIV term| factor MOD term| factor
factor ::= primary STARSTAR factor | primary
primary ::= INTCONST | FLOATCONST | ID |
LPAREN expr RPAREN

我对 C++ 还是比较陌生,所以我不确定是我搞砸了什么还是我的编译器。我在 genome/ubuntu 终端环境中使用 VIM,这些是我在编译时遇到的错误,它看起来像是缺少包含头文件,但我 100% 确定我已经包含了头文件:

我的代码:

#include <iostream>
#include <string>
using namespace std;

#include "p3lexer.h"

// this is a list of node types
enum PTreeNodetype {
SLIST,
DECL,
ASSIGN,
PRINT,
MATH,
EQUAL,
TERM,
MINUS,
STAR,
MOD,
STARSTAR
};

// this is a parse tree node
class PTree {
PTreeNodetype type;
PTree *left;
PTree *right;
public:
PTree(PTreeNodetype t, PTree *l=0, PTree *r=0) {
type = t;
left = l;
right = r;
}
};


PTree *Slist();
PTree *Stmt();
PTree *Decl();
PTree *Print();
PTree *Assign();
PTree *Expr();
PTree *Term();
PTree *Factor();
PTree *Primary();

int
main(int argc, char *argv[])
{
if(argc == 2){
if(!setLexFile(argv[1]))
return 0;
}
else setLexStdin();


Token *t;

// this code just tests the lexer; you should comment it out later
while( t = getToken() ) {
cout << t->getType() << ":" << t->getValue() << endl;
delete t;
}

// instead, this code!
PTree *parse;

parse = Slist();

if( parse == 0 ) {
cout << "Syntax error! " << endl;
return 0;
}

// here... a successful parse means we have to look at the tree
}

// slist :: = stmt slist | stmt
PTree *
Slist() {
TokenType tt = t->getType();
//fix this!!
if(tt != T_SLIST){
pushbacktoken(t);
return 0;
}
delete t;

PTree *stmt = Stmt();
if(!stmt){
cout << "Syntax error on line " << endl;
}
else{
return new PTree(stmt, slist);
}
}

// stmt ::= decl | assign | print
PTree *
Stmt() {
Token *t = getToken();
TokenType tt = tt->getType();

if(tt != T_DECL && T_ASSIGN && T_PRINT){
pusbbacktoken(t);
return 0;
}
delete t;
return new(DECL, PRINT, ASSIGN);
}

// decl ::= INT ID SC | FLOAT ID SC
PTree*
Decl(){
Token *t = getToken();
TokenType tt = t->getType();

if(tt != T_INT && tt != T_FLOAT){
pushbacktoken(t);
return 0;
}

delete t;

t = getToken();
if(t->getToken() != T_ID){
cout << "Syntax Error!" << endl;

delete t;
return 0;
}

string id = t->getValue();
delete t;

t = getToke();
if(t->getType() != T_SC){
cout << "syntax Error! " << endl;

delete t;
return 0;
}
return new PTree(DECL);
}

//print := PRINT expr SC
PTree*
Print(){
Token *t = getToken();
TokenType tt = t->getType();

if(tt != T_PRINT){
pushbacktoken(t);
return 0;
}
delete t;

PTree *expr = Expr();
if(!expr){
cout << "Syntax error on line " << endl;
return 0;
}

t = getToken();
if(t->getToken() != T_SC){
cout << "Syntax error on line " << endl;
delete t;
return 0;
}
return new PTree(PRINT, expr);
}


// assign ::= ID EQUAL expr SC
PTree *
Assign() {
Token *t = getToke();
TokenType tt = t->getToken();
if(tt != T_ID){
pushbacktoken(t);
return 0;
//recheck this, something is missing from here
}

t = getToken();
if(t->getType != T_ID && T_EQUAL){
cout << "Syntax error on line " << endl;
return 0;
delete t;
}

string id = t-> getValue();
delete t;

Ptree *expr = Expr();
if(!expr){
cout << "Syntax error on line " << endl;
return 0;
}

t = getToken();
if(r->getToken() != T_SC){
cout << "Syntrax error on line " << endl;
delete t;
return 0;
}
return new PTree(Assign, expr);
}


// expr ::= term PLUS expr | term MINUS expr | term
PTree *
Expr() {
Token *t = getToken();
TokenType tt = t->getType();
PTree *term = Term();

if(!term){
cout << "Syntax error on line " << endl;
return 0;
}
if(t->getType != T_PLUS && t->getType() != T_MINUS)
pushbacktoken(t);
return *term;
}
else{
return Term(l);
return Expr(r);
}

PTree *expr = Term();
if(!expr){
cout << "Syntax error on line " << endl;
return 0;

}
delete t;
return new PTree(expr, term);
}


// term ::= factor STAR term | factor DIV term | factor MOD term | factor
PTree *
Term() {
Token *t = getToken();
TokenType tt = t->getType();

PTree *factor = Factor();

if(!factor){
cout << "Syntax error on line " << endl;
return 0;
}
t = getToken();
if(t->getType() != T_STAR && t->getType() != T_DIV && t->getType != T_MOD){
pushbacktoken(t);
return *factor;
}
else{
return Factor(l);
return Expr(r);
}

PTree *term = Term();

if (!term){
cout << "Syntax error on line " << endl;
}
delete t;

PTree *factor = Factor();

if(!factor){
cout << "Syntax error on line " << endl;
return 0;
}

PTree *factor = Factor();
if(!factor){
cout << "Syntax error on line " << endl;
return 0;
}
return new PTree(term);
}

// factor ::= primary STARSTAR factor | primary
PTree *
Factor(){
Token *t = getToken();
TokenType tt = t->getType();

PTree *primary = Primary();
if(!primary){
cout << "Syntac error on line " << endl;
return 0;
}
delete t;

t = getToken();
if(t->getToke() != T_STARSTAR){
cout << "Syntax error! " << endl;
}
else{
return Primary(l);
return Factor(r);
}

return new PTree(factor, primary);
}


// primary ::= INTCONST | FLOATCONST | ID | LPAREN expr RPAREN
PTree *
Primary(){
Token *t = getToken();
TokenType tt = t->getType();

if(tt != T-INTCONST && T_FLOATCONST){
pushbacktoken(t);
return 0;
}
delete t;

t = getToken();
if(t->getType() != T_ID){
cout << "Syntax Error on line " << endl;
delete t;
return 0;
}
string id = t->getValue();
delete t;

of(tt != T_LPAREN && T_RPAREN){
pusbbacktoken(t);
return 0;
}
delete t;

PTree *expr = Expr();
if(!expr){
cout << "Syntax error on line " << endl;
return 0;
}
delete t;

return new(primary)
}




9895114/edit

}

这是头文件

extern int linenum;
extern int errcnt;

enum Tokentype {
T_ERR,
T_INT,
T_FLOAT,
T_PRINT,
T_SC,
T_EQUAL,
T_PLUS,
T_MINUS,
T_STAR,
T_DIV,
T_MOD,
T_STARSTAR,
T_INTCONST,
T_FLOATCONST,
T_ID,
T_LPAREN,
T_RPAREN,
};

class Token {
Tokentype type;
string value;
int linenum;

public:
Token(Tokentype t, string v="") {
type = t;
value = v;

linenum = ::linenum;
}

Tokentype getType() { return type; }
string getValue() { return value; }
int getLinenum() { return linenum; }
};

extern void error(string);
extern int setLexFile(char *);
extern int setLexStdin();
extern Token *getToken();
extern void pushbacktoken( Token * );

我想看看下一个东西是否不是加号或减号标记,然后将其推回并返回返回的术语。例如 expr() 如果它是加号或减号,则使用为左 child 返回的术语,并为右 child 再次调用 Expr()。我为它们编写了功能,但我对它们不太了解,我将它们添加到顶部,有人可以告诉我我的方向是否正确吗?我应该解决什么问题?

// expr ::= term PLUS expr | term MINUS expr | term
PTree *
Expr() {
Token *t = getToken();
Tokentype tt = t->getType();
PTree l = left();
PTree r = right();
PTree *term = Term();

if(!term){
cout << "Syntax error on line " << endl;
return 0;
}
else{
if(t->getType() != T_PLUS && t->getType() != T_MINUS){
return 0;
}
else{
return left();
}
}

PTree *expr = Expr();
if(!expr){
cout << "Syntax error on line " << endl;
return 0;
}
else{
if(t->getType() != T_PLUS && t->getType() != T_MINUS){
return 0;
}
else{
return right();
}
delete t;
return new PTree(expr, term);
}

// term ::= factor STAR term | factor DIV term | factor MOD term | factor
PTree *
Term() {
Token *t = getToken();
Tokentype tt = t->getType();
t = getToken()
PTree l = left();
PTree r = right();

PTree *factor = Factor();
if(!factor){
cout << "Syntax error on line " << endl;
return 0;
}
else{
if(t->getType() != T_STAR && t->getType() != T_DIV && t->getType() != T_MOD){
pushbacktoken(t)
return 0;
}
else{
return left();
}
}

PTree *term = Term();
if (!term){
cout << "Syntax error on line " << endl;
return 0;
}
else{
if(t->getType() != T_STAR && t->getType() != T_DIV && t->getType() !=T_MOD){
pushbacktoken(t);
return 0;
}
else{
return right();
}
}
return new PTree(term);
}

// factor ::= primary STARSTAR factor | primary
PTree *
Factor(){
Token *t = getToken();
Tokentype tt = t->getType();
PTree l = left();
PTree r = right();

PTree *primary = Primary();
if(!primary){
cout << "Syntac error on line " << endl;
return 0;
}
else{
if(t->getType() != T_STARSTAR){
return 0;
}
else{
return left();
}
}

PTree *factor = Factor();
if(!factor){
cout << "Syntax error on line " << endl;
return 0;
}
else{
if(t->getType() != T_STARSTAR){
return 0;
}
else{
return right();
}

return new PTree(factor, primary);
}

我遇到了这些错误

problem3.cpp: In function ‘PTree* Expr()’:
problem3.cpp:225:17: error: too few arguments to function ‘std::ios_base& std::left(std::ios_base&)’
/usr/include/c++/4.6/bits/ios_base.h:918:3: note: declared here
problem3.cpp:226:18: error: too few arguments to function ‘std::ios_base& std::right(std::ios_base&)’
/usr/include/c++/4.6/bits/ios_base.h:926:3: note: declared here
problem3.cpp:238:16: error: too few arguments to function ‘std::ios_base& std::left(std::ios_base&)’
/usr/include/c++/4.6/bits/ios_base.h:918:3: note: declared here
problem3.cpp:252:16: error: too few arguments to function ‘std::ios_base& std::right(std::ios_base&)’

最佳答案

你有各种各样的错误。大多数源于此:
header 显示 Tokentype,您的代码显示

PTree *
Slist() {
TokenType tt = t->getType();

C++是区分大小写的,所以你需要把这个改成

PTree *
Slist() {
Tokentype tt = t->getType();
// ^------- lowercase 't'

还有一个错误说

‘class Token’ has no member named ‘getToken’

它确实有一个 getType() 函数 - 也许您打算改为调用它。

另外,对于错误

in ‘t->Token::getType != (Tokentype)14u’

getType 是一个函数,所以你应该调用它

t->Token::getType() != (Tokentype)14u

我相信来自 tthis 行:

if(t->getType() != T_STAR && t->getType() != T_DIV && t->getType != T_MOD)
// ^^^^

即改成

if(t->getType() != T_STAR && t->getType() != T_DIV && t->getType() != T_MOD)
// ^^^^

另外,注意你有

Token *t;

在 main 中,所以 Slist 函数看不到它。你可以传递它:

PTree *
Slist(Token *t) {

我怀疑当我们修复您报告的错误时,随着编译器的进一步发展,您会得到更多。尝试阅读消息并修复您理解的内容。

关于c++ - Recursive Decent 解析器 (c++) 的编译问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19895114/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com