if 测试和语法规则

if语句通用格式

if <test1>:
    <statements1>
else <test2>:
    <statements2>

Python中没有switch case 语句。然而python,多路分支是写成一系列的if/elif测试,或者对字典进行索引运算或搜索列表。

if/elif:

if choic == 'spam':
    print(1)
elif choice == 'ham':
    print(2)
else:
    print('bad choice')

字典:

choice = 'spam'
print({'spam':1,
        'ham':2}[choice])

if and in:

choice = 'spam'
if choice in branch:    #branch is an dict
    print(branch[choice])

 

真值测试

敲黑板,布尔运算符在python中是字符and, or, not,不是C的&&,||和| 。此外,布尔and和or运算在python中会返回真或假对象,而不是True或False

>>> 2<3, 3<2
(True, False)
>>> 

像这类值的比较会返回True or False作为其真值结果,但and or运算符会返回对象,不是运算符的左侧就是运算符的右侧对象。Python会在找到的第一个真值操作数地方停止,即短路计算。

 

if/else 三元表达式

if x:
    A = Y
else:
    A = Z
A = Y if X else Z

 

Learning Python, Fourth Edition, by Mark Lutz.

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我选择使用递归下降分析算法,并以C++语言实现一个简单的四则运算表达式语法分析器。 首先,我们需要定义该语言的语法规则。假设该语言的语法规则如下: ``` <expression> ::= <term> | <term> <addop> <expression> <term> ::= <factor> | <factor> <mulop> <term> <factor> ::= <number> | ‘(’ <expression> ‘)’ <addop> ::= ‘+’ | ‘-’ <mulop> ::= ‘*’ | ‘/’ <number> ::= <digit> | <number> <digit> <digit> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 ``` 接下来,我们可以开始实现语法分析程序。首先,我们需要定义一个Token类,表示具有类型和值的单词: ```c++ class Token { public: enum class Type { NUMBER, ADD_OP, MUL_OP, LPAREN, RPAREN, EOL }; Type type; string value; }; ``` 然后,我们需要实现词法分析器,将输入的字符串转换为Token序列: ```c++ vector<Token> lexer(string input) { vector<Token> tokens; for (int i = 0; i < input.size(); i++) { if (isdigit(input[i])) { string value = ""; while (isdigit(input[i])) { value += input[i]; i++; } i--; tokens.push_back({ Token::Type::NUMBER, value }); } else if (input[i] == '+') { tokens.push_back({ Token::Type::ADD_OP, "+" }); } else if (input[i] == '-') { tokens.push_back({ Token::Type::ADD_OP, "-" }); } else if (input[i] == '*') { tokens.push_back({ Token::Type::MUL_OP, "*" }); } else if (input[i] == '/') { tokens.push_back({ Token::Type::MUL_OP, "/" }); } else if (input[i] == '(') { tokens.push_back({ Token::Type::LPAREN, "(" }); } else if (input[i] == ')') { tokens.push_back({ Token::Type::RPAREN, ")" }); } else if (input[i] == ' ') { continue; } else { cerr << "Invalid character: " << input[i] << endl; return {}; } } tokens.push_back({ Token::Type::EOL, "" }); return tokens; } ``` 接下来,我们可以实现语法分析程序。由于我们选择递归下降分析算法,我们需要实现一个函数来处理每个语法规则。 首先,我们实现一个函数来处理<factor>规则: ```c++ double factor(vector<Token>& tokens, int& index) { if (tokens[index].type == Token::Type::NUMBER) { double value = stod(tokens[index].value); index++; return value; } else if (tokens[index].type == Token::Type::LPAREN) { index++; double value = expression(tokens, index); if (tokens[index].type != Token::Type::RPAREN) { cerr << "Missing right parentheses" << endl; exit(1); } index++; return value; } else { cerr << "Invalid factor" << endl; exit(1); } } ``` 然后,我们实现一个函数来处理<term>规则: ```c++ double term(vector<Token>& tokens, int& index) { double value = factor(tokens, index); while (tokens[index].type == Token::Type::MUL_OP) { if (tokens[index].value == "*") { index++; value *= factor(tokens, index); } else if (tokens[index].value == "/") { index++; double divisor = factor(tokens, index); if (divisor == 0) { cerr << "Division by zero" << endl; exit(1); } value /= divisor; } } return value; } ``` 最后,我们实现一个函数来处理<expression>规则: ```c++ double expression(vector<Token>& tokens, int& index) { double value = term(tokens, index); while (tokens[index].type == Token::Type::ADD_OP) { if (tokens[index].value == "+") { index++; value += term(tokens, index); } else if (tokens[index].value == "-") { index++; value -= term(tokens, index); } } return value; } ``` 现在,我们的语法分析程序已经完成了。我们可以编写一个简单的驱动程序来测试它: ```c++ int main() { string input; cout << "Enter an expression: "; getline(cin, input); vector<Token> tokens = lexer(input); int index = 0; double result = expression(tokens, index); cout << "Result: " << result << endl; return 0; } ``` 现在,我们可以输入一个四则运算表达式,例如`(1+2)*3-4/2`,程序就会输出它的计算结果`6.5`。 当然,这只是一个非常简单的示例,实际的语法分析程序可能会更加复杂。不过,递归下降分析算法是一种非常常用的语法分析算法,可以用于处理许多不同类型的语言。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值