Scala语法规范不匹配if-else,以分号结尾的一行表达式? [英] Scala Syntax Specification mismatch if-else with one line expression end by semicolon?

查看:83
本文介绍了Scala语法规范不匹配if-else,以分号结尾的一行表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 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:


  1. 当第一个 println(1 ) else 前匹配 semi [[semi]' else'Expr] ),如何匹配之后的第二个'\n'; println(1)之后?

  2. 如何匹配第三个'\n' else 之后的c $ c>吗?

  3. 如何匹配第二个; 以及 println(2)之后的第四个'\n'吗?由于 if-else 与任何; '\n'放在尾部。

  1. When the 1st ; after println(1) match semi before else( [[semi] ‘else’ Expr] ), how to match the 2nd '\n' after ; after println(1) ?
  2. How to match the 3rd '\n' after else ?
  3. How to match the 2nd ; and the 4th '\n' after println(2) ? Since if-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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆