Scala语法规范不匹配if-else,以分号结尾的一行表达式? [英] Scala Syntax Specification mismatch if-else with one line expression end by semicolon?
问题描述
我正在学习 Scala语法规范。
与 if-else
语法混淆:
Expr1 ::= ‘if’ ‘(’ Expr ‘)’ {nl} Expr [[semi] ‘else’ Expr]
| ...
在if-else下如何用分号结尾一个行表达式来匹配下面的内容?
How could it match below if-else with one line expression end by semicolon ?
if (true) // \n
println(1); //\n
else //\n
println(2); //\n
请注意,这里有4行,每行后跟'\ n'
。我有以下问题:
Notice there're 4 lines and each followed by a '\n'
. I have these questions:
- 当第一个
时在
println(1 )
在else
前匹配semi
([[semi]' else'Expr]
),如何匹配之后的第二个
'\n'
;println(1)
之后? - 如何匹配第三个
'\n'$
else
之后的c $ c>吗? - 如何匹配第二个
;
以及println(2)
之后的第四个'\n'
吗?由于if-else
与任何;
或'\n'
放在尾部。
- When the 1st
;
afterprintln(1)
matchsemi
beforeelse
([[semi] ‘else’ Expr]
), how to match the 2nd'\n'
after;
afterprintln(1)
? - How to match the 3rd
'\n'
afterelse
? - How to match the 2nd
;
and the 4th'\n'
afterprintln(2)
? Sinceif-else
don't match any;
or'\n'
at tail.
推荐答案
我认为您由于感到困惑而感到困惑所有换行符必须与 nl
令牌匹配。
I think you are being confused by thinking that all newlines must match the nl
token. That is not correct.
换行符通常被 视为空白。在词法语法一章中,关于换行符的章节很长,第1.2节换行符 ,其中详细说明了准确时,换行符是 nl $
Newlines are in general simply treated as whitespace. There is a very long subsection on newlines in the Lexical Syntax chapter section 1.2 Newline characters which explains in detail, when, exactly, a newline character is an nl
token and when it isn't.
示例中只有第一个换行符是 nl
令牌,其他三个只是空白。
Only the first newline character in your example is an nl
token, the other three are just whitespace.
这篇关于Scala语法规范不匹配if-else,以分号结尾的一行表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!