我正在编译我的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 ++,所以我不确定我搞砸了什么,或者它是我的编译器。我在基因组/ 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()如果它是加号或减号,则使用为左子项返回的术语,并为右侧调用另一个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&)’
答案 0 :(得分:5)
您有各种各样的错误。大多数源于此:
标题为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
我相信这来自于这条线:
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) {
我怀疑在我们修复您报告的错误时,随着编译器的进一步发展,您将获得更多。尝试阅读消息并修复您的理解。