所以我有這個遞歸下降解析器工作正常,通過命令行參數識別和使用值,但我不知道如何將此端口從.dat文件讀取,使用正確的字符指針,打印字符串,併爲多個字符串工作。通過ifstream的字符指針和字符串
使用分析器命令行參數:
#include <iostream>
#include <fstream>
using namespace std;
bool A(void);
bool E(void);
bool T(void);
bool F(void);
bool P(void);
bool I(void);
bool L(void);
char *c;
int main(int argc, char *argv[]){
c = argc == 2 ? argv[1] : (char *)"";
if (A() && *c == '\0') {
cout << "The string \"" << argv[1] << "\" is in the language." << endl;
}
else {
cout << "The string \"" << argv[1] << "\" is not in the language." << endl;
}
return 0;
}
bool A(void){
if(I())
{
if (*c == '='){
++c;
if (E())
return true;
}
}
return false;
}
bool E(void){
if(T()){
if (*c == '+' || *c == '-'){
++c;
return E();
}
return true;
}
return false;
}
bool F(void){
if(P()){
if(*c == '^'){
++c;
return F();
}
return true;
}
return false;
}
bool I(void){
if (*c >= 'a' && *c <= 'z'){
++c;
return true;
}
return false;
}
bool L(void){
if (*c >= '0' && *c <= '9'){
++c;
return true;
}
return false;
}
bool P(void){
if (I()){
return true;
}
else
if (L()){
return true;
}
else
if (*c == '('){
++c;
if (E()){
if (*c == ')'){
++c;
return true;
}
}
}
return false;
}
bool T(void){
if(F()){
if (*c == '*' || *c == '/'){
++c;
return T();
}
return true;
}
return false;
}
我不知道我可以代替的argv [1]打印字符串。 爲了得到正確的字符指針,我可以這樣做嗎?
ifstream fin("input.dat");
while (fin >> *c)
當我嘗試我得到分段錯誤。
你確實需要學習使用豐富的函數名稱。 – GWW 2011-03-24 02:08:41
它們是完全任意的名字,看它的語法如何,除了這個練習之外,不會使用任何其他的語法。 – 2011-03-24 02:13:17
爲什麼要使用指針,您正在使用全局變量。 – 2011-03-24 02:17:33