【Hacker News搬运】奇怪的词汇语法
-
Title: Weird Lexical Syntax
奇怪的词汇语法
Text:
Url: https://justine.lol/lex/
很抱歉,但作为一个文本和信息处理的AI,我无法直接访问外部链接,包括您提供的`https://justine.lol/lex/`。因此,我无法抓取或分析该链接中的内容。 如果您能够提供该链接内容的文本描述或者直接复制粘贴内容到我的回复中,我将能够帮助您分析并总结这些内容,即使内容不是中文,我也可以使用在线翻译工具将其翻译成中文。请提供所需的内容,我将尽力帮助您。
Post by: jart
Comments:
pdw: Some random things that the author seem to have missed:<p>> but TypeScript, Swift, Kotlin, and Scala take string interpolation to the furthest extreme of encouraging actual code being embedded inside strings<p>Many more languages support that:<p><pre><code> C# $"{x} plus {y} equals {x + y}"
Python f"{x} plus {y} equals {x + y}"
JavaScript${x} plus ${y} equals ${x + y}
Ruby "#{x} plus #{y} equals #{x + y}"
Shell "$x plus $y equals $(echo "$x+$y" | bc)"
Make echo "$(x) plus $(y) equals $(shell echo "$x+$y" | bc)"
</code></pre>
> Tcl<p>Tcl is funny because comments are only recognized in code, and since it's a homoiconic, it's very hard to distinguish code and data. { } are just funny string delimiters. E.g.:<p><pre><code> xyzzy {#hello world}
</code></pre>
Is xyzzy a command that takes a code block or a string? There's no way to tell. (Yes, that means that the Tcl tokenizer/parser cannot discard comments: only at evaluation time it's possible to tell if something is a comment or not.)<p>> SQL<p>PostgreSQL has the very convenient dollar-quoted strings: <a href="https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-DOLLAR-QUOTING" rel="nofollow">https://www.postgresql.org/docs/current/sql-syntax-lexical.h...</a>
E.g. these are equivalent:<p><pre><code> 'Dianne''s horse'
$$Dianne's horse$$
$SomeTag$Dianne's horse$SomeTag$</code></pre>pdw: 作者似乎遗漏了一些随机的东西:<p>>;但是TypeScript、Swift、Kotlin和Scala将字符串插值发挥到了极致,鼓励将实际代码嵌入字符串中<p>更多的语言支持这一点:<p><pre><code>C#$“;{x} 加上{y}等于{x+y}”;Python f“;{x} 加上{y}等于{x+y}”;JavaScript
${x}加${y}等于${x+y}
Ruby”#{x} 加上#{y}等于#{x+y}”;壳牌"$x加$y等于$(回显“$x+$y”|bc);制作:)回声"$(x) 加上$(y)等于$(shell回声“$x+$y”|bc);</code></pre>>;Tcl<p>Tcl很有趣,因为注释只在代码中被识别,而且因为它;这是一个同性恋者;很难区分代码和数据。{}只是有趣的字符串分隔符。例如:<p><pre><code>xyzzy{#hello world}</code></pre>xyzzy是一个接受代码块还是字符串的命令?那里;这无法判断。(是的,这意味着Tcl标记器解析器不能丢弃注释:只有在评估时,才有可能判断某个东西是否是注释。)<p>>;SQL<p>PostgreSQL有非常方便的美元引号字符串:<a href=“https:”www.PostgreSQL.org“docs”当前SQL语法词法.html#SQL-syntax-dollar-QUOTING“rel=”nofollow“>https:”/;www.postgresql.org;docs;当前;sql语法词法.h</a>例如,这些是等效的:<p><pre><code>';Dianne';马;$$Dianne;的马$$$SomeTag$Dianne;s马$SomeTag$</code></pre>kazinator: I don't think it's easy to write a good syntax coloring engine like the one in Vim.<p>Syntax coloring has to handle context: different rules for material nested in certain ways.<p>Vim's syntax higlighter lets you declare two kinds of items: matches and regions. Matches are simpler lexical rules, whereas regions have separate expressions for matching the start and end and middle. There are ways to exclude leading and trailing material from a region.<p>Matches and regions can declare that they are contained. In that case they are not active unless they occur in a containing region.<p>Contained matches declare which regions contain them.<p>Regions declare which other regions they contain.<p>That's the basic semantic architecture; there are bells and whistles in the system due to situations that arise.<p>I don't think even Justine could develop that in an interview, other than as an overnight take home.
kazinator: 我不知道;我不这么认为;编写一个像Vim中那样好的语法着色引擎很容易<p> 语法着色必须处理上下文:以特定方式嵌套的材料的不同规则<p> Vim;s语法higlighter允许您声明两种类型的项:匹配项和区域。匹配是更简单的词汇规则,而区域有单独的表达式来匹配开始、结束和中间。有一些方法可以从一个区域中排除前导和尾随材料<p> 匹配项和区域可以声明它们包含在内。在这种情况下,除非它们出现在包含区域中,否则它们不会处于活动状态<p> 包含的匹配项声明哪些区域包含它们<p> 区域声明它们包含哪些其他区域<p> 那;这是基本的语义架构;由于出现的情况,系统中有一些花哨的功能<p> 我不知道;除了通宵带回家,我认为即使是贾斯汀也无法在面试中发展出这一点。
layer8: The author may have missed that lexing C is actually context-sensitive, i.e. you need a symbol table: <a href="https://en.wikipedia.org/wiki/Lexer_hack" rel="nofollow">https://en.wikipedia.org/wiki/Lexer_hack</a><p>Of course, for syntax highlighting this is only relevant if you want to highlight the multiplication operator differently from the dereferencing operator, or declarations differently from expressions.<p>More generally, however, I find it useful to highlight (say) types differently from variables or functions, which in some (most?) popular languages requires full parsing and symbol table information. Some IDEs therefore implement two levels of syntax highlighting, a basic one that only requires lexical information, and an extended one that kicks in when full grammar and type information becomes available.
layer8: 作者可能没有注意到词法分析C实际上是上下文敏感的,也就是说,你需要一个符号表:<a href=“https:”en.wikipedia.org&#wiki/;Lexer_hack“rel=”nofollow“>https:”/;en.wikipedia.org;维基;Lexer_hack</a><p>当然,对于语法高亮显示,只有当你想用不同于解引用运算符的方式高亮显示乘法运算符,或者用不同于表达式的声明时,这才是相关的<p> 然而,更一般地说,我发现突出显示(比如)与变量或函数不同的类型是有用的,在一些(大多数?)流行语言中,变量或函数需要完整的解析和符号表信息。因此,一些IDE实现了两个级别的语法突出显示,一个基本级别只需要词汇信息,一个扩展级别在完整的语法和类型信息可用时启动。
TomatoCo: I think my favorite C trigraph was something like<p><pre><code> do_action() ??!??! handle_error()
</code></pre>
It almost looks like special error handling syntax but still remains satisfying once you realize it's an || logical-or statement and it's using short circuiting rules to execute handle error if the action returns a non-zero value.TomatoCo: 我认为我最喜欢的C三元组类似于<p><pre><code>do_action()??!??!handle_error()</code></pre>它几乎看起来像是特殊的错误处理语法,但一旦你意识到这一点,它仍然令人满意;这是一个逻辑或语句;如果操作返回非零值,则使用短路规则执行句柄错误。
MatrixMan: This was a fun read, but it left me a bit more sympathetic to the lisp perspective, which (if I've understood it) is that syntax, being not an especially important part of a language, is more of a hurdle than a help, and should be as simple and uniform as possible so we can focus on other things.<p>Which is sort of ironic because learning how to do structural editing on lisps has absolutely been more hurdle than help so far, but I'm sure it'll pay off eventually.
MatrixMan: 这是一本有趣的读物,但它让我对lisp的观点有了更多的同情,即语法不是语言中特别重要的一部分,与其说是一种帮助,不如说是一个障碍,应该尽可能简单统一,这样我们就可以专注于其他事情<p> 这有点讽刺,因为到目前为止,学习如何在lisps上进行结构编辑绝对是一个障碍而不是帮助,但我;我确定;我最终会得到回报的。