小编Nil*_*ils的帖子

用PEG.js解析缩进级别

我有与Python样式缩进的PEG基本相同的问题,但我想对这个答案有更多的指导.

答案成功生成了一个字符串数组,每行输入行之间有'INDENT'和'DEDENT'.看起来他几乎用PEG.js来标记,但是没有真正的解析.

那么我怎样才能扩展他的例子来做一些实际的解析呢?

作为一个例子,我该如何改变这个语法:

start = obj
obj = id:id children:(indent obj* outdent)?
    {
        if (children) {
            let o = {}; o[id] = children[1];
            return o;
        } else {
            return id;
        }
    }
id = [a-z]
indent = '{'
outdent = '}'
Run Code Online (Sandbox Code Playgroud)

使用缩进而不是大括号来描述块,仍然得到相同的输出?

(使用http://pegjs.majda.cz/online来测试语法具有以下输入:a{bcd{zyx{}}})

javascript syntax parsing peg

9
推荐指数
2
解决办法
2421
查看次数

如何在Python中与Chess引擎通信?

在win 7上,我可以通过命令行与国际象棋引擎进行通信.Win 7 上与Stockfish的小例子会话:

C:\run\Stockfish>stockfish-x64.exe
Stockfish 2.2.2 JA SSE42 by Tord Romstad, Marco Costalba and Joona Kiiski
quit

C:\run\Stockfish>
Run Code Online (Sandbox Code Playgroud)

第一行由引擎输出,'quit'是我输入的退出引擎(我还可以做其他事情,但这对我来说很清楚).

现在我想从python与该引擎通信:

import subprocess
engine = subprocess.Popen(
    'stockfish-x64.exe',
    stdin=subprocess.PIPE,
    stdout=subprocess.PIPE,
)
for line in engine.stdout:
    print(line.strip())
engine.stdin.write('quit\n')
Run Code Online (Sandbox Code Playgroud)

我明白了

C:\run\Stockfish>communicate.py
b'Stockfish 2.2.2 JA SSE42 by Tord Romstad, Marco Costalba and Joona Kiiski'
Run Code Online (Sandbox Code Playgroud)

但它没有退出引擎(没有C:\ run\Stockfish>提示),它一直在等待输入.我必须手动关上窗户.似乎没有把我的退出消息(python脚本的最后一行)写入stdin.

换句话说,我可以从stdout读取,但是当我写入stdin时,没有任何事情发生.

我做错了什么,怎么做对了?


编辑:好的,感谢larsmans的帮助我解决了它:

示例Python脚本:

import subprocess, time

engine = subprocess.Popen(
    'stockfish-x64.exe',
    universal_newlines=True,
    stdin=subprocess.PIPE,
    stdout=subprocess.PIPE,
)

def put(command):
    print('\nyou:\n\t'+command)
    engine.stdin.write(command+'\n')

def get():
    # using the …
Run Code Online (Sandbox Code Playgroud)

python chess process python-3.x

8
推荐指数
1
解决办法
7013
查看次数

Node.js:如何在控制台中禁用颜色?

我是node.js的新手,目前正在使用npm。

我发现控制台输出在我的系统上以一种奇怪的方式着色,我只有在将其复制粘贴到文本编辑器中时才能读取它。将背景更改为黑色,将字体颜色更改为白色也无济于事。这是屏幕截图:

Node.js彩色控制台输出不可读

如何禁用这些颜色?

console node.js npm

3
推荐指数
1
解决办法
1706
查看次数

What is the meaning of '?' in the Racket docs?

Example:

(number? v) ? boolean?

  v : any/c
Run Code Online (Sandbox Code Playgroud)

I understand the '?' behind 'number' but the second '?', behind 'boolean' irritates me. Does it mean that it maybe returns a boolean, and maybe not?

Edit for clarity

I come from Python, to me this reads as: is_number::function returns is_boolean::function and not is_number::function returns bool::bool.

Solution

As Jérôme explained, it reads as:

is_number(v) returns b where is_boolean(b) == True
Run Code Online (Sandbox Code Playgroud)

racket

2
推荐指数
1
解决办法
421
查看次数

标签 统计

chess ×1

console ×1

javascript ×1

node.js ×1

npm ×1

parsing ×1

peg ×1

process ×1

python ×1

python-3.x ×1

racket ×1

syntax ×1