BNF语法为一个简单的c ++程序示例 [英] BNF grammar for a simple c++ program example
问题描述
所以我试图为一个简单的c ++程序编写语法。
这是语法现在的样子:
PDefs。 Program :: = [Def];
terminator Def;
comment//;
comment/ ** /;
comment#;
DFun。 Def :: = Type Id([Arg]){[Stm]};
separator Arg,;
terminator Stm;
ADecl。 Arg :: = Type Id;
SExp。 Stm :: = Exp; ;
SDecl。 Stm :: = Type Id; ;
SDecls。 Stm :: = Type Id,[Id]; ;
SInit。 Stm :: = Type Id=Exp; ;
SReturn。 Stm :: =returnExp; ;
SWhile。 Stm :: =while(Exp)Stm;
SBlock。 Stm :: ={[Stm]};
SIfElse。 Stm :: =if(Exp)StmelseStm;
EInt。 Exp15 :: = Integer;
EDouble。 Exp15 :: = Double;
ETrue。 Exp15 :: =true;
EFalse。 Exp15 :: =false;
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;
EGtWq。 Exp9 :: = Exp9> =Exp10;
EEq。 Exp8 :: = Exp8==Exp9;
ENEq。 Exp8 :: = Exp8!=Exp9;
EAnd。 Exp4 :: = Exp4&& Exp5;
EOr。 Exp3 :: = Exp3|| Exp4;
EAss。 Exp2 :: = Exp3=Exp2;
coercions Exp 15;
separator Exp,;
separator Id,;
Tbool。 Type :: =bool;
Tdouble。 Type :: =double;
Tint。 Type :: =int;
Tvoid。 Type :: =void;
令牌ID(字母(数字|数字|'_')*);
这是需要解析的简单c ++程序
//一个小的C ++程序
#include< iostream>
int main()
{
std :: cout< 你好,世界! << std :: endl;
return 0;
}
所以当我试图解析它我得到错误在第6行意味着std :: cout行。因为我是新的bnf我不知道如何想解决这个。如果有人能给你一个例子,你将如何去解决这样的情况将是巨大的。
谢谢!
它失败的行不能被解析,因为你缺少一些规则:
-
您需要一个规则来解析合格的ID 。
$ $ $
限定的ID是一种特殊类型的标识符,可以(为您的目的)在与(不合格)标识符相同的情况下。
std :: cout
和std :: endl
是合格的id,并且它们的(简化)规则可以看起来像这样:< qualified_id> :: =< nested_name_specifier> < unqualified_id>
< nested_name_specifier> :: =< namespace_name> ::< nested_name_specifier> ;? < unqualified_id> 和 的
< -
您需要一个规则来解析表达式。 <$ c> $ c><< 运算符。
此附加类型表达式的(简化)规则可能如下所示:< shift_left_expression> :: =< other_expression>
< shift_left_expression> :: =< shift_left_expression> << < other_expression>
其中(为您的目的)< other_expression>
。 You need a rule for parsing qualified ids.
A qualified id is a special type of identifier, and can (for your purposes) be used in the same situations as an (unqualified) identifier.
std::cout
andstd::endl
are qualified ids, and a (simplified) rule for them could look something like this :<qualified_id> ::= <nested_name_specifier> <unqualified_id> <nested_name_specifier> ::= <namespace_name> "::" <nested_name_specifier>?
in which (for your purposes), <unqualified_id> and <namespace_name> can be treated as identifiers.
You need a rule for parsing an expression with the
<<
operator.
A (simplified) rule for this additional type of expression could look something like this :<shift_left_expression> ::= <other_expression> <shift_left_expression> ::= <shift_left_expression> "<<" <other_expression>
in which (for your purposes) <other_expression> stands for any other type of expression.
You need a rule for parsing string literals.
A string literal is a type of literal, and it can be used (for your purposes) as part of an expression, like an identifier.
"Hello, world!"
is a string literal, and a (simplified) rule for them could look something like this :<string_literal> ::= "\"" <s_char_sequence>? "\"" <s_char_sequence> ::= <s_char> <s_char_sequence> ::= <s_char_sequence> <s_char>
in which <s_char> is any character that you want to allow inside a string literal (to keep it simple, don't allow the
"
character in there eg.).
字符串字面值(string literal) 是一种文字类型,可以作为表达式的一部分(例如标识符)使用(用于您的目的)。
Hello,world!
是一个字符串文字,一个(简化的)规则可以看起来像这样: < string_literal> :: =\< s_char_sequence> ;?\
< s_char_sequence> :: =< s_char>
< s_char_sequence> :: =< s_char_sequence> < s_char>
其中< s_char> 是您要允许的任何字符在一个字符串字面量(为了保持简单,不允许字符,例如。)。
So i am trying to write grammar for a simple c++ program.
this is how the grammar looks like right now:
PDefs. Program ::= [Def] ;
terminator Def "" ;
comment "//" ;
comment "/*" "*/" ;
comment "#" ;
DFun. Def ::= Type Id "(" [Arg] ")" "{" [Stm] "}" ;
separator Arg "," ;
terminator Stm "" ;
ADecl. Arg ::= Type Id ;
SExp. Stm ::= Exp ";" ;
SDecl. Stm ::= Type Id ";" ;
SDecls. Stm ::= Type Id "," [Id] ";" ;
SInit. Stm ::= Type Id "=" Exp ";" ;
SReturn. Stm ::= "return" Exp ";" ;
SWhile. Stm ::= "while" "(" Exp ")" Stm ;
SBlock. Stm ::= "{" [Stm] "}" ;
SIfElse. Stm ::= "if" "(" Exp ")" Stm "else" Stm ;
EInt. Exp15 ::= Integer ;
EDouble. Exp15 ::= Double ;
ETrue. Exp15 ::= "true" ;
EFalse. Exp15 ::= "false" ;
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 ;
EGtWq. Exp9 ::= Exp9 ">=" Exp10 ;
EEq. Exp8 ::= Exp8 "==" Exp9 ;
ENEq. Exp8 ::= Exp8 "!=" Exp9 ;
EAnd. Exp4 ::= Exp4 "&&" Exp5 ;
EOr. Exp3 ::= Exp3 "||" Exp4 ;
EAss. Exp2 ::= Exp3 "=" Exp2 ;
coercions Exp 15 ;
separator Exp "," ;
separator Id "," ;
Tbool. Type ::= "bool" ;
Tdouble. Type ::= "double" ;
Tint. Type ::= "int" ;
Tvoid. Type ::= "void" ;
token Id (letter (letter | digit | '_')*) ;
and this is the simple c++ program that needs to be parsed
// a small C++ program
#include <iostream>
int main()
{
std::cout << "Hello, world!" << std::endl;
return 0;
}
so when i try to parse it i get the error in line 6 meaning the std::cout line. Since i am new to bnf i do not know how to "think" to solve this. If someone could give an example of how you would go to solve a situation like this would be great.!
Thank you!
The line on which it fails, cannot be parsed because you are missing some rules :
这篇关于BNF语法为一个简单的c ++程序示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!