1

我正在尝试修改此语法以能够解析 C++ 程序,并且它的 using 语句出现问题,引发错误syntax error at line 10 before using std::cin ; using

PDefs.   Program ::= [Def] ;

DFun.    Def    ::= Type Id "(" [Arg] ")" "{" [Stm] "}" ;

terminator Def "" ;

ADecl.   Arg    ::= Type Id ;

separator Arg "," ;

SExp.        Stm ::= Exp ";" ;
SDecls.      Stm ::= "std::" Type [Id] ";" ;
SInit.       Stm ::= Type Id "=" Exp ";" ;
SReturn.     Stm ::= "return" Exp ";" ;
SReturnVoid. Stm ::= "return" ";" ;
SWhile.      Stm ::= "while" "(" Exp ")" Stm ;
SBlock.      Stm ::= "{" [Stm] "}" ;
SIfElse.     Stm ::= "if" "(" Exp ")" Stm "else" Stm ;
SCOut.       Stm ::= "std::cout" [Stm] ";" ;
SCIn.        Stm ::= "std::cin" ">>" [Id] ";" ;
SPrint.      Stm ::= "<<" Exp ;
SEndL.       Stm ::= "<<" "std::endl" ;
SUsing.      Stm ::= "using" Id ";" ;

terminator Stm "" ;

ETrue.   Exp15  ::= "true" ;
EFalse.  Exp15  ::= "false" ;
EInt.    Exp15  ::= Integer ;
EDouble. Exp15  ::= Double ;
EString. Exp15  ::= String ;
EId.     Exp15  ::= Id ;

EApp.    Exp15  ::= Id "(" [Exp] ")" ;

EPIncr.  Exp14  ::= Exp15 "++" ;
EPDecr.  Exp14  ::= Exp15 "--" ;

EIncr.   Exp13  ::= "++" Exp14 ;
EDecr.   Exp13  ::= "--" Exp14 ;

ETimes.  Exp12  ::= Exp12 "*"  Exp13 ;
EDiv.    Exp12  ::= Exp12 "/"  Exp13 ;
EPlus.   Exp11  ::= Exp11 "+"  Exp12 ;
EMinus.  Exp11  ::= Exp11 "-"  Exp12 ;
ELt.     Exp9   ::= Exp9  "<"  Exp10 ;
EGt.     Exp9   ::= Exp9  ">"  Exp10 ;
ELtEq.   Exp9   ::= Exp9  "<=" Exp10 ;
EGtEq.   Exp9   ::= Exp9  ">=" Exp10 ;
EEq.     Exp8   ::= Exp8  "==" Exp9 ;
ENEq.    Exp8   ::= Exp8  "!=" Exp9 ;
EAnd.    Exp4   ::= Exp4  "&&" Exp5 ;
EOr.     Exp3   ::= Exp3  "||" Exp4 ;
EAss.    Exp2   ::= Exp3 "=" Exp2 ;

internal ETyped. Exp15 ::= "(" Exp ":" Type ")" ;

coercions Exp 15 ;

separator Exp "," ;

rules Type   ::= "bool" | "int" | "double" | "void" | "string" ;

token Id (letter (letter | digit | '_')*) ;

separator nonempty Id "," ;

comment "#" ;
comment "//" ;
comment "/*" "*/" ;

有问题的程序:

#include <algorithm>
#include <iomanip>
#ifndef __GNUC__
#include <ios>
#endif
#include <iostream>
#include <string>
#include <vector>

using std::cin;             using std::sort;
using std::cout;            using std::streamsize;
using std::endl;            using std::string;
using std::setprecision;    using std::vector;

int main()
{
    // ask for and read the student's name
    cout << "Please enter your first name: ";
    string name;
    cin >> name;
    cout << "Hello, " << name << "!" << endl;

    // ask for and read the midterm and final grades
    cout << "Please enter your midterm and final exam grades: ";
    double midterm, final;
    cin >> midterm >> final;

    // ask for and read the homework grades
    cout << "Enter all your homework grades, "
            "followed by end-of-file: ";

    vector<double> homework;
    double x;
    // invariant: `homework' contains all the homework grades read so far
    while (cin >> x)
        homework.push_back(x);

    // check that the student entered some homework grades
#ifdef _MSC_VER
    typedef std::vector<double>::size_type vec_sz;
#else
    typedef vector<double>::size_type vec_sz;
#endif
    vec_sz size = homework.size();
    if (size == 0) {
        cout << endl << "You must enter your grades.  "
                        "Please try again." << endl;
        return 1;
    }

    // sort the grades
    sort(homework.begin(), homework.end());

    // compute the median homework grade
    vec_sz mid = size/2;
    double median;
    median = size % 2 == 0 ? (homework[mid] + homework[mid-1]) / 2
                           : homework[mid];

    // compute and write the final grade
    streamsize prec = cout.precision();
    cout << "Your final grade is " << setprecision(3)
         << 0.2 * midterm + 0.4 * final + 0.4 * median
         << setprecision(prec) << endl;

    return 0;
}

我不确定为什么它不能识别“using”关键字,因为我在语法中将它定义为 SUsing。非常感谢任何和所有帮助。谢谢!

4

1 回答 1

1

问题在于std::cin它在 rule 中的出现被声明为关键字SCIn

SCIN。stm ::= "std::cin" ">>" [Id] ";" ;
...
使用。stm ::= "使用" ID ";" ;

因此,它不被识别Id,因此规则SUsing没有触发。关键字优先于其他标记。

要么概括SCIn为,要么添加forId的专门版本。SUsingstd::cin

于 2021-03-21T18:09:02.177 回答